Skip to content

Commit e66f6b5

Browse files
committed
Move TypeAttribute to SwiftSyntaxBuilder
1 parent 1237f87 commit e66f6b5

File tree

11 files changed

+234
-54
lines changed

11 files changed

+234
-54
lines changed

CodeGeneration/Package.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let package = Package(
99
],
1010
products: [
1111
.executable(name: "generate-swiftbasicformat", targets: ["generate-swiftbasicformat"]),
12+
.executable(name: "generate-swiftparser", targets: ["generate-swiftparser"]),
1213
.executable(name: "generate-swiftsyntaxbuilder", targets: ["generate-swiftsyntaxbuilder"]),
1314
],
1415
dependencies: [
@@ -26,6 +27,16 @@ let package = Package(
2627
"Utils"
2728
]
2829
),
30+
.executableTarget(
31+
name: "generate-swiftparser",
32+
dependencies: [
33+
.product(name: "SwiftSyntax", package: "swift-syntax"),
34+
.product(name: "SwiftSyntaxBuilder", package: "swift-syntax"),
35+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
36+
"SyntaxSupport",
37+
"Utils"
38+
]
39+
),
2940
.executableTarget(
3041
name: "generate-swiftsyntaxbuilder",
3142
dependencies: [
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_helpers import make_swift_child, make_swift_node
4+
# -*- mode: Swift -*-
5+
# Ignore the following admonition it applies to the resulting .swift file only
6+
}%
7+
//// Automatically Generated From AttributeNodes.swift.gyb.
8+
//// Do Not Edit Directly!
9+
//===----------------------------------------------------------------------===//
10+
//
11+
// This source file is part of the Swift.org open source project
12+
//
13+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
14+
// Licensed under Apache License v2.0 with Runtime Library Exception
15+
//
16+
// See https://swift.org/LICENSE.txt for license information
17+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
public class Attribute {
22+
public let name: String
23+
public let swiftName: String?
24+
25+
public init(name: String, swiftName: String? = nil) {
26+
self.name = name
27+
self.swiftName = swiftName
28+
}
29+
}
30+
31+
public class TypeAttribute: Attribute {
32+
public init(name: String) {
33+
super.init(name: name)
34+
}
35+
}
36+
37+
// Type attributes
38+
public let TYPE_ATTR_KINDS = [
39+
TypeAttribute(name: "autoclosure"),
40+
TypeAttribute(name: "convention"),
41+
TypeAttribute(name: "noescape"),
42+
TypeAttribute(name: "escaping"),
43+
TypeAttribute(name: "differentiable"),
44+
TypeAttribute(name: "noDerivative"),
45+
TypeAttribute(name: "async"),
46+
TypeAttribute(name: "Sendable"),
47+
TypeAttribute(name: "unchecked"),
48+
TypeAttribute(name: "_typeSequence"),
49+
TypeAttribute(name: "_local"),
50+
TypeAttribute(name: "_noMetadata"),
51+
52+
// Generated interface attributes
53+
TypeAttribute(name: "_opaqueReturnTypeOf"),
54+
]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//// Automatically Generated From AttributeNodes.swift.gyb.
2+
//// Do Not Edit Directly!
3+
//===----------------------------------------------------------------------===//
4+
//
5+
// This source file is part of the Swift.org open source project
6+
//
7+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
8+
// Licensed under Apache License v2.0 with Runtime Library Exception
9+
//
10+
// See https://swift.org/LICENSE.txt for license information
11+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
public class Attribute {
16+
public let name: String
17+
public let swiftName: String?
18+
19+
public init(name: String, swiftName: String? = nil) {
20+
self.name = name
21+
self.swiftName = swiftName
22+
}
23+
}
24+
25+
public class TypeAttribute: Attribute {
26+
public init(name: String) {
27+
super.init(name: name)
28+
}
29+
}
30+
31+
// Type attributes
32+
public let TYPE_ATTR_KINDS = [
33+
TypeAttribute(name: "autoclosure"),
34+
TypeAttribute(name: "convention"),
35+
TypeAttribute(name: "noescape"),
36+
TypeAttribute(name: "escaping"),
37+
TypeAttribute(name: "differentiable"),
38+
TypeAttribute(name: "noDerivative"),
39+
TypeAttribute(name: "async"),
40+
TypeAttribute(name: "Sendable"),
41+
TypeAttribute(name: "unchecked"),
42+
TypeAttribute(name: "_typeSequence"),
43+
TypeAttribute(name: "_local"),
44+
TypeAttribute(name: "_noMetadata"),
45+
46+
// Generated interface attributes
47+
TypeAttribute(name: "_opaqueReturnTypeOf"),
48+
]
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 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+
import ArgumentParser
14+
import Foundation
15+
import SwiftSyntaxBuilder
16+
import Utils
17+
18+
@main
19+
struct GenerateSwiftSyntaxBuilder: ParsableCommand {
20+
@Argument(help: "The path to the destination directory where the source files are to be generated")
21+
var generatedPath: String
22+
23+
@Flag(help: "Enable verbose output")
24+
var verbose: Bool = false
25+
26+
func run() throws {
27+
try generateTemplates(
28+
templates: [
29+
(typeAttributeFile, "TypeAttribute.swift"),
30+
],
31+
destination: URL(fileURLWithPath: generatedPath),
32+
verbose: verbose
33+
)
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 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+
let copyrightHeader = """
14+
//// Automatically Generated by generate-swiftparser
15+
//// Do Not Edit Directly!
16+
//===----------------------------------------------------------------------===//
17+
//
18+
// This source file is part of the Swift.org open source project
19+
//
20+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
21+
// Licensed under Apache License v2.0 with Runtime Library Exception
22+
//
23+
// See https://swift.org/LICENSE.txt for license information
24+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
25+
//
26+
//===----------------------------------------------------------------------===//
27+
28+
"""
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 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+
import Foundation
14+
import SwiftSyntax
15+
import SwiftSyntaxBuilder
16+
import SyntaxSupport
17+
import Utils
18+
19+
let typeAttributeFile = SourceFile {
20+
ImportDecl(
21+
"""
22+
\(copyrightHeader)
23+
@_spi(RawSyntax) import SwiftSyntax
24+
"""
25+
)
26+
27+
ExtensionDecl(leadingTrivia: .newline, extendedType: "Parser") {
28+
EnumDecl(
29+
identifier: "TypeAttribute",
30+
inheritanceClause: TypeInheritanceClause {
31+
InheritedType(typeName: "SyntaxText")
32+
InheritedType(typeName: "ContextualKeywords")
33+
}) {
34+
for attribute in TYPE_ATTR_KINDS {
35+
EnumCaseDecl("case \(attribute.name) = \"\(attribute.name)\"")
36+
}
37+
}
38+
}
39+
40+
// for node in SYNTAX_NODES where !node.isBase {
41+
// if node.isSyntaxCollection {
42+
// makeSyntaxCollectionRewriteFunc(node: node)
43+
// } else {
44+
// makeLayoutNodeRewriteFunc(node: node)
45+
// }
46+
// }
47+
//
48+
// createTokenFormatFunction()
49+
}

Package.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ let package = Package(
119119
exclude: [
120120
"CMakeLists.txt",
121121
"README.md",
122-
"TypeAttribute.swift.gyb",
123122
"DeclarationModifier.swift.gyb",
124123
"DeclarationAttribute.swift.gyb",
125124
]

Sources/SwiftParser/TypeAttribute.swift.gyb

Lines changed: 0 additions & 29 deletions
This file was deleted.

Sources/SwiftParser/gyb_generated/TypeAttribute.swift renamed to Sources/SwiftParser/generated/TypeAttribute.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//// Automatically Generated From TypeAttribute.swift.gyb.
1+
2+
//// Automatically Generated by generate-swiftparser
23
//// Do Not Edit Directly!
34
//===----------------------------------------------------------------------===//
45
//

build-script.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ def generate_gyb_files(
345345

346346
def run_code_generation(
347347
swiftbasicformat_destination: str,
348+
swiftparser_destination: str,
348349
swiftsyntaxbuilder_destination: str,
349350
toolchain: str,
350351
verbose: bool
@@ -353,6 +354,7 @@ def run_code_generation(
353354

354355
target_to_dest_dir = {
355356
"generate-swiftbasicformat": swiftbasicformat_destination,
357+
"generate-swiftparser": swiftparser_destination,
356358
"generate-swiftsyntaxbuilder": swiftsyntaxbuilder_destination,
357359
}
358360

@@ -498,11 +500,13 @@ def verify_code_generated_files(
498500
)
499501

500502
self_swiftbasicformat_generated_dir = tempfile.mkdtemp()
503+
self_swiftparser_generated_dir = tempfile.mkdtemp()
501504
self_swiftsyntaxbuilder_generated_dir = tempfile.mkdtemp()
502505

503506
try:
504507
run_code_generation(
505508
swiftbasicformat_destination=self_swiftsyntaxbuilder_generated_dir,
509+
swiftparser_destination=self_swiftparser_generated_dir,
506510
swiftsyntaxbuilder_destination=self_swiftsyntaxbuilder_generated_dir,
507511
toolchain=toolchain,
508512
verbose=verbose
@@ -692,10 +696,13 @@ def generate_source_code_command(args: argparse.Namespace) -> None:
692696
if not args.gyb_only:
693697
swiftbasicformat_destination = \
694698
os.path.join(SWIFTBASICFORMAT_DIR, "generated")
699+
swiftparser_destination = \
700+
os.path.join(SWIFTPARSER_DIR, "generated")
695701
swiftsyntaxbuilder_destination = \
696702
os.path.join(SWIFTSYNTAXBUILDER_DIR, "generated")
697703
run_code_generation(
698704
swiftbasicformat_destination=swiftbasicformat_destination,
705+
swiftparser_destination=swiftparser_destination,
699706
swiftsyntaxbuilder_destination=swiftsyntaxbuilder_destination,
700707
toolchain=args.toolchain,
701708
verbose=args.verbose

gyb_syntax_support/AttributeKinds.py

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ def __init__(self, name, swift_name=None):
77
self.name = name
88
self.swift_name = swift_name or name
99

10-
class TypeAttribute(Attribute):
11-
def __init__(self, name):
12-
super().__init__(name, name)
13-
1410
class DeclAttribute(Attribute):
1511
def __init__(self, name, class_name, *options, code, swift_name=None):
1612
super().__init__(name, swift_name)
@@ -123,25 +119,6 @@ def __init__(self, name, swift_name=None):
123119
# Whether this attribute is valid on additional decls in ClangImporter.
124120
OnAnyClangDecl = 'OnAnyClangDecl'
125121

126-
# Type attributes
127-
TYPE_ATTR_KINDS = [
128-
TypeAttribute('autoclosure'),
129-
TypeAttribute('convention'),
130-
TypeAttribute('noescape'),
131-
TypeAttribute('escaping'),
132-
TypeAttribute('differentiable'),
133-
TypeAttribute('noDerivative'),
134-
TypeAttribute('async'),
135-
TypeAttribute('Sendable'),
136-
TypeAttribute('unchecked'),
137-
TypeAttribute('_typeSequence'),
138-
TypeAttribute('_local'),
139-
TypeAttribute('_noMetadata'),
140-
141-
# Generated interface attributes
142-
TypeAttribute('_opaqueReturnTypeOf'),
143-
]
144-
145122
# Schema for `DeclAttribute`s:
146123
#
147124
# - Attribute name.

0 commit comments

Comments
 (0)