-
Notifications
You must be signed in to change notification settings - Fork 5
/
CGCodeGenerator.swift
2111 lines (1791 loc) · 72.3 KB
/
CGCodeGenerator.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
public __abstract class CGCodeGenerator {
internal var currentUnit: CGCodeUnit!
internal var tabSize = 2
internal var useTabs = false
internal var definitionOnly = false
internal final var StatementTerminator = ";"
internal var keywords: List<String>?
internal var keywordsAreCaseSensitive = true // keywords List must be lowercase when this is set to false
internal var codeCompletionMode = false
internal var combineConditions = true /* debug option */
override public init() {
}
//
// Public APIS
//
public __abstract var defaultFileExtension: String { get }
public var omitNamespacePrefixes: Boolean = false
public var splitLinesLongerThan: Integer = 2048
public var preserveUnicodeCharactersInStringLiterals: Boolean = false
public var wrapEnums: Boolean = false
public final func GenerateUnit(_ unit: CGCodeUnit) -> String { // overload for VC# compastibility
return GenerateUnit(unit, definitionOnly: false)
}
public final func GenerateUnit(_ unit: CGCodeUnit, definitionOnly: Boolean /*= false*/) -> String {
// extra initialization
indent = 0
atStart = true
currentLocation.column = 0
currentLocation.virtualColumn = 0
currentLocation.offset = 0
// end extra initialization
currentUnit = unit
currentCode = StringBuilder()
self.definitionOnly = definitionOnly
generateAll()
return currentCode.ToString()
}
// VB uses different order: Directives, Import, Header
internal func generateType(_ type: CGTypeDefinition) {
generateHeader()
generateDirectives()
generateImports()
if type is CGGlobalTypeDefinition {
generateGlobals()
} else {
generateTypeDefinition(type)
}
generateFooter()
}
//
// Additional public APIs used by IDE Smarts & Co
//
public final func GenerateUnitForSingleType(_ type: CGTypeDefinition, unit: CGCodeUnit? = nil) -> String {
currentUnit = unit
currentCode = StringBuilder()
definitionOnly = false
generateType(type)
return currentCode.ToString()
}
public final func GenerateType(_ type: CGTypeDefinition, unit: CGCodeUnit? = nil) -> String {
currentUnit = unit
currentCode = StringBuilder()
definitionOnly = false
if type is CGGlobalTypeDefinition {
generateGlobals()
} else {
generateTypeDefinition(type)
}
return currentCode.ToString()
}
public final func GenerateTypeDefinitionOnly(_ type: CGTypeDefinition, unit: CGCodeUnit? = nil) -> String {
currentUnit = unit
currentCode = StringBuilder()
definitionOnly = true
if type is CGGlobalTypeDefinition {
generateGlobals()
} else {
generateTypeDefinition(type)
}
return currentCode.ToString()
}
public final func GenerateMember(_ member: CGMemberDefinition, type: CGTypeDefinition?, unit: CGCodeUnit? = nil) -> String {
return doGenerateMember(member, type: type, unit: unit, definitionOnly: false)
}
public final func GenerateMemberDefinition(_ member: CGMemberDefinition, type: CGTypeDefinition?, unit: CGCodeUnit? = nil) -> String {
return doGenerateMember(member, type: type, unit: unit, definitionOnly: true)
}
internal final func doGenerateMember(_ member: CGMemberDefinition, type: CGTypeDefinition?, unit: CGCodeUnit? = nil, definitionOnly: Boolean) -> String {
currentUnit = unit
currentCode = StringBuilder()
self.definitionOnly = definitionOnly
if let type = type {
generateTypeMember(member, type: type)
} else {
generateTypeMember(member, type: CGGlobalTypeDefinition.GlobalType)
}
return currentCode.ToString()
}
public final func GenerateMemberImplementation(_ member: CGMemberDefinition, type: CGTypeDefinition?, unit: CGCodeUnit? = nil) -> String? {
currentUnit = unit
currentCode = StringBuilder()
self.definitionOnly = definitionOnly
if let type = type {
doGenerateMemberImplementation(member, type: type)
} else {
doGenerateMemberImplementation(member, type: CGGlobalTypeDefinition.GlobalType)
}
return currentCode.ToString()
}
public final func GenerateParameterDefinition(_ parameter: CGParameterDefinition, unit: CGCodeUnit? = nil) -> String {
currentUnit = unit
currentCode = StringBuilder()
self.definitionOnly = definitionOnly
generateParameterDefinition(parameter)
return currentCode.ToString()
}
public final func GenerateStatement(_ statement: CGStatement, unit: CGCodeUnit? = nil) -> String? {
currentUnit = unit
currentCode = StringBuilder()
definitionOnly = false
generateStatement(statement)
return currentCode.ToString()
}
public func doGenerateMemberImplementation(_ member: CGMemberDefinition, type: CGTypeDefinition) {
// no-op for most languages, except Pascal
}
//
// Private
//
internal func generateAll() {
generateHeader()
generateDirectives()
generateImports()
generateForwards()
generateGlobals()
generateAttributes()
generateTypeDefinitions()
generateFooter()
}
internal final func incIndent(step: Int32 = 1) {
indent += step
}
internal final func decIndent(step: Int32 = 1) {
indent -= step
if indent < 0 {
indent = 0
}
}
/* */
public var failOnAsserts: Boolean = false
@inline(__always) internal func assert(_ ok: Boolean, _ message: String) {
if !ok {
assert(message)
}
}
internal func assert(_ message: String) {
if failOnAsserts {
throw Exception(message)
} else {
generateInlineComment(message)
}
}
/* These following functions *can* be overriden by descendants, if needed */
internal func generateHeader() {
// descendant can override, if needed
if let rawHeader = currentUnit.RawHeader, rawHeader.Count > 0 {
AppendLine()
for s in rawHeader {
AppendLine(s)
}
AppendLine()
}
if let comment = currentUnit.HeaderComment, comment.Lines.Count > 0 {
generateStatement(comment)
AppendLine()
}
}
internal func generateFooter() {
// descendant can override, if needed
if let rawHFooter = currentUnit.RawFooter, rawHFooter.Count > 0 {
AppendLine()
for s in rawHFooter {
AppendLine(s)
}
AppendLine()
}
}
internal func generateForwards() {
// descendant can override, if needed
}
internal func generateDirectives() {
if currentUnit.Directives.Count > 0 {
for index in (0 ..< currentUnit.Directives.Count) {
generateDirective(currentUnit.Directives, index)
}
AppendLine()
}
}
internal func compareCondition(_ a: CGConditionalDefine?,_ b: CGConditionalDefine?) -> Boolean {
if (a == nil) && (b == nil) {
return false
}
if ((a != nil) && (b == nil)) || ((a == nil) && (b != nil)) {
return false
}
var name_a: String = ""
var unary_a: Boolean = false
var name_b: String = ""
var unary_b: Boolean = false
var exp: CGExpression? = nil
if let a = a as? CGConditionalDefine {
exp = a.Expression
}
if exp is CGUnaryOperatorExpression {
if let exp1 = exp as! CGUnaryOperatorExpression {
unary_a = true
exp = exp1.Value
}
}
if let exp1 = exp as! CGNamedIdentifierExpression {
name_a = exp1.Name
} else {
// non standard case
return false
}
if let b = b as? CGConditionalDefine {
exp = b.Expression
}
if exp is CGUnaryOperatorExpression {
if let exp1 = exp as! CGUnaryOperatorExpression {
unary_b = true
exp = exp1.Value
}
}
if let exp1 = exp as! CGNamedIdentifierExpression {
name_b = exp1.Name
} else {
// non standard case
return false
}
return (name_a? == name_b?) && (unary_a == unary_b)
}
internal func SameCondition<T>(_ list: List<T>,_ index: Int32) -> Boolean {
if !combineConditions {
return false
}
// 1st item or last item
if (index == -1) || (index == list.Count - 1) {
return false
}
var a: CGConditionalDefine? = (list[index] as! ICGHasCondition).Condition
var b: CGConditionalDefine? = (list[index + 1] as! ICGHasCondition).Condition
return compareCondition(a, b)
}
internal func generateImports() {
if currentUnit.Imports.Count > 0 {
for index in (0 ..< currentUnit.Imports.Count) {
generateImport(currentUnit.Imports, index)
}
AppendLine()
}
if currentUnit.FileImports.Count > 0 {
for index in (0 ..< currentUnit.FileImports.Count) {
generateFileImport(currentUnit.FileImports, index)
}
AppendLine()
}
}
internal func generateTypeDefinitions() {
// descendant should not usually override
generateTypeDefinitions(currentUnit.Types)
}
internal func generateTypeDefinitions(_ Types : List<CGTypeDefinition>) {
// descendant should not usually override
for index in (0 ..< Types.Count) {
generateTypeDefinition(Types, index)
AppendLine()
}
}
internal func generateGlobals() {
var lastGlobal: CGGlobalDefinition? = nil
for index in (0 ..< currentUnit.Globals.Count) {
var g = currentUnit.Globals[index]
if let lastGlobal = lastGlobal, globalNeedsSpace(g, afterGlobal: lastGlobal) {
AppendLine()
}
generateGlobal(currentUnit.Globals, index)
lastGlobal = g
}
if lastGlobal != nil {
AppendLine()
}
}
internal func generateAttributes() {
var hadAttributes = false
for index in (0 ..< currentUnit.Attributes.Count) {
generateAttribute(currentUnit.Attributes, index)
hadAttributes = true
}
if hadAttributes {
AppendLine()
}
}
internal final func generateDirective(_ list: List<CGCompilerDirective>, _ index: Integer) {
var directive = list[index]
if let condition = directive.Condition {
if !SameCondition(list, index - 1) {
generateConditionStart(condition)
incIndent()
}
}
AppendLine(directive.Directive)
if let condition = directive.Condition {
if !SameCondition(currentUnit.Imports, index) {
decIndent()
generateConditionEnd(condition)
}
}
}
internal func generateSingleLineCommentPrefix() {
// descendant may override, but this will work for all current languages we support.
Append("// ")
}
internal func generateXmlDocumentationPrefix() {
// descendant may override, but this will work for all current languages we support.
Append("/// ")
}
internal func generateImport(_ `import`: CGImport) {
// descendant must override this or generateImports()
assert(false, "generateImport not implemented")
}
internal final func generateImport(_ list: List<CGImport>,_ index: Integer) {
var i = list[index]
generateConditionStart(list, index)
generateImport(i)
generateConditionEnd(list, index)
}
internal func generateFileImport(_ `import`: CGImport) {
// descendant should override if it supports file imports
}
internal final func generateFileImport(_ list: List<CGImport>,_ index: Integer) {
var i = list[index]
generateConditionStart(list, index)
generateFileImport(i)
generateConditionEnd(list, index)
}
//
// Helpers
//
internal func memberNeedsSpace(_ member: CGMemberDefinition, afterMember lastMember: CGMemberDefinition) -> Boolean {
if typeOf(member) != typeOf(lastMember) {
return true
}
if memberIsSingleLine(member) && memberIsSingleLine(lastMember) {
return false
}
return true
}
internal func globalNeedsSpace(_ global: CGGlobalDefinition, afterGlobal lastGlobal: CGGlobalDefinition) -> Boolean {
if globalIsSingleLine(global) && globalIsSingleLine(lastGlobal) {
return false
}
return true
}
internal func memberIsSingleLine(_ member: CGMemberDefinition) -> Boolean {
// reasonable default, works for all current languages
if member is CGFieldDefinition {
return true
}
if let property = member as? CGPropertyDefinition {
return !property.HasGetterMethod && !property.HasSetterMethod
}
return false
}
internal func globalIsSingleLine(_ global: CGGlobalDefinition) -> Boolean {
if global is CGGlobalVariableDefinition {
return true
}
return false
}
//
// Indentifiers
//
@inline(__always) internal final func generateIdentifier(_ name: String) {
generateIdentifier(name, escaped: true)
}
@inline(__always) internal final func generateIdentifier(_ name: String, escaped: Boolean) {
generateIdentifier(name, escaped: escaped, alwaysEmitNamespace: false)
}
@inline(__always) internal final func generateIdentifier(_ name: String, keywords: List<String>?) {
generateIdentifier(name, keywords: keywords, alwaysEmitNamespace: false)
}
@inline(__always) internal final func generateIdentifier(_ name: String, alwaysEmitNamespace: Boolean) {
generateIdentifier(name, escaped: true, alwaysEmitNamespace: alwaysEmitNamespace)
}
@inline(__always) internal final func generateIdentifier(_ name: String, escaped: Boolean, alwaysEmitNamespace: Boolean) {
generateIdentifier(name, keywords: escaped ? keywords : nil, alwaysEmitNamespace: alwaysEmitNamespace)
}
internal final func generateIdentifier(_ name: String, keywords: List<String>?, alwaysEmitNamespace: Boolean) {
if omitNamespacePrefixes && !alwaysEmitNamespace {
if name.Contains(".") {
if let parts = name.Split("."), length(parts) > 0 {
generateIdentifier(parts[length(parts)-1], keywords: keywords)
return
}
}
}
if let keywords = keywords {
if name.Contains(".") {
let parts = name.Split(".")
helpGenerateCommaSeparatedList(parts, separator: { self.Append(".") }, wrapMode: WrapMode.Never, callback: { part in self.generateIdentifier(part, escaped: true) })
} else {
var checkName = name
if !keywordsAreCaseSensitive {
checkName = checkName.ToLower()
}
if keywords?.Contains(checkName) {
Append(escapeIdentifier(name))
} else {
Append(name)
}
}
} else {
Append(name)
}
}
internal func escapeIdentifier(_ name: String) -> String {
// descendant must override this
assert(false, "escapeIdentifier not implemented")
return name
}
//
// Invariants
//
internal open var invariantCommentSeparator: String { " : " }
internal final func generateInvariantExpressions(_ expressions: List<CGInvariant>) {
for g in expressions {
generateExpression(g.Expression)
if let message = g.Message, length(message) > 0 {
Append(invariantCommentSeparator)
generateStringLiteralExpression(message.AsLiteralExpression())
}
generateStatementTerminator()
}
}
//
// Statements
//
internal final func generateStatements(_ statements: List<CGStatement>) {
// descendant should not override
for g in statements {
generateStatement(g)
}
}
internal final func generateStatements(variables statements: List<CGVariableDeclarationStatement>?) {
// descendant should not override
if let statements = statements {
for index in (0 ..< statements.Count) {
generateConditionStart(statements, index)
generateStatement(statements[index])
generateConditionEnd(statements, index)
}
}
}
internal final func generateStatementsSkippingOuterBeginEndBlock(_ statements: List<CGStatement>) {
//if statements.Count == 1, let block = statements[0] as? CGBeginEndBlockStatement {
if statements.Count == 1 && statements[0] is CGBeginEndBlockStatement {
//generateStatements(block.Statements)
generateStatements((statements[0] as! CGBeginEndBlockStatement).Statements)
} else {
generateStatements(statements)
}
}
internal final func generateStatementSkippingOuterBeginEndBlock(_ statement: CGStatement) {
if let block = statement as? CGBeginEndBlockStatement {
generateStatements(block.Statements)
} else {
generateStatement(statement)
}
}
internal func generateStatementIndentedUnlessItsABeginEndBlock(_ statement: CGStatement) {
if let block = statement as? CGBeginEndBlockStatement {
generateStatement(block)
} else {
incIndent()
generateStatement(statement)
decIndent()
}
}
internal func generateStatementsIndentedUnlessItsASingleBeginEndBlock(_ statements: List<CGStatement>) {
if statements.Count == 1 && statements[0] is CGBeginEndBlockStatement {
generateStatement(statements[0])
} else {
incIndent()
generateStatements(statements)
decIndent()
}
}
internal func generateStatementIndentedOrTrailingIfItsABeginEndBlock(_ statement: CGStatement) {
if let block = statement as? CGBeginEndBlockStatement {
Append(" ")
generateStatement(block)
} else {
AppendLine()
incIndent()
generateStatement(statement)
decIndent()
}
}
internal final func generateStatement(_ statement: CGStatement) {
statement.startLocation = currentLocation
// descendant should not override
if let commentStatement = statement as? CGXmlDocumentationStatement {
generateXmlDocumentationStatement(commentStatement)
} else if let commentStatement = statement as? CGCommentStatement {
generateCommentStatement(commentStatement)
} else if let commentStatement = statement as? CGSingleLineCommentStatement {
generateSingleLineCommentStatement(commentStatement)
} else if let commentStatement = statement as? CGCodeCommentStatement {
generateCodeCommentStatement(commentStatement)
} else if let rawStatement = statement as? CGRawStatement {
for line in rawStatement.Lines {
AppendLine(line)
}
} else if let statement = statement as? CGBeginEndBlockStatement {
generateBeginEndStatement(statement)
} else if let statement = statement as? CGConditionalBlockStatement {
generateConditionalBlockStatement(statement)
} else if let statement = statement as? CGIfThenElseStatement {
generateIfElseStatement(statement)
} else if let statement = statement as? CGForToLoopStatement {
generateForToLoopStatement(statement)
} else if let statement = statement as? CGForEachLoopStatement {
generateForEachLoopStatement(statement)
} else if let statement = statement as? CGWhileDoLoopStatement {
generateWhileDoLoopStatement(statement)
} else if let statement = statement as? CGDoWhileLoopStatement {
generateDoWhileLoopStatement(statement)
} else if let statement = statement as? CGInfiniteLoopStatement {
generateInfiniteLoopStatement(statement)
} else if let statement = statement as? CGSwitchStatement {
generateSwitchStatement(statement)
} else if let statement = statement as? CGLockingStatement {
generateLockingStatement(statement)
} else if let statement = statement as? CGUsingStatement {
generateUsingStatement(statement)
} else if let statement = statement as? CGAutoReleasePoolStatement {
generateAutoReleasePoolStatement(statement)
} else if let statement = statement as? CGCheckedStatement {
generateCheckedStatement(statement)
} else if let statement = statement as? CGCUnsafeStatement {
generateUnsafeStatement(statement)
} else if let statement = statement as? CGTryFinallyCatchStatement {
generateTryFinallyCatchStatement(statement)
} else if let statement = statement as? CGReturnStatement {
generateReturnStatement(statement)
} else if let statement = statement as? CGBreakStatement {
generateBreakStatement(statement)
} else if let statement = statement as? CGContinueStatement {
generateContinueStatement(statement)
} else if let statement = statement as? CGFallThroughStatement {
generateFallThroughStatement(statement)
} else if let statement = statement as? CGVariableDeclarationStatement {
generateVariableDeclarationStatement(statement)
} else if let statement = statement as? CGAssignmentStatement {
generateAssignmentStatement(statement)
} else if let statement = statement as? CGConstructorCallStatement {
generateConstructorCallStatement(statement)
} else if let statement = statement as? CGEmptyStatement {
AppendLine()
} else if let expression = statement as? CGGotoStatement {
generateGotoStatement(expression)
} else if let expression = statement as? CGLabelStatement {
generateLabelStatement(expression)
} else if let expression = statement as? CGLocalMethodStatement {
generateLocalMethodStatement(expression)
} else if let expression = statement as? CGExpression { // should be last but one
generateExpressionStatement(expression)
}
else {
assert(false, "unsupported statement found: \(typeOf(statement).ToString())")
}
//if !assigned(statement.endLocation) {
statement.endLocation = currentLocation
//} // 72543: Silver: cannot check if nullable struct is assigned
}
internal func generateCommentStatement(_ commentStatement: CGCommentStatement?) {
if let commentStatement = commentStatement {
if !atStart {
AppendLine()
}
for line in commentStatement.Lines {
generateSingleLineCommentPrefix()
AppendLine(line)
}
}
}
internal func generateXmlDocumentationStatement(_ xmlDocumentationStatement: CGXmlDocumentationStatement?) {
if let xmlDocumentationStatement = xmlDocumentationStatement {
if !atStart {
AppendLine()
}
for line in xmlDocumentationStatement.Lines {
generateXmlDocumentationPrefix()
AppendLine(line)
}
}
}
internal func isXmlDocumentationPresent(_ xmlDocumentationStatement: CGXmlDocumentationStatement?) -> Boolean {
if let xmlDocumentationStatement = xmlDocumentationStatement {
for line in xmlDocumentationStatement.Lines {
if !String.IsNullOrEmpty(line) {
return true
}
}
}
return false
}
internal func generateSingleLineCommentStatement(_ commentStatement: CGSingleLineCommentStatement?) {
if let commentStatement = commentStatement {
generateSingleLineCommentPrefix()
AppendLine(commentStatement.Comment)
}
}
private var inCodeCommentStatement = false
internal func generateCodeCommentStatement(_ commentStatement: CGCodeCommentStatement) {
assert(!inCodeCommentStatement, "Cannot nest CGCodeCommentStatements")
inCodeCommentStatement = true
__try {
generateStatement(commentStatement.CommentedOutStatement)
} __finally {
inCodeCommentStatement = false
}
}
internal func generateConditionalDefine(_ condition: CGConditionalDefine) {
inConditionExpression = true
defer { inConditionExpression = false }
generateExpression(condition.Expression)
}
internal func generateConditionStart(_ condition: CGConditionalDefine) {
// descendant must override this
assert(false, "generateConditionStart not implemented")
}
internal func generateConditionStart<T>(_ list: List<T>,_ index: Integer) {
if let condition = (list[index] as! ICGHasCondition).Condition {
if !SameCondition(list, index - 1) {
generateConditionStart(condition)
}
}
}
internal func generateConditionElse() {
// descendant must override this
assert(false, "generateConditionElse not implemented")
}
internal func generateConditionEnd(_ condition: CGConditionalDefine) {
// descendant must override this
assert(false, "generateConditionEnd not implemented")
}
internal func generateConditionEnd<T>(_ list: List<T>, _ index: Integer) {
if let condition = (list[index] as! ICGHasCondition).Condition {
if !SameCondition(list, index) {
generateConditionEnd(condition)
}
}
}
internal func generateConditionalBlockStatement(_ statement: CGConditionalBlockStatement) {
generateConditionStart(statement.Condition)
incIndent()
generateStatements(statement.Statements)
decIndent()
if let elseStatements = statement.ElseStatements {
generateConditionElse()
incIndent()
generateStatements(elseStatements)
decIndent()
}
generateConditionEnd(statement.Condition)
}
internal func generateBeginEndStatement(_ statement: CGBeginEndBlockStatement) {
// descendant must override this
assert(false, "generateBeginEndStatement not implemented")
}
internal func generateIfElseStatement(_ statement: CGIfThenElseStatement) {
// descendant must override this
assert(false, "generateIfElseStatement not implemented")
}
internal func generateForToLoopStatement(_ statement: CGForToLoopStatement) {
// descendant must override this
assert(false, "generateForToLoopStatement not implemented")
}
internal func generateForEachLoopStatement(_ statement: CGForEachLoopStatement) {
// descendant must override this
assert(false, "generateForEachLoopStatement not implemented")
}
internal func generateWhileDoLoopStatement(_ statement: CGWhileDoLoopStatement) {
// descendant must override this
assert(false, "generateWhileDoLoopStatementteImport not implemented")
}
internal func generateDoWhileLoopStatement(_ statement: CGDoWhileLoopStatement) {
// descendant must override this
assert(false, "generateDoWhileLoopStatement not implemented")
}
internal func generateInfiniteLoopStatement(_ statement: CGInfiniteLoopStatement) {
// descendant may override, but this will work for all languages.
generateWhileDoLoopStatement(CGWhileDoLoopStatement(CGBooleanLiteralExpression(true), statement.NestedStatement))
}
internal func generateSwitchStatement(_ statement: CGSwitchStatement) {
// descendant must override this
assert(false, "generateSwitchStatement not implemented")
}
internal func generateLockingStatement(_ statement: CGLockingStatement) {
// descendant must override this
assert(false, "generateLockingStatement not implemented")
}
internal func generateUsingStatement(_ statement: CGUsingStatement) {
// descendant must override this
assert(false, "generateUsingStatement not implemented")
}
internal func generateAutoReleasePoolStatement(_ statement: CGAutoReleasePoolStatement) {
// descendant must override this
assert(false, "generateAutoReleasePoolStatement not implemented")
generateStatement(statement.NestedStatement) // as fallback code
}
internal func generateCheckedStatement(_ statement: CGCheckedStatement) {
// descendant must override this
assert(false, "generateCheckedStatement not implemented")
generateStatement(statement.NestedStatement) // as fallback code
}
internal func generateUnsafeStatement(_ statement: CGCUnsafeStatement) {
// descendant must override this
assert(false, "generateUnsafeStatement not implemented")
generateStatement(statement.NestedStatement) // as fallback code
}
internal func generateTryFinallyCatchStatement(_ statement: CGTryFinallyCatchStatement) {
// descendant must override this
assert(false, "generateTryFinallyCatchStatement not implemented")
}
internal func generateReturnStatement(_ statement: CGReturnStatement) {
// descendant must override this
assert(false, "generateReturnStatement not implemented")
}
internal func generateYieldExpression(_ statement: CGYieldExpression) {
// descendant must override this
assert(false, "generateYieldExpression not implemented")
}
internal func generateThrowExpression(_ statement: CGThrowExpression) {
// descendant must override this
assert(false, "generateThrowExpression not implemented")
}
internal func generateBreakStatement(_ statement: CGBreakStatement) {
// descendant must override this
assert(false, "generateBreakStatement not implemented")
}
internal func generateContinueStatement(_ statement: CGContinueStatement) {
// descendant must override this
assert(false, "generateContinueStatement not implemented")
}
internal func generateFallThroughStatement(_ statement: CGFallThroughStatement) {
// descendant must override this
assert(false, "fallthrough is not supported")
}
internal func generateVariableDeclarationStatement(_ statement: CGVariableDeclarationStatement) {
// descendant must override this
assert(false, "generateVariableDeclarationStatement not implemented")
}
internal func generateAssignmentStatement(_ statement: CGAssignmentStatement) {
// descendant must override this
assert(false, "generateAssignmentStatement not implemented")
}
internal func generateConstructorCallStatement(_ statement: CGConstructorCallStatement) {
// descendant must override this
assert(false, "generateConstructorCallStatement not implemented")
}
internal func generateGotoStatement(_ statement: CGGotoStatement) {
// descendant must override this
assert(false, "generateGotoStatement not implemented")
}
internal func generateLabelStatement(_ statement: CGLabelStatement) {
// descendant must override this
assert(false, "generateLabelStatement not implemented")
}
internal func generateLocalMethodStatement(_ statement: CGLocalMethodStatement) {
// descendant must override this
assert(false, "generateLocalMethodStatement not implemented")
}
internal func generateStatementTerminator() {
AppendLine(StatementTerminator)
}
internal func generateExpressionStatement(_ expression: CGExpression) {
generateExpression(expression)
generateStatementTerminator()
}
//
// Expressions
//
internal final func generateExpression(_ expression: CGExpression, ignoreNullability: Boolean) {
if let expression = expression as? CGTypeReferenceExpression {
generateTypeReferenceExpression(expression, ignoreNullability: ignoreNullability)
} else {
generateExpression(expression)
}
}
internal final func generateExpression(_ expression: CGExpression) {
// descendant should not override
expression.startLocation = currentLocation
if let rawExpression = expression as? CGRawExpression {
helpGenerateCommaSeparatedList(rawExpression.Lines, separator: { self.AppendLine() }) { line in
self.Append(line)
}
} else if let expression = expression as? CGNamedIdentifierExpression {
generateNamedIdentifierExpression(expression)
} else if let expression = expression as? CGAssignedExpression {
generateAssignedExpression(expression)
} else if let expression = expression as? CGSizeOfExpression {
generateSizeOfExpression(expression)
} else if let expression = expression as? CGTypeOfExpression {
generateTypeOfExpression(expression)
} else if let expression = expression as? CGDefaultExpression {
generateDefaultExpression(expression)
} else if let expression = expression as? CGSelectorExpression {
generateSelectorExpression(expression)
} else if let expression = expression as? CGTypeCastExpression {
generateTypeCastExpression(expression)
} else if let expression = expression as? CGInheritedExpression {
generateInheritedExpression(expression)
} else if let expression = expression as? CGMappedExpression {
generateMappedExpression(expression)
} else if let expression = expression as? CGOldExpression {
generateOldExpression(expression)
} else if let expression = expression as? CGSelfExpression {
generateSelfExpression(expression)
} else if let expression = expression as? CGResultExpression {
generateResultExpression(expression)
} else if let expression = expression as? CGNilExpression {
generateNilExpression(expression)
} else if let expression = expression as? CGPropertyValueExpression {
generatePropertyValueExpression(expression)
} else if let expression = expression as? CGAwaitExpression {
generateAwaitExpression(expression)
} else if let expression = expression as? CGLocalMethodStatement {
generateLocalMethodStatement(expression)
} else if let expression = expression as? CGAnonymousMethodExpression {
generateAnonymousMethodExpression(expression)
} else if let expression = expression as? CGAnonymousTypeExpression {
generateAnonymousTypeExpression(expression)
} else if let expression = expression as? CGPointerDereferenceExpression {
generatePointerDereferenceExpression(expression)