-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCGDelphiCodeGenerator.swift
707 lines (648 loc) · 25.1 KB
/
CGDelphiCodeGenerator.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
public class CGDelphiCodeGenerator : CGPascalCodeGenerator {
public init() {
super.init()
/*
// current as of — which version? need to check. XE7?
keywords = ["abstract", "and", "add", "async", "as", "begin", "break", "case", "class", "const", "constructor", "continue",
"delegate", "default", "div", "do", "downto", "each", "else", "empty", "end", "enum", "ensure", "event", "except",
"exit", "external", "false", "final", "finalizer", "finally", "flags", "for", "forward", "function", "global", "has",
"if", "implementation", "implements", "implies", "in", /*"index",*/ "inline", "inherited", "interface", "invariants", "is",
"iterator", "locked", "locking", "loop", "matching", "method", "mod", "namespace", "nested", "new", "nil", "not",
"nullable", "of", "old", "on", "operator", "or", "out", "override", "pinned", "partial", "private", "property",
"protected", "public", "reintroduce", "raise", "read", /*"readonly",*/ "remove", "repeat", "require", "result", "sealed",
"self", "sequence", "set", "shl", "shr", "static", "step", "then", "to", "true", "try", "type", "typeof", "until",
"unsafe", "uses", "using", "var", "virtual", "where", "while", "with", "write", "xor", "yield"].ToList() as! List<String>
*/
// Delphi Seattle + FPC reserved list
// http://docwiki.embarcadero.com/RADStudio/Seattle/en/Fundamental_Syntactic_Elements#Reserved_Words
// http://www.freepascal.org/docs-html/ref/refse3.html
keywords = ["absolute", "abstract", "alias", "and", "array", "as", "asm", "assembler", "at", "automated", "begin",
"bitpacked", "break", "case", "cdecl", "class", "const", "constructor", "continue", "cppdecl", "cvar", "default",
"deprecated", "destructor", "dispinterface", "dispose", "div", "do", "downto", "dynamic", "else", "end", "enumerator",
"except", "exit", "experimental", "export", "exports", "external", "false", "far", "far16", "file", "finalization",
"finally", "for", "forward", "function", "generic", "goto", "helper", "if", "implementation", "implements", "in",
"index", "inherited", "initialization", "inline", "interface", "interrupt", "iochecks", "is", "label", "library",
"local", "message", "mod", "name", "near", "new", "nil", "nodefault", "noreturn", "nostackframe", "not", "object",
"of", "oldfpccall", "on", "operator", "or", "otherwise", "out", "overload", "override", "packed", "pascal", "platform",
"private", "procedure", "program", "property", "protected", "public", "published", "raise", "read", "record", "register",
"reintroduce", "repeat", "resourcestring", "result", "safecall", "saveregisters", "self", "set", "shl", "shr", "softfloat",
"specialize", "static", "stdcall", "stored", "strict", "string", "then", "threadvar", "to", "true", "try", "type", "unaligned",
"unimplemented", "unit", "until", "uses", "var", "varargs", "virtual", "while", "with", "write", "xor"].ToList() as! List<String>
splitLinesLongerThan = 200;
}
override func escapeIdentifier(_ name: String) -> String {
if self.Dialect == .Delphi2009 {
return super.escapeIdentifier(name)
} else {
return name
}
}
//override func generateHeader() {
//Append("unit ")
//if let fileName = currentUnit.FileName {
//Append(fileName)
//} else if let namespace = currentUnit.Namespace {
//generateIdentifier(namespace.Name, alwaysEmitNamespace: true)
//} else {
//Append("{unit name unknown}")
//}
//AppendLine(";")
//AppendLine()
//super.generateHeader()
//}
internal func generateForwards(_ Types : List<CGTypeDefinition>) {
if Types.Count > 0 {
AppendLine("{ Forward declarations }")
var t = List<CGTypeDefinition>()
t.Add(Types)
if AlphaSortImplementationMembers {
t.Sort({return $0.Name.CompareTo/*IgnoreCase*/($1.Name)})
}
var list = List<CGTypeDefinition>()
for type in t {
if let type = type as? CGInterfaceTypeDefinition {
list.Add(type)
}
}
for type in t {
if let type = type as? CGClassTypeDefinition {
list.Add(type)
}
}
for index in (0 ..< list.Count) {
generateConditionStart(list, index)
if let type = list[index] as? CGInterfaceTypeDefinition {
AppendLine(type.Name + " = interface;")
} else if let type = list[index] as? CGClassTypeDefinition {
AppendLine(type.Name + " = class;")
}
generateConditionEnd(list, index)
}
AppendLine()
}
}
override func generateFooter() {
if let initialization = currentUnit.Initialization {
AppendLine("initialization")
incIndent()
generateStatements(initialization)
decIndent()
}
if let finalization = currentUnit.Finalization {
AppendLine("finalization")
incIndent()
generateStatements(finalization)
decIndent()
}
super.generateFooter()
}
override func pascalGenerateMemberVisibilityKeyword(_ visibility: CGMemberVisibilityKind) {
/* https://docwiki.embarcadero.com/RADStudio/Sydney/en/Classes_and_Objects_(Delphi)
#Private, Protected, and Public Members
A private member is invisible outside of the unit or program where its class is declared. In other words, a private method
cannot be called from another module, and a private field or property cannot be read or written to from another module.
By placing related class declarations in the same module, you can give each class access to the private members of another
class without making those members more widely accessible. For a member to be visible only inside its class, it needs to be
declared strict private.
A protected member is visible anywhere in the module where its class is declared and from any descendent class, regardless of
the module where the descendent class appears. A protected method can be called, and a protected field or property read or
written to, from the definition of any method belonging to a class that descends from the one where the protected member is
declared. Members that are intended for use only in the implementation of derived classes are usually protected.
A public member is visible wherever its class can be referenced.
#Strict Visibility Specifiers
In addition to private and protected visibility specifiers, the Delphi compiler supports additional visibility settings with
greater access constraints. These settings are strict private and strict protected visibility.
Class members with strict private visibility are accessible only within the class in which they are declared. They are not
visible to procedures or functions declared within the same unit. Class members with strict protected visibility are visible
within the class in which they are declared, and within any descendent class, regardless of where it is declared. Furthermore,
when instance members (those declared without the class or class var keywords) are declared strict private or strict protected,
they are inaccessible outside of the instance of a class in which they appear. An instance of a class cannot access strict private
or strict protected instance members in other instances of the same class.
#Published Members
Published members have the same visibility as public members. The difference is that run-time type information (RTTI) is generated
for published members. RTTI allows an application to query the fields and properties of an object dynamically and to locate its methods.
RTTI is used to access the values of properties when saving and loading form files, to display properties in the Object Inspector, and
to associate specific methods (called event handlers) with specific properties (called events).
Published properties are restricted to certain data types. Ordinal, string, class, interface, variant, and method-pointer types can be
published. So can set types, provided the upper and lower bounds of the base type have ordinal values from 0 through 31. (In other words,
the set must fit in a byte, word, or double word.) Any real type except Real48 can be published. Properties of an array type (as distinct
from array properties, discussed below) cannot be published.
Some properties, although publishable, are not fully supported by the streaming system. These include properties of record types, array
properties of all publishable types, and properties of enumerated types that include anonymous values. If you publish a property of this
kind, the Object Inspector will not display it correctly, nor will the property's value be preserved when objects are streamed to disk.
All methods are publishable, but a class cannot publish two or more overloaded methods with the same name. Fields can be published only
if they are of a class or interface type.
A class cannot have published members unless it is compiled in the {$M+} state or descends from a class compiled in the {$M+} state.
Most classes with published members derive from Classes.TPersistent, which is compiled in the {$M+} state, so it is seldom necessary
to use the $M directive.
*/
if Dialect == .Delphi2009 {
switch visibility {
case .Unspecified: break /* no-op */
case .Private: Append("strict private")
case .Unit: Append("private")
case .UnitAndProtected: fallthrough
case .AssemblyAndProtected: fallthrough
case .Protected: Append("strict protected")
case .UnitOrProtected: fallthrough
case .AssemblyOrProtected: Append("protected")
case .Assembly: fallthrough
case .Published: Append("published")
case .Public: Append("public")
}
} else {
switch visibility {
case .Unspecified: break /* no-op */
case .Private: fallthrough
case .Unit: Append("private")
case .UnitAndProtected: fallthrough
case .AssemblyAndProtected: fallthrough
case .UnitOrProtected: fallthrough
case .AssemblyOrProtected: fallthrough
case .Protected: Append("protected")
case .Assembly: fallthrough
case .Published: Append("published")
case .Public: Append("public")
}
}
}
override func pascalGenerateCallingConversion(_ callingConvention: CGCallingConventionKind){
switch callingConvention {
case .Register: break //default case
case .Pascal: Append(" pascal;") //backward compatibility
case .CDecl: Append(" cdecl;")
case .SafeCall: Append(" safecall;")
case .StdCall: Append(" stdcall;")
default:
}
}
override func generateEnumValueAccessExpression(_ expression: CGEnumValueAccessExpression) {
// don't prefix with typename in Delphi (but do in base Pascal/Oxygene)
generateIdentifier(expression.ValueName)
}
//
// Type Definitions
//
override func pascalGenerateImplementedInterface(_ member: CGMemberDefinition) {
if let implementsInterface = member.ImplementsInterface, member.ImplementsInterfaceMember == nil {
Append(" implements ")
generateTypeReference(implementsInterface)
Append(";")
}
}
override func pascalGenerateImplementedInterfaceMethodResolution(_ member: CGMethodDefinition, type: CGTypeDefinition) {
if let implementsInterface = member.ImplementsInterface, let implementsInterfaceMember = member.ImplementsInterfaceMember {
Append(pascalKeywordForMethod(member))
Append(" ")
generateTypeReference(implementsInterface, ignoreNullability: true)
Append(".")
generateIdentifier(implementsInterfaceMember)
Append(" = ")
generateIdentifier(member.Name)
AppendLine(";")
}
}
override func generateExtensionTypeStart(_ type: CGExtensionTypeDefinition) {
generateIdentifier(type.Name)
pascalGenerateGenericParameters(type.GenericParameters)
Append(" = ")
pascalGenerateTypeVisibilityPrefix(type.Visibility)
pascalGenerateStaticPrefix(type.Static)
if type.Ancestors.Count > 0 {
Append("class helper for ")
generateTypeReference(type.Ancestors[0], ignoreNullability: true)
}
AppendLine()
incIndent()
}
override func generateAll() {
generateHeader()
generateDirectives()
if !definitionOnly {
AppendLine("interface")
AppendLine()
}
generateAttributes()
pascalGenerateImports(currentUnit.Imports)
delphiGenerateGlobalInterfaceVariables()
delphiGenerateInterfaceTypeDefinition()
if !definitionOnly {
delphiGenerateGlobalInterfaceMethods()
AppendLine("implementation")
AppendLine()
delphiGenerateImplementationDirectives()
pascalGenerateImports(currentUnit.ImplementationImports)
delphiGenerateGlobalImplementations()
delphiGenerateImplementationTypeDefinition()
pascalGenerateTypeImplementations()
generateFooter()
}
}
final func delphiGenerateImplementationDirectives() {
if currentUnit.ImplementationDirectives.Count > 0 {
for index in (0 ..< currentUnit.ImplementationDirectives.Count) {
generateDirective(currentUnit.ImplementationDirectives, index)
}
AppendLine()
}
}
var needCR: Boolean = false;
final func delphiGenerateGlobalImplementations() {
// step1: generate global consts and vars
needCR = false;
var list = List<CGGlobalDefinition>()
for g in currentUnit.Globals {
if let global = g as? CGGlobalVariableDefinition {
if (global.Variable.Visibility == .Private)||(global.Variable.Visibility == .Unit) {
list.Add(global)
}
} else if let global = g as? CGGlobalFunctionDefinition {
// will be processed at step2
} else if let global = g as? CGGlobalPropertyDefinition {
// skip global properties
Append("// global properties are not supported.")
} else {
assert(false, "unsupported global found: \(typeOf(g).ToString())")
}
}
for index in (0 ..< list.Count) {
generateGlobal(list, index)
needCR = true;
}
if needCR { AppendLine();}
list.RemoveAll()
// step2: generate global methods
for g in currentUnit.Globals {
if let global = g as? CGGlobalVariableDefinition {
// already processed in step1
} else if let global = g as? CGGlobalFunctionDefinition {
list.Add(global)
} else if let global = g as? CGGlobalPropertyDefinition {
// skip global properties
Append("// global proerties are not supported.")
} else {
assert(false, "unsupported global found: \(typeOf(g).ToString())")
}
}
for index in (0 ..< list.Count) {
generateGlobal(list, index)
}
}
final func delphiGenerateGlobalInterfaceVariables() {
// generate global consts and vars
var list = List<CGGlobalDefinition>();
for g in currentUnit.Globals {
if let global = g as? CGGlobalVariableDefinition {
if global.Variable.Visibility != CGMemberVisibilityKind.Private {
list.Add(global)
}
} else if let global = g as? CGGlobalFunctionDefinition {
// will be processed in delphiGenerateGlobalInterfaceMethods
} else {
assert(false, "unsupported global found: \(typeOf(g).ToString())")
}
}
for index in (0 ..< list.Count) {
generateGlobal(list, index)
}
}
final func delphiGenerateGlobalInterfaceMethods() {
// generate global methods
var list = List<CGGlobalDefinition>()
for g in currentUnit.Globals {
if let global = g as? CGGlobalVariableDefinition {
// already processed in delphiGenerateGlobalInterfaceVariables
} else if let global = g as? CGGlobalFunctionDefinition {
if global.Function.Visibility != CGMemberVisibilityKind.Private {
list.Add(global)
}
} else {
assert(false, "unsupported global found: \(typeOf(g).ToString())")
}
}
for index in (0 ..< list.Count) {
generateGlobal(list, index)
}
}
func delphiGenerateInterfaceTypeDefinition() {
var t = List<CGTypeDefinition>()
for type in currentUnit.Types {
if type.Visibility != CGTypeVisibilityKind.Unit {
t.Add(type)
}
}
if t.Count > 0 {
AppendLine("type")
incIndent()
generateForwards(t)
generateTypeDefinitions(t)
decIndent()
}
}
func delphiGenerateImplementationTypeDefinition() {
var t = List<CGTypeDefinition>()
for type in currentUnit.Types {
if type.Visibility == CGTypeVisibilityKind.Unit {
t.Add(type)
}
}
if t.Count > 0 {
AppendLine("type")
incIndent()
generateForwards(t)
generateTypeDefinitions(t)
decIndent()
}
}
override func generateForToLoopStatement(_ statement: CGForToLoopStatement) {
Append("for ")
generateIdentifier(statement.LoopVariableName)
Append(" := ")
generateExpression(statement.StartValue)
if statement.Direction == CGLoopDirectionKind.Forward {
Append(" to ")
} else {
Append(" downto ")
}
generateExpression(statement.EndValue)
Append(" do")
generateStatementIndentedOrTrailingIfItsABeginEndBlock(statement.NestedStatement)
}
override func generateSelfExpression(_ expression: CGSelfExpression) {
Append("Self")
}
override func generateBinaryOperatorExpression(_ expression: CGBinaryOperatorExpression) {
// base class generates statements like
// if aIndex < 0 or aIndex >= Self.Count then begin
// which will be treated by Pascal/Delphi compiler as
// if aIndex < (0 or aIndex) >= Self.Count then begin
// lets put it into "()" if left or right is CGBinaryOperatorExpression
if let expression = expression.LefthandValue as? CGBinaryOperatorExpression {
Append("(")
}
generateExpression(expression.LefthandValue)
if let expression = expression.LefthandValue as? CGBinaryOperatorExpression {
Append(")")
}
Append(" ")
if let operatorString = expression.OperatorString {
Append(operatorString)
} else if let `operator` = expression.Operator {
generateBinaryOperator(`operator`)
}
Append(" ")
if let expression = expression.RighthandValue as? CGBinaryOperatorExpression {
Append("(")
}
generateExpression(expression.RighthandValue)
if let expression = expression.RighthandValue as? CGBinaryOperatorExpression {
Append(")")
}
}
override func generateEnumType(_ type: CGEnumTypeDefinition) {
generateIdentifier(type.Name)
Append(" = ")
Append("(")
helpGenerateCommaSeparatedList(type.Members, wrapAlways: wrapEnums) { m in
if let member = m as? CGEnumValueDefinition {
self.generateXmlDocumentationStatement(member.XmlDocumentation)
self.generateAttributes(member.Attributes, inline: true)
self.generateIdentifier(member.Name)
if let value = member.Value {
self.Append(" = ")
self.generateExpression(value)
}
}
}
Append(")")
if let baseType = type.BaseType {
Append(" of ")
generateTypeReference(baseType)
}
generateStatementTerminator()
}
override func generateInterfaceTypeStart(_ type: CGInterfaceTypeDefinition) {
generateIdentifier(type.Name)
pascalGenerateGenericParameters(type.GenericParameters)
Append(" = ")
pascalGenerateTypeVisibilityPrefix(type.Visibility)
pascalGenerateSealedPrefix(type.Sealed)
Append("interface")
pascalGenerateAncestorList(type)
pascalGenerateGenericConstraints(type.GenericParameters)
AppendLine()
if let k = type.InterfaceGuid {
AppendLine("['{" + k.ToString() + "}']")
}
incIndent()
}
override func generateFieldDefinition(_ variable: CGFieldDefinition, type: CGTypeDefinition) {
if variable.Static {
Append("class ")
}
if variable.Constant, let initializer = variable.Initializer {
Append("const ")
generateIdentifier(variable.Name)
if let type = variable.`Type` {
Append(": ")
generateTypeReference(type)
}
Append(" = ")
generateExpression(initializer)
} else {
if type is CGGlobalTypeDefinition {
Append("var ")
}
generateIdentifier(variable.Name)
if let type = variable.`Type` {
Append(": ")
generateTypeReference(type)
}
if let initializer = variable.Initializer { // todo: Oxygene only?
Append(" := ")
generateExpression(initializer)
}
}
generateStatementTerminator()
}
override func generatePointerTypeReference(_ type: CGPointerTypeReference) {
if let type = type.`Type` as? CGPredefinedTypeReference {
if type.Kind == CGPredefinedTypeKind.Void {
// generate "Pointer" instead of "^Pointer"
generateTypeReference(type)
return;
}
}
Append("^")
generateTypeReference(type.`Type`)
}
//
// Statements
//
override func generateConditionStart(_ condition: CGConditionalDefine, inline: Boolean) {
if let name = condition.Expression as? CGNamedIdentifierExpression {
Append("{$IFDEF ")
Append(name.Name)
} else {
//if let not = condition.Expression as? CGUnaryOperatorExpression, not.Operator == .Not,
if let not = condition.Expression as? CGUnaryOperatorExpression, not.Operator == CGUnaryOperatorKind.Not,
let name = not.Value as? CGNamedIdentifierExpression {
Append("{$IFNDEF ")
Append(name.Name)
} else {
Append("{$IF ")
generateExpression(condition.Expression)
}
}
Append("}")
if (!inline) {
AppendLine()
}
}
override func generateConditionEnd(_ condition: CGConditionalDefine, inline: Boolean) {
if let name = condition.Expression as? CGNamedIdentifierExpression {
Append("{$ENDIF}")
} else {
//if let not = condition.Expression as? CGUnaryOperatorExpression, not.Operator == .Not,
if let not = condition.Expression as? CGUnaryOperatorExpression, not.Operator == CGUnaryOperatorKind.Not,
let name = not.Value as? CGNamedIdentifierExpression {
Append("{$ENDIF}")
} else {
Append("{$IFEND}")
}
}
if (!inline) {
AppendLine()
}
}
// override func generateIfElseStatement(_ statement: CGIfThenElseStatement) {
// Append("if ")
// generateExpression(statement.Condition)
// Append(" then")
// var b = true;
// if let statement1 = statement.IfStatement as? CGBeginEndBlockStatement { b = false }
// if let elseStatement1 = statement.ElseStatement { b = false; }
// if b {
// /*generate code like
// if Result then
// System.Inc(fCurrentIndex)
// instead of
// if Result then begin
// System.Inc(fCurrentIndex)
// end;
// works only if else statement isn't used
// otherwise need to add global variable and handle it in "generateStatementTerminator"
// */
// generateStatementIndentedOrTrailingIfItsABeginEndBlock(statement.IfStatement)
// } else {
// AppendLine(" begin")
// incIndent()
// generateStatementSkippingOuterBeginEndBlock(statement.IfStatement)
// decIndent()
// Append("end")
// if let elseStatement = statement.ElseStatement {
// AppendLine()
// AppendLine("else begin")
// incIndent()
// generateStatementSkippingOuterBeginEndBlock(elseStatement)
// decIndent()
// Append("end")
// }
// generateStatementTerminator()
// }
// }
override func generateTryFinallyCatchStatement(_ statement: CGTryFinallyCatchStatement) {
if let finallyStatements = statement.FinallyStatements, finallyStatements.Count > 0 {
AppendLine("try")
incIndent()
}
if let catchBlocks = statement.CatchBlocks, catchBlocks.Count > 0 {
AppendLine("try")
incIndent()
}
generateStatements(statement.Statements)
if let finallyStatements = statement.FinallyStatements, finallyStatements.Count > 0 {
decIndent()
AppendLine("finally")
incIndent()
generateStatements(finallyStatements)
decIndent()
Append("end")
generateStatementTerminator()
}
if let catchBlocks = statement.CatchBlocks, catchBlocks.Count > 0 {
decIndent()
AppendLine("except")
incIndent()
for b in catchBlocks {
if let type = b.`Type` {
Append("on ")
if let name = b.Name {
generateIdentifier(name)
Append(": ")
}
generateTypeReference(type)
Append(" do ")
var b1 = false;
// if (b.Statements.Count == 1) && !(b.Statements[0] is CGBeginEndBlockStatement) { b1 = true}
if b1 {
//optimization
AppendLine()
incIndent()
generateStatementSkippingOuterBeginEndBlock(b.Statements[0])
decIndent()
} else {
AppendLine("begin")
incIndent()
generateStatements(b.Statements)
decIndent()
Append("end")
generateStatementTerminator()
}
} else {
assert(catchBlocks.Count == 1, "Can only have a single Catch block, if there is no type filter")
generateStatements(b.Statements)
}
}
decIndent()
Append("end")
generateStatementTerminator()
}
}
override func generatePredefinedTypeReference(_ type: CGPredefinedTypeReference, ignoreNullability: Boolean = false) {
switch (type.Kind) {
case .Int: Append("Integer")
case .UInt: Append("")
case .Int8: Append("Shortint")
case .UInt8: Append("Byte")
case .Int16: Append("Smallint")
case .UInt16: Append("Word")
case .Int32: Append("Integer")
case .UInt32: Append("Cardinal")
case .Int64: Append("Int64")
case .UInt64: Append("UInt64")
case .IntPtr: Append("IntPtr")
case .UIntPtr: Append("UIntPtr")
case .Single: Append("Single")
case .Double: Append("Double")
case .Boolean: Append("Boolean")
case .String: Append("string")
case .AnsiChar: Append("AnsiChar")
case .UTF16Char: Append("")
case .UTF32Char: Append("")
case .Dynamic: Append("{DYNAMIC}")
case .InstanceType: Append("{INSTANCETYPE}")
case .Void: Append("Pointer")
case .Object: Append("Object")
case .Class: Append("")
}
}
//override func generateCharacterLiteralExpression(_ expression: CGCharacterLiteralExpression) {
//var x = ord(expression.Value)
//if (x >= 32) && (x < 127) {
//Append("'"+expression.Value+"'");
//} else {
//super.generateCharacterLiteralExpression(expression);
//}
//}
}