-
Notifications
You must be signed in to change notification settings - Fork 5
/
CGObjectiveCHCodeGenerator.swift
176 lines (151 loc) · 4.22 KB
/
CGObjectiveCHCodeGenerator.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
public class CGObjectiveCHCodeGenerator : CGObjectiveCCodeGenerator {
public override var defaultFileExtension: String { return "h" }
override func generateForwards() {
for t in currentUnit.Types {
if let type = t as? CGClassTypeDefinition {
Append("@class ")
generateIdentifier(type.Name)
AppendLine(";")
} else if let type = t as? CGInterfaceTypeDefinition {
Append("@protocol ")
generateIdentifier(type.Name)
AppendLine(";")
}
}
}
override func generateImport(_ imp: CGImport) {
AppendLine("#import <\(imp.Name)/\(imp.Name).h>")
}
override func generateFileImport(_ imp: CGImport) {
AppendLine("#import \"\(imp.Name).h\"")
}
//
// Types
//
override func generateAliasType(_ type: CGTypeAliasDefinition) {
Append("typedef ")
generateTypeReference(type.ActualType)
Append(" ")
generateIdentifier(type.Name)
AppendLine(";")
}
override func generateBlockType(_ type: CGBlockTypeDefinition) {
}
override func generateEnumType(_ type: CGEnumTypeDefinition) {
Append("typedef NS_ENUM(")
if let baseType = type.BaseType {
generateTypeReference(baseType, ignoreNullability: true)
} else {
Append("NSUInteger")
}
Append(", ")
generateIdentifier(type.Name)
AppendLine(")")
AppendLine("{")
incIndent()
helpGenerateCommaSeparatedList(type.Members, wrapAlways: wrapEnums) { m in
if let member = m as? CGEnumValueDefinition {
self.generateIdentifier(type.Name+"_"+member.Name) // Obj-C enums must be unique
if let value = member.Value {
self.Append(" = ")
self.generateExpression(value)
}
}
}
AppendLine()
decIndent()
AppendLine("};")
}
override func generateClassTypeStart(_ type: CGClassTypeDefinition) {
Append("@interface ")
generateIdentifier(type.Name)
objcGenerateAncestorList(type)
AppendLine()
// 32-bit OS X Objective-C needs fields declared in @interface, not @implementation
objcGenerateFields(type)
AppendLine()
}
/*override func generateClassTypeEnd(_ type: CGClassTypeDefinition) {
decIndent()
AppendLine(@"end")
}*/
override func generateStructTypeStart(_ type: CGStructTypeDefinition) {
}
override func generateStructTypeEnd(_ type: CGStructTypeDefinition) {
}
override func generateInterfaceTypeStart(_ type: CGInterfaceTypeDefinition) {
Append("@protocol ")
generateIdentifier(type.Name)
//objcGenerateAncestorList(type)
//AppendLine()
AppendLine()
}
override func generateInterfaceTypeEnd(_ type: CGInterfaceTypeDefinition) {
AppendLine()
AppendLine("@end")
}
//
// Type Members
//
override func generateMethodDefinition(_ method: CGMethodDefinition, type: CGTypeDefinition) {
generateMethodDefinitionHeader(method, type: type)
AppendLine(";")
}
override func generateConstructorDefinition(_ ctor: CGConstructorDefinition, type: CGTypeDefinition) {
generateMethodDefinitionHeader(ctor, type: type)
AppendLine(";")
}
override func generatePropertyDefinition(_ property: CGPropertyDefinition, type: CGTypeDefinition) {
if property.Static {
Append("+ (")
if let type = property.`Type` {
objcGenerateStorageModifierPrefixIfNeeded(property.StorageModifier)
generateTypeReference(type)
if !objcTypeRefereneIsPointer(type) {
Append(" ")
}
} else {
Append("id ")
}
Append(")")
generateIdentifier(property.Name)
AppendLine(";")
} else {
if property.Virtuality == CGMemberVirtualityKind.Override || property.Virtuality == CGMemberVirtualityKind.Final {
Append("// overriden ") // we don't need to re-emit overriden properties in header?
}
Append("@property ")
Append("(")
if property.Atomic {
Append("atomic")
} else {
Append("nonatomic")
}
if let type = property.`Type` {
if type.IsClassType {
//switch type.StorageModifier {
//case .Strong: Append(", strong")
//case .Weak: Append(", weak")
//case .Unretained: Append(", unsafe_unretained")
//}
} else {
//todo?
}
}
if property.ReadOnly {
Append(", readonly")
}
Append(") ")
if let type = property.`Type` {
generateTypeReference(type)
if !objcTypeRefereneIsPointer(type) {
Append(" ")
}
} else {
Append("id ")
}
generateIdentifier(property.Name)
AppendLine(";")
}
}
}