-
Notifications
You must be signed in to change notification settings - Fork 5
/
CGCPlusPlusCPPCodeGenerator.swift
186 lines (168 loc) · 4.98 KB
/
CGCPlusPlusCPPCodeGenerator.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
public class CGCPlusPlusCPPCodeGenerator : CGCPlusPlusCodeGenerator {
public override var defaultFileExtension: String { return "cpp" }
public var UseHdrStop: Boolean = true
override func generateAll() {
generateHeader()
generateDirectives()
if let namespace = currentUnit.Namespace {
AppendLine();
generateImports()
generateForwards()
cppGenerateCPPGlobals()
generateTypeDefinitions()
}
generateFooter()
}
func cppGenerateCPPGlobals(){
var lastGlobal: CGGlobalDefinition? = nil
for g in currentUnit.Globals {
var visibility: CGMemberVisibilityKind = .Unspecified;
if let method = g as? CGGlobalFunctionDefinition {
visibility = .Unit;
}
if let variable = g as? CGGlobalVariableDefinition {
visibility = variable.Variable.Visibility;
}
// generate only .Unit & .Private visibility
if ((visibility == .Unit)||(visibility == .Private)){
if let lastGlobal = lastGlobal, globalNeedsSpace(g, afterGlobal: lastGlobal) {
AppendLine()
}
generateGlobal(g)
lastGlobal = g;
}
}
if lastGlobal != nil {
AppendLine()
}
}
override func generateHeader() {
super.generateHeader()
if UseHdrStop {
AppendLine("#pragma hdrstop")
}
var lnamespace = "";
if let namespace = currentUnit.Namespace {
lnamespace = namespace.Name;
}
if let fileName = currentUnit.FileName {
AppendLine("#include \"\(Path.ChangeExtension(fileName, ".h"))\"")
}
}
override func generateFooter(){
var lnamespace = currentUnit.FileName;
if isCBuilder() {
// c++Builder part
if let initialization = currentUnit.Initialization {
AppendLine("void __initialization_\(lnamespace)();")
generatePragma("startup __initialization_\(lnamespace)")
AppendLine("void __initialization_\(lnamespace)()")
AppendLine("{")
incIndent()
generateStatements(initialization)
decIndent()
AppendLine("}")
}
if let finalization = currentUnit.Finalization {
AppendLine("void __finalization_\(lnamespace)();")
generatePragma("exit __finalization_\(lnamespace)")
AppendLine("void __finalization_\(lnamespace)()")
AppendLine("{")
incIndent()
generateStatements(finalization)
decIndent()
AppendLine("}")
}
}
super.generateFooter()
}
override func generateImport(_ imp: CGImport) {
// ignore imports, they are in the .h
}
override func generateDirectives() {
if currentUnit.ImplementationDirectives.Count > 0 {
for d in currentUnit.ImplementationDirectives {
generateDirective(d)
}
AppendLine()
}
}
//
// Types
//
override func generateStructType(_ type: CGStructTypeDefinition) {
// structs don't appear in .m
}
override func generateInterfaceType(_ type: CGInterfaceTypeDefinition) {
// protocols don't appear in .m
}
//
// Type Members
//
override func generateMethodDefinition(_ method: CGMethodDefinition, type: CGTypeDefinition) {
cppGenerateMethodDefinitionHeader(method, type: type, header: false)
AppendLine()
AppendLine("{")
incIndent()
// process local variables
if let localVariables = method.LocalVariables, localVariables.Count > 0 {
for v in localVariables {
generateVariableDeclarationStatement(v);
}
}
generateStatements(method.Statements)
decIndent()
AppendLine("}")
}
override func generatePropertyDefinition(_ property: CGPropertyDefinition, type: CGTypeDefinition) {
if let getStatements = property.GetStatements, let method = property.GetterMethodDefinition() {
method.Name = "get__" + property.Name
if isCBuilder() {
method.CallingConvention = .Register
}
generateMethodDefinition(method, type: type)
}
if let setStatements = property.SetStatements, let method = property.SetterMethodDefinition() {
method.Name = "set__" + uppercaseFirstLetter(property.Name)
if isCBuilder() {
method.CallingConvention = .Register
}
generateMethodDefinition(method, type: type)
}
}
override func generateFieldDefinition(_ field: CGFieldDefinition, type: CGTypeDefinition) {
if type is CGGlobalTypeDefinition {
super.generateFieldDefinition(field, type: type)
}
}
override func generateConstructorDefinition(_ ctor: CGConstructorDefinition, type: CGTypeDefinition) {
cppGenerateMethodDefinitionHeader(ctor, type: type, header: false)
AppendLine()
AppendLine("{")
incIndent()
// process local variables
if let localVariables = ctor.LocalVariables, localVariables.Count > 0 {
for v in localVariables {
generateVariableDeclarationStatement(v);
}
}
generateStatements(ctor.Statements)
decIndent()
AppendLine("}")
}
override func generateDestructorDefinition(_ dtor: CGDestructorDefinition, type: CGTypeDefinition) {
cppGenerateMethodDefinitionHeader(dtor, type: type, header: false)
AppendLine()
AppendLine("{")
incIndent()
// process local variables
if let localVariables = dtor.LocalVariables, localVariables.Count > 0 {
for v in localVariables {
generateVariableDeclarationStatement(v);
}
}
generateStatements(dtor.Statements)
decIndent()
AppendLine("}")
}
}