-
Notifications
You must be signed in to change notification settings - Fork 5
/
CGObjectiveCMCodeGenerator.swift
104 lines (89 loc) · 2.75 KB
/
CGObjectiveCMCodeGenerator.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
public class CGObjectiveCMCodeGenerator : CGObjectiveCCodeGenerator {
public override var defaultFileExtension: String { return "m" }
override func generateHeader() {
if let fileName = currentUnit.FileName {
Append("#import \"\(Path.ChangeExtension(fileName, ".h"))\"")
}
}
override func generateImport(_ imp: CGImport) {
// ignore imports, they are in the .h
}
//
// Types
//
override func generateClassTypeStart(_ type: CGClassTypeDefinition) {
Append("@implementation ")
generateIdentifier(type.Name)
AppendLine()
//objcGenerateFields(type)
AppendLine()
}
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) {
generateMethodDefinitionHeader(method, type: type)
AppendLine()
AppendLine("{")
incIndent()
generateStatements(method.Statements)
decIndent()
AppendLine("}")
}
override func generateConstructorDefinition(_ ctor: CGConstructorDefinition, type: CGTypeDefinition) {
generateMethodDefinitionHeader(ctor, type: type)
AppendLine()
AppendLine("{")
incIndent()
generateStatements(ctor.Statements)
AppendLine("return self;")
decIndent()
AppendLine("}")
}
override func generatePropertyDefinition(_ property: CGPropertyDefinition, type: CGTypeDefinition) {
if property.GetStatements == nil && property.SetStatements == nil && property.GetExpression == nil && property.SetExpression == nil {
if property.Static {
assert(false, "static properties w/ storage are not supported for Objective-C")
} else {
Append("@synthesize ")
generateIdentifier(property.Name)
// 32-bit OS X Objective-C needs properies explicitly synthesized
Append(" = __p_")
generateIdentifier(property.Name, escaped: false)
AppendLine(";")
}
} else {
if let method = property.GetterMethodDefinition() {
method.Name = property.Name
generateMethodDefinition(method, type: type)
}
if let method = property.SetterMethodDefinition() {
method.Name = "set"+uppercaseFirstLetter(property.Name)
generateMethodDefinition(method, type: type)
}
}
}
override func generateFieldDefinition(_ field: CGFieldDefinition, type: CGTypeDefinition) {
if field.Static {
Append("static ")
objcGenerateStorageModifierPrefixIfNeeded(field.StorageModifier)
if let type = field.`Type` {
generateTypeReference(type)
if !objcTypeRefereneIsPointer(type) {
Append(" ")
}
} else {
Append("id ")
}
generateIdentifier(field.Name)
AppendLine(";")
}
// instance fields are generated in TypeStart
}
}