forked from grpc/grpc-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructuredSwiftRepresentation.swift
1927 lines (1617 loc) · 58.6 KB
/
StructuredSwiftRepresentation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2023, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftOpenAPIGenerator open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftOpenAPIGenerator project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftOpenAPIGenerator project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
/// A description of an import declaration.
///
/// For example: `import Foo`.
package struct ImportDescription: Equatable, Codable, Sendable {
/// The access level of the imported module.
///
/// For example, the `public` in `public import Foo`.
///
/// - Note: This is optional, as explicit access-level modifiers are not required on `import` statements.
var accessLevel: AccessModifier? = nil
/// The name of the imported module.
///
/// For example, the `Foo` in `import Foo`.
var moduleName: String
/// An array of module types imported from the module, if applicable.
///
/// For example, if there are type imports like `import Foo.Bar`, they would be listed here.
var moduleTypes: [String]?
/// The name of the private interface for an `@_spi` import.
///
/// For example, if `spi` was "Secret" and the module name was "Foo" then the import
/// would be `@_spi(Secret) import Foo`.
var spi: String? = nil
/// Requirements for the `@preconcurrency` attribute.
var preconcurrency: PreconcurrencyRequirement = .never
/// If the dependency is an item, the property's value is the item representation.
/// If the dependency is a module, this property is nil.
var item: Item? = nil
/// Describes any requirement for the `@preconcurrency` attribute.
enum PreconcurrencyRequirement: Equatable, Codable, Sendable {
/// The attribute is always required.
case always
/// The attribute is not required.
case never
/// The attribute is required only on the named operating systems.
case onOS([String])
}
/// Represents an item imported from a module.
struct Item: Equatable, Codable, Sendable {
/// The keyword that specifies the item's kind (e.g. `func`, `struct`).
var kind: Kind
/// The name of the imported item.
var name: String
init(kind: Kind, name: String) {
self.kind = kind
self.name = name
}
}
enum Kind: String, Equatable, Codable, Sendable {
case `typealias`
case `struct`
case `class`
case `enum`
case `protocol`
case `let`
case `var`
case `func`
}
}
/// A description of an access modifier.
///
/// For example: `public`.
package enum AccessModifier: String, Sendable, Equatable, Codable, CaseIterable {
/// A declaration accessible outside of the module.
case `public`
/// A declaration accessible outside of the module but only inside the containing package or project.
case `package`
/// A declaration only accessible inside of the module.
case `internal`
/// A declaration only accessible inside the same Swift file.
case `fileprivate`
/// A declaration only accessible inside the same type or scope.
case `private`
}
/// A description of a comment.
///
/// For example `/// Hello`.
enum Comment: Equatable, Codable, Sendable {
/// An inline comment.
///
/// For example: `// Great code below`.
case inline(String)
/// A documentation comment.
///
/// For example: `/// Important type`.
case doc(String)
/// A mark comment.
///
/// For example: `// MARK: - Public methods`, with the optional
/// section break (`-`).
case mark(String, sectionBreak: Bool)
/// A comment that is already formatted,
/// meaning that it already has the `///` and
/// can contain multiple lines
///
/// For example both the string and the comment
/// can look like `/// Important type`.
case preFormatted(String)
}
/// A description of a literal.
///
/// For example `"hello"` or `42`.
enum LiteralDescription: Equatable, Codable, Sendable {
/// A string literal.
///
/// For example `"hello"`.
case string(String)
/// An integer literal.
///
/// For example `42`.
case int(Int)
/// A Boolean literal.
///
/// For example `true`.
case bool(Bool)
/// The nil literal: `nil`.
case `nil`
/// An array literal.
///
/// For example `["hello", 42]`.
case array([Expression])
/// A dictionary literal.
///
/// For example: `["hello": "42"]`
case dictionary([KeyValue])
struct KeyValue: Codable, Equatable {
var key: Expression
var value: Expression
}
}
/// A description of an identifier, such as a variable name.
///
/// For example, in `let foo = 42`, `foo` is an identifier.
enum IdentifierDescription: Equatable, Codable, Sendable {
/// A pattern identifier.
///
/// For example, `foo` in `let foo = 42`.
case pattern(String)
/// A type identifier.
///
/// For example, `Swift.String` in `let foo: Swift.String = "hi"`.
case type(ExistingTypeDescription)
}
/// A description of a member access expression.
///
/// For example `foo.bar`.
struct MemberAccessDescription: Equatable, Codable, Sendable {
/// The expression of which a member `right` is accessed.
///
/// For example, in `foo.bar`, `left` represents `foo`.
var left: Expression?
/// The member name to access.
///
/// For example, in `foo.bar`, `right` is `bar`.
var right: String
}
/// A description of a function argument.
///
/// For example in `foo(bar: 42)`, the function argument is `bar: 42`.
struct FunctionArgumentDescription: Equatable, Codable, Sendable {
/// An optional label of the function argument.
///
/// For example, in `foo(bar: 42)`, the `label` is `bar`.
var label: String?
/// The expression passed as the function argument value.
///
/// For example, in `foo(bar: 42)`, `expression` represents `42`.
var expression: Expression
}
/// A description of a function call.
///
/// For example `foo(bar: 42)`.
struct FunctionCallDescription: Equatable, Codable, Sendable {
/// The expression that returns the function to be called.
///
/// For example, in `foo(bar: 42)`, `calledExpression` represents `foo`.
var calledExpression: Expression
/// The arguments to be passed to the function.
var arguments: [FunctionArgumentDescription]
/// A trailing closure.
var trailingClosure: ClosureInvocationDescription?
/// Creates a new function call description.
/// - Parameters:
/// - calledExpression: An expression that returns the function to be called.
/// - arguments: Arguments to be passed to the function.
/// - trailingClosure: A trailing closure.
init(
calledExpression: Expression,
arguments: [FunctionArgumentDescription] = [],
trailingClosure: ClosureInvocationDescription? = nil
) {
self.calledExpression = calledExpression
self.arguments = arguments
self.trailingClosure = trailingClosure
}
/// Creates a new function call description.
/// - Parameters:
/// - calledExpression: An expression that returns the function to be called.
/// - arguments: Arguments to be passed to the function.
/// - trailingClosure: A trailing closure.
init(
calledExpression: Expression,
arguments: [Expression],
trailingClosure: ClosureInvocationDescription? = nil
) {
self.init(
calledExpression: calledExpression,
arguments: arguments.map { .init(label: nil, expression: $0) },
trailingClosure: trailingClosure
)
}
}
/// A type of a variable binding: `let` or `var`.
enum BindingKind: Equatable, Codable, Sendable {
/// A mutable variable.
case `var`
/// An immutable variable.
case `let`
}
/// A description of a variable declaration.
///
/// For example `let foo = 42`.
struct VariableDescription: Equatable, Codable, Sendable {
/// An access modifier.
var accessModifier: AccessModifier?
/// A Boolean value that indicates whether the variable is static.
var isStatic: Bool = false
/// The variable binding kind.
var kind: BindingKind
/// The name of the variable.
///
/// For example, in `let foo = 42`, `left` is `foo`.
var left: Expression
/// The type of the variable.
///
/// For example, in `let foo: Int = 42`, `type` is `Int`.
var type: ExistingTypeDescription?
/// The expression to be assigned to the variable.
///
/// For example, in `let foo = 42`, `right` represents `42`.
var right: Expression? = nil
/// Body code for the getter.
///
/// For example, in `var foo: Int { 42 }`, `body` represents `{ 42 }`.
var getter: [CodeBlock]? = nil
/// Effects for the getter.
///
/// For example, in `var foo: Int { get throws { 42 } }`, effects are `[.throws]`.
var getterEffects: [FunctionKeyword] = []
/// Body code for the setter.
///
/// For example, in `var foo: Int { set { _foo = newValue } }`, `body`
/// represents `{ _foo = newValue }`.
var setter: [CodeBlock]? = nil
/// Body code for the `_modify` accessor.
///
/// For example, in `var foo: Int { _modify { yield &_foo } }`, `body`
/// represents `{ yield &_foo }`.
var modify: [CodeBlock]? = nil
}
/// A requirement of a where clause.
enum WhereClauseRequirement: Equatable, Codable, Sendable {
/// A conformance requirement.
///
/// For example, in `extension Array where Element: Foo {`, the first tuple value is `Element` and the second `Foo`.
case conformance(String, String)
}
/// A description of a where clause.
///
/// For example: `extension Array where Element: Foo {`.
struct WhereClause: Equatable, Codable, Sendable {
/// One or more requirements to be added after the `where` keyword.
var requirements: [WhereClauseRequirement]
}
/// A description of an extension declaration.
///
/// For example `extension Foo {`.
struct ExtensionDescription: Equatable, Codable, Sendable {
/// An access modifier.
var accessModifier: AccessModifier? = nil
/// The name of the extended type.
///
/// For example, in `extension Foo {`, `onType` is `Foo`.
var onType: String
/// Additional type names that the extension conforms to.
///
/// For example: `["Sendable", "Codable"]`.
var conformances: [String] = []
/// A where clause constraining the extension declaration.
var whereClause: WhereClause? = nil
/// The declarations that the extension adds on the extended type.
var declarations: [Declaration]
}
/// A description of a struct declaration.
///
/// For example `struct Foo {`.
struct StructDescription: Equatable, Codable, Sendable {
/// An access modifier.
var accessModifier: AccessModifier? = nil
/// The name of the struct.
///
/// For example, in `struct Foo {`, `name` is `Foo`.
var name: String
/// The generic types of the struct.
var generics: [ExistingTypeDescription] = []
/// The type names that the struct conforms to.
///
/// For example: `["Sendable", "Codable"]`.
var conformances: [String] = []
/// A where clause constraining the struct declaration.
var whereClause: WhereClause? = nil
/// The declarations that make up the main struct body.
var members: [Declaration] = []
}
/// A description of an enum declaration.
///
/// For example `enum Bar {`.
struct EnumDescription: Equatable, Codable, Sendable {
/// A Boolean value that indicates whether the enum has a `@frozen`
/// attribute.
var isFrozen: Bool = false
/// A Boolean value that indicates whether the enum has the `indirect`
/// keyword.
var isIndirect: Bool = false
/// An access modifier.
var accessModifier: AccessModifier? = nil
/// The name of the enum.
///
/// For example, in `enum Bar {`, `name` is `Bar`.
var name: String
/// The type names that the enum conforms to.
///
/// For example: `["Sendable", "Codable"]`.
var conformances: [String] = []
/// The declarations that make up the enum body.
var members: [Declaration] = []
}
/// A description of a type reference.
indirect enum ExistingTypeDescription: Equatable, Codable, Sendable {
/// A type with the `any` keyword in front of it.
///
/// For example, `any Foo`.
case any(ExistingTypeDescription)
/// An optional type.
///
/// For example, `Foo?`.
case optional(ExistingTypeDescription)
/// A wrapper type generic over a list of wrapped types.
///
/// For example, `Wrapper<Wrapped>`.
case generic(wrapper: ExistingTypeDescription, wrapped: [ExistingTypeDescription])
/// A type reference represented by the components.
///
/// For example, `MyModule.Foo`.
case member([String])
/// An array with an element type.
///
/// For example, `[Foo]`.
case array(ExistingTypeDescription)
/// A dictionary where the key is `Swift.String` and the value is
/// the provided type.
///
/// For example, `[String: Foo]`.
case dictionaryValue(ExistingTypeDescription)
/// A type with the `some` keyword in front of it.
///
/// For example, `some Foo`.
case some(ExistingTypeDescription)
/// A closure signature as a type.
///
/// For example: `(String) async throws -> Int`.
case closure(ClosureSignatureDescription)
/// A wrapper type generic over a list of wrapped types.
///
/// For example, `Wrapper<Wrapped>`.
static func generic(
wrapper: ExistingTypeDescription,
wrapped: ExistingTypeDescription...
) -> Self {
return .generic(wrapper: wrapper, wrapped: Array(wrapped))
}
}
/// A description of a typealias declaration.
///
/// For example `typealias Foo = Int`.
struct TypealiasDescription: Equatable, Codable, Sendable {
/// An access modifier.
var accessModifier: AccessModifier?
/// The name of the typealias.
///
/// For example, in `typealias Foo = Int`, `name` is `Foo`.
var name: String
/// The existing type that serves as the underlying type of the alias.
///
/// For example, in `typealias Foo = Int`, `existingType` is `Int`.
var existingType: ExistingTypeDescription
}
/// A description of a protocol declaration.
///
/// For example `protocol Foo {`.
struct ProtocolDescription: Equatable, Codable, Sendable {
/// An access modifier.
var accessModifier: AccessModifier? = nil
/// The name of the protocol.
///
/// For example, in `protocol Foo {`, `name` is `Foo`.
var name: String
/// The type names that the protocol conforms to.
///
/// For example: `["Sendable", "Codable"]`.
var conformances: [String] = []
/// The function and property declarations that make up the protocol
/// requirements.
var members: [Declaration] = []
}
/// A description of a function parameter declaration.
///
/// For example, in `func foo(bar baz: String = "hi")`, the parameter
/// description represents `bar baz: String = "hi"`
struct ParameterDescription: Equatable, Codable, Sendable {
/// An external parameter label.
///
/// For example, in `bar baz: String = "hi"`, `label` is `bar`.
var label: String? = nil
/// An internal parameter name.
///
/// For example, in `bar baz: String = "hi"`, `name` is `baz`.
var name: String? = nil
/// The type name of the parameter.
///
/// For example, in `bar baz: String = "hi"`, `type` is `String`.
var type: ExistingTypeDescription? = nil
/// A default value of the parameter.
///
/// For example, in `bar baz: String = "hi"`, `defaultValue`
/// represents `"hi"`.
var defaultValue: Expression? = nil
/// An inout parameter type.
///
/// For example, `bar baz: inout String`.
var `inout`: Bool = false
}
/// A function kind: `func` or `init`.
enum FunctionKind: Equatable, Codable, Sendable {
/// An initializer.
///
/// For example: `init()`, or `init?()` when `failable` is `true`.
case initializer(failable: Bool)
/// A function or a method. Can be static.
///
/// For example `foo()`, where `name` is `foo`.
case function(
name: String,
isStatic: Bool
)
}
/// A function keyword, such as `async` and `throws`.
enum FunctionKeyword: Equatable, Codable, Sendable {
/// An asynchronous function.
case `async`
/// A function that can throw an error.
case `throws`
/// A function that can rethrow an error.
case `rethrows`
}
/// A description of a function signature.
///
/// For example: `func foo(bar: String) async throws -> Int`.
struct FunctionSignatureDescription: Equatable, Codable, Sendable {
/// An access modifier.
var accessModifier: AccessModifier? = nil
/// The kind of the function.
var kind: FunctionKind
/// The generic types of the function.
var generics: [ExistingTypeDescription] = []
/// The parameters of the function.
var parameters: [ParameterDescription] = []
/// The keywords of the function, such as `async` and `throws.`
var keywords: [FunctionKeyword] = []
/// The return type name of the function, such as `Int`.
var returnType: Expression? = nil
/// The where clause for a generic function.
var whereClause: WhereClause?
}
/// A description of a function definition.
///
/// For example: `func foo() { }`.
struct FunctionDescription: Equatable, Codable, Sendable {
/// The signature of the function.
var signature: FunctionSignatureDescription
/// The body definition of the function.
///
/// If nil, does not generate `{` and `}` at all for the body scope.
var body: [CodeBlock]? = nil
/// Creates a new function description.
/// - Parameters:
/// - signature: The signature of the function.
/// - body: The body definition of the function.
init(signature: FunctionSignatureDescription, body: [CodeBlock]? = nil) {
self.signature = signature
self.body = body
}
/// Creates a new function description.
/// - Parameters:
/// - accessModifier: An access modifier.
/// - kind: The kind of the function.
/// - parameters: The parameters of the function.
/// - keywords: The keywords of the function, such as `async`.
/// - returnType: The return type name of the function, such as `Int`.
/// - body: The body definition of the function.
init(
accessModifier: AccessModifier? = nil,
kind: FunctionKind,
generics: [ExistingTypeDescription] = [],
parameters: [ParameterDescription] = [],
keywords: [FunctionKeyword] = [],
returnType: Expression? = nil,
whereClause: WhereClause? = nil,
body: [CodeBlock]? = nil
) {
self.signature = .init(
accessModifier: accessModifier,
kind: kind,
generics: generics,
parameters: parameters,
keywords: keywords,
returnType: returnType,
whereClause: whereClause
)
self.body = body
}
/// Creates a new function description.
/// - Parameters:
/// - accessModifier: An access modifier.
/// - kind: The kind of the function.
/// - parameters: The parameters of the function.
/// - keywords: The keywords of the function, such as `async`.
/// - returnType: The return type name of the function, such as `Int`.
/// - body: The body definition of the function.
init(
accessModifier: AccessModifier? = nil,
kind: FunctionKind,
generics: [ExistingTypeDescription] = [],
parameters: [ParameterDescription] = [],
keywords: [FunctionKeyword] = [],
returnType: Expression? = nil,
whereClause: WhereClause? = nil,
body: [Expression]
) {
self.init(
accessModifier: accessModifier,
kind: kind,
generics: generics,
parameters: parameters,
keywords: keywords,
returnType: returnType,
whereClause: whereClause,
body: body.map { .expression($0) }
)
}
}
/// A description of a closure signature.
///
/// For example: `(String) async throws -> Int`.
struct ClosureSignatureDescription: Equatable, Codable, Sendable {
/// The parameters of the function.
var parameters: [ParameterDescription] = []
/// The keywords of the function, such as `async` and `throws.`
var keywords: [FunctionKeyword] = []
/// The return type name of the function, such as `Int`.
var returnType: Expression? = nil
/// The ``@Sendable`` attribute.
var sendable: Bool = false
/// The ``@escaping`` attribute.
var escaping: Bool = false
}
/// A description of the associated value of an enum case.
///
/// For example, in `case foo(bar: String)`, the associated value
/// represents `bar: String`.
struct EnumCaseAssociatedValueDescription: Equatable, Codable, Sendable {
/// A variable label.
///
/// For example, in `bar: String`, `label` is `bar`.
var label: String?
/// A variable type name.
///
/// For example, in `bar: String`, `type` is `String`.
var type: ExistingTypeDescription
}
/// An enum case kind.
///
/// For example: `case foo` versus `case foo(String)`, and so on.
enum EnumCaseKind: Equatable, Codable, Sendable {
/// A case with only a name.
///
/// For example: `case foo`.
case nameOnly
/// A case with a name and a raw value.
///
/// For example: `case foo = "Foo"`.
case nameWithRawValue(LiteralDescription)
/// A case with a name and associated values.
///
/// For example: `case foo(String)`.
case nameWithAssociatedValues([EnumCaseAssociatedValueDescription])
}
/// A description of an enum case.
///
/// For example: `case foo(String)`.
struct EnumCaseDescription: Equatable, Codable, Sendable {
/// The name of the enum case.
///
/// For example, in `case foo`, `name` is `foo`.
var name: String
/// The kind of the enum case.
var kind: EnumCaseKind = .nameOnly
}
/// A declaration of a Swift entity.
indirect enum Declaration: Equatable, Codable, Sendable {
/// A declaration that adds a comment on top of the provided declaration.
case commentable(Comment?, Declaration)
/// A declaration that adds a comment on top of the provided declaration.
case deprecated(DeprecationDescription, Declaration)
/// A variable declaration.
case variable(VariableDescription)
/// An extension declaration.
case `extension`(ExtensionDescription)
/// A struct declaration.
case `struct`(StructDescription)
/// An enum declaration.
case `enum`(EnumDescription)
/// A typealias declaration.
case `typealias`(TypealiasDescription)
/// A protocol declaration.
case `protocol`(ProtocolDescription)
/// A function declaration.
case function(FunctionDescription)
/// An enum case declaration.
case enumCase(EnumCaseDescription)
}
/// A description of a deprecation notice.
///
/// For example: `@available(*, deprecated, message: "This is going away", renamed: "other(param:)")`
struct DeprecationDescription: Equatable, Codable, Sendable {
/// A message used by the deprecation attribute.
var message: String?
/// A new name of the symbol, allowing the user to get a fix-it.
var renamed: String?
}
/// A description of an availability guard.
///
/// For example: `@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)`
struct AvailabilityDescription: Equatable, Codable, Sendable {
/// The array of OSes and versions which are specified in the availability guard.
var osVersions: [OSVersion]
init(osVersions: [OSVersion]) {
self.osVersions = osVersions
}
/// An OS and its version.
struct OSVersion: Equatable, Codable, Sendable {
var os: OS
var version: String
init(os: OS, version: String) {
self.os = os
self.version = version
}
}
/// One of the possible OSes.
// swift-format-ignore: DontRepeatTypeInStaticProperties
struct OS: Equatable, Codable, Sendable {
var name: String
init(name: String) {
self.name = name
}
static let macOS = Self(name: "macOS")
static let iOS = Self(name: "iOS")
static let watchOS = Self(name: "watchOS")
static let tvOS = Self(name: "tvOS")
static let visionOS = Self(name: "visionOS")
}
}
/// A description of an assignment expression.
///
/// For example: `foo = 42`.
struct AssignmentDescription: Equatable, Codable, Sendable {
/// The left-hand side expression, the variable to assign to.
///
/// For example, in `foo = 42`, `left` is `foo`.
var left: Expression
/// The right-hand side expression, the value to assign.
///
/// For example, in `foo = 42`, `right` is `42`.
var right: Expression
}
/// A switch case kind, either a `case` or a `default`.
enum SwitchCaseKind: Equatable, Codable, Sendable {
/// A case.
///
/// For example: `case let foo(bar):`.
case `case`(Expression, [String])
/// A case with multiple comma-separated expressions.
///
/// For example: `case "foo", "bar":`.
case multiCase([Expression])
/// A default. Spelled as `default:`.
case `default`
}
/// A description of a switch case definition.
///
/// For example: `case foo: print("foo")`.
struct SwitchCaseDescription: Equatable, Codable, Sendable {
/// The kind of the switch case.
var kind: SwitchCaseKind
/// The body of the switch case.
///
/// For example, in `case foo: print("foo")`, `body`
/// represents `print("foo")`.
var body: [CodeBlock]
}
/// A description of a switch statement expression.
///
/// For example: `switch foo {`.
struct SwitchDescription: Equatable, Codable, Sendable {
/// The expression evaluated by the switch statement.
///
/// For example, in `switch foo {`, `switchedExpression` is `foo`.
var switchedExpression: Expression
/// The cases defined in the switch statement.
var cases: [SwitchCaseDescription]
}
/// A description of an if branch and the corresponding code block.
///
/// For example: in `if foo { bar }`, the condition pair represents
/// `foo` + `bar`.
struct IfBranch: Equatable, Codable, Sendable {
/// The expressions evaluated by the if statement and their corresponding
/// body blocks. If more than one is provided, an `else if` branch is added.
///
/// For example, in `if foo { bar }`, `condition` is `foo`.
var condition: Expression
/// The body executed if the `condition` evaluates to true.
///
/// For example, in `if foo { bar }`, `body` is `bar`.
var body: [CodeBlock]
}
/// A description of an if[[/elseif]/else] statement expression.
///
/// For example: `if foo { } else if bar { } else { }`.
struct IfStatementDescription: Equatable, Codable, Sendable {
/// The primary `if` branch.
var ifBranch: IfBranch
/// Additional `else if` branches.
var elseIfBranches: [IfBranch]
/// The body of an else block.
///
/// No `else` statement is added when `elseBody` is nil.
var elseBody: [CodeBlock]?
}
/// A description of a do statement.
///
/// For example: `do { try foo() } catch { return bar }`.
struct DoStatementDescription: Equatable, Codable, Sendable {
/// The code blocks in the `do` statement body.
///
/// For example, in `do { try foo() } catch { return bar }`,
/// `doBody` is `try foo()`.
var doStatement: [CodeBlock]
/// The code blocks in the `catch` statement.
///
/// If nil, no `catch` statement is added.
///
/// For example, in `do { try foo() } catch { return bar }`,
/// `catchBody` is `return bar`.
var catchBody: [CodeBlock]?
}
/// A description of a value binding used in enums with associated values.
///
/// For example: `let foo(bar)`.
struct ValueBindingDescription: Equatable, Codable, Sendable {
/// The binding kind: `let` or `var`.
var kind: BindingKind
/// The bound values in a function call expression syntax.
///