├── .gitignore ├── Base.swift ├── CGCPlusPlusCPPCodeGenerator.swift ├── CGCPlusPlusCodeGenerator.swift ├── CGCPlusPlusHCodeGenerator.swift ├── CGCSharpCodeGenerator.swift ├── CGCStyleCodeGenerator.swift ├── CGCodeGenerator.swift ├── CGDelphiCodeGenerator.swift ├── CGGoCodeGenerator.swift ├── CGJavaCodeGenerator.swift ├── CGJavaScriptCodeGenerator.swift ├── CGObjectiveCCodeGenerator.swift ├── CGObjectiveCHCodeGenerator.swift ├── CGObjectiveCMCodeGenerator.swift ├── CGOxygeneCodeGenerator.swift ├── CGPascalCodeGenerator.swift ├── CGSkeletonCodeGenerator.swift ├── CGSwiftCodeGenerator.swift ├── CGVisualBasicNetCodeGenerator.swift ├── CodeDomToCG4.swift ├── CodeGen4.Cooper.elements ├── CodeGen4.Echoes.elements ├── CodeGen4.Island.elements ├── CodeGen4.Toffee.elements ├── CodeGen4.sln ├── Codegen4.Shared.elements ├── Codegen4.Shared.projitems ├── Expressions.swift ├── Extensions.swift ├── LICENSE.md ├── Properties └── AssemblyInfo.swift ├── README.md ├── Statements.swift ├── Tests ├── CodeGen4.Tests.Echoes.elements ├── CodeGen4.Tests.Echoes.sln ├── CodeGen4.Tests.Shared.elements ├── CodeGen4.Tests.Shared.projitems ├── Playground │ ├── Playground.elements │ ├── Program.swift │ └── Properties │ │ ├── App.ico │ │ ├── AssemblyInfo.swift │ │ ├── Resources.Designer.swift │ │ ├── Resources.resx │ │ ├── Settings.Designer.swift │ │ └── Settings.settings ├── Program.swift └── Properties │ ├── App.ico │ ├── AssemblyInfo.swift │ ├── Resources.Designer.swift │ ├── Resources.resx │ ├── Settings.Designer.swift │ └── Settings.settings ├── TypeDefinitions.swift └── TypeReferences.swift /.gitignore: -------------------------------------------------------------------------------- 1 | bin 2 | obj 3 | *.cache 4 | *.user 5 | -------------------------------------------------------------------------------- /Base.swift: -------------------------------------------------------------------------------- 1 | public enum CGPlatform { 2 | case Echoes 3 | case Cooper 4 | case Toffee 5 | case Gotham 6 | case Island 7 | } 8 | 9 | public struct CGLocation { 10 | public var column: Integer 11 | public var virtualColumn: Integer 12 | public var line: Integer 13 | public var offset: Integer 14 | } 15 | 16 | public __abstract class CGEntity { 17 | public var startLocation: CGLocation? 18 | public var endLocation: CGLocation? 19 | } 20 | 21 | public class CGCodeUnit { 22 | 23 | public var FileName: String? 24 | public var Namespace: CGNamespaceReference? 25 | public var HeaderComment = CGCommentStatement() 26 | public var RawHeader: List? 27 | public var RawFooter: List? 28 | 29 | public var Directives = List() /* will not be language agnostic */ 30 | public var Imports = List() 31 | public var FileImports = List() 32 | public var Types = List() 33 | public var Globals = List() 34 | public var Attributes = List() 35 | 36 | public var ImplementationDirectives = List() /* Pascal only */ 37 | public var ImplementationImports = List() /* Pascal only */ 38 | public var Initialization: List? /* Delphi only */ 39 | public var Finalization: List? /* Delphi only */ 40 | 41 | public init() { 42 | } 43 | public init(_ namespace: String) { 44 | Namespace = CGNamespaceReference(namespace) 45 | } 46 | public init(_ namespace: CGNamespaceReference) { 47 | Namespace = namespace 48 | } 49 | } 50 | 51 | public class CGCompilerDirective : ICGHasCondition { 52 | public var Directive: String /* will not be language agnostic */ 53 | public var Condition: CGConditionalDefine? 54 | 55 | public init(_ directive: String) { 56 | Directive = directive 57 | } 58 | public convenience init(_ directive: String, _ condition: CGConditionalDefine) { 59 | init(directive) 60 | Condition = condition 61 | } 62 | } 63 | 64 | public class CGImport : ICGHasCondition { 65 | public var Namespace: CGNamespaceReference? 66 | public var StaticClass: CGNamedTypeReference? 67 | public var Condition: CGConditionalDefine? 68 | 69 | public var Name: String! { 70 | if let ns = Namespace { 71 | return ns.Name 72 | } else if let sc = StaticClass { 73 | return sc.Name 74 | } 75 | return nil 76 | } 77 | 78 | public init(_ namespace: String) { 79 | Namespace = CGNamespaceReference(namespace) 80 | } 81 | public init(_ namespace: CGNamespaceReference) { 82 | Namespace = namespace 83 | } 84 | public init(_ staticClass: CGNamedTypeReference) { 85 | StaticClass = staticClass 86 | } 87 | } 88 | 89 | public class CGNamespaceReference { 90 | public var Name: String 91 | 92 | public init(_ name: String) { 93 | Name = name 94 | } 95 | } 96 | 97 | public class CGConditionalDefine { 98 | public var Expression: CGExpression 99 | 100 | public init(_ expression: CGExpression) { 101 | Expression = expression 102 | } 103 | 104 | public convenience init(_ define: String) { 105 | init(CGNamedIdentifierExpression(define)) 106 | } 107 | 108 | public init(_ define: String, inverted: Boolean) { 109 | if inverted { 110 | Expression = CGUnaryOperatorExpression.NotExpression(CGNamedIdentifierExpression(define)) 111 | } else { 112 | Expression = CGNamedIdentifierExpression(define) 113 | } 114 | } 115 | } 116 | 117 | public __abstract class CGGlobalDefinition { 118 | public var RawHeader: List? 119 | public var RawFooter: List? 120 | } 121 | 122 | public class CGGlobalFunctionDefinition : CGGlobalDefinition, ICGHasCondition { 123 | public let Function: CGMethodDefinition 124 | 125 | public init(_ function: CGMethodDefinition) { 126 | Function = function 127 | } 128 | 129 | public var Condition: CGConditionalDefine? { Function.Condition } 130 | } 131 | 132 | public class CGGlobalVariableDefinition : CGGlobalDefinition, ICGHasCondition { 133 | public let Variable: CGFieldDefinition 134 | 135 | public init(_ variable: CGFieldDefinition) { 136 | Variable = variable 137 | } 138 | 139 | public var Condition: CGConditionalDefine? { Variable.Condition } 140 | } 141 | 142 | public class CGGlobalPropertyDefinition : CGGlobalDefinition, ICGHasCondition { 143 | public let Property: CGPropertyDefinition 144 | 145 | public init(_ property: CGPropertyDefinition) { 146 | Property = property 147 | } 148 | 149 | public var Condition: CGConditionalDefine? { Property.Condition } 150 | } 151 | 152 | public class CGInvariant { 153 | public var Expression: CGExpression 154 | public var Message: String? 155 | 156 | public init(_ expression: CGExpression) { 157 | Expression = expression 158 | } 159 | public init(_ expression: CGExpression, _ message: String) { 160 | Expression = expression 161 | if length(message) > 0 { 162 | Message = message; 163 | } 164 | } 165 | } -------------------------------------------------------------------------------- /CGCPlusPlusCPPCodeGenerator.swift: -------------------------------------------------------------------------------- 1 | public class CGCPlusPlusCPPCodeGenerator : CGCPlusPlusCodeGenerator { 2 | public override var defaultFileExtension: String { return "cpp" } 3 | public var UseHdrStop: Boolean = true 4 | 5 | override func generateAll() { 6 | generateHeader() 7 | generateDirectives() 8 | if let namespace = currentUnit.Namespace { 9 | AppendLine(); 10 | generateImports() 11 | generateForwards() 12 | cppGenerateCPPGlobals() 13 | generateTypeDefinitions() 14 | } 15 | generateFooter() 16 | } 17 | 18 | func cppGenerateCPPGlobals(){ 19 | var lastGlobal: CGGlobalDefinition? = nil 20 | var list = List() 21 | for g in currentUnit.Globals { 22 | var visibility: CGMemberVisibilityKind = .Unspecified; 23 | if let method = g as? CGGlobalFunctionDefinition { 24 | visibility = .Unit; 25 | } 26 | if let variable = g as? CGGlobalVariableDefinition { 27 | visibility = variable.Variable.Visibility; 28 | } 29 | // generate only .Unit & .Private visibility 30 | if ((visibility == .Unit)||(visibility == .Private)){ 31 | list.Add(g) 32 | } 33 | } 34 | 35 | for index in (0 ..< list.Count) { 36 | var g = list[index] 37 | if let lastGlobal = lastGlobal, globalNeedsSpace(g, afterGlobal: lastGlobal) { 38 | AppendLine() 39 | } 40 | 41 | generateGlobal(list, index) 42 | lastGlobal = g 43 | } 44 | if lastGlobal != nil { 45 | AppendLine() 46 | } 47 | } 48 | 49 | override func generateHeader() { 50 | super.generateHeader() 51 | if UseHdrStop { 52 | AppendLine("#pragma hdrstop") 53 | } 54 | var lnamespace = ""; 55 | if let namespace = currentUnit.Namespace { 56 | lnamespace = namespace.Name; 57 | } 58 | if let fileName = currentUnit.FileName { 59 | AppendLine("#include \"\(Path.ChangeExtension(fileName, ".h"))\"") 60 | } 61 | } 62 | 63 | override func generateFooter(){ 64 | var lnamespace = currentUnit.FileName; 65 | if isCBuilder() { 66 | // c++Builder part 67 | if let initialization = currentUnit.Initialization { 68 | AppendLine("void __initialization_\(lnamespace)();") 69 | generatePragma("startup __initialization_\(lnamespace)") 70 | AppendLine("void __initialization_\(lnamespace)()") 71 | AppendLine("{") 72 | incIndent() 73 | generateStatements(initialization) 74 | decIndent() 75 | AppendLine("}") 76 | } 77 | if let finalization = currentUnit.Finalization { 78 | AppendLine("void __finalization_\(lnamespace)();") 79 | generatePragma("exit __finalization_\(lnamespace)") 80 | AppendLine("void __finalization_\(lnamespace)()") 81 | AppendLine("{") 82 | incIndent() 83 | generateStatements(finalization) 84 | decIndent() 85 | AppendLine("}") 86 | } 87 | } 88 | super.generateFooter() 89 | 90 | } 91 | 92 | override func generateImport(_ imp: CGImport) { 93 | // ignore imports, they are in the .h 94 | } 95 | 96 | override func generateDirectives() { 97 | if currentUnit.ImplementationDirectives.Count > 0 { 98 | for index in (0 ..< currentUnit.ImplementationDirectives.Count) { 99 | generateDirective(currentUnit.ImplementationDirectives, index) 100 | } 101 | AppendLine() 102 | } 103 | } 104 | 105 | // 106 | // Types 107 | // 108 | 109 | override func generateStructType(_ type: CGStructTypeDefinition) { 110 | // structs don't appear in .m 111 | } 112 | 113 | override func generateInterfaceType(_ type: CGInterfaceTypeDefinition) { 114 | // protocols don't appear in .m 115 | } 116 | 117 | // 118 | // Type Members 119 | // 120 | 121 | override func generateMethodDefinition(_ method: CGMethodDefinition, type: CGTypeDefinition) { 122 | cppGenerateMethodDefinitionHeader(method, type: type, header: false) 123 | AppendLine() 124 | AppendLine("{") 125 | incIndent() 126 | // process local variables 127 | if let localVariables = method.LocalVariables, localVariables.Count > 0 { 128 | for v in localVariables { 129 | generateVariableDeclarationStatement(v); 130 | } 131 | } 132 | generateStatements(method.Statements) 133 | decIndent() 134 | AppendLine("}") 135 | } 136 | 137 | override func generatePropertyDefinition(_ property: CGPropertyDefinition, type: CGTypeDefinition) { 138 | if let getStatements = property.GetStatements, let method = property.GetterMethodDefinition() { 139 | method.Name = "get__" + property.Name 140 | if isCBuilder() { 141 | method.CallingConvention = .Register 142 | } 143 | generateMethodDefinition(method, type: type) 144 | } 145 | if let setStatements = property.SetStatements, let method = property.SetterMethodDefinition() { 146 | method.Name = "set__" + uppercaseFirstLetter(property.Name) 147 | if isCBuilder() { 148 | method.CallingConvention = .Register 149 | } 150 | generateMethodDefinition(method, type: type) 151 | } 152 | } 153 | 154 | override func generateFieldDefinition(_ field: CGFieldDefinition, type: CGTypeDefinition) { 155 | if type is CGGlobalTypeDefinition { 156 | super.generateFieldDefinition(field, type: type) 157 | } 158 | } 159 | 160 | override func generateConstructorDefinition(_ ctor: CGConstructorDefinition, type: CGTypeDefinition) { 161 | cppGenerateMethodDefinitionHeader(ctor, type: type, header: false) 162 | AppendLine() 163 | AppendLine("{") 164 | incIndent() 165 | // process local variables 166 | if let localVariables = ctor.LocalVariables, localVariables.Count > 0 { 167 | for v in localVariables { 168 | generateVariableDeclarationStatement(v); 169 | } 170 | } 171 | generateStatements(ctor.Statements) 172 | decIndent() 173 | AppendLine("}") 174 | } 175 | 176 | override func generateDestructorDefinition(_ dtor: CGDestructorDefinition, type: CGTypeDefinition) { 177 | cppGenerateMethodDefinitionHeader(dtor, type: type, header: false) 178 | AppendLine() 179 | AppendLine("{") 180 | incIndent() 181 | // process local variables 182 | if let localVariables = dtor.LocalVariables, localVariables.Count > 0 { 183 | for v in localVariables { 184 | generateVariableDeclarationStatement(v); 185 | } 186 | } 187 | generateStatements(dtor.Statements) 188 | decIndent() 189 | AppendLine("}") 190 | } 191 | 192 | 193 | } -------------------------------------------------------------------------------- /CGCPlusPlusHCodeGenerator.swift: -------------------------------------------------------------------------------- 1 | public class CGCPlusPlusHCodeGenerator: CGCPlusPlusCodeGenerator { 2 | 3 | public override var defaultFileExtension: String { return "h" } 4 | 5 | override func generateAll() { 6 | generateHeader() 7 | generateDirectives() 8 | generateAttributes() 9 | if let namespace = currentUnit.Namespace { 10 | AppendLine(); 11 | cppHgenerateImports() 12 | AppendLine("namespace \(namespace.Name)"); 13 | AppendLine("{") 14 | incIndent(); 15 | generateForwards() 16 | cppGenerateHeaderGlobals() 17 | generateTypeDefinitions() 18 | decIndent() 19 | AppendLine("}") 20 | AppendLine("using namespace \(namespace.Name);"); 21 | } 22 | generateFooter() 23 | } 24 | 25 | func cppGenerateHeaderGlobals(){ 26 | var lastGlobal: CGGlobalDefinition? = nil 27 | var list = List() 28 | for g in currentUnit.Globals { 29 | var visibility: CGMemberVisibilityKind = .Unspecified; 30 | if let method = g as? CGGlobalFunctionDefinition { 31 | visibility = method.Function.Visibility; 32 | } 33 | if let variable = g as? CGGlobalVariableDefinition { 34 | visibility = variable.Variable.Visibility; 35 | } 36 | // skip .Unit & .Private visibility - they will be put into .cpp 37 | if !((visibility == .Unit)||(visibility == .Private)){ 38 | list.Add(g) 39 | } 40 | } 41 | for index in (0 ..< list.Count) { 42 | var g = list[index] 43 | if let lastGlobal = lastGlobal, globalNeedsSpace(g, afterGlobal: lastGlobal) { 44 | AppendLine() 45 | } 46 | generateGlobal(list, index) 47 | lastGlobal = g 48 | } 49 | if lastGlobal != nil { 50 | AppendLine() 51 | } 52 | } 53 | 54 | func cppHgenerateImports(){ 55 | var needLF = false; 56 | if currentUnit.Imports.Count > 0 { 57 | for index in (0 ..< currentUnit.Imports.Count) { 58 | generateImport(currentUnit.Imports, index) 59 | } 60 | needLF = true; 61 | } 62 | if currentUnit.ImplementationImports.Count > 0 { 63 | for index in (0 ..< currentUnit.ImplementationImports.Count) { 64 | generateImport(currentUnit.ImplementationImports, index) 65 | } 66 | needLF = true; 67 | } 68 | if needLF {AppendLine()} 69 | } 70 | 71 | override func generateHeader() { 72 | super.generateHeader() 73 | var lnamespace = currentUnit.FileName+"H"; 74 | AppendLine("#ifndef \(lnamespace)"); 75 | AppendLine("#define \(lnamespace)"); 76 | AppendLine(); 77 | 78 | if isCBuilder() { 79 | generatePragma("delphiheader begin"); 80 | generatePragma("option push"); 81 | generatePragma("option -w- // All warnings off"); 82 | generatePragma("option -Vx // Zero-length empty class member functions"); 83 | generatePragma("pack(push,8)"); 84 | } 85 | } 86 | 87 | override func generateFooter(){ 88 | var lnamespace = currentUnit.FileName+"H"; 89 | if isCBuilder() { 90 | generatePragma("pack(pop)"); 91 | generatePragma("option pop"); 92 | AppendLine(""); 93 | generatePragma("delphiheader end."); 94 | } 95 | AppendLine("#endif // \(lnamespace)"); 96 | super.generateFooter() 97 | } 98 | 99 | override func generateForwards() { 100 | for t in currentUnit.Types { 101 | if let type = t as? CGClassTypeDefinition { 102 | Append("class ") 103 | if isCBuilder() { 104 | Append("DELPHICLASS ") 105 | } 106 | generateIdentifier(type.Name) 107 | AppendLine(";") 108 | } else if let type = t as? CGInterfaceTypeDefinition { 109 | Append("__interface ") 110 | generateIdentifier(type.Name) 111 | AppendLine(";") 112 | 113 | if isCBuilder() { 114 | //typedef System::DelphiInterface _di_IMegaDemoService; 115 | Append("typedef System::DelphiInterface<"); 116 | generateIdentifier(type.Name); 117 | Append("> _di_"); 118 | generateIdentifier(type.Name); 119 | AppendLine(";"); 120 | } 121 | } 122 | } 123 | } 124 | 125 | override func generateImport(_ imp: CGImport) { 126 | 127 | if imp.StaticClass != nil { 128 | AppendLine("#include <\(imp.Name)>") 129 | } else { 130 | AppendLine("#include \"\(imp.Name)\"") 131 | } 132 | } 133 | 134 | // 135 | // Types 136 | // 137 | 138 | override func generateAliasType(_ type: CGTypeAliasDefinition) { 139 | Append("typedef ") 140 | generateTypeReference(type.ActualType) 141 | Append(" ") 142 | generateIdentifier(type.Name) 143 | AppendLine(";") 144 | } 145 | 146 | override func generateBlockType(_ type: CGBlockTypeDefinition) { 147 | 148 | } 149 | 150 | override func generateEnumType(_ type: CGEnumTypeDefinition) { 151 | 152 | //#pragma option push -b- 153 | //enum TSex { 154 | // TSex_sxMale, 155 | // TSex_sxFemale 156 | // }; 157 | //#pragma option pop 158 | 159 | if isCBuilder() { 160 | generatePragma("option push -b-"); 161 | } 162 | Append("enum ") 163 | generateIdentifier(type.Name) 164 | AppendLine("{") 165 | incIndent() 166 | helpGenerateCommaSeparatedList(type.Members, wrapAlways: wrapEnums) { m in 167 | if let member = m as? CGEnumValueDefinition { 168 | self.generateIdentifier(member.Name) 169 | if let value = member.Value { 170 | self.Append(" = ") 171 | self.generateExpression(value) 172 | } 173 | } 174 | } 175 | 176 | AppendLine() 177 | decIndent() 178 | AppendLine("};") 179 | if isCBuilder() { 180 | generatePragma("option pop"); 181 | } 182 | } 183 | 184 | override func generateClassTypeStart(_ type: CGClassTypeDefinition) { 185 | // if isCBuilder() { 186 | // AppendLine("class DELPHICLASS \(type.Name);"); 187 | // } 188 | Append("class ") 189 | generateIdentifier(type.Name) 190 | cppGenerateAncestorList(type) 191 | AppendLine() 192 | AppendLine("{") 193 | incIndent(); 194 | if isCBuilder() { 195 | if type.Ancestors.Count > 0 { 196 | for a in 0 ..< type.Ancestors.Count { 197 | if let ancestor = type.Ancestors[a] { 198 | Append("typedef "); 199 | generateTypeReference(ancestor, ignoreNullability: true); 200 | AppendLine(" inherited;") 201 | } 202 | } 203 | 204 | } 205 | } 206 | // cppGenerateFields(type) 207 | AppendLine() 208 | if isCBuilder() { 209 | // generate empty "__published:" 210 | decIndent(); 211 | cppHGenerateMemberVisibilityPrefix(CGMemberVisibilityKind.Published); 212 | incIndent(); 213 | } 214 | } 215 | 216 | override func generateClassTypeEnd(_ type: CGClassTypeDefinition) { 217 | decIndent() 218 | AppendLine() 219 | AppendLine("};") 220 | } 221 | 222 | override func generateStructTypeStart(_ type: CGStructTypeDefinition) { 223 | Append("struct "); 224 | generateIdentifier(type.Name) 225 | cppGenerateAncestorList(type) 226 | AppendLine() 227 | AppendLine("{") 228 | incIndent(); 229 | } 230 | 231 | override func generateStructTypeEnd(_ type: CGStructTypeDefinition) { 232 | decIndent(); 233 | AppendLine() 234 | AppendLine("}") 235 | } 236 | 237 | override func generateInterfaceTypeStart(_ type: CGInterfaceTypeDefinition) { 238 | // Append("__interface ") 239 | // generateIdentifier(type.Name) 240 | // AppendLine(";"); 241 | Append("__interface ") 242 | if isCBuilder() { 243 | if let k = type.InterfaceGuid { 244 | Append("INTERFACE_UUID(\"{" + k.ToString() + "}\") "); 245 | } 246 | } 247 | generateIdentifier(type.Name) 248 | cppGenerateAncestorList(type) 249 | AppendLine() 250 | AppendLine("{") 251 | incIndent() 252 | } 253 | 254 | override func generateInterfaceTypeEnd(_ type: CGInterfaceTypeDefinition) { 255 | decIndent() 256 | AppendLine() 257 | AppendLine("};") 258 | } 259 | 260 | // 261 | // Type Members 262 | // 263 | 264 | override func generateMethodDefinition(_ method: CGMethodDefinition, type: CGTypeDefinition) { 265 | cppGenerateMethodDefinitionHeader(method, type: type, header: true) 266 | AppendLine(";") 267 | } 268 | 269 | override func generateConstructorDefinition(_ ctor: CGConstructorDefinition, type: CGTypeDefinition) { 270 | cppGenerateMethodDefinitionHeader(ctor, type: type, header: true) 271 | AppendLine(";") 272 | } 273 | 274 | override func generateDestructorDefinition(_ dtor: CGDestructorDefinition, type: CGTypeDefinition) { 275 | cppGenerateMethodDefinitionHeader(dtor, type: type, header: true) 276 | AppendLine(";") 277 | } 278 | 279 | override func generatePropertyDefinition(_ property: CGPropertyDefinition, type: CGTypeDefinition) { 280 | 281 | if property.Virtuality == CGMemberVirtualityKind.Override || property.Virtuality == CGMemberVirtualityKind.Final { 282 | Append("// overriden ") // we don't need to re-emit overriden properties in header? 283 | } 284 | 285 | Append("__property ") 286 | 287 | // Append("(") 288 | // if property.Atomic { 289 | // Append("atomic") 290 | // } else { 291 | // Append("nonatomic") 292 | // } 293 | // if let type = property.`Type` { 294 | // if type.IsClassType { 295 | // switch type.StorageModifier { 296 | // case .Strong: Append(", strong") 297 | // case .Weak: Append(", weak") 298 | // case .Unretained: Append(", unsafe_unretained") 299 | // } 300 | // } else { 301 | // //todo? 302 | // } 303 | // } 304 | // if property.ReadOnly { 305 | // Append(", readonly") 306 | // } 307 | // Append(") ") 308 | 309 | if let type = property.`Type` { 310 | generateTypeReference(type/*, ignoreNullability:true*/) 311 | Append(" ") 312 | } else { 313 | // Append("id ") 314 | } 315 | generateIdentifier(property.Name) 316 | if let parameters = property.Parameters, parameters.Count > 0 { 317 | Append("[") 318 | cppGenerateDefinitionParameters(parameters, header: true) 319 | Append("]") 320 | } 321 | Append(" = {") 322 | var readerExist = false; 323 | if let getStatements = property.GetStatements, let getterMethod = property.GetterMethodDefinition() { 324 | readerExist = true; 325 | Append("read=") 326 | if !definitionOnly { 327 | generateIdentifier(getterMethod.Name) 328 | } 329 | } else if let getExpression = property.GetExpression { 330 | readerExist = true; 331 | Append("read=") 332 | if !definitionOnly { 333 | generateExpression(getExpression) 334 | } 335 | } 336 | 337 | if let setStatements = property.SetStatements, let setterMethod = property.SetterMethodDefinition() { 338 | if readerExist { 339 | Append(", ") 340 | } 341 | Append("write=") 342 | if !definitionOnly { 343 | generateIdentifier(setterMethod.Name) 344 | } 345 | } else if let setExpression = property.SetExpression { 346 | if readerExist { 347 | Append(", ") 348 | } 349 | Append("write=") 350 | if !definitionOnly { 351 | generateExpression(setExpression) 352 | } 353 | } 354 | AppendLine("};") 355 | } 356 | 357 | //internal final func cppHGenerateTypeMember(_ member: CGMemberDefinition, type: CGTypeDefinition, lastVisibility: CGMemberVisibilityKind) { 358 | //if let type = type as? CGInterfaceTypeDefinition { 359 | //// none 360 | //} else { 361 | //if var mVisibility = member.Visibility { 362 | //if (mVisibility != lastVisibility) { 363 | //decIndent(); 364 | //cppHGenerateMemberVisibilityPrefix(mVisibility) 365 | //incIndent(); 366 | //} 367 | //} 368 | //} 369 | //generateTypeMember(member, type: type); 370 | //} 371 | 372 | func cppHGenerateMemberVisibilityPrefix(_ visibility: CGMemberVisibilityKind) { 373 | switch visibility { 374 | case .Private: AppendLine("private:"); 375 | case .Public: AppendLine("public:"); 376 | case .Protected: AppendLine("protected:"); 377 | case .Published: if isCBuilder() { 378 | AppendLine("__published:") 379 | } 380 | default: 381 | } 382 | } 383 | 384 | override func generateTypeMembers(_ type: CGTypeDefinition) { 385 | if let type = type as? CGInterfaceTypeDefinition { 386 | decIndent(); 387 | cppHGenerateMemberVisibilityPrefix(CGMemberVisibilityKind.Public); 388 | incIndent(); 389 | super.generateTypeMembers(type); 390 | } else { 391 | // var lastMember: CGMemberDefinition? = nil 392 | // var lastVisibility: CGMemberVisibilityKind = CGMemberVisibilityKind.Unspecified; 393 | // for m in type.Members { 394 | // if let lastMember = lastMember, memberNeedsSpace(m, afterMember: lastMember) && !definitionOnly { 395 | // AppendLine() 396 | // } 397 | // cppHGenerateTypeMember(m, type: type, lastVisibility: lastVisibility); 398 | // lastMember = m; 399 | // lastVisibility = m.Visibility; 400 | // } 401 | cppGenerateTypeMembers(type, forVisibility: CGMemberVisibilityKind.Unspecified) 402 | cppGenerateTypeMembers(type, forVisibility: CGMemberVisibilityKind.Private) 403 | cppGenerateTypeMembers(type, forVisibility: CGMemberVisibilityKind.Unit) 404 | cppGenerateTypeMembers(type, forVisibility: CGMemberVisibilityKind.UnitOrProtected) 405 | cppGenerateTypeMembers(type, forVisibility: CGMemberVisibilityKind.UnitAndProtected) 406 | cppGenerateTypeMembers(type, forVisibility: CGMemberVisibilityKind.Assembly) 407 | cppGenerateTypeMembers(type, forVisibility: CGMemberVisibilityKind.AssemblyOrProtected) 408 | cppGenerateTypeMembers(type, forVisibility: CGMemberVisibilityKind.AssemblyAndProtected) 409 | cppGenerateTypeMembers(type, forVisibility: CGMemberVisibilityKind.Protected) 410 | cppGenerateTypeMembers(type, forVisibility: CGMemberVisibilityKind.Public) 411 | cppGenerateTypeMembers(type, forVisibility: CGMemberVisibilityKind.Published) 412 | } 413 | } 414 | 415 | func cppGeneratePropertyAccessorDefinition(_ property: CGPropertyDefinition, type: CGTypeDefinition) { 416 | if !definitionOnly { 417 | if let getStatements = property.GetStatements, let getterMethod = property.GetterMethodDefinition() { 418 | if isCBuilder() { 419 | getterMethod.CallingConvention = .Register 420 | } 421 | getterMethod.Visibility = .Private 422 | generateMethodDefinition(getterMethod, type: type) 423 | } 424 | if let setStatements = property.SetStatements, let setterMethod = property.SetterMethodDefinition() { 425 | if isCBuilder() { 426 | setterMethod.CallingConvention = .Register 427 | } 428 | setterMethod.Visibility = .Private 429 | generateMethodDefinition(setterMethod!, type: type) 430 | } 431 | } 432 | } 433 | 434 | private func cppGenerateTypeMembers(inout list: List!, type: CGTypeDefinition, inout first: Boolean) { 435 | for index in (0 ..< list.Count) { 436 | if first { 437 | decIndent() 438 | if list[index].Visibility != CGMemberVisibilityKind.Unspecified { 439 | cppHGenerateMemberVisibilityPrefix(list[index].Visibility) 440 | } 441 | first = false 442 | incIndent() 443 | } 444 | generateTypeMember(list, index, type: type) 445 | } 446 | list.RemoveAll() 447 | } 448 | 449 | final func cppGenerateTypeMembers(_ type: CGTypeDefinition, forVisibility visibility: CGMemberVisibilityKind?) { 450 | var first = true 451 | var list = List() 452 | for index in (0 ..< type.Members.Count) { 453 | var m = type.Members[index] 454 | if visibility == CGMemberVisibilityKind.Private { 455 | if let m = m as? CGPropertyDefinition { 456 | cppGenerateTypeMembers(list: &list, type: type, first: &first) 457 | cppGeneratePropertyAccessorDefinition(m, type: type) 458 | } 459 | } 460 | if let visibility = visibility { 461 | if m.Visibility == visibility { 462 | list.Add(m) 463 | } 464 | } else { 465 | generateTypeMember(type.Members, index, type: type) 466 | } 467 | } 468 | cppGenerateTypeMembers(list: &list, type: type, first: &first) 469 | 470 | } 471 | 472 | } -------------------------------------------------------------------------------- /CGCStyleCodeGenerator.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Abstract base implementation for all C-style languages (C#, Obj-C, Swift, Java, C++) 3 | // 4 | 5 | public __abstract class CGCStyleCodeGenerator : CGCodeGenerator { 6 | 7 | override public init() { 8 | useTabs = true 9 | tabSize = 4 10 | } 11 | 12 | func wellKnownSymbolForCustomOperator(name: String!) -> String? { 13 | switch name.ToUpper() { 14 | case "plus": return "+" 15 | case "minus": return "-" 16 | case "bitwisenot": return "!" 17 | case "increment": return "++" 18 | case "decrement": return "--" 19 | //case "implicit": return "__implicit" 20 | //case "explicit": return "__explicit" 21 | case "true": return "true" 22 | case "false": return "false" 23 | case "add": return "+" 24 | case "subtract": return "-" 25 | case "multiply": return "*" 26 | case "divide": return "/" 27 | case "modulus": return "%" 28 | case "bitwiseand": return "&" 29 | case "bitwiseor": return "|" 30 | case "bitwisexor": return "^" 31 | case "shiftlft": return "<<" 32 | case "shiftright": return ">>" 33 | case "equal": return "=" 34 | case "notequal": return "<>" 35 | case "less": return "<" 36 | case "lessorequal": return "<=" 37 | case "greater": return ">" 38 | case "greaterorequal": return ">=" 39 | case "in": return "in" 40 | default: return nil 41 | } 42 | } 43 | 44 | override func generateInlineComment(_ comment: String) { 45 | var comment = comment.Replace("*/", "* /") 46 | Append("/* \(comment) */") 47 | } 48 | 49 | override func generateConditionStart(_ condition: CGConditionalDefine) { 50 | if let name = condition.Expression as? CGNamedIdentifierExpression { 51 | Append("#ifdef ") 52 | Append(name.Name) 53 | } else { 54 | //if let not = statement.Condition.Expression as? CGUnaryOperatorExpression, not.Operator == .Not, 55 | if let not = condition.Expression as? CGUnaryOperatorExpression, not.Operator == CGUnaryOperatorKind.Not, 56 | let name = not.Value as? CGNamedIdentifierExpression { 57 | Append("#ifndef ") 58 | Append(name.Name) 59 | } else { 60 | Append("#if ") 61 | generateExpression(condition.Expression) 62 | } 63 | } 64 | // generateConditionalDefine(condition) 65 | AppendLine() 66 | } 67 | 68 | override func generateConditionElse() { 69 | AppendLine("#else") 70 | } 71 | 72 | override func generateConditionEnd(_ condition: CGConditionalDefine) { 73 | AppendLine("#endif") 74 | } 75 | 76 | override func generateBeginEndStatement(_ statement: CGBeginEndBlockStatement) { 77 | AppendLine("{") 78 | incIndent() 79 | generateStatements(statement.Statements) 80 | decIndent() 81 | AppendLine("}") 82 | } 83 | 84 | override func generateIfElseStatement(_ statement: CGIfThenElseStatement) { 85 | Append("if (") 86 | generateExpression(statement.Condition) 87 | AppendLine(")") 88 | generateStatementIndentedUnlessItsABeginEndBlock(statement.IfStatement) 89 | if let elseStatement = statement.ElseStatement { 90 | AppendLine("else") 91 | generateStatementIndentedUnlessItsABeginEndBlock(elseStatement) 92 | } 93 | } 94 | 95 | override func generateForToLoopStatement(_ statement: CGForToLoopStatement) { 96 | Append("for (") 97 | if let type = statement.LoopVariableType { 98 | generateTypeReference(type) 99 | Append(" ") 100 | } 101 | generateIdentifier(statement.LoopVariableName) 102 | Append(" = ") 103 | generateExpression(statement.StartValue) 104 | Append("; ") 105 | 106 | generateIdentifier(statement.LoopVariableName) 107 | if statement.Direction == CGLoopDirectionKind.Forward { 108 | Append(" <= ") 109 | } else { 110 | Append(" >= ") 111 | } 112 | generateExpression(statement.EndValue) 113 | Append("; ") 114 | 115 | generateIdentifier(statement.LoopVariableName) 116 | if let step = statement.Step { 117 | if statement.Direction == CGLoopDirectionKind.Forward { 118 | Append(" += ") 119 | } else { 120 | Append(" -= ") 121 | } 122 | generateExpression(step) 123 | } else { 124 | if statement.Direction == CGLoopDirectionKind.Forward { 125 | Append("++ ") 126 | } else { 127 | Append("-- ") 128 | } 129 | } 130 | 131 | AppendLine(")") 132 | 133 | generateStatementIndentedUnlessItsABeginEndBlock(statement.NestedStatement) 134 | } 135 | 136 | override func generateWhileDoLoopStatement(_ statement: CGWhileDoLoopStatement) { 137 | Append("while (") 138 | generateExpression(statement.Condition) 139 | AppendLine(")") 140 | generateStatementIndentedUnlessItsABeginEndBlock(statement.NestedStatement) 141 | } 142 | 143 | override func generateDoWhileLoopStatement(_ statement: CGDoWhileLoopStatement) { 144 | AppendLine("do") 145 | AppendLine("{") 146 | incIndent() 147 | generateStatementsSkippingOuterBeginEndBlock(statement.Statements) 148 | decIndent() 149 | AppendLine("}") 150 | Append("while (") 151 | generateExpression(statement.Condition) 152 | AppendLine(");") 153 | } 154 | 155 | override func generateSwitchStatement(_ statement: CGSwitchStatement) { 156 | Append("switch (") 157 | generateExpression(statement.Expression) 158 | AppendLine(")") 159 | AppendLine("{") 160 | incIndent() 161 | for c in statement.Cases { 162 | for e in c.CaseExpressions { 163 | Append("case ") 164 | generateExpression(e) 165 | AppendLine(":") 166 | } 167 | generateStatementsIndentedUnlessItsASingleBeginEndBlock(c.Statements) 168 | } 169 | if let defaultStatements = statement.DefaultCase, defaultStatements.Count > 0 { 170 | AppendLine("default:") 171 | generateStatementsIndentedUnlessItsASingleBeginEndBlock(defaultStatements) 172 | } 173 | decIndent() 174 | AppendLine("}") 175 | } 176 | 177 | override func generateReturnStatement(_ statement: CGReturnStatement) { 178 | if let value = statement.Value { 179 | Append("return ") 180 | generateExpression(value) 181 | generateStatementTerminator() 182 | } else { 183 | Append("return") 184 | generateStatementTerminator() 185 | } 186 | } 187 | 188 | override func generateBreakStatement(_ statement: CGBreakStatement) { 189 | Append("break") 190 | generateStatementTerminator() 191 | } 192 | 193 | override func generateContinueStatement(_ statement: CGContinueStatement) { 194 | Append("continue") 195 | generateStatementTerminator() 196 | } 197 | 198 | override func generateAssignmentStatement(_ statement: CGAssignmentStatement) { 199 | generateExpression(statement.Target) 200 | Append(" = ") 201 | generateExpression(statement.Value) 202 | generateStatementTerminator() 203 | } 204 | 205 | override func generateGotoStatement(_ statement: CGGotoStatement) { 206 | Append("goto "); 207 | Append(statement.Target); 208 | generateStatementTerminator(); 209 | } 210 | 211 | override func generateLabelStatement(_ statement: CGLabelStatement) { 212 | Append(statement.Name); 213 | Append(":"); 214 | generateStatementTerminator(); 215 | } 216 | 217 | // 218 | // Expressions 219 | // 220 | 221 | override func generateSizeOfExpression(_ expression: CGSizeOfExpression) { 222 | Append("sizeof(") 223 | generateExpression(expression.Expression) 224 | Append(")") 225 | } 226 | 227 | override func generatePointerDereferenceExpression(_ expression: CGPointerDereferenceExpression) { 228 | Append("(*(") 229 | generateExpression(expression.PointerExpression) 230 | Append("))") 231 | } 232 | 233 | override func generateUnaryOperator(_ `operator`: CGUnaryOperatorKind) { 234 | switch (`operator`) { 235 | case .Plus: Append("+") 236 | case .Minus: Append("-") 237 | case .Not: Append("!") 238 | case .BitwiseNot: Append("~") 239 | case .AddressOf: Append("&") 240 | case .AddressOfBlock: break // no-op 241 | case .ForceUnwrapNullable: break // no-op 242 | } 243 | } 244 | 245 | override func generateBinaryOperator(_ `operator`: CGBinaryOperatorKind) { 246 | switch (`operator`) { 247 | case .Concat: fallthrough 248 | case .Addition: Append("+") 249 | case .Subtraction: Append("-") 250 | case .Multiplication: Append("*") 251 | case .Division: Append("/") 252 | case .LegacyPascalDivision: Append("/") // not really supported in C-Style 253 | case .Modulus: Append("%") 254 | case .Equals: Append("==") 255 | case .NotEquals: Append("!=") 256 | case .LessThan: Append("<") 257 | case .LessThanOrEquals: Append("<=") 258 | case .GreaterThan: Append(">") 259 | case .GreatThanOrEqual: Append(">=") 260 | case .LogicalAnd: Append("&&") 261 | case .LogicalOr: Append("||") 262 | case .LogicalXor: Append("^^") 263 | case .Shl: Append("<<") 264 | case .Shr: Append(">>") 265 | case .BitwiseAnd: Append("&") 266 | case .BitwiseOr: Append("|") 267 | case .BitwiseXor: Append("^") 268 | //case .Implies: 269 | case .Is: Append("is") 270 | //case .IsNot: 271 | //case .In: 272 | //case .NotIn: 273 | case .Assign: Append("=") 274 | case .AssignAddition: Append("+=") 275 | case .AssignSubtraction: Append("-=") 276 | case .AssignMultiplication: Append("*=") 277 | case .AssignDivision: Append("/=") 278 | default: Append("/* NOT SUPPORTED */") /* Oxygene only */ 279 | } 280 | } 281 | 282 | override func generateIfThenElseExpression(_ expression: CGIfThenElseExpression) { 283 | Append("(") 284 | generateExpression(expression.Condition) 285 | Append(" ? ") 286 | generateExpression(expression.IfExpression) 287 | if let elseExpression = expression.ElseExpression { 288 | Append(" : ") 289 | generateExpression(elseExpression) 290 | } 291 | Append(")") 292 | } 293 | 294 | internal func cStyleEscapeCharactersInStringLiteral(_ string: String) -> String { 295 | let result = StringBuilder() 296 | let len = length(string) 297 | for i in 0 ..< len { 298 | let ch = string[i] 299 | switch ch { 300 | case "\0": result.Append("\\0") 301 | case "\\": result.Append("\\\\") 302 | case "\'": result.Append("\\'") 303 | case "\"": result.Append("\\\"") 304 | //case "\b": result.Append("\\b") // swift doesn't do \b 305 | case "\t": result.Append("\\t") 306 | case "\r": result.Append("\\r") 307 | case "\n": result.Append("\\n") 308 | /* 309 | case "\0".."\31": result.Append("\\"+Integer(ch).ToString()) // Cannot use the binary operator ".." 310 | case "\u{0080}".."\u{ffffffff}": result.Append("\\u{"+Sugar.Cryptography.Utils.ToHexString(Integer(ch), 4)) // Cannot use the binary operator ".." 311 | */ 312 | default: 313 | if ch < 32 { 314 | result.Append(cStyleEscapeSequenceForCharacter(ch)) 315 | } else if ch > 0x7f { 316 | if preserveUnicodeCharactersInStringLiterals { 317 | result.Append(ch) 318 | } else { 319 | result.Append(cStyleEscapeSequenceForCharacter(ch)) 320 | } 321 | } else { 322 | result.Append(ch) 323 | } 324 | 325 | } 326 | } 327 | return result.ToString() 328 | } 329 | 330 | internal func cStyleEscapeSequenceForCharacter(_ ch: Char) -> String { 331 | return "\\U"+Convert.ToHexString(Integer(ch), 8) // plain C: always use 8 hex digits with "\U" 332 | } 333 | 334 | override func generateStringLiteralExpression(_ expression: CGStringLiteralExpression) { 335 | Append("\"\(cStyleEscapeCharactersInStringLiteral(expression.Value))\"") 336 | } 337 | 338 | override func generateCharacterLiteralExpression(_ expression: CGCharacterLiteralExpression) { 339 | Append("'\(cStyleEscapeCharactersInStringLiteral(expression.Value.ToString()))'") 340 | } 341 | 342 | private func cStyleAppendNumberKind(_ numberKind: CGNumberKind?) { 343 | if let numberKind = numberKind { 344 | switch numberKind { 345 | case .Unsigned: Append("U") 346 | case .Long: Append("L") 347 | case .UnsignedLong: Append("UL") 348 | case .Float: Append("F") 349 | case .Double: Append("D") 350 | case .Decimal: Append("M") 351 | } 352 | } 353 | } 354 | 355 | override func generateIntegerLiteralExpression(_ literalExpression: CGIntegerLiteralExpression) { 356 | switch literalExpression.Base { 357 | case 16: Append("0x"+literalExpression.StringRepresentation(base:16)) 358 | case 10: Append(literalExpression.StringRepresentation(base:10)) 359 | case 8: Append("0"+literalExpression.StringRepresentation(base:8)) 360 | default: throw Exception("Base \(literalExpression.Base) integer literals are not currently supported for C-Style languages.") 361 | } 362 | cStyleAppendNumberKind(literalExpression.NumberKind) 363 | } 364 | 365 | override func generateFloatLiteralExpression(_ literalExpression: CGFloatLiteralExpression) { 366 | switch literalExpression.Base { 367 | case 10: Append(literalExpression.StringRepresentation()) 368 | default: throw Exception("Base \(literalExpression.Base) integer literals are not currently supported for C-Style languages.") 369 | } 370 | cStyleAppendNumberKind(literalExpression.NumberKind) 371 | } 372 | 373 | override func generatePointerTypeReference(_ type: CGPointerTypeReference) { 374 | generateTypeReference(type.`Type`) 375 | Append("*") 376 | } 377 | } -------------------------------------------------------------------------------- /CGJavaScriptCodeGenerator.swift: -------------------------------------------------------------------------------- 1 | public class CGJavaScriptCodeGenerator : CGCStyleCodeGenerator { 2 | 3 | public override var defaultFileExtension: String { return "js" } 4 | 5 | internal func javascriptGenerateCallSiteForExpression(_ expression: CGMemberAccessExpression) { 6 | if let callSite = expression.CallSite { 7 | generateExpression(callSite) 8 | if (expression.Name != "") { 9 | Append(".") 10 | } 11 | } 12 | } 13 | 14 | override func generateFieldAccessExpression(_ expression: CGFieldAccessExpression) { 15 | javascriptGenerateCallSiteForExpression(expression) 16 | generateIdentifier(expression.Name) 17 | } 18 | 19 | override func generatePropertyAccessExpression(_ property: CGPropertyAccessExpression) { 20 | javascriptGenerateCallSiteForExpression(property) 21 | generateIdentifier(property.Name) 22 | if let params = property.Parameters, params.Count > 0 { 23 | Append("[") 24 | javascriptGenerateCallParameters(property.Parameters) 25 | Append("]") 26 | } 27 | } 28 | 29 | override func generateSelfExpression(_ expression: CGSelfExpression) { 30 | Append("this") 31 | } 32 | 33 | override func generateNilExpression(_ expression: CGNilExpression) { 34 | Append("null") 35 | } 36 | 37 | override func generateVariableDeclarationStatement(_ statement: CGVariableDeclarationStatement) { 38 | if let type = statement.`Type` { 39 | generateTypeReference(type) 40 | Append(" ") 41 | } else { 42 | if (statement.Constant) { 43 | Append("const ") 44 | } else { 45 | Append("var ") 46 | } 47 | } 48 | generateIdentifier(statement.Name) 49 | if let value = statement.Value { 50 | Append(" = ") 51 | generateExpression(value) 52 | } 53 | AppendLine(";") 54 | } 55 | 56 | internal func javascriptGenerateDefinitionParameters(_ parameters: List) { 57 | for p in 0 ..< parameters.Count { 58 | let param = parameters[p] 59 | if p > 0 { 60 | Append(", ") 61 | } 62 | param.startLocation = currentLocation 63 | generateParameterDefinition(param) 64 | param.endLocation = currentLocation 65 | } 66 | } 67 | override func generateParameterDefinition(_ param: CGParameterDefinition) { 68 | generateIdentifier(param.Name) 69 | } 70 | 71 | override func generateLocalMethodStatement(_ method: CGLocalMethodStatement) { 72 | Append("function ") 73 | generateIdentifier(method.Name) 74 | Append("(") 75 | javascriptGenerateDefinitionParameters(method.Parameters) 76 | Append(")") 77 | AppendLine() 78 | 79 | AppendLine("{") 80 | incIndent() 81 | generateStatements(variables: method.LocalVariables) 82 | generateStatements(method.Statements) 83 | decIndent() 84 | Append("}") 85 | } 86 | 87 | override func generateAnonymousMethodExpression(_ expression: CGAnonymousMethodExpression) { 88 | Append("function ") 89 | Append("(") 90 | javascriptGenerateDefinitionParameters(expression.Parameters) 91 | Append(")") 92 | AppendLine() 93 | 94 | AppendLine("{") 95 | incIndent() 96 | generateStatements(variables: expression.LocalVariables) 97 | generateStatements(expression.Statements) 98 | decIndent() 99 | Append("}") 100 | } 101 | 102 | func javascriptGenerateCallParameters(_ parameters: List) { 103 | for p in 0 ..< parameters.Count { 104 | let param = parameters[p] 105 | if p > 0 { 106 | Append(", ") 107 | } 108 | generateExpression(param.Value) 109 | } 110 | } 111 | override func generateNewInstanceExpression(_ expression: CGNewInstanceExpression) { 112 | if (expression.`Type` is CGNilExpression) 113 | { 114 | //untyped object 115 | Append("{") 116 | for p in 0 ..< expression.Parameters.Count { 117 | let param = expression.Parameters[p] 118 | if p > 0 { 119 | Append(", ") 120 | } 121 | generateIdentifier(param.Name) 122 | Append(": ") 123 | generateExpression(param.Value) 124 | } 125 | Append("}") 126 | 127 | } else { 128 | Append("new ") 129 | generateExpression(expression.`Type`) 130 | //generateGenericArguments(expression.GenericArguments) 131 | Append("(") 132 | javascriptGenerateCallParameters(expression.Parameters) 133 | Append(")") 134 | } 135 | } 136 | /* 137 | if let propertyInitializers = expression.PropertyInitializers, propertyInitializers.Count > 0 { 138 | Append(" /* Property Initializers : ") 139 | helpGenerateCommaSeparatedList(propertyInitializers) { param in 140 | self.Append(param.Name) 141 | self.Append(" = ") 142 | self.generateExpression(param.Value) 143 | } 144 | Append(" */") 145 | } 146 | */ 147 | 148 | 149 | override func generateMethodCallExpression(_ method: CGMethodCallExpression) { 150 | javascriptGenerateCallSiteForExpression(method) 151 | generateIdentifier(method.Name) 152 | // generateGenericArguments(method.GenericArguments) 153 | Append("(") 154 | javascriptGenerateCallParameters(method.Parameters) 155 | Append(")") 156 | } 157 | 158 | override func generateArrayLiteralExpression(_ array: CGArrayLiteralExpression) { 159 | Append("[") 160 | for e in 0 ..< array.Elements.Count { 161 | if e > 0 { 162 | Append(", ") 163 | } 164 | generateExpression(array.Elements[e]) 165 | } 166 | Append("]") 167 | } 168 | 169 | override func generateFooter() { 170 | if let initialization = currentUnit.Initialization { 171 | generateStatements(initialization) 172 | } 173 | super.generateFooter() 174 | } 175 | 176 | override func generateForToLoopStatement(_ statement: CGForToLoopStatement) { 177 | Append("for (var ") // js has no types so we can't use LoopVariableType 178 | generateIdentifier(statement.LoopVariableName) 179 | Append(" = ") 180 | generateExpression(statement.StartValue) 181 | Append("; ") 182 | 183 | generateIdentifier(statement.LoopVariableName) 184 | if statement.Direction == CGLoopDirectionKind.Forward { 185 | Append(" <= ") 186 | } else { 187 | Append(" >= ") 188 | } 189 | generateExpression(statement.EndValue) 190 | Append("; ") 191 | 192 | generateIdentifier(statement.LoopVariableName) 193 | if let step = statement.Step { 194 | if statement.Direction == CGLoopDirectionKind.Forward { 195 | Append(" += ") 196 | } else { 197 | Append(" -= ") 198 | } 199 | generateExpression(step) 200 | } else { 201 | if statement.Direction == CGLoopDirectionKind.Forward { 202 | Append("++ ") 203 | } else { 204 | Append("-- ") 205 | } 206 | } 207 | 208 | AppendLine(")") 209 | 210 | generateStatementIndentedUnlessItsABeginEndBlock(statement.NestedStatement) 211 | } 212 | 213 | override func generateTryFinallyCatchStatement(_ statement: CGTryFinallyCatchStatement) { 214 | AppendLine("try") 215 | AppendLine("{") 216 | incIndent() 217 | generateStatements(statement.Statements) 218 | decIndent() 219 | AppendLine("}") 220 | if let catchBlocks = statement.CatchBlocks, catchBlocks.Count > 0 { 221 | for b in catchBlocks { 222 | if let name = b.Name { 223 | Append("catch (") 224 | generateIdentifier(name) 225 | AppendLine(")") 226 | AppendLine("{") 227 | } else { 228 | AppendLine("catch ") 229 | AppendLine("{") 230 | } 231 | incIndent() 232 | generateStatements(b.Statements) 233 | decIndent() 234 | AppendLine("}") 235 | } 236 | } 237 | if let finallyStatements = statement.FinallyStatements, finallyStatements.Count > 0 { 238 | AppendLine("finally") 239 | AppendLine("{") 240 | incIndent() 241 | generateStatements(finallyStatements) 242 | decIndent() 243 | AppendLine("}") 244 | } 245 | } 246 | 247 | override func generateThrowExpression(_ statement: CGThrowExpression) { 248 | if let value = statement.Exception { 249 | Append("throw ") 250 | generateExpression(value) 251 | } else { 252 | Append("throw") 253 | } 254 | } 255 | 256 | } -------------------------------------------------------------------------------- /CGObjectiveCCodeGenerator.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Abstract base implementation for Objective-C. Inherited by specific .m and .h Generators 3 | // 4 | 5 | public __abstract class CGObjectiveCCodeGenerator : CGCStyleCodeGenerator { 6 | 7 | public init() { 8 | keywords = ["__nonnull", "__null_unspecified", "__nullable", "__strong", "__unsafe_unretained", "__weak", 9 | "id", "in", "self", "super", "auto", "break", "case", "char", "const", "continue", "do", "double", "else", "enum", "extern", 10 | "float", "for", "goto", "if", "return", "int", "long", "register", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", 11 | "union", "unsigned", "void", "colatile", "while"].ToList() as! List 12 | } 13 | 14 | // 15 | // Statements 16 | // 17 | 18 | // in C-styleCG Base class 19 | /* 20 | override func generateBeginEndStatement(_ statement: CGBeginEndBlockStatement) { 21 | // handled in base 22 | } 23 | */ 24 | 25 | /* 26 | override func generateIfElseStatement(_ statement: CGIfThenElseStatement) { 27 | // handled in base 28 | } 29 | */ 30 | 31 | /* 32 | override func generateForToLoopStatement(_ statement: CGForToLoopStatement) { 33 | // handled in base 34 | } 35 | */ 36 | 37 | override func generateForEachLoopStatement(_ statement: CGForEachLoopStatement) { 38 | Append("for (") 39 | generateSingleNameOrTupleWithNames(statement.LoopVariableNames) 40 | Append(" in ") 41 | generateExpression(statement.Collection) 42 | AppendLine(")") 43 | generateStatementIndentedUnlessItsABeginEndBlock(statement.NestedStatement) 44 | } 45 | 46 | /* 47 | override func generateWhileDoLoopStatement(_ statement: CGWhileDoLoopStatement) { 48 | // handled in base 49 | } 50 | */ 51 | 52 | /* 53 | override func generateDoWhileLoopStatement(_ statement: CGDoWhileLoopStatement) { 54 | // handled in base 55 | } 56 | */ 57 | 58 | /* 59 | override func generateInfiniteLoopStatement(_ statement: CGInfiniteLoopStatement) { 60 | // handled in base 61 | } 62 | */ 63 | 64 | /* 65 | override func generateSwitchStatement(_ statement: CGSwitchStatement) { 66 | // handled in base 67 | } 68 | */ 69 | 70 | override func generateLockingStatement(_ statement: CGLockingStatement) { 71 | AppendLine("@synchronized(") 72 | generateExpression(statement.Expression) 73 | Append(")") 74 | AppendLine("{") 75 | incIndent() 76 | generateStatementSkippingOuterBeginEndBlock(statement.NestedStatement) 77 | decIndent() 78 | AppendLine("}") 79 | } 80 | 81 | override func generateUsingStatement(_ statement: CGUsingStatement) { 82 | assert(false, "generateUsingStatement is not supported in Objective-C") 83 | } 84 | 85 | override func generateAutoReleasePoolStatement(_ statement: CGAutoReleasePoolStatement) { 86 | AppendLine("@autoreleasepool") 87 | AppendLine("{") 88 | incIndent() 89 | generateStatementSkippingOuterBeginEndBlock(statement.NestedStatement) 90 | decIndent() 91 | AppendLine("}") 92 | } 93 | 94 | override func generateTryFinallyCatchStatement(_ statement: CGTryFinallyCatchStatement) { 95 | AppendLine("@try") 96 | AppendLine("{") 97 | incIndent() 98 | generateStatements(statement.Statements) 99 | decIndent() 100 | AppendLine("}") 101 | if let finallyStatements = statement.FinallyStatements, finallyStatements.Count > 0 { 102 | AppendLine("@finally") 103 | AppendLine("{") 104 | incIndent() 105 | generateStatements(finallyStatements) 106 | decIndent() 107 | AppendLine("}") 108 | } 109 | if let catchBlocks = statement.CatchBlocks, catchBlocks.Count > 0 { 110 | for b in catchBlocks { 111 | if let name = b.Name, let type = b.`Type` { 112 | Append("@catch (") 113 | generateTypeReference(type) 114 | if !objcTypeRefereneIsPointer(type) { 115 | Append(" ") 116 | } 117 | generateIdentifier(name) 118 | AppendLine(")") 119 | } else { 120 | AppendLine("@catch") 121 | } 122 | AppendLine("{") 123 | incIndent() 124 | generateStatements(b.Statements) 125 | decIndent() 126 | AppendLine("}") 127 | } 128 | } 129 | } 130 | 131 | /* 132 | override func generateReturnStatement(_ statement: CGReturnStatement) { 133 | // handled in base 134 | } 135 | */ 136 | 137 | override func generateThrowExpression(_ statement: CGThrowExpression) { 138 | if let value = statement.Exception { 139 | Append("@throw ") 140 | generateExpression(value) 141 | Append(";") 142 | } else { 143 | Append("@throw") 144 | } 145 | } 146 | 147 | /* 148 | override func generateBreakStatement(_ statement: CGBreakStatement) { 149 | // handled in base 150 | } 151 | */ 152 | 153 | /* 154 | override func generateContinueStatement(_ statement: CGContinueStatement) { 155 | // handled in base 156 | } 157 | */ 158 | 159 | override func generateVariableDeclarationStatement(_ statement: CGVariableDeclarationStatement) { 160 | if let type = statement.`Type` { 161 | generateTypeReference(type) 162 | if !objcTypeRefereneIsPointer(type) { 163 | Append(" ") 164 | } 165 | } else { 166 | Append("id ") 167 | } 168 | generateIdentifier(statement.Name) 169 | if let value = statement.Value { 170 | Append(" = ") 171 | generateExpression(value) 172 | } 173 | AppendLine(";") 174 | } 175 | 176 | /* 177 | override func generateAssignmentStatement(_ statement: CGAssignmentStatement) { 178 | // handled in base 179 | } 180 | */ 181 | 182 | override func generateConstructorCallStatement(_ statement: CGConstructorCallStatement) { 183 | Append("self = [") 184 | if let callSite = statement.CallSite { 185 | generateExpression(callSite) 186 | } else { 187 | generateExpression(CGSelfExpression.`Self`) 188 | } 189 | Append(" init") 190 | if let name = statement.ConstructorName { 191 | generateIdentifier(uppercaseFirstLetter(name)) 192 | objcGenerateCallParameters(statement.Parameters, skipFirstName: true) 193 | } else { 194 | objcGenerateCallParameters(statement.Parameters) 195 | } 196 | Append("]") 197 | AppendLine(";") 198 | } 199 | 200 | // 201 | // Expressions 202 | // 203 | 204 | /* 205 | override func generateNamedIdentifierExpression(_ expression: CGNamedIdentifierExpression) { 206 | // handled in base 207 | } 208 | */ 209 | 210 | /* 211 | override func generateAssignedExpression(_ expression: CGAssignedExpression) { 212 | // handled in base 213 | } 214 | */ 215 | 216 | /* 217 | override func generateSizeOfExpression(_ expression: CGSizeOfExpression) { 218 | // handled in base 219 | } 220 | */ 221 | 222 | override func generateTypeOfExpression(_ expression: CGTypeOfExpression) { 223 | Append("[") 224 | if let typeReferenceExpression = expression.Expression as? CGTypeReferenceExpression { 225 | generateTypeReference(typeReferenceExpression.`Type`, ignoreNullability: true) 226 | } else { 227 | generateExpression(expression.Expression) 228 | } 229 | Append(" class]") 230 | } 231 | 232 | override func generateDefaultExpression(_ expression: CGDefaultExpression) { 233 | assert(false, "generateDefaultExpression is not supported in Objective-C") 234 | } 235 | 236 | override func generateSelectorExpression(_ expression: CGSelectorExpression) { 237 | Append("@selector(\(expression.Name))") 238 | } 239 | 240 | override func generateTypeCastExpression(_ cast: CGTypeCastExpression) { 241 | Append("((") 242 | generateTypeReference(cast.TargetType)//, ignoreNullability: true) 243 | Append(")(") 244 | generateExpression(cast.Expression) 245 | Append("))") 246 | } 247 | 248 | override func generateInheritedExpression(_ expression: CGInheritedExpression) { 249 | Append("super") 250 | } 251 | 252 | override func generateSelfExpression(_ expression: CGSelfExpression) { 253 | Append("self") 254 | } 255 | 256 | override func generateNilExpression(_ expression: CGNilExpression) { 257 | Append("nil") 258 | } 259 | 260 | override func generatePropertyValueExpression(_ expression: CGPropertyValueExpression) { 261 | Append(CGPropertyDefinition.MAGIC_VALUE_PARAMETER_NAME) 262 | } 263 | 264 | override func generateAwaitExpression(_ expression: CGAwaitExpression) { 265 | assert(false, "generateAwaitExpression is not supported in Objective-C") 266 | } 267 | 268 | override func generateAnonymousMethodExpression(_ expression: CGAnonymousMethodExpression) { 269 | // todo 270 | } 271 | 272 | override func generateAnonymousTypeExpression(_ expression: CGAnonymousTypeExpression) { 273 | // todo 274 | } 275 | 276 | /* 277 | override func generatePointerDereferenceExpression(_ expression: CGPointerDereferenceExpression) { 278 | // handled in base 279 | } 280 | */ 281 | 282 | /* 283 | override func generateUnaryOperatorExpression(_ expression: CGUnaryOperatorExpression) { 284 | // handled in base 285 | } 286 | */ 287 | 288 | /* 289 | override func generateBinaryOperatorExpression(_ expression: CGBinaryOperatorExpression) { 290 | // handled in base 291 | } 292 | */ 293 | 294 | /* 295 | override func generateUnaryOperator(_ `operator`: CGUnaryOperatorKind) { 296 | // handled in base 297 | } 298 | */ 299 | 300 | /* 301 | override func generateBinaryOperator(_ `operator`: CGBinaryOperatorKind) { 302 | // handled in base 303 | } 304 | */ 305 | 306 | /* 307 | override func generateIfThenElseExpression(_ expression: CGIfThenElseExpression) { 308 | // handled in base 309 | } 310 | */ 311 | 312 | /* 313 | override func generateArrayElementAccessExpression(_ expression: CGArrayElementAccessExpression) { 314 | // handled in base 315 | } 316 | */ 317 | 318 | internal func objcGenerateCallSiteForExpression(_ expression: CGMemberAccessExpression, forceSelf: Boolean = false) { 319 | if let callSite = expression.CallSite { 320 | if let typeReferenceExpression = expression.CallSite as? CGTypeReferenceExpression { 321 | generateTypeReference(typeReferenceExpression.`Type`, ignoreNullability: true) 322 | } else { 323 | generateExpression(callSite) 324 | } 325 | } else if forceSelf { 326 | generateExpression(CGSelfExpression.`Self`) 327 | } 328 | } 329 | 330 | internal func objcGenerateStorageModifierPrefixIfNeeded(_ storageModifier: CGStorageModifierKind) { 331 | switch storageModifier { 332 | case .Strong: Append("__strong ") 333 | case .Weak: Append("__weak ") 334 | case .Unretained: Append("__unsafe_unretained") 335 | } 336 | } 337 | 338 | func objcGenerateCallParameters(_ parameters: List, skipFirstName: Boolean = false) { 339 | for p in 0 ..< parameters.Count { 340 | let param = parameters[p] 341 | 342 | if param.EllipsisParameter { 343 | if p > 0 { 344 | Append(", ") 345 | } else { 346 | Append(":") 347 | } 348 | } else { 349 | if p > 0 { 350 | Append(" ") 351 | } 352 | if let name = param.Name, p > 0 || !skipFirstName { 353 | generateIdentifier(name) 354 | } 355 | Append(":") 356 | } 357 | generateExpression(param.Value) 358 | } 359 | } 360 | 361 | func objcGenerateFunctionCallParameters(_ parameters: List) { 362 | for p in 0 ..< parameters.Count { 363 | let param = parameters[p] 364 | 365 | if p > 0 { 366 | Append(", ") 367 | } 368 | generateExpression(param.Value) 369 | } 370 | } 371 | 372 | func objcGenerateAttributeParameters(_ parameters: List) { 373 | // not needed 374 | } 375 | 376 | func objcGenerateDefinitionParameters(_ parameters: List) { 377 | for p in 0 ..< parameters.Count { 378 | let param = parameters[p] 379 | if p > 0 { 380 | Append(" ") 381 | } 382 | if let externalName = param.ExternalName { 383 | generateIdentifier(externalName) 384 | } 385 | Append(":(") 386 | if let type = param.`Type` { 387 | generateTypeReference(type) 388 | } else { 389 | assert("CGParameterDefinition needs a type, for Objective-C") 390 | } 391 | switch param.Modifier { 392 | case .Var: Append("*") 393 | case .Out: Append("*") 394 | default: 395 | } 396 | Append(")") 397 | generateIdentifier(param.Name) 398 | } 399 | } 400 | 401 | func objcGenerateAncestorList(_ type: CGClassOrStructTypeDefinition) { 402 | if type.Ancestors.Count > 0 { 403 | Append(" : ") 404 | for a in 0 ..< type.Ancestors.Count { 405 | if let ancestor = type.Ancestors[a] { 406 | if a > 0 { 407 | Append(", ") 408 | } 409 | generateTypeReference(ancestor, ignoreNullability: true) 410 | } 411 | } 412 | } else if type is CGClassTypeDefinition { 413 | Append(" : NSObject") 414 | } 415 | if type.ImplementedInterfaces.Count > 0 { 416 | Append(" <") 417 | for a in 0 ..< type.ImplementedInterfaces.Count { 418 | if let interface = type.ImplementedInterfaces[a] { 419 | if a > 0 { 420 | Append(", ") 421 | } 422 | generateTypeReference(interface, ignoreNullability: true) 423 | } 424 | } 425 | Append(">") 426 | } 427 | } 428 | 429 | override func generateFieldAccessExpression(_ expression: CGFieldAccessExpression) { 430 | if let callSite = expression.CallSite { 431 | if expression.CallSiteKind != .Static || !(callSite is CGSelfExpression) { 432 | objcGenerateCallSiteForExpression(expression, forceSelf: true) 433 | Append("->") 434 | } 435 | } 436 | generateIdentifier(expression.Name) 437 | } 438 | 439 | override func generateMethodCallExpression(_ method: CGMethodCallExpression) { 440 | if method.CallSite != nil { 441 | Append("[") 442 | objcGenerateCallSiteForExpression(method, forceSelf: true) 443 | Append(" ") 444 | Append(method.Name) 445 | objcGenerateCallParameters(method.Parameters) 446 | Append("]") 447 | } else { 448 | // nil means its a function 449 | Append(method.Name) 450 | Append("(") 451 | objcGenerateFunctionCallParameters(method.Parameters) 452 | Append(")") 453 | } 454 | } 455 | 456 | override func generateNewInstanceExpression(_ expression: CGNewInstanceExpression) { 457 | Append("[[") 458 | generateExpression(expression.`Type`, ignoreNullability:true) 459 | generateGenericArguments(expression.GenericArguments) 460 | Append(" alloc] init") 461 | if let name = expression.ConstructorName { 462 | generateIdentifier(uppercaseFirstLetter(name)) 463 | objcGenerateCallParameters(expression.Parameters, skipFirstName: true) 464 | } else { 465 | objcGenerateCallParameters(expression.Parameters) 466 | } 467 | Append("]") 468 | } 469 | 470 | override func generatePropertyAccessExpression(_ property: CGPropertyAccessExpression) { 471 | objcGenerateCallSiteForExpression(property, forceSelf: true) 472 | Append(".") 473 | Append(property.Name) 474 | 475 | if let params = property.Parameters, params.Count > 0 { 476 | assert(false, "Index properties are not supported in Objective-C") 477 | } 478 | } 479 | 480 | override func generateEnumValueAccessExpression(_ expression: CGEnumValueAccessExpression) { 481 | if let namedType = expression.Type as? CGNamedTypeReference { 482 | generateIdentifier(namedType.Name+"_"+expression.ValueName) // Obj-C enums must be unique 483 | } 484 | } 485 | 486 | override func generateStringLiteralExpression(_ expression: CGStringLiteralExpression) { 487 | Append("@") 488 | super.generateStringLiteralExpression(expression) 489 | } 490 | 491 | 492 | /* 493 | override func generateCharacterLiteralExpression(_ expression: CGCharacterLiteralExpression) { 494 | // handled in base 495 | } 496 | */ 497 | 498 | /* 499 | override func generateIntegerLiteralExpression(_ expression: CGIntegerLiteralExpression) { 500 | // handled in base 501 | } 502 | */ 503 | 504 | /* 505 | override func generateFloatLiteralExpression(_ literalExpression: CGFloatLiteralExpression) { 506 | // handled in base 507 | } 508 | */ 509 | 510 | override func generateArrayLiteralExpression(_ array: CGArrayLiteralExpression) { 511 | Append("@[") 512 | for e in 0 ..< array.Elements.Count { 513 | if e > 0 { 514 | Append(", ") 515 | } 516 | generateExpression(array.Elements[e]) 517 | } 518 | Append("]") 519 | } 520 | 521 | override func generateSetLiteralExpression(_ expression: CGSetLiteralExpression) { 522 | assert(false, "Sets are not supported in Objective-C") 523 | } 524 | 525 | override func generateDictionaryExpression(_ dictionary: CGDictionaryLiteralExpression) { 526 | assert(dictionary.Keys.Count == dictionary.Values.Count, "Number of keys and values in Dictionary doesn't match.") 527 | Append("@{") 528 | for e in 0 ..< dictionary.Keys.Count { 529 | if e > 0 { 530 | Append(", ") 531 | } 532 | generateExpression(dictionary.Keys[e]) 533 | Append(": ") 534 | generateExpression(dictionary.Values[e]) 535 | } 536 | Append("}") 537 | } 538 | 539 | /* 540 | override func generateTupleExpression(_ expression: CGTupleLiteralExpression) { 541 | // default handled in base 542 | } 543 | */ 544 | 545 | override func generateSetTypeReference(_ setType: CGSetTypeReference, ignoreNullability: Boolean = false) { 546 | assert(false, "generateSetTypeReference is not supported in Objective-C") 547 | } 548 | 549 | override func generateSequenceTypeReference(_ sequence: CGSequenceTypeReference, ignoreNullability: Boolean = false) { 550 | assert(false, "generateSequenceTypeReference is not supported in Objective-C") 551 | } 552 | 553 | // 554 | // Type Definitions 555 | // 556 | 557 | override func generateAttribute(_ attribute: CGAttribute, inline: Boolean) { 558 | // no-op, we dont support attribtes in Objective-C 559 | } 560 | 561 | override func generateAliasType(_ type: CGTypeAliasDefinition) { 562 | 563 | } 564 | 565 | override func generateBlockType(_ block: CGBlockTypeDefinition) { 566 | 567 | } 568 | 569 | override func generateEnumType(_ type: CGEnumTypeDefinition) { 570 | // overriden in H 571 | } 572 | 573 | override func generateClassTypeStart(_ type: CGClassTypeDefinition) { 574 | // overriden in M and H 575 | } 576 | 577 | override func generateClassTypeEnd(_ type: CGClassTypeDefinition) { 578 | AppendLine() 579 | AppendLine("@end") 580 | } 581 | 582 | func objcGenerateFields(_ type: CGTypeDefinition) { 583 | var hasFields = false 584 | for m in type.Members { 585 | if let property = m as? CGPropertyDefinition { 586 | 587 | // 32-bit OS X Objective-C needs properies explicitly synthesized 588 | if property.GetStatements == nil && property.SetStatements == nil && property.GetExpression == nil && property.SetExpression == nil { 589 | if !hasFields { 590 | hasFields = true 591 | AppendLine("{") 592 | incIndent() 593 | } 594 | if let type = property.`Type` { 595 | generateTypeReference(type) 596 | if !objcTypeRefereneIsPointer(type) { 597 | Append(" ") 598 | } 599 | } else { 600 | Append("id ") 601 | } 602 | Append("__p_") 603 | generateIdentifier(property.Name, escaped: false) 604 | AppendLine(";") 605 | } 606 | } else if let field = m as? CGFieldDefinition { 607 | if !hasFields { 608 | hasFields = true 609 | AppendLine("{") 610 | incIndent() 611 | } 612 | if let type = field.`Type` { 613 | generateTypeReference(type) 614 | if !objcTypeRefereneIsPointer(type) { 615 | Append(" ") 616 | } 617 | } else { 618 | Append("id ") 619 | } 620 | generateIdentifier(field.Name) 621 | AppendLine(";") 622 | } 623 | } 624 | if hasFields { 625 | decIndent() 626 | AppendLine("}") 627 | } 628 | } 629 | 630 | override func generateStructTypeStart(_ type: CGStructTypeDefinition) { 631 | // overriden in H 632 | } 633 | 634 | override func generateStructTypeEnd(_ type: CGStructTypeDefinition) { 635 | // overriden in H 636 | } 637 | 638 | override func generateInterfaceTypeStart(_ type: CGInterfaceTypeDefinition) { 639 | // overriden in H 640 | } 641 | 642 | override func generateInterfaceTypeEnd(_ type: CGInterfaceTypeDefinition) { 643 | // overriden in H 644 | } 645 | 646 | override func generateExtensionTypeStart(_ type: CGExtensionTypeDefinition) { 647 | // overriden in M and H 648 | } 649 | 650 | override func generateExtensionTypeEnd(_ type: CGExtensionTypeDefinition) { 651 | AppendLine("@end") 652 | } 653 | 654 | // 655 | // Type Members 656 | // 657 | 658 | func generateMethodDefinitionHeader(_ method: CGMethodLikeMemberDefinition, type: CGTypeDefinition) { 659 | if method.Static { 660 | Append("+ ") 661 | } else { 662 | Append("- ") 663 | } 664 | 665 | if let ctor = method as? CGConstructorDefinition { 666 | Append("(instancetype)init") 667 | generateIdentifier(uppercaseFirstLetter(ctor.Name)) 668 | } else { 669 | Append("(") 670 | if let returnType = method.ReturnType { 671 | generateTypeReference(returnType) 672 | } else { 673 | Append("void") 674 | } 675 | Append(")") 676 | generateIdentifier(method.Name) 677 | } 678 | objcGenerateDefinitionParameters(method.Parameters) 679 | } 680 | 681 | override func generateMethodDefinition(_ method: CGMethodDefinition, type: CGTypeDefinition) { 682 | // overriden in H 683 | } 684 | 685 | override func generateConstructorDefinition(_ ctor: CGConstructorDefinition, type: CGTypeDefinition) { 686 | // overriden in H 687 | } 688 | 689 | override func generateDestructorDefinition(_ dtor: CGDestructorDefinition, type: CGTypeDefinition) { 690 | 691 | } 692 | 693 | override func generateFinalizerDefinition(_ finalizer: CGFinalizerDefinition, type: CGTypeDefinition) { 694 | 695 | } 696 | 697 | override func generateFieldDefinition(_ field: CGFieldDefinition, type: CGTypeDefinition) { 698 | // overriden in M 699 | } 700 | 701 | override func generatePropertyDefinition(_ property: CGPropertyDefinition, type: CGTypeDefinition) { 702 | // overriden in H and M 703 | } 704 | 705 | override func generateEventDefinition(_ event: CGEventDefinition, type: CGTypeDefinition) { 706 | 707 | } 708 | 709 | override func generateCustomOperatorDefinition(_ customOperator: CGCustomOperatorDefinition, type: CGTypeDefinition) { 710 | 711 | } 712 | 713 | // 714 | // Type References 715 | // 716 | 717 | internal func objcTypeRefereneIsPointer(_ type: CGTypeReference) -> Boolean { 718 | if let type = type as? CGNamedTypeReference { 719 | return type.IsClassType 720 | } else if let type = type as? CGPredefinedTypeReference { 721 | return type.Kind == CGPredefinedTypeKind.String || type.Kind == CGPredefinedTypeKind.Object 722 | } 723 | return false 724 | } 725 | 726 | override func generateNamedTypeReference(_ type: CGNamedTypeReference, ignoreNullability: Boolean) { 727 | super.generateNamedTypeReference(type, ignoreNamespace: true, ignoreNullability: ignoreNullability) 728 | if type.IsClassType && !ignoreNullability { 729 | Append(" *") 730 | } 731 | } 732 | 733 | override func generatePredefinedTypeReference(_ type: CGPredefinedTypeReference, ignoreNullability: Boolean = false) { 734 | switch (type.Kind) { 735 | case .Int: Append("NSInteger") 736 | case .UInt: Append("NSUInteger") 737 | case .Int8: Append("int8") 738 | case .UInt8: Append("uint8") 739 | case .Int16: Append("int16") 740 | case .UInt16: Append("uint16") 741 | case .Int32: Append("int32") 742 | case .UInt32: Append("uint32") 743 | case .Int64: Append("int64") 744 | case .UInt64: Append("uint64") 745 | case .IntPtr: Append("NSInteger") 746 | case .UIntPtr: Append("NSUInteger") 747 | case .Single: Append("float") 748 | case .Double: Append("double") 749 | case .Boolean: Append("BOOL") 750 | case .String: if ignoreNullability { Append("NSString") } else { Append("NSString *") } 751 | case .AnsiChar: Append("char") 752 | case .UTF16Char: Append("UInt16") 753 | case .UTF32Char: Append("UInt32") 754 | case .Dynamic: Append("id") 755 | case .InstanceType: Append("instancetype") 756 | case .Void: Append("void") 757 | case .Object: if ignoreNullability { Append("NSObject") } else { Append("NSObject *") } 758 | case .Class: Append("Class") 759 | } 760 | } 761 | 762 | override func generateInlineBlockTypeReference(_ type: CGInlineBlockTypeReference, ignoreNullability: Boolean = false) { 763 | 764 | let block = type.Block 765 | 766 | if let returnType = block.ReturnType { 767 | generateTypeReference(returnType) 768 | } else { 769 | Append("void") 770 | } 771 | Append("(^)(") 772 | for p in 0 ..< block.Parameters.Count { 773 | if p > 0 { 774 | Append(", ") 775 | } 776 | if let type = block.Parameters[p].`Type` { 777 | generateTypeReference(type) 778 | } else { 779 | Append("id") 780 | } 781 | } 782 | Append(")") 783 | } 784 | 785 | override func generateKindOfTypeReference(_ type: CGKindOfTypeReference, ignoreNullability: Boolean = false) { 786 | Append("__kindof ") 787 | generateTypeReference(type.`Type`) 788 | } 789 | 790 | override func generateArrayTypeReference(_ type: CGArrayTypeReference, ignoreNullability: Boolean = false) { 791 | 792 | } 793 | 794 | override func generateDictionaryTypeReference(_ type: CGDictionaryTypeReference, ignoreNullability: Boolean = false) { 795 | 796 | } 797 | } -------------------------------------------------------------------------------- /CGObjectiveCHCodeGenerator.swift: -------------------------------------------------------------------------------- 1 | public class CGObjectiveCHCodeGenerator : CGObjectiveCCodeGenerator { 2 | 3 | public override var defaultFileExtension: String { return "h" } 4 | 5 | override func generateForwards() { 6 | for t in currentUnit.Types { 7 | if let type = t as? CGClassTypeDefinition { 8 | Append("@class ") 9 | generateIdentifier(type.Name) 10 | AppendLine(";") 11 | } else if let type = t as? CGInterfaceTypeDefinition { 12 | Append("@protocol ") 13 | generateIdentifier(type.Name) 14 | AppendLine(";") 15 | } 16 | } 17 | } 18 | 19 | override func generateImport(_ imp: CGImport) { 20 | AppendLine("#import <\(imp.Name)/\(imp.Name).h>") 21 | } 22 | 23 | override func generateFileImport(_ imp: CGImport) { 24 | AppendLine("#import \"\(imp.Name).h\"") 25 | } 26 | 27 | // 28 | // Types 29 | // 30 | 31 | override func generateAliasType(_ type: CGTypeAliasDefinition) { 32 | Append("typedef ") 33 | generateTypeReference(type.ActualType) 34 | Append(" ") 35 | generateIdentifier(type.Name) 36 | AppendLine(";") 37 | } 38 | 39 | override func generateBlockType(_ type: CGBlockTypeDefinition) { 40 | 41 | } 42 | 43 | override func generateEnumType(_ type: CGEnumTypeDefinition) { 44 | Append("typedef NS_ENUM(") 45 | if let baseType = type.BaseType { 46 | generateTypeReference(baseType, ignoreNullability: true) 47 | } else { 48 | Append("NSUInteger") 49 | } 50 | Append(", ") 51 | generateIdentifier(type.Name) 52 | AppendLine(")") 53 | AppendLine("{") 54 | incIndent() 55 | helpGenerateCommaSeparatedList(type.Members, wrapAlways: wrapEnums) { m in 56 | if let member = m as? CGEnumValueDefinition { 57 | self.generateIdentifier(type.Name+"_"+member.Name) // Obj-C enums must be unique 58 | if let value = member.Value { 59 | self.Append(" = ") 60 | self.generateExpression(value) 61 | } 62 | } 63 | } 64 | AppendLine() 65 | decIndent() 66 | AppendLine("};") 67 | } 68 | 69 | override func generateClassTypeStart(_ type: CGClassTypeDefinition) { 70 | Append("@interface ") 71 | generateIdentifier(type.Name) 72 | objcGenerateAncestorList(type) 73 | AppendLine() 74 | // 32-bit OS X Objective-C needs fields declared in @interface, not @implementation 75 | objcGenerateFields(type) 76 | AppendLine() 77 | } 78 | 79 | /*override func generateClassTypeEnd(_ type: CGClassTypeDefinition) { 80 | decIndent() 81 | AppendLine(@"end") 82 | }*/ 83 | 84 | override func generateStructTypeStart(_ type: CGStructTypeDefinition) { 85 | 86 | } 87 | 88 | override func generateStructTypeEnd(_ type: CGStructTypeDefinition) { 89 | 90 | } 91 | 92 | override func generateInterfaceTypeStart(_ type: CGInterfaceTypeDefinition) { 93 | Append("@protocol ") 94 | generateIdentifier(type.Name) 95 | //objcGenerateAncestorList(type) 96 | //AppendLine() 97 | AppendLine() 98 | } 99 | 100 | override func generateInterfaceTypeEnd(_ type: CGInterfaceTypeDefinition) { 101 | AppendLine() 102 | AppendLine("@end") 103 | } 104 | 105 | // 106 | // Type Members 107 | // 108 | 109 | override func generateMethodDefinition(_ method: CGMethodDefinition, type: CGTypeDefinition) { 110 | generateMethodDefinitionHeader(method, type: type) 111 | AppendLine(";") 112 | } 113 | 114 | override func generateConstructorDefinition(_ ctor: CGConstructorDefinition, type: CGTypeDefinition) { 115 | generateMethodDefinitionHeader(ctor, type: type) 116 | AppendLine(";") 117 | } 118 | 119 | override func generatePropertyDefinition(_ property: CGPropertyDefinition, type: CGTypeDefinition) { 120 | if property.Static { 121 | Append("+ (") 122 | if let type = property.`Type` { 123 | objcGenerateStorageModifierPrefixIfNeeded(property.StorageModifier) 124 | generateTypeReference(type) 125 | if !objcTypeRefereneIsPointer(type) { 126 | Append(" ") 127 | } 128 | } else { 129 | Append("id ") 130 | } 131 | Append(")") 132 | generateIdentifier(property.Name) 133 | AppendLine(";") 134 | } else { 135 | 136 | if property.Virtuality == CGMemberVirtualityKind.Override || property.Virtuality == CGMemberVirtualityKind.Final { 137 | Append("// overriden ") // we don't need to re-emit overriden properties in header? 138 | } 139 | 140 | Append("@property ") 141 | 142 | Append("(") 143 | if property.Atomic { 144 | Append("atomic") 145 | } else { 146 | Append("nonatomic") 147 | } 148 | if let type = property.`Type` { 149 | if type.IsClassType { 150 | //switch type.StorageModifier { 151 | //case .Strong: Append(", strong") 152 | //case .Weak: Append(", weak") 153 | //case .Unretained: Append(", unsafe_unretained") 154 | //} 155 | } else { 156 | //todo? 157 | } 158 | } 159 | if property.ReadOnly { 160 | Append(", readonly") 161 | } 162 | Append(") ") 163 | 164 | if let type = property.`Type` { 165 | generateTypeReference(type) 166 | if !objcTypeRefereneIsPointer(type) { 167 | Append(" ") 168 | } 169 | } else { 170 | Append("id ") 171 | } 172 | generateIdentifier(property.Name) 173 | AppendLine(";") 174 | } 175 | } 176 | } -------------------------------------------------------------------------------- /CGObjectiveCMCodeGenerator.swift: -------------------------------------------------------------------------------- 1 | public class CGObjectiveCMCodeGenerator : CGObjectiveCCodeGenerator { 2 | 3 | public override var defaultFileExtension: String { return "m" } 4 | 5 | override func generateHeader() { 6 | 7 | if let fileName = currentUnit.FileName { 8 | Append("#import \"\(Path.ChangeExtension(fileName, ".h"))\"") 9 | } 10 | } 11 | 12 | override func generateImport(_ imp: CGImport) { 13 | // ignore imports, they are in the .h 14 | } 15 | 16 | // 17 | // Types 18 | // 19 | 20 | override func generateClassTypeStart(_ type: CGClassTypeDefinition) { 21 | Append("@implementation ") 22 | generateIdentifier(type.Name) 23 | AppendLine() 24 | 25 | //objcGenerateFields(type) 26 | 27 | AppendLine() 28 | } 29 | 30 | override func generateStructType(_ type: CGStructTypeDefinition) { 31 | // structs don't appear in .m 32 | } 33 | 34 | override func generateInterfaceType(_ type: CGInterfaceTypeDefinition) { 35 | // protocols don't appear in .m 36 | } 37 | 38 | // 39 | // Type Members 40 | // 41 | 42 | override func generateMethodDefinition(_ method: CGMethodDefinition, type: CGTypeDefinition) { 43 | generateMethodDefinitionHeader(method, type: type) 44 | AppendLine() 45 | AppendLine("{") 46 | incIndent() 47 | generateStatements(method.Statements) 48 | decIndent() 49 | AppendLine("}") 50 | } 51 | 52 | override func generateConstructorDefinition(_ ctor: CGConstructorDefinition, type: CGTypeDefinition) { 53 | generateMethodDefinitionHeader(ctor, type: type) 54 | AppendLine() 55 | AppendLine("{") 56 | incIndent() 57 | generateStatements(ctor.Statements) 58 | AppendLine("return self;") 59 | decIndent() 60 | AppendLine("}") 61 | } 62 | 63 | override func generatePropertyDefinition(_ property: CGPropertyDefinition, type: CGTypeDefinition) { 64 | if property.GetStatements == nil && property.SetStatements == nil && property.GetExpression == nil && property.SetExpression == nil { 65 | if property.Static { 66 | assert(false, "static properties w/ storage are not supported for Objective-C") 67 | } else { 68 | Append("@synthesize ") 69 | generateIdentifier(property.Name) 70 | // 32-bit OS X Objective-C needs properies explicitly synthesized 71 | Append(" = __p_") 72 | generateIdentifier(property.Name, escaped: false) 73 | AppendLine(";") 74 | } 75 | } else { 76 | if let method = property.GetterMethodDefinition() { 77 | method.Name = property.Name 78 | generateMethodDefinition(method, type: type) 79 | } 80 | if let method = property.SetterMethodDefinition() { 81 | method.Name = "set"+uppercaseFirstLetter(property.Name) 82 | generateMethodDefinition(method, type: type) 83 | } 84 | } 85 | } 86 | 87 | override func generateFieldDefinition(_ field: CGFieldDefinition, type: CGTypeDefinition) { 88 | if field.Static { 89 | Append("static ") 90 | objcGenerateStorageModifierPrefixIfNeeded(field.StorageModifier) 91 | if let type = field.`Type` { 92 | generateTypeReference(type) 93 | if !objcTypeRefereneIsPointer(type) { 94 | Append(" ") 95 | } 96 | } else { 97 | Append("id ") 98 | } 99 | generateIdentifier(field.Name) 100 | AppendLine(";") 101 | } 102 | // instance fields are generated in TypeStart 103 | } 104 | } -------------------------------------------------------------------------------- /CGSkeletonCodeGenerator.swift: -------------------------------------------------------------------------------- 1 | // 2 | // An Empty Code Generator with stubs for all methids that usually need implementing 3 | // Useful as a starting oint for creating a new codegen, or check for missing implementations via diff 4 | // 5 | // All concrete implementations should use the same sort order for methods as this class. 6 | // 7 | // All methods named "generate*" should be overrides. For language-specific generators, add a prefix 8 | // to the method name to indicate the language — see Swift of Pascal codegen implementations for reference. 9 | // 10 | 11 | public class CGSkeletonCodeGenerator : CGCodeGenerator { 12 | 13 | public override var defaultFileExtension: String { return "" } 14 | 15 | override func escapeIdentifier(_ name: String) -> String { 16 | return name 17 | } 18 | 19 | override func generateHeader() { 20 | 21 | } 22 | 23 | override func generateFooter() { 24 | 25 | } 26 | 27 | /*override func generateImports() { 28 | }*/ 29 | 30 | override func generateImport(_ imp: CGImport) { 31 | 32 | } 33 | 34 | override func generateInlineComment(_ comment: String) { 35 | 36 | } 37 | 38 | // 39 | // Statements 40 | // 41 | 42 | override func generateConditionStart(_ condition: CGConditionalDefine) { 43 | 44 | } 45 | 46 | override func generateConditionElse() { 47 | 48 | } 49 | 50 | override func generateConditionEnd(_ condition: CGConditionalDefine) { 51 | 52 | } 53 | 54 | override func generateBeginEndStatement(_ statement: CGBeginEndBlockStatement) { 55 | 56 | } 57 | 58 | override func generateIfElseStatement(_ statement: CGIfThenElseStatement) { 59 | 60 | } 61 | 62 | override func generateForToLoopStatement(_ statement: CGForToLoopStatement) { 63 | 64 | } 65 | 66 | override func generateForEachLoopStatement(_ statement: CGForEachLoopStatement) { 67 | 68 | } 69 | 70 | override func generateWhileDoLoopStatement(_ statement: CGWhileDoLoopStatement) { 71 | 72 | } 73 | 74 | override func generateDoWhileLoopStatement(_ statement: CGDoWhileLoopStatement) { 75 | 76 | } 77 | 78 | /* 79 | override func generateInfiniteLoopStatement(_ statement: CGInfiniteLoopStatement) { 80 | } 81 | */ 82 | 83 | override func generateSwitchStatement(_ statement: CGSwitchStatement) { 84 | 85 | } 86 | 87 | override func generateLockingStatement(_ statement: CGLockingStatement) { 88 | } 89 | 90 | override func generateUsingStatement(_ statement: CGUsingStatement) { 91 | 92 | } 93 | 94 | override func generateAutoReleasePoolStatement(_ statement: CGAutoReleasePoolStatement) { 95 | 96 | } 97 | 98 | override func generateTryFinallyCatchStatement(_ statement: CGTryFinallyCatchStatement) { 99 | 100 | } 101 | 102 | override func generateReturnStatement(_ statement: CGReturnStatement) { 103 | 104 | } 105 | 106 | override func generateYieldExpression(_ statement: CGYieldExpression) { 107 | 108 | } 109 | 110 | override func generateThrowExpression(_ statement: CGThrowExpression) { 111 | 112 | } 113 | 114 | override func generateBreakStatement(_ statement: CGBreakStatement) { 115 | 116 | } 117 | 118 | override func generateContinueStatement(_ statement: CGContinueStatement) { 119 | 120 | } 121 | 122 | override func generateVariableDeclarationStatement(_ statement: CGVariableDeclarationStatement) { 123 | 124 | } 125 | 126 | override func generateAssignmentStatement(_ statement: CGAssignmentStatement) { 127 | 128 | } 129 | 130 | override func generateConstructorCallStatement(_ statement: CGConstructorCallStatement) { 131 | 132 | } 133 | 134 | // 135 | // Expressions 136 | // 137 | 138 | override func generateNamedIdentifierExpression(_ expression: CGNamedIdentifierExpression) { 139 | 140 | } 141 | 142 | override func generateAssignedExpression(_ expression: CGAssignedExpression) { 143 | 144 | } 145 | 146 | override func generateSizeOfExpression(_ expression: CGSizeOfExpression) { 147 | 148 | } 149 | 150 | override func generateTypeOfExpression(_ expression: CGTypeOfExpression) { 151 | 152 | } 153 | 154 | override func generateDefaultExpression(_ expression: CGDefaultExpression) { 155 | 156 | } 157 | 158 | override func generateSelectorExpression(_ expression: CGSelectorExpression) { 159 | 160 | } 161 | 162 | override func generateTypeCastExpression(_ expression: CGTypeCastExpression) { 163 | 164 | } 165 | 166 | override func generateInheritedExpression(_ expression: CGInheritedExpression) { 167 | 168 | } 169 | 170 | override func generateSelfExpression(_ expression: CGSelfExpression) { 171 | 172 | } 173 | 174 | override func generateNilExpression(_ expression: CGNilExpression) { 175 | 176 | } 177 | 178 | override func generatePropertyValueExpression(_ expression: CGPropertyValueExpression) { 179 | 180 | } 181 | 182 | override func generateAwaitExpression(_ expression: CGAwaitExpression) { 183 | 184 | } 185 | 186 | override func generateAnonymousMethodExpression(_ expression: CGAnonymousMethodExpression) { 187 | 188 | } 189 | 190 | override func generateAnonymousTypeExpression(_ expression: CGAnonymousTypeExpression) { 191 | 192 | } 193 | 194 | override func generatePointerDereferenceExpression(_ expression: CGPointerDereferenceExpression) { 195 | 196 | } 197 | 198 | override func generateUnaryOperatorExpression(_ expression: CGUnaryOperatorExpression) { 199 | 200 | } 201 | 202 | override func generateBinaryOperatorExpression(_ expression: CGBinaryOperatorExpression) { 203 | 204 | } 205 | 206 | override func generateUnaryOperator(_ `operator`: CGUnaryOperatorKind) { 207 | 208 | } 209 | 210 | override func generateBinaryOperator(_ `operator`: CGBinaryOperatorKind) { 211 | 212 | } 213 | 214 | override func generateIfThenElseExpression(_ expression: CGIfThenElseExpression) { 215 | 216 | } 217 | 218 | override func generateFieldAccessExpression(_ expression: CGFieldAccessExpression) { 219 | 220 | } 221 | 222 | override func generateArrayElementAccessExpression(_ expression: CGArrayElementAccessExpression) { 223 | 224 | } 225 | 226 | override func generateMethodCallExpression(_ expression: CGMethodCallExpression) { 227 | 228 | } 229 | 230 | override func generateNewInstanceExpression(_ expression: CGNewInstanceExpression) { 231 | 232 | } 233 | 234 | override func generatePropertyAccessExpression(_ expression: CGPropertyAccessExpression) { 235 | 236 | } 237 | 238 | override func generateEnumValueAccessExpression(_ expression: CGEnumValueAccessExpression) { 239 | 240 | } 241 | 242 | override func generateStringLiteralExpression(_ expression: CGStringLiteralExpression) { 243 | 244 | } 245 | 246 | override func generateCharacterLiteralExpression(_ expression: CGCharacterLiteralExpression) { 247 | 248 | } 249 | 250 | override func generateIntegerLiteralExpression(_ expression: CGIntegerLiteralExpression) { 251 | 252 | } 253 | 254 | override func generateFloatLiteralExpression(_ expression: CGFloatLiteralExpression) { 255 | 256 | } 257 | 258 | override func generateArrayLiteralExpression(_ expression: CGArrayLiteralExpression) { 259 | 260 | } 261 | 262 | override func generateSetLiteralExpression(_ expression: CGSetLiteralExpression) { 263 | 264 | } 265 | 266 | override func generateDictionaryExpression(_ expression: CGDictionaryLiteralExpression) { 267 | 268 | } 269 | 270 | /* 271 | override func generateTupleExpression(_ expression: CGTupleLiteralExpression) { 272 | // default handled in base 273 | } 274 | */ 275 | 276 | override func generateSetTypeReference(_ type: CGSetTypeReference, ignoreNullability: Boolean = false) { 277 | 278 | } 279 | 280 | override func generateSequenceTypeReference(_ type: CGSequenceTypeReference, ignoreNullability: Boolean = false) { 281 | 282 | } 283 | 284 | // 285 | // Type Definitions 286 | // 287 | 288 | override func generateAttribute(_ attribute: CGAttribute, inline: Boolean) { 289 | 290 | } 291 | 292 | override func generateAliasType(_ type: CGTypeAliasDefinition) { 293 | 294 | } 295 | 296 | override func generateBlockType(_ type: CGBlockTypeDefinition) { 297 | 298 | } 299 | 300 | override func generateEnumType(_ type: CGEnumTypeDefinition) { 301 | 302 | } 303 | 304 | override func generateClassTypeStart(_ type: CGClassTypeDefinition) { 305 | 306 | } 307 | 308 | override func generateClassTypeEnd(_ type: CGClassTypeDefinition) { 309 | 310 | } 311 | 312 | override func generateStructTypeStart(_ type: CGStructTypeDefinition) { 313 | 314 | } 315 | 316 | override func generateStructTypeEnd(_ type: CGStructTypeDefinition) { 317 | 318 | } 319 | 320 | override func generateInterfaceTypeStart(_ type: CGInterfaceTypeDefinition) { 321 | 322 | } 323 | 324 | override func generateInterfaceTypeEnd(_ type: CGInterfaceTypeDefinition) { 325 | 326 | } 327 | 328 | override func generateExtensionTypeStart(_ type: CGExtensionTypeDefinition) { 329 | 330 | } 331 | 332 | override func generateExtensionTypeEnd(_ type: CGExtensionTypeDefinition) { 333 | 334 | } 335 | 336 | // 337 | // Type Members 338 | // 339 | 340 | override func generateMethodDefinition(_ method: CGMethodDefinition, type: CGTypeDefinition) { 341 | 342 | } 343 | 344 | override func generateConstructorDefinition(_ ctor: CGConstructorDefinition, type: CGTypeDefinition) { 345 | 346 | } 347 | 348 | override func generateDestructorDefinition(_ dtor: CGDestructorDefinition, type: CGTypeDefinition) { 349 | 350 | } 351 | 352 | override func generateFinalizerDefinition(_ finalizer: CGFinalizerDefinition, type: CGTypeDefinition) { 353 | 354 | } 355 | 356 | override func generateFieldDefinition(_ field: CGFieldDefinition, type: CGTypeDefinition) { 357 | 358 | } 359 | 360 | override func generatePropertyDefinition(_ property: CGPropertyDefinition, type: CGTypeDefinition) { 361 | 362 | } 363 | 364 | override func generateEventDefinition(_ event: CGEventDefinition, type: CGTypeDefinition) { 365 | 366 | } 367 | 368 | override func generateCustomOperatorDefinition(_ customOperator: CGCustomOperatorDefinition, type: CGTypeDefinition) { 369 | 370 | } 371 | 372 | override func generateNestedTypeDefinition(_ member: CGNestedTypeDefinition, type: CGTypeDefinition) { 373 | 374 | } 375 | 376 | // 377 | // Type References 378 | // 379 | 380 | override func generateNamedTypeReference(_ type: CGNamedTypeReference) { 381 | 382 | } 383 | 384 | override func generatePredefinedTypeReference(_ type: CGPredefinedTypeReference, ignoreNullability: Boolean = false) { 385 | switch (type.Kind) { 386 | case .Int: Append("") 387 | case .UInt: Append("") 388 | case .Int8: Append("") 389 | case .UInt8: Append("") 390 | case .Int16: Append("") 391 | case .UInt16: Append("") 392 | case .Int32: Append("") 393 | case .UInt32: Append("") 394 | case .Int64: Append("") 395 | case .UInt64: Append("") 396 | case .IntPtr: Append("") 397 | case .UIntPtr: Append("") 398 | case .Single: Append("") 399 | case .Double: Append("") 400 | case .Boolean: Append("") 401 | case .String: Append("") 402 | case .AnsiChar: Append("") 403 | case .UTF16Char: Append("") 404 | case .UTF32Char: Append("") 405 | case .Dynamic: Append("") 406 | case .InstanceType: Append("") 407 | case .Void: Append("") 408 | case .Object: Append("") 409 | case .Class: Append("") 410 | } 411 | } 412 | 413 | override func generateIntegerRangeTypeReference(_ type: CGIntegerRangeTypeReference, ignoreNullability: Boolean = false) { 414 | Append(type.Start.ToString()) 415 | Append("..") 416 | Append(type.End.ToString()) 417 | } 418 | 419 | override func generateInlineBlockTypeReference(_ type: CGInlineBlockTypeReference, ignoreNullability: Boolean = false) { 420 | 421 | } 422 | 423 | override func generatePointerTypeReference(_ type: CGPointerTypeReference) { 424 | 425 | } 426 | 427 | override func generateKindOfTypeReference(_ type: CGKindOfTypeReference, ignoreNullability: Boolean = false) { 428 | 429 | } 430 | 431 | override func generateTupleTypeReference(_ type: CGTupleTypeReference, ignoreNullability: Boolean = false) { 432 | 433 | } 434 | 435 | override func generateArrayTypeReference(_ type: CGArrayTypeReference, ignoreNullability: Boolean = false) { 436 | 437 | } 438 | 439 | override func generateDictionaryTypeReference(_ type: CGDictionaryTypeReference, ignoreNullability: Boolean = false) { 440 | 441 | } 442 | } -------------------------------------------------------------------------------- /CodeDomToCG4.swift: -------------------------------------------------------------------------------- 1 | import System.Collections.Generic 2 | import System.Linq 3 | import System.Text 4 | import System.CodeDom 5 | 6 | public static class CodeDomToCG4 { 7 | 8 | /*public func convertUnit(_ codeDomUnit: CodeCompileUnit) -> CGCodeUnit? { 9 | 10 | if codeDomUnit.Namespaces.Count >= 1 { 11 | var namespace = codeDomUnit.Namespaces[0]; 12 | 13 | let unit = CGCodeUnit(namespace.Name) 14 | 15 | for i: CodeNamespaceImport in namespace.Imports { 16 | unit.Imports.Add(CGImport(i.Namespace)) 17 | } 18 | for t: CodeTypeDeclaration in namespace.Types { 19 | if let newType = convertType(t) { 20 | unit.Types.Add(newType) 21 | } 22 | } 23 | for c: CodeCommentStatement in namespace.Comments { 24 | unit.HeaderComment.Lines.Add(c.Comment.Text) 25 | } 26 | 27 | // Process additinal namespaces 28 | /*if codeDomUnit.Namespaces.Count > 1 { 29 | for x in 1 ..< codeDomUnit.Namespaces.Count { 30 | var namespace2 = codeDomUnit.Namespaces[x]; 31 | 32 | for i: CodeNamespaceImport in namespace2.Imports { 33 | unit.Imports.Add(CGImport(i.Namespace)) 34 | } 35 | 36 | } 37 | // ToDo: handle additional namespaces? 38 | }*/ 39 | 40 | return unit 41 | } 42 | 43 | return nil 44 | }*/ 45 | 46 | public func convertType(_ codeDomType: CodeTypeDeclaration, ignoreMembers: Boolean = false) -> CGTypeDefinition? { 47 | 48 | if !ignoreMembers && codeDomType.Members.Count > 0 { 49 | throw Exception("convertType does not support converting members yet.") 50 | } 51 | 52 | if codeDomType.IsClass { 53 | let result = CGClassTypeDefinition(codeDomType.Name) 54 | 55 | for b: CodeTypeReference in codeDomType.BaseTypes { 56 | if let ancestor = convertTypeReference(b) { 57 | result.Ancestors.Add(ancestor) 58 | } 59 | } 60 | 61 | return result 62 | } 63 | 64 | throw Exception("convertType does not support converting this kind of type yet.") 65 | } 66 | 67 | public func convertTypeReference(_ codeDomType: CodeTypeReference) -> CGTypeReference? { 68 | 69 | if let codeDomArrayType = codeDomType.ArrayElementType { 70 | if let arrayType = convertTypeReference(codeDomArrayType) { 71 | var bounds = List() 72 | for i in 0 ..< codeDomType.ArrayRank { 73 | bounds.Add(CGArrayBounds()) 74 | } 75 | return CGArrayTypeReference(arrayType, bounds) 76 | } 77 | return nil 78 | } else if codeDomType.UserData.Contains("OxygeneNullable") && String.EqualsIgnoringCaseInvariant(codeDomType.BaseType, "SYSTEM.NULLABLE`1") { 79 | return convertTypeReference(codeDomType.TypeArguments.Item[0])?.NullableNotUnwrapped 80 | } else { 81 | return CGNamedTypeReference(codeDomType.BaseType) 82 | } 83 | } 84 | 85 | public func convertMethod(_ codeDomMethod: CodeMemberMethod, ignoreBody: Boolean = false) -> CGMethodDefinition { 86 | 87 | if !ignoreBody && codeDomMethod.Statements.Count > 0 { 88 | throw Exception("convertMethod does not support converting method bodies yet.") 89 | } 90 | 91 | let result = CGMethodDefinition(codeDomMethod.Name) 92 | 93 | for p: CodeParameterDeclarationExpression in codeDomMethod.Parameters { 94 | let param = CGParameterDefinition(p.Name, convertTypeReference(p.`Type`) ?? CGPredefinedTypeReference.Void) 95 | result.Parameters.Add(param); 96 | } 97 | 98 | if let returnType = codeDomMethod.ReturnType { 99 | result.ReturnType = convertTypeReference(codeDomMethod.ReturnType) 100 | } 101 | 102 | return result 103 | } 104 | 105 | public func convertConstructor(_ codeDomCtor: CodeConstructor, ignoreBody: Boolean = false) -> CGConstructorDefinition { 106 | 107 | if !ignoreBody && codeDomCtor.Statements.Count > 0 { 108 | throw Exception("convertConstructor does not support converting constructor bodies yet.") 109 | } 110 | 111 | let result = CGConstructorDefinition() 112 | 113 | for p: CodeParameterDeclarationExpression in codeDomCtor.Parameters { 114 | let param = CGParameterDefinition(p.Name, convertTypeReference(p.`Type`) ?? CGPredefinedTypeReference.Void) 115 | result.Parameters.Add(param); 116 | } 117 | 118 | return result 119 | } 120 | } -------------------------------------------------------------------------------- /CodeGen4.Cooper.elements: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 3.5 5 | 258FD135-ABBB-46D5-8B7E-00266CD01152 6 | Library 7 | Release 8 | False 9 | False 10 | True 11 | JW0 12 | codegen4 13 | CodegGen4 (Java) 14 | AnyCPU 15 | RemObjects.Elements.RTL 16 | RemObjects.CodeGen4 17 | 18 | 19 | false 20 | .\bin\Debug 21 | DEBUG;TRACE; 22 | True 23 | True 24 | False 25 | False 26 | Project 27 | False 28 | anycpu 29 | v25 30 | False 31 | WarningOnPublicMembers 32 | True 33 | False 34 | 35 | 36 | true 37 | .\bin\Release 38 | False 39 | False 40 | False 41 | False 42 | Project 43 | False 44 | anycpu 45 | v25 46 | False 47 | WarningOnPublicMembers 48 | True 49 | False 50 | 51 | 52 | 53 | 54 | True 55 | 56 | 57 | True 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /CodeGen4.Echoes.elements: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 3.5 5 | RemObjects.CodeGen4 6 | 2C3408F4-2270-4310-9A38-6A5296129576 7 | library 8 | CodeGen4 9 | False 10 | False 11 | False 12 | False 13 | False 14 | True 15 | Release 16 | v4.0 17 | RemObjects.Elements.RTL 18 | CodegGen4 (.NET) 19 | AnyCPU 20 | 21 | 22 | false 23 | .\bin\Debug 24 | True 25 | True 26 | True 27 | False 28 | False 29 | Project 30 | False 31 | anycpu 32 | v25 33 | False 34 | WarningOnPublicMembers 35 | False 36 | True 37 | 38 | 39 | true 40 | .\bin\Release 41 | False 42 | False 43 | False 44 | False 45 | False 46 | Project 47 | False 48 | anycpu 49 | v25 50 | False 51 | WarningOnPublicMembers 52 | False 53 | True 54 | 55 | 56 | 57 | 58 | True 59 | 60 | 61 | 62 | 63 | 64 | 3.5 65 | 66 | 67 | 3.5 68 | 69 | 70 | 3.5 71 | 72 | 73 | True 74 | 75 | 76 | True 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /CodeGen4.Island.elements: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | RemObjects.CodeGen4 5 | {9D54D745-25D9-418E-913A-8D0C292E1C5B} 6 | StaticLibrary 7 | CodeGen4 8 | False 9 | False 10 | False 11 | False 12 | False 13 | True 14 | Release 15 | OS X 16 | True 17 | CodegGen4 (Island) 18 | AnyCPU 19 | RemObjects.Elements.RTL 20 | Island 21 | 22 | 23 | false 24 | .\bin\Debug 25 | DEBUG;TRACE; 26 | True 27 | True 28 | False 29 | False 30 | True 31 | 32 | 33 | true 34 | .\bin\Release 35 | False 36 | False 37 | False 38 | False 39 | True 40 | 41 | 42 | Darwin 43 | macOS 44 | 45 | 46 | Windows 47 | Windows 48 | 49 | 50 | Linux 51 | Ubuntu 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /CodeGen4.Toffee.elements: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | RemObjects.CodeGen4 5 | EA4135EC-C7A2-454F-A53C-A9B6FE92DA94 6 | StaticLibrary 7 | CodeGen4 8 | False 9 | False 10 | False 11 | False 12 | False 13 | True 14 | Release 15 | OS X 16 | True 17 | CodegGen4 (Cocoa) 18 | AnyCPU 19 | RemObjects.Elements.RTL 20 | 21 | 22 | false 23 | .\bin\Debug 24 | DEBUG;TRACE; 25 | True 26 | True 27 | False 28 | False 29 | True 30 | 31 | 32 | true 33 | .\bin\Release 34 | False 35 | False 36 | False 37 | False 38 | True 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /CodeGen4.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # RemObjects Fire 4 | Project("{656346D9-4656-40DA-A068-22D5425D4639}") = "CodegGen4 (.NET)", "CodeGen4.Echoes.elements", "{2C3408F4-2270-4310-9A38-6A5296129576}" 5 | EndProject 6 | Project("{656346D9-4656-40DA-A068-22D5425D4639}") = "CodegGen4 (Cocoa)", "CodeGen4.Toffee.elements", "{EA4135EC-C7A2-454F-A53C-A9B6FE92DA94}" 7 | EndProject 8 | Project("{656346D9-4656-40DA-A068-22D5425D4639}") = "CodegGen4 (Island)", "CodeGen4.Island.elements", "{9D54D745-25D9-418E-913A-8D0C292E1C5B}" 9 | EndProject 10 | Project("{656346D9-4656-40DA-A068-22D5425D4639}") = "CodegGen4 (Java)", "CodeGen4.Cooper.elements", "{258FD135-ABBB-46D5-8B7E-00266CD01152}" 11 | EndProject 12 | Project("{656346D9-4656-40DA-A068-22D5425D4639}") = "CodegGen4 (Shared)", "Codegen4.Shared.elements", "{615FC69D-8D8A-4996-9D58-B042CD883941}" 13 | EndProject 14 | Project("{656346D9-4656-40DA-A068-22D5425D4639}") = "Playground", "Tests\Playground\Playground.elements", "{FE58CBFE-5EFA-4E2A-8CE8-291682A121B0}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|AnyCPU = Debug|AnyCPU 19 | Release|AnyCPU = Release|AnyCPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {2C3408F4-2270-4310-9A38-6A5296129576}.Debug|AnyCPU.ActiveCfg = Debug|AnyCPU 23 | {2C3408F4-2270-4310-9A38-6A5296129576}.Debug|AnyCPU.Build.0 = Debug|AnyCPU 24 | {2C3408F4-2270-4310-9A38-6A5296129576}.Release|AnyCPU.ActiveCfg = Release|AnyCPU 25 | {2C3408F4-2270-4310-9A38-6A5296129576}.Release|AnyCPU.Build.0 = Release|AnyCPU 26 | {EA4135EC-C7A2-454F-A53C-A9B6FE92DA94}.Debug|AnyCPU.ActiveCfg = Debug|AnyCPU 27 | {EA4135EC-C7A2-454F-A53C-A9B6FE92DA94}.Debug|AnyCPU.Build.0 = Debug|AnyCPU 28 | {EA4135EC-C7A2-454F-A53C-A9B6FE92DA94}.Release|AnyCPU.ActiveCfg = Release|AnyCPU 29 | {EA4135EC-C7A2-454F-A53C-A9B6FE92DA94}.Release|AnyCPU.Build.0 = Release|AnyCPU 30 | {9D54D745-25D9-418E-913A-8D0C292E1C5B}.Debug|AnyCPU.ActiveCfg = Debug|AnyCPU 31 | {9D54D745-25D9-418E-913A-8D0C292E1C5B}.Debug|AnyCPU.Build.0 = Debug|AnyCPU 32 | {9D54D745-25D9-418E-913A-8D0C292E1C5B}.Release|AnyCPU.ActiveCfg = Release|AnyCPU 33 | {9D54D745-25D9-418E-913A-8D0C292E1C5B}.Release|AnyCPU.Build.0 = Release|AnyCPU 34 | {258FD135-ABBB-46D5-8B7E-00266CD01152}.Debug|AnyCPU.ActiveCfg = Debug|AnyCPU 35 | {258FD135-ABBB-46D5-8B7E-00266CD01152}.Debug|AnyCPU.Build.0 = Debug|AnyCPU 36 | {258FD135-ABBB-46D5-8B7E-00266CD01152}.Release|AnyCPU.ActiveCfg = Release|AnyCPU 37 | {258FD135-ABBB-46D5-8B7E-00266CD01152}.Release|AnyCPU.Build.0 = Release|AnyCPU 38 | {FE58CBFE-5EFA-4E2A-8CE8-291682A121B0}.Debug|AnyCPU.ActiveCfg = Debug|AnyCPU 39 | {FE58CBFE-5EFA-4E2A-8CE8-291682A121B0}.Debug|AnyCPU.Build.0 = Debug|AnyCPU 40 | {FE58CBFE-5EFA-4E2A-8CE8-291682A121B0}.Release|AnyCPU.ActiveCfg = Release|AnyCPU 41 | {FE58CBFE-5EFA-4E2A-8CE8-291682A121B0}.Release|AnyCPU.Build.0 = Release|AnyCPU 42 | EndGlobalSection 43 | GlobalSection(SolutionProperties) = preSolution 44 | HideSolutionNode = FALSE 45 | EndGlobalSection 46 | EndGlobal -------------------------------------------------------------------------------- /Codegen4.Shared.elements: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 615FC69D-8D8A-4996-9D58-B042CD883941 5 | CodegGen4 (Shared) 6 | Codegen4.Shared 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Codegen4.Shared.projitems: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath) 5 | true 6 | 615FC69D-8D8A-4996-9D58-B042CD883941 7 | CodeGen4.Shared 8 | {615FC69D-8D8A-4996-9D58-B042CD883941} 9 | RemObjects.CodeGen4 10 | 11 | 12 | 13 | Code Generation — C-Style Languages 14 | 15 | 16 | Code Generation — C-Style Languages 17 | 18 | 19 | Code Definition 20 | 21 | 22 | Code Definition 23 | 24 | 25 | Code Definition 26 | 27 | 28 | Code Definition 29 | 30 | 31 | Code Definition 32 | 33 | 34 | 35 | Code Generation 36 | 37 | 38 | Code Generation — C-Style Languages 39 | 40 | 41 | Code Generation — Pascal 42 | 43 | 44 | Code Generation — Pascal 45 | 46 | 47 | Code Generation — C-Style Languages 48 | 49 | 50 | Code Generation — C-Style Languages 51 | 52 | 53 | Code Generation — C-Style Languages 54 | 55 | 56 | Code Generation — Pascal 57 | 58 | 59 | Code Generation — C-Style Languages 60 | 61 | 62 | Code Generation — C-Style Languages 63 | 64 | 65 | Code Generation — C-Style Languages 66 | 67 | 68 | Code Generation — C-Style Languages 69 | 70 | 71 | Code Generation 72 | 73 | 74 | Code Generation 75 | 76 | 77 | 78 | Code Generation — C-Style Languages 79 | 80 | 81 | Code Generation 82 | 83 | 84 | Code Generation — C-Style Languages 85 | 86 | 87 | 88 | 89 | True 90 | 91 | 92 | True 93 | 94 | 95 | True 96 | 97 | 98 | True 99 | 100 | 101 | True 102 | 103 | 104 | True 105 | 106 | 107 | True 108 | 109 | 110 | True 111 | 112 | 113 | True 114 | 115 | 116 | True 117 | 118 | 119 | True 120 | 121 | 122 | True 123 | 124 | 125 | True 126 | 127 | 128 | True 129 | 130 | 131 | True 132 | 133 | 134 | True 135 | 136 | 137 | -------------------------------------------------------------------------------- /Extensions.swift: -------------------------------------------------------------------------------- 1 | public extension RemObjects.Elements.RTL.String { 2 | 3 | public func AsTypeReference() -> CGTypeReference { 4 | return CGNamedTypeReference(self) 5 | } 6 | 7 | public func AsTypeReference(defaultNullability: CGTypeNullabilityKind) -> CGTypeReference { 8 | return CGNamedTypeReference(self, defaultNullability: defaultNullability) 9 | } 10 | 11 | public func AsTypeReference(isClassType: Boolean) -> CGTypeReference { 12 | return CGNamedTypeReference(self, isClassType: isClassType) 13 | } 14 | 15 | public func AsTypeReferenceExpression() -> CGTypeReferenceExpression { 16 | return CGTypeReferenceExpression(CGNamedTypeReference(self)) 17 | } 18 | 19 | public func AsTypeReferenceExpression(defaultNullability: CGTypeNullabilityKind) -> CGTypeReferenceExpression { 20 | return CGTypeReferenceExpression(CGNamedTypeReference(self, defaultNullability: defaultNullability)) 21 | } 22 | 23 | public func AsNamedIdentifierExpression() -> CGNamedIdentifierExpression { 24 | return CGNamedIdentifierExpression(self) 25 | } 26 | 27 | public func AsLiteralExpression() -> CGStringLiteralExpression { 28 | return CGStringLiteralExpression(self) 29 | } 30 | 31 | public func AsRawExpression() -> CGRawExpression { 32 | return CGRawExpression(self) 33 | } 34 | 35 | public func AsCompilerDirective() -> CGCompilerDirective { 36 | return CGCompilerDirective(self) 37 | } 38 | } 39 | 40 | public extension RemObjects.Elements.System.String { 41 | 42 | public func AsTypeReference() -> CGTypeReference { 43 | return CGNamedTypeReference(self) 44 | } 45 | 46 | public func AsTypeReference(defaultNullability: CGTypeNullabilityKind) -> CGTypeReference { 47 | return CGNamedTypeReference(self, defaultNullability: defaultNullability) 48 | } 49 | 50 | public func AsTypeReference(isClassType: Boolean) -> CGTypeReference { 51 | return CGNamedTypeReference(self, isClassType: isClassType) 52 | } 53 | 54 | public func AsTypeReferenceExpression() -> CGTypeReferenceExpression { 55 | return CGTypeReferenceExpression(CGNamedTypeReference(self)) 56 | } 57 | 58 | public func AsTypeReferenceExpression(defaultNullability: CGTypeNullabilityKind) -> CGTypeReferenceExpression { 59 | return CGTypeReferenceExpression(CGNamedTypeReference(self, defaultNullability: defaultNullability)) 60 | } 61 | 62 | public func AsNamedIdentifierExpression() -> CGNamedIdentifierExpression { 63 | return CGNamedIdentifierExpression(self) 64 | } 65 | 66 | public func AsLiteralExpression() -> CGStringLiteralExpression { 67 | return CGStringLiteralExpression(self) 68 | } 69 | 70 | public func AsRawExpression() -> CGRawExpression { 71 | return CGRawExpression(self) 72 | } 73 | 74 | public func AsCompilerDirective() -> CGCompilerDirective { 75 | return CGCompilerDirective(self) 76 | } 77 | } 78 | 79 | public extension Char { 80 | public func AsLiteralExpression() -> CGCharacterLiteralExpression { 81 | return CGCharacterLiteralExpression(self) 82 | } 83 | } 84 | 85 | public extension Integer { 86 | public func AsLiteralExpression() -> CGIntegerLiteralExpression { 87 | return CGIntegerLiteralExpression(self) 88 | } 89 | //74375: Can't overload extension method, compiler claims signatures are same. 90 | public func AsLiteralExpression(# base: Int32) -> CGIntegerLiteralExpression { 91 | return CGIntegerLiteralExpression(self, base: base) 92 | } 93 | } 94 | 95 | public extension Single { 96 | public func AsLiteralExpression() -> CGFloatLiteralExpression { 97 | return CGFloatLiteralExpression(self) 98 | } 99 | } 100 | 101 | public extension Double { 102 | public func AsLiteralExpression() -> CGFloatLiteralExpression { 103 | return CGFloatLiteralExpression(self) 104 | } 105 | } 106 | 107 | public extension Boolean { 108 | public func AsLiteralExpression() -> CGBooleanLiteralExpression { 109 | return CGBooleanLiteralExpression(self) 110 | } 111 | } 112 | 113 | public extension CGExpression { 114 | public func AsReturnStatement() -> CGReturnStatement { 115 | return CGReturnStatement(self) 116 | } 117 | 118 | public func AsCallParameter() -> CGCallParameter { 119 | return CGCallParameter(self) 120 | } 121 | 122 | public func AsCallParameter(_ name: String?) -> CGCallParameter { 123 | if let name = name { 124 | return CGCallParameter(self, name) 125 | } else { 126 | return CGCallParameter(self) 127 | } 128 | } 129 | 130 | public func AsEllipsisCallParameter() -> CGCallParameter { 131 | let result = CGCallParameter(self) 132 | result.EllipsisParameter = true 133 | return result 134 | } 135 | 136 | public func AsInvariant() -> CGInvariant { 137 | return CGInvariant(self) 138 | } 139 | 140 | public func AsInvariant(_ message: String) -> CGInvariant { 141 | return CGInvariant(self, message) 142 | } 143 | } 144 | 145 | public extension CGTypeReference { 146 | public func AsExpression() -> CGTypeReferenceExpression { 147 | return CGTypeReferenceExpression(self) 148 | } 149 | } 150 | 151 | public extension CGFieldDefinition { 152 | public func AsGlobal() -> CGGlobalVariableDefinition { 153 | return CGGlobalVariableDefinition(self) 154 | } 155 | } 156 | 157 | public extension CGMethodDefinition { 158 | public func AsGlobal() -> CGGlobalFunctionDefinition { 159 | return CGGlobalFunctionDefinition(self) 160 | } 161 | } 162 | 163 | public extension CGParameterDefinition { 164 | public func AsExpression() -> CGParameterAccessExpression { 165 | return CGParameterAccessExpression(self.Name) 166 | } 167 | public func AsCallParameter() -> CGCallParameter { 168 | return self.AsExpression().AsCallParameter() 169 | } 170 | public func AsCallParameter(_ name: String?) -> CGCallParameter { 171 | return self.AsExpression().AsCallParameter(name) 172 | } 173 | } 174 | 175 | public extension CGVariableDeclarationStatement { 176 | public func AsExpression() -> CGLocalVariableAccessExpression { 177 | return CGLocalVariableAccessExpression(self.Name) 178 | } 179 | public func AsCallParameter() -> CGCallParameter { 180 | return self.AsExpression().AsCallParameter() 181 | } 182 | public func AsCallParameter(_ name: String?) -> CGCallParameter { 183 | return self.AsExpression().AsCallParameter(name) 184 | } 185 | } 186 | 187 | public extension CGFieldDefinition { 188 | public func AsExpression() -> CGFieldAccessExpression { 189 | return AsExpression(nil) 190 | } 191 | 192 | public func AsExpression(_ callSite: CGExpression?) -> CGFieldAccessExpression { 193 | return CGFieldAccessExpression(callSite, self.Name) 194 | } 195 | 196 | public func AsCallParameter() -> CGCallParameter { 197 | return self.AsExpression().AsCallParameter() 198 | } 199 | public func AsCallParameter(_ name: String?) -> CGCallParameter { 200 | return self.AsExpression().AsCallParameter(name) 201 | } 202 | } 203 | 204 | @if(defined("TOFFEE") && exists(Swift.Array)) 205 | public extension Swift.Array { 206 | public func ToList() -> List { 207 | return self.platformList.ToList() 208 | } 209 | } -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015, RemObjects Software. All rights reserved. 2 | Written by marc hoffman (mh@remobjects.com) 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /Properties/AssemblyInfo.swift: -------------------------------------------------------------------------------- 1 | import System.Reflection 2 | import System.Runtime.CompilerServices 3 | import System.Runtime.InteropServices 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | @assembly:AssemblyTitle("$rootnamespace$") 9 | @assembly:AssemblyDescription("") 10 | @assembly:AssemblyConfiguration("") 11 | @assembly:AssemblyCompany("RemObjects Software") 12 | @assembly:AssemblyProduct("$rootnamespace$") 13 | @assembly:AssemblyCopyright("Copyright © RemObjects Software 2015") 14 | @assembly:AssemblyTrademark("") 15 | @assembly:AssemblyCulture("") 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | @assembly:ComVisible(false) 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | @assembly:Guid("2C3408F4-2270-4310-9A38-6A5296129576") 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | @assembly:AssemblyVersion("1.0.0.0") 33 | @assembly:AssemblyFileVersion("1.0.0.0") 34 | 35 | // In order to sign your assembly you must specify a key to use. Refer to the 36 | // Microsoft .NET Framework documentation for more information on assembly signing. 37 | // 38 | // Use the attributes below to control which key is used for signing. 39 | // 40 | // Notes: 41 | // (*) If no key is specified, the assembly is not signed. 42 | // (*) KeyName refers to a key that has been installed in the Crypto Service 43 | // Provider (CSP) on your machine. KeyFile refers to a file which contains 44 | // a key. 45 | // (*) If the KeyFile and the KeyName values are both specified, the 46 | // following processing occurs: 47 | // (1) If the KeyName can be found in the CSP, that key is used. 48 | // (2) If the KeyName does not exist and the KeyFile does exist, the key 49 | // in the KeyFile is installed into the CSP and used. 50 | // (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility. 51 | // When specifying the KeyFile, the location of the KeyFile should be 52 | // relative to the project output directory, which in Oxygene by default is the 53 | // same as the project directory. For example, if your KeyFile is 54 | // located in the project directory, you would specify the AssemblyKeyFile 55 | // attribute as @assembly:AssemblyKeyFile('mykey.snk') 56 | // (*) Delay Signing is an advanced option - see the Microsoft .NET Framework 57 | // documentation for more information on this. 58 | // 59 | @assembly:AssemblyDelaySign(false) 60 | @assembly:AssemblyKeyFile("") 61 | @assembly:AssemblyKeyName("") -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeGen4 2 | 3 | CodeGen4 is an open source cross-platform, language agnostic code generation engine that will 4 | drive the future code generation in RemObjects products from RemObjects SDK and Data Abstract 5 | to Elements, as well as secondary projects such as Marzipan, Oxidizer and others. 6 | 7 | CodeGen4 is designed so that it can easily be expanded to cover additional target languages, with 8 | the implementation of one class. 9 | 10 | Currently in very early prototype stage, and not ready for consumption, just yet. 11 | But feedback and contributions are welcome. 12 | 13 | Implemented in Swift with [RemObjects Silver](http://elementscompiler.com/silver). 14 | 15 | ## Platform support: 16 | 17 | CodeGen4 can be used *on* (i.e. linked into tools written for) the following platforms: 18 | 19 | * .NET, Cocoa and Java, via the [Elements](http://elementscompiler.com) compiler 20 | 21 | ## Language Support 22 | 23 | * Oxygene _(mostly done)_ 24 | * C# (RemObjects C# and Visual C#) _(mostly done)_ 25 | * Swift (Silver and Apple's) _(mostly done)_ 26 | * Objective-C _(incomplete, in progress)_ 27 | * Java _(missing)_ 28 | * Delphi _(mostly done)_ 29 | * C++ Builder _(missing)_ 30 | * JavaScript _(missing)_ -------------------------------------------------------------------------------- /Statements.swift: -------------------------------------------------------------------------------- 1 | /* Statements */ 2 | 3 | public __abstract class CGStatement: CGEntity { 4 | } 5 | 6 | public __abstract class CGBaseMultilineStatement : CGStatement { 7 | public var Lines: List 8 | 9 | public init() { 10 | Lines = List() 11 | } 12 | public init(_ lines: List) { 13 | Lines = lines 14 | } 15 | public init(_ lines: String) { 16 | Lines = lines.Replace("\r", "").Split("\n").MutableVersion() 17 | } 18 | } 19 | 20 | public class CGRawStatement : CGBaseMultilineStatement { // not language-agnostic. obviosuly. 21 | } 22 | 23 | public class CGCommentStatement : CGBaseMultilineStatement { 24 | } 25 | 26 | public class CGXmlDocumentationStatement : CGBaseMultilineStatement { 27 | } 28 | 29 | public class CGSingleLineCommentStatement : CGStatement { 30 | public let Comment: String 31 | 32 | public init(_ comment: String) { 33 | Comment = comment.Trim() 34 | } 35 | } 36 | 37 | public class CGUnsupportedStatement : CGCommentStatement { 38 | public init() { 39 | super.init("Unsupported statement.") 40 | } 41 | public init(_ statement: String) { 42 | super.init("Unsupported statement: \(statement)") 43 | } 44 | } 45 | 46 | public __abstract class CGBlockStatement : CGStatement { 47 | public var Statements: List 48 | 49 | public init() { 50 | Statements = List() 51 | } 52 | public init(_ statements: List) { 53 | Statements = statements 54 | } 55 | public convenience init(_ statements: CGStatement...) { 56 | init(statements.ToList()) 57 | } 58 | } 59 | 60 | public __abstract class CGNestingStatement : CGStatement { 61 | public var NestedStatement: CGStatement 62 | 63 | public init(_ nestedStatement: CGStatement) { 64 | NestedStatement = nestedStatement 65 | } 66 | } 67 | 68 | public class CGCodeCommentStatement : CGStatement { 69 | public var CommentedOutStatement: CGStatement 70 | 71 | public init(_ statement: CGStatement) { 72 | CommentedOutStatement = statement 73 | } 74 | } 75 | 76 | public class CGConditionalBlockStatement : CGBlockStatement { 77 | public var Condition: CGConditionalDefine 78 | public var ElseStatements: List? 79 | 80 | public init(_ condition: CGConditionalDefine) { 81 | Condition = condition 82 | Statements = List() 83 | } 84 | 85 | public init(_ condition: CGConditionalDefine, _ statements: List) { 86 | Condition = condition 87 | Statements = statements 88 | } 89 | 90 | public convenience init(_ condition: CGConditionalDefine, _ statements: CGStatement...) { 91 | init(condition, statements.ToList()) 92 | } 93 | 94 | public init(_ condition: CGConditionalDefine, _ statements: List, _ elseStatements: List) { 95 | Condition = condition 96 | Statements = statements 97 | ElseStatements = elseStatements 98 | } 99 | } 100 | 101 | public class CGBeginEndBlockStatement : CGBlockStatement { //"begin/end" or "{/}" 102 | } 103 | 104 | public class CGIfThenElseStatement: CGStatement { 105 | public var Condition: CGExpression 106 | public var IfStatement: CGStatement 107 | public var ElseStatement: CGStatement? 108 | 109 | public init(_ condition: CGExpression, _ ifStatement: CGStatement, _ elseStatement: CGStatement? = nil) { 110 | Condition = condition 111 | IfStatement = ifStatement 112 | ElseStatement = elseStatement 113 | } 114 | } 115 | 116 | public enum CGLoopDirectionKind { 117 | case Forward 118 | case Backward 119 | } 120 | 121 | public class CGForToLoopStatement: CGNestingStatement { 122 | public var LoopVariableName: String 123 | public var LoopVariableType: CGTypeReference? // nil means it won't be declared, just used 124 | public var StartValue: CGExpression 125 | public var EndValue: CGExpression 126 | public var Step: CGExpression? 127 | public var Direction: CGLoopDirectionKind = .Forward 128 | 129 | public init(_ loopVariableName: String, _ loopVariableType: CGTypeReference, _ startValue: CGExpression, _ endValue: CGExpression, _ statement: CGStatement) { 130 | super.init(statement) 131 | LoopVariableName = loopVariableName 132 | LoopVariableType = loopVariableType 133 | StartValue = startValue 134 | EndValue = endValue 135 | } 136 | } 137 | 138 | public class CGForEachLoopStatement: CGNestingStatement { 139 | public var LoopVariableNames: List 140 | public var LoopVariableType: CGTypeReference? //not all languages require this but some do, so we'll require it 141 | public var Collection: CGExpression 142 | 143 | public init(_ loopVariableName: String, _ loopVariableType: CGTypeReference? = nil, _ collection: CGExpression, _ statement: CGStatement) { 144 | super.init(statement) 145 | LoopVariableNames = List(loopVariableName) 146 | LoopVariableType = loopVariableType 147 | Collection = collection 148 | } 149 | 150 | public init(_ loopVariableNames: List, _ collection: CGExpression, _ statement: CGStatement) { 151 | super.init(statement) 152 | LoopVariableNames = loopVariableNames 153 | Collection = collection 154 | } 155 | } 156 | 157 | public class CGWhileDoLoopStatement: CGNestingStatement { 158 | public var Condition: CGExpression 159 | 160 | public init(_ condition: CGExpression, _ statement: CGStatement) { 161 | super.init(statement) 162 | Condition = condition 163 | } 164 | } 165 | 166 | public class CGDoWhileLoopStatement: CGBlockStatement { // also "repeat/until" 167 | public var Condition: CGExpression 168 | 169 | public init(_ condition: CGExpression, _ statements: List) { 170 | super.init(statements) 171 | Condition = condition 172 | } 173 | public convenience init(_ condition: CGExpression, _ statements: CGStatement...) { 174 | init(condition, statements.ToList()) 175 | } 176 | } 177 | 178 | public class CGInfiniteLoopStatement: CGNestingStatement {} 179 | 180 | public class CGSwitchStatement: CGStatement { 181 | public var Expression: CGExpression 182 | public var Cases: List 183 | public var DefaultCase: List? 184 | 185 | public init(_ expression: CGExpression, _ cases: List, _ defaultCase: List? = nil) { 186 | Expression = expression 187 | DefaultCase = defaultCase 188 | if let cases = cases { 189 | Cases = cases 190 | } else { 191 | Cases = List() 192 | } 193 | } 194 | public convenience init(_ expression: CGExpression, _ cases: CGSwitchStatementCase[], _ defaultCase: List? = nil) { 195 | init(expression, cases.ToList(), defaultCase) 196 | } 197 | } 198 | 199 | public class CGSwitchStatementCase : CGEntity { 200 | public var CaseExpressions: List 201 | public var Statements: List 202 | 203 | public init(_ caseExpression: CGExpression, _ statements: List) { 204 | CaseExpressions = List() 205 | CaseExpressions.Add(caseExpression) 206 | Statements = statements 207 | } 208 | public init(_ caseExpressions: List, _ statements: List) { 209 | CaseExpressions = caseExpressions 210 | Statements = statements 211 | } 212 | public convenience init(_ caseExpression: CGExpression, _ statements: CGStatement...) { 213 | init(caseExpression, statements.ToList()) 214 | } 215 | public convenience init(_ caseExpressions: List, _ statements: CGStatement...) { 216 | init(caseExpressions, statements.ToList()) 217 | } 218 | } 219 | 220 | public class CGLockingStatement: CGNestingStatement { 221 | public var Expression: CGExpression 222 | 223 | public init(_ expression: CGExpression, _ nestedStatement: CGStatement) { 224 | super.init(nestedStatement) 225 | Expression = expression 226 | } 227 | } 228 | 229 | public class CGUsingStatement: CGNestingStatement { 230 | public var Name: String? 231 | public var `Type`: CGTypeReference? 232 | public var Value: CGExpression 233 | 234 | public init(_ name: String, _ value: CGExpression, _ nestedStatement: CGStatement) { 235 | super.init(nestedStatement) 236 | Name = name 237 | Value = value 238 | } 239 | public init(_ value: CGExpression, _ nestedStatement: CGStatement) { 240 | super.init(nestedStatement) 241 | Value = value 242 | } 243 | } 244 | 245 | public class CGAutoReleasePoolStatement: CGNestingStatement { 246 | } 247 | 248 | public class CGCheckedStatement : CGNestingStatement { 249 | } 250 | 251 | public class CGCUnsafeStatement : CGNestingStatement { 252 | } 253 | 254 | public class CGTryFinallyCatchStatement: CGBlockStatement { 255 | public var FinallyStatements = List() 256 | public var CatchBlocks = List() 257 | } 258 | 259 | public class CGCatchBlockStatement: CGBlockStatement { 260 | public let Name: String? 261 | public let `Type`: CGTypeReference? 262 | public var Filter: CGExpression? 263 | 264 | public init() { 265 | Name = nil 266 | Type = nil 267 | } 268 | public init(_ name: String) { 269 | Name = name 270 | Type = nil 271 | } 272 | public init(_ name: String, _ type: CGTypeReference) { 273 | Name = name 274 | `Type` = type 275 | } 276 | } 277 | 278 | /* Simple Statements */ 279 | 280 | public class CGReturnStatement: CGStatement { 281 | public var Value: CGExpression? 282 | 283 | public init() { 284 | } 285 | public init(_ value: CGExpression) { 286 | Value = value 287 | } 288 | } 289 | 290 | 291 | @Obsolete("use CGYieldExpression, instead") 292 | public typealias CGYieldStatement = CGYieldExpression 293 | @Obsolete("use CGThrowExpression, instead") 294 | public typealias CGThrowStatement = CGThrowExpression 295 | 296 | public class CGBreakStatement: CGStatement {} 297 | public class CGContinueStatement: CGStatement {} 298 | public class CGFallThroughStatement: CGStatement {} 299 | 300 | public class CGEmptyStatement: CGStatement {} 301 | 302 | public class CGConstructorCallStatement : CGStatement { 303 | public var CallSite: CGExpression = CGSelfExpression.`Self` //Should be set to CGSelfExpression or CGInheritedExpression 304 | public var ConstructorName: String? // an optionally be provided for languages that support named .ctors 305 | public var Parameters: List 306 | public var PropertyInitializers = List() // for Oxygene extnded .ctor calls 307 | 308 | public init(_ callSite: CGExpression, _ parameters: List?) { 309 | CallSite = callSite 310 | if let parameters = parameters { 311 | Parameters = parameters 312 | } else { 313 | Parameters = List() 314 | } 315 | } 316 | public convenience init(_ callSite: CGExpression, _ parameters: CGCallParameter...) { 317 | init(callSite, parameters.ToList()) 318 | } 319 | } 320 | 321 | public class CGLocalMethodStatement : CGAnonymousMethodExpression { 322 | public var Name: String 323 | public var Throws = false /* Swift and Java only */ 324 | 325 | public init(_ name: String) { 326 | super.init() 327 | Name = name 328 | } 329 | public init(_ name: String, _ statements: List) { 330 | super.init(statements) 331 | Name = name 332 | } 333 | public init(_ name: String, _ statements: CGStatement...) { 334 | super.init(statements) 335 | Name = name 336 | } 337 | public init(_ name: String, _ parameters: List, _ statements: List) { 338 | super.init(parameters, statements) 339 | Name = name 340 | } 341 | public init(_ name: String, _ parameters: CGParameterDefinition[], _ statements: CGStatement[]) { 342 | super.init(parameters, statements) 343 | Name = name 344 | } 345 | } 346 | 347 | /* Operator statements */ 348 | 349 | public class CGVariableDeclarationStatement: CGStatement, ICGHasCondition { 350 | public var Name: String 351 | public var `Type`: CGTypeReference? 352 | public var Value: CGExpression? 353 | public var Constant = false 354 | public var ReadOnly = false 355 | public var Condition: CGConditionalDefine? 356 | 357 | public init(_ name: String, _ type: CGTypeReference?, _ value: CGExpression? = nil) { 358 | Name = name 359 | `Type` = type 360 | Value = value 361 | } 362 | /*public convenience init(_ name: String, _ type: CGTypeReference?, _ value: CGExpression? = nil, constant: Boolean) { 363 | init(name, type, value) 364 | Constant = constant 365 | }*/ 366 | public convenience init(_ name: String, _ type: CGTypeReference?, _ value: CGExpression? = nil, readOnly: Boolean) { 367 | init(name, type, value) 368 | ReadOnly = readOnly 369 | } 370 | } 371 | 372 | public class CGAssignmentStatement: CGStatement { 373 | public var Target: CGExpression 374 | public var Value: CGExpression 375 | 376 | public init(_ target: CGExpression, _ value: CGExpression) { 377 | Target = target 378 | Value = value 379 | } 380 | } 381 | 382 | public class CGLabelStatement: CGStatement { 383 | public var Name: String 384 | 385 | public init(_ name: String) { 386 | Name = name 387 | } 388 | } 389 | 390 | public class CGGotoStatement: CGStatement { 391 | public var Target: String 392 | 393 | public init(_ target: String) { 394 | Target = target 395 | } 396 | } -------------------------------------------------------------------------------- /Tests/CodeGen4.Tests.Echoes.elements: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 3.5 5 | CodeGen4.Tests.Echoes 6 | {EF12A564-EECC-4E41-A96C-85AC6E6FA4C8} 7 | Exe 8 | CodeGen4.Tests.Echoes 9 | False 10 | False 11 | False 12 | False 13 | False 14 | Properties\App.ico 15 | Release 16 | RemObjects.Elements.RTL 17 | 18 | 19 | False 20 | bin\Debug\ 21 | DEBUG;TRACE; 22 | True 23 | True 24 | False 25 | Project 26 | anycpu 27 | v25 28 | False 29 | WarningOnPublicMembers 30 | False 31 | 32 | 33 | true 34 | .\bin\Release 35 | False 36 | False 37 | False 38 | False 39 | False 40 | Project 41 | False 42 | anycpu 43 | v25 44 | False 45 | WarningOnPublicMembers 46 | False 47 | True 48 | 49 | 50 | 51 | 52 | True 53 | 54 | 55 | True 56 | 57 | 58 | True 59 | 60 | 61 | 62 | 63 | 64 | 3.5 65 | 66 | 67 | 3.5 68 | 69 | 70 | 3.5 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | ResXFileCodeGenerator 79 | 80 | 81 | 82 | SettingsSingleFileGenerator 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /Tests/CodeGen4.Tests.Echoes.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # RemObjects Fire 4 | Project("{656346D9-4656-40DA-A068-22D5425D4639}") = "CodeGen4.Tests.Echoes", "CodeGen4.Tests.Echoes.elements", "{EF12A564-EECC-4E41-A96C-85AC6E6FA4C8}" 5 | EndProject 6 | Project("{656346D9-4656-40DA-A068-22D5425D4639}") = "CodeGen4.Tests.Shared", "CodeGen4.Tests.Shared.elements", "{311E71AB-9BA2-407F-8934-FB4EC6E69806}" 7 | EndProject 8 | Project("{656346D9-4656-40DA-A068-22D5425D4639}") = "CodegGen4 (Shared)", "..\Codegen4.Shared.elements", "{615FC69D-8D8A-4996-9D58-B042CD883941}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|AnyCPU = Debug|AnyCPU 13 | Release|AnyCPU = Release|AnyCPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {EF12A564-EECC-4E41-A96C-85AC6E6FA4C8}.Debug|AnyCPU.ActiveCfg = Debug|AnyCPU 17 | {EF12A564-EECC-4E41-A96C-85AC6E6FA4C8}.Debug|AnyCPU.Build.0 = Debug|AnyCPU 18 | {EF12A564-EECC-4E41-A96C-85AC6E6FA4C8}.Release|AnyCPU.ActiveCfg = Release|AnyCPU 19 | {EF12A564-EECC-4E41-A96C-85AC6E6FA4C8}.Release|AnyCPU.Build.0 = Release|AnyCPU 20 | EndGlobalSection 21 | GlobalSection(SolutionProperties) = preSolution 22 | HideSolutionNode = FALSE 23 | EndGlobalSection 24 | EndGlobal -------------------------------------------------------------------------------- /Tests/CodeGen4.Tests.Shared.elements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | {311E71AB-9BA2-407F-8934-FB4EC6E69806} 5 | 6 | 7 | CodeGen4.Tests.Shared 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Tests/CodeGen4.Tests.Shared.projitems: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath) 5 | true 6 | {311E71AB-9BA2-407F-8934-FB4EC6E69806} 7 | 8 | 9 | CodeGen4.Tests.Shared 10 | 11 | -------------------------------------------------------------------------------- /Tests/Playground/Playground.elements: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 3.5 5 | Playground 6 | {FE58CBFE-5EFA-4E2A-8CE8-291682A121B0} 7 | Exe 8 | Playground 9 | False 10 | False 11 | False 12 | False 13 | False 14 | Properties\App.ico 15 | Release 16 | v4.0 17 | 4.0 18 | RemObjects.Elements.RTL 19 | 20 | 21 | false 22 | .\bin\Debug 23 | DEBUG;TRACE; 24 | True 25 | True 26 | True 27 | False 28 | False 29 | Project 30 | False 31 | anycpu 32 | v25 33 | False 34 | WarningOnPublicMembers 35 | False 36 | True 37 | 38 | 39 | true 40 | .\bin\Release 41 | False 42 | False 43 | False 44 | False 45 | False 46 | Project 47 | False 48 | anycpu 49 | v25 50 | False 51 | WarningOnPublicMembers 52 | False 53 | True 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 3.5 62 | 63 | 64 | 3.5 65 | 66 | 67 | 3.5 68 | 69 | 70 | True 71 | 72 | 73 | True 74 | 75 | 76 | True 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | ResXFileCodeGenerator 85 | 86 | 87 | 88 | SettingsSingleFileGenerator 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /Tests/Playground/Program.swift: -------------------------------------------------------------------------------- 1 | import System.Collections.Generic 2 | import System.Linq 3 | import System.Text 4 | import RemObjects.CodeGen4 5 | 6 | print("CodeGen4 Playground") 7 | 8 | var unit = CGCodeUnit() 9 | unit.Namespace = CGNamespaceReference("UnitOne"); 10 | 11 | //var e = CGEnumTypeDefinition("MyEnum") 12 | //unit.Types.Add(e); 13 | //e.Members.Add(CGEnumValueDefinition("ValueA")) 14 | //e.Members.Add(CGEnumValueDefinition("ValueB")) 15 | //e.Members.Add(CGEnumValueDefinition("ValueC")) 16 | 17 | var cls1 = CGClassTypeDefinition("Test") 18 | var m = CGMethodDefinition("x") 19 | //var n = CGTypeNullabilityKind.Nullable 20 | //var t = "RemObjects.SDK.IMessage".AsTypeReference().copyWithNullability() 21 | var p = CGParameterDefinition("msg", CGPredefinedTypeReference.Object); 22 | cls1.Members.Add(m) 23 | m.Parameters.Add(p) 24 | unit.Types.Add(cls1); 25 | 26 | var v = CGVariableDeclarationStatement("foo", CGArrayTypeReference(CGPredefinedTypeReference.Double, RemObjects.Elements.RTL.List(CGArrayBounds(0, end: 5)))); 27 | m.Statements.Add(v); 28 | 29 | //var cls2 = CGClassTypeDefinition("Nested2") 30 | //var cls3 = CGClassTypeDefinition("Nested3") 31 | //cls1.Members.Add(CGMethodDefinition("NestedMethod")) 32 | 33 | 34 | //var cls = CGClassTypeDefinition("DotTest") 35 | ////cls.Ancestors.Add("Foo".AsTypeReference()) 36 | 37 | //cls.Members.Add(CGNestedTypeDefinition(cls1)) 38 | //cls.Members.Add(CGNestedTypeDefinition(cls2)) 39 | //cls.Members.Add(CGNestedTypeDefinition(cls3)) 40 | 41 | 42 | //var td = CGMethodDefinition("TestDot") 43 | //td.Visibility = .Public 44 | //// Simple expressions 45 | //var e1 = CGNamedIdentifierExpression("Named1") 46 | //var m1 = CGMethodCallExpression(nil, "Methodcall", "value".AsNamedIdentifierExpression().AsCallParameter() ) 47 | //var arrayname = CGNamedIdentifierExpression("MyData") 48 | //var ArrayParam = List() 49 | //ArrayParam.Add(CGIntegerLiteralExpression(1)) 50 | //var a1 = CGArrayElementAccessExpression(arrayname, ArrayParam) 51 | 52 | //var p = CGParameterDefinition("foo", "String".AsTypeReference()) 53 | //p.ExternalName = "bar" 54 | //td.Parameters.Add(p); 55 | 56 | //td.Statements.Add(CGCommentStatement("Simple Expressions")) 57 | 58 | //td.Preconditions = List() 59 | //td.Postconditions = List() 60 | //td.Preconditions!.Add(CGInvariant(e1)) 61 | //td.Preconditions!.Add(CGInvariant(e1, "Foo")) 62 | 63 | //td.Postconditions!.Add(CGBinaryOperatorExpression(CGPropertyAccessExpression(CGOldExpression.Old, "Foo"), 5.AsLiteralExpression(), CGBinaryOperatorKind.Equals).AsInvariant()) 64 | //td.Postconditions!.Add(e1.AsInvariant()) 65 | 66 | //td.Statements.Add(e1) 67 | //td.Statements.Add(m1) 68 | //td.Statements.Add(a1) 69 | 70 | //td.Statements.Add("xy".AsLiteralExpression()) 71 | //td.Statements.Add("x".AsLiteralExpression()) 72 | //td.Statements.Add("'".AsLiteralExpression()) 73 | //td.Statements.Add("\"".AsLiteralExpression()) 74 | //td.Statements.Add("\n".AsLiteralExpression()) 75 | //td.Statements.Add("\"\"".AsLiteralExpression()) 76 | 77 | //td.Statements.Add(CGVariableDeclarationStatement("x", CGPredefinedTypeReference.Double, CGFloatLiteralExpression(0.00000000000001))) 78 | 79 | //td.Statements.Add(CGCommentStatement("Now Property Access?")) 80 | //td.Statements.Add(CGCommentStatement("Would like to see: Methodcall(value).Named1.MyData[1];")) 81 | 82 | //var lpn = CGPropertyAccessExpression(m1, "Named1") 83 | //var lpd = CGPropertyAccessExpression(lpn, "MyData") 84 | //var lpa = CGArrayElementAccessExpression(lpd, 1.AsLiteralExpression()) 85 | //td.Statements.Add(lpa) 86 | 87 | //var intf = CGInterfaceTypeDefinition("NestedInterface") 88 | ////intf.Members.Add(CGNestedTypeDefinition(cls)) 89 | //intf.Members.Add(td); 90 | ////td.Visibility = CGMemberVisibilityKind.Protected 91 | 92 | ////var lp2 = CGPropertyAccessExpression(lpm, "Named1") 93 | ////var lp3 = CGPropertyAccessExpression(lp2, "MyData") 94 | ////td.Statements.Add(lp2) 95 | ////td.Statements.Add(lp3) 96 | 97 | //cls.Members.Add(td) 98 | 99 | //var ctor = CGConstructorDefinition() 100 | //ctor.Name = "withFoo"; 101 | //cls.Members.Add(ctor) 102 | 103 | //var p2 = CGPropertyDefinition("TestWithGetterAndSetter") 104 | //p2.Type = "String".AsTypeReference() 105 | //cls.Members.Add(p2) 106 | 107 | //p2.GetStatements = List() 108 | //p2.GetStatements?.Add(CGMethodCallExpression(nil, "Foo")) 109 | //p2.SetStatements = List() 110 | //p2.SetStatements?.Add(CGMethodCallExpression(nil, "Foo")) 111 | 112 | //for var i = 0; i < 10; i++ { 113 | //var p4 = CGPropertyDefinition("Test"+i) 114 | //p4.Type = "String".AsTypeReference() 115 | //p4.Visibility = .Private 116 | //cls.Members.Add(p4) 117 | //} 118 | 119 | //for var i = 0; i < 10; i++ { 120 | //var p4 = CGFieldDefinition("fTest"+i) 121 | //p4.Type = "String".AsTypeReference() 122 | //cls.Members.Add(p4) 123 | //} 124 | 125 | //for var i = 0; i < 10; i++ { 126 | //var p4 = CGPropertyDefinition("PublicTest"+i) 127 | //p4.Type = "String".AsTypeReference() 128 | //p4.Visibility = .Public 129 | //cls.Members.Add(p4) 130 | //} 131 | 132 | //var p3 = CGPropertyDefinition("Test") 133 | //p3.Type = "String".AsTypeReference() 134 | ////p3.GetStatements = List() 135 | ////p3.SetStatements = List() 136 | //cls.Members.Add(p3) 137 | 138 | //unit.Types.Add(cls) 139 | //unit.Types.Add(intf) 140 | 141 | var cg = CGCPlusPlusCPPCodeGenerator() 142 | //var cg = CGSwiftCodeGenerator(dialect: .Silver) 143 | //var cg = CGOxygeneCodeGenerator(style: .Unified) 144 | cg.wrapEnums = true 145 | //cg.QuoteStyle = .CodeDomSafe 146 | ////var cg = CGDelphiCodeGenerator() 147 | ////var cg = CGVisualBasicNetCodeGenerator() 148 | print(cg.GenerateUnit(unit, definitionOnly: false)) 149 | 150 | //var cgm = CGVisualBasicNetCodeGenerator(dialect: .Mercury) 151 | ////var cg = CGDelphiCodeGenerator() 152 | ////var cg = CGVisualBasicNetCodeGenerator() 153 | //print(cgm.GenerateUnit(unit, definitionOnly: false)) 154 | 155 | //var cgcs = CGCSharpCodeGenerator(dialect: .Hydrogene) 156 | ////var cg = CGDelphiCodeGenerator() 157 | ////var cg = CGVisualBasicNetCodeGenerator() 158 | //print(cgcs.GenerateUnit(unit, definitionOnly: false)) 159 | 160 | //var cg = CGJavaCodeGenerator() 161 | //var code = cg.GenerateUnit(unit) 162 | 163 | //var cgw = CGWLanguageCodeGenerator() 164 | //cgw.QuoteStyle = .CodeDomSafe 165 | //var cg = CGDelphiCodeGenerator() 166 | //var cg = CGVisualBasicNetCodeGenerator() 167 | //print(cgw.GenerateUnit(unit, definitionOnly: false)) -------------------------------------------------------------------------------- /Tests/Playground/Properties/App.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/remobjects/CodeGen4/15ba423fedb45d8bdd05b3c549f38adc992e06cf/Tests/Playground/Properties/App.ico -------------------------------------------------------------------------------- /Tests/Playground/Properties/AssemblyInfo.swift: -------------------------------------------------------------------------------- 1 | import System.Reflection 2 | import System.Runtime.CompilerServices 3 | import System.Runtime.InteropServices 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | @assembly:AssemblyTitle("Playground") 9 | @assembly:AssemblyDescription("") 10 | @assembly:AssemblyConfiguration("") 11 | @assembly:AssemblyCompany("RemObjects Software") 12 | @assembly:AssemblyProduct("Playground") 13 | @assembly:AssemblyCopyright("Copyright © RemObjects Software 2017") 14 | @assembly:AssemblyTrademark("") 15 | @assembly:AssemblyCulture("") 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | @assembly:ComVisible(false) 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | @assembly:Guid("{FE58CBFE-5EFA-4E2A-8CE8-291682A121B0}") 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | @assembly:AssemblyVersion("1.0.0.0") 33 | @assembly:AssemblyFileVersion("1.0.0.0") 34 | 35 | // In order to sign your assembly you must specify a key to use. Refer to the 36 | // Microsoft .NET Framework documentation for more information on assembly signing. 37 | // 38 | // Use the attributes below to control which key is used for signing. 39 | // 40 | // Notes: 41 | // (*) If no key is specified, the assembly is not signed. 42 | // (*) KeyName refers to a key that has been installed in the Crypto Service 43 | // Provider (CSP) on your machine. KeyFile refers to a file which contains 44 | // a key. 45 | // (*) If the KeyFile and the KeyName values are both specified, the 46 | // following processing occurs: 47 | // (1) If the KeyName can be found in the CSP, that key is used. 48 | // (2) If the KeyName does not exist and the KeyFile does exist, the key 49 | // in the KeyFile is installed into the CSP and used. 50 | // (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility. 51 | // When specifying the KeyFile, the location of the KeyFile should be 52 | // relative to the project output directory, which in Oxygene by default is the 53 | // same as the project directory. For example, if your KeyFile is 54 | // located in the project directory, you would specify the AssemblyKeyFile 55 | // attribute as @assembly:AssemblyKeyFile('mykey.snk') 56 | // (*) Delay Signing is an advanced option - see the Microsoft .NET Framework 57 | // documentation for more information on this. 58 | // 59 | @assembly:AssemblyDelaySign(false) 60 | @assembly:AssemblyKeyFile("") 61 | @assembly:AssemblyKeyName("") -------------------------------------------------------------------------------- /Tests/Playground/Properties/Resources.Designer.swift: -------------------------------------------------------------------------------- 1 | /* The .Designer.swift file will be automatically generated when the parent file changes. 2 | You can also right-click the parent file and select 'Run Custom Tool' to update it now. */ -------------------------------------------------------------------------------- /Tests/Playground/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 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 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /Tests/Playground/Properties/Settings.Designer.swift: -------------------------------------------------------------------------------- 1 | /* The .Designer.swift file will be automatically generated when the parent file changes. 2 | You can also right-click the parent file and select 'Run Custom Tool' to update it now. */ -------------------------------------------------------------------------------- /Tests/Playground/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Tests/Program.swift: -------------------------------------------------------------------------------- 1 | import RemObjects.Elements.EUnit 2 | 3 | let lTests = Discovery.DiscoverTests() 4 | Runner.RunTests(lTests, withListener: Runner.DefaultListener) -------------------------------------------------------------------------------- /Tests/Properties/App.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/remobjects/CodeGen4/15ba423fedb45d8bdd05b3c549f38adc992e06cf/Tests/Properties/App.ico -------------------------------------------------------------------------------- /Tests/Properties/AssemblyInfo.swift: -------------------------------------------------------------------------------- 1 | import System.Reflection 2 | import System.Runtime.CompilerServices 3 | import System.Runtime.InteropServices 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | @assembly:AssemblyTitle("CodeGen4.Tests.Echoes") 9 | @assembly:AssemblyDescription("") 10 | @assembly:AssemblyConfiguration("") 11 | @assembly:AssemblyCompany("RemObjects Software") 12 | @assembly:AssemblyProduct("CodeGen4.Tests.Echoes") 13 | @assembly:AssemblyCopyright("Copyright © RemObjects Software 2017") 14 | @assembly:AssemblyTrademark("") 15 | @assembly:AssemblyCulture("") 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | @assembly:ComVisible(false) 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | @assembly:Guid("{EF12A564-EECC-4E41-A96C-85AC6E6FA4C8}") 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | @assembly:AssemblyVersion("1.0.0.0") 33 | @assembly:AssemblyFileVersion("1.0.0.0") 34 | 35 | // In order to sign your assembly you must specify a key to use. Refer to the 36 | // Microsoft .NET Framework documentation for more information on assembly signing. 37 | // 38 | // Use the attributes below to control which key is used for signing. 39 | // 40 | // Notes: 41 | // (*) If no key is specified, the assembly is not signed. 42 | // (*) KeyName refers to a key that has been installed in the Crypto Service 43 | // Provider (CSP) on your machine. KeyFile refers to a file which contains 44 | // a key. 45 | // (*) If the KeyFile and the KeyName values are both specified, the 46 | // following processing occurs: 47 | // (1) If the KeyName can be found in the CSP, that key is used. 48 | // (2) If the KeyName does not exist and the KeyFile does exist, the key 49 | // in the KeyFile is installed into the CSP and used. 50 | // (*) In order to create a KeyFile, you can use the sn.exe (Strong Name) utility. 51 | // When specifying the KeyFile, the location of the KeyFile should be 52 | // relative to the project output directory, which in Oxygene by default is the 53 | // same as the project directory. For example, if your KeyFile is 54 | // located in the project directory, you would specify the AssemblyKeyFile 55 | // attribute as @assembly:AssemblyKeyFile('mykey.snk') 56 | // (*) Delay Signing is an advanced option - see the Microsoft .NET Framework 57 | // documentation for more information on this. 58 | // 59 | @assembly:AssemblyDelaySign(false) 60 | @assembly:AssemblyKeyFile("") 61 | @assembly:AssemblyKeyName("") -------------------------------------------------------------------------------- /Tests/Properties/Resources.Designer.swift: -------------------------------------------------------------------------------- 1 | /* The .Designer.swift file will be automatically generated when the parent file changes. 2 | You can also right-click the parent file and select 'Run Custom Tool' to update it now. */ -------------------------------------------------------------------------------- /Tests/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 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 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /Tests/Properties/Settings.Designer.swift: -------------------------------------------------------------------------------- 1 | /* The .Designer.swift file will be automatically generated when the parent file changes. 2 | You can also right-click the parent file and select 'Run Custom Tool' to update it now. */ -------------------------------------------------------------------------------- /Tests/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /TypeDefinitions.swift: -------------------------------------------------------------------------------- 1 | /* Types */ 2 | 3 | public enum CGTypeVisibilityKind { 4 | case Unspecified 5 | case Unit 6 | case Assembly 7 | case Public 8 | } 9 | 10 | public protocol ICGHasCondition { 11 | var Condition: CGConditionalDefine? { get } 12 | } 13 | 14 | public __abstract class CGTypeDefinition : CGEntity, ICGHasCondition { 15 | public var GenericParameters = List() 16 | public var Name: String 17 | public var Members = List() 18 | public var Visibility: CGTypeVisibilityKind = .Unspecified //in delphi, types with .Unit will be put into implementation section 19 | public var Static = false 20 | public var JavaStatic = false // Java language static types arent static in the regular sense 21 | public var Sealed = false 22 | public var Abstract = false 23 | public var Comment: CGCommentStatement? 24 | public var XmlDocumentation: CGXmlDocumentationStatement? 25 | public var Attributes = List() 26 | public var Condition: CGConditionalDefine? 27 | 28 | public init(_ name: String) { 29 | Name = name 30 | } 31 | 32 | @ToString func ToString() -> String { 33 | return Name; 34 | } 35 | 36 | } 37 | 38 | public final class CGGlobalTypeDefinition : CGTypeDefinition { 39 | private init() { 40 | super.init("") 41 | Static = true 42 | } 43 | 44 | public static lazy let GlobalType = CGGlobalTypeDefinition() 45 | } 46 | 47 | public class CGTypeAliasDefinition : CGTypeDefinition { 48 | public var ActualType: CGTypeReference 49 | public var Strict: Boolean = false 50 | 51 | public init(_ name: String, _ actualType: CGTypeReference) { 52 | super.init(name) 53 | ActualType = actualType 54 | } 55 | } 56 | 57 | public class CGCombinedInterfaceDefinition : CGTypeDefinition { 58 | public var Interfaces: List 59 | 60 | public init(_ name: String, _ interfaces: List) { 61 | super.init(name) 62 | Interfaces = interfaces 63 | } 64 | } 65 | 66 | public class CGBlockTypeDefinition : CGTypeDefinition { 67 | public var Parameters = List() 68 | public var ReturnType: CGTypeReference? 69 | public var IsPlainFunctionPointer = false 70 | public var Throws = false /* Swift and Java only */ 71 | } 72 | 73 | public class CGEnumTypeDefinition : CGTypeDefinition { 74 | public var BaseType: CGTypeReference? 75 | } 76 | 77 | public __abstract class CGClassOrStructTypeDefinition : CGTypeDefinition { 78 | public var Ancestors: List 79 | public var ImplementedInterfaces: List 80 | public var Partial = false 81 | 82 | //public var PublicInvariants: List? 83 | //public var PrivateInvariants: List? 84 | 85 | public init(_ name: String) { 86 | super.init(name) 87 | Ancestors = List() 88 | ImplementedInterfaces = List() 89 | } 90 | public init(_ name: String, _ ancestor: CGTypeReference) { 91 | super.init(name) 92 | Ancestors = List() 93 | Ancestors.Add(ancestor) 94 | ImplementedInterfaces = List() 95 | } 96 | public init(_ name: String, _ ancestors: List) { 97 | super.init(name) 98 | Ancestors = ancestors 99 | ImplementedInterfaces = List() 100 | } 101 | public init(_ name: String, _ ancestor: CGTypeReference, _ interfaces: List) { 102 | super.init(name) 103 | Ancestors = List() 104 | Ancestors.Add(ancestor) 105 | ImplementedInterfaces = interfaces 106 | } 107 | public init(_ name: String, _ ancestors: List, _ interfaces: List) { 108 | super.init(name) 109 | Ancestors = ancestors 110 | ImplementedInterfaces = interfaces 111 | } 112 | } 113 | 114 | public class CGClassTypeDefinition : CGClassOrStructTypeDefinition { 115 | } 116 | 117 | public class CGStructTypeDefinition : CGClassOrStructTypeDefinition { 118 | } 119 | 120 | public class CGInterfaceTypeDefinition : CGClassOrStructTypeDefinition { 121 | public var InterfaceGuid: Guid?;// legacy delphi only. declaration is : 122 | // type interfaceName = interface (ancestorInterface) ['{GUID}'] memberList end; 123 | } 124 | 125 | public class CGExtensionTypeDefinition : CGClassOrStructTypeDefinition { 126 | } 127 | 128 | public class CGMappedTypeDefinition : CGClassOrStructTypeDefinition { 129 | public var mappedType: CGTypeReference 130 | 131 | public init(_ name: String, mappedType: CGTypeReference) { 132 | super.init(name) 133 | self.mappedType = mappedType 134 | } 135 | public init(_ name: String, mappedType: CGTypeReference, _ ancestor: CGTypeReference) { 136 | super.init(name, ancestor) 137 | self.mappedType = mappedType 138 | } 139 | public init(_ name: String, mappedType: CGTypeReference, _ ancestors: List) { 140 | super.init(name, ancestors) 141 | self.mappedType = mappedType 142 | } 143 | public init(_ name: String, mappedType: CGTypeReference, _ ancestor: CGTypeReference, _ interfaces: List) { 144 | super.init(name, ancestor, interfaces) 145 | self.mappedType = mappedType 146 | } 147 | public init(_ name: String, mappedType: CGTypeReference, _ ancestors: List, _ interfaces: List) { 148 | super.init(name, ancestors, interfaces) 149 | self.mappedType = mappedType 150 | }} 151 | 152 | /* Type members */ 153 | 154 | public enum CGMemberVisibilityKind { 155 | case Unspecified 156 | case Private 157 | case Unit 158 | case UnitOrProtected 159 | case UnitAndProtected 160 | case Assembly 161 | case AssemblyOrProtected 162 | case AssemblyAndProtected 163 | case Protected 164 | case Public 165 | case Published /* Delphi only */ 166 | } 167 | 168 | public enum CGMemberVirtualityKind { 169 | case None 170 | case Virtual 171 | case Abstract 172 | case Override 173 | case Final 174 | case Dynamic // Delphi only 175 | } 176 | 177 | public __abstract class CGMemberDefinition: CGEntity, ICGHasCondition { 178 | public var Name: String 179 | public var Visibility: CGMemberVisibilityKind = .Private 180 | public var Virtuality: CGMemberVirtualityKind = .None 181 | public var Reintroduced = false 182 | public var Static = false 183 | public var Overloaded = false 184 | public var Locked = false /* Oxygene only */ 185 | public var LockedOn: CGExpression? /* Oxygene only */ 186 | public var Comment: CGCommentStatement? 187 | public var XmlDocumentation: CGXmlDocumentationStatement? 188 | public var Attributes = List() 189 | public var InlineAttributes = true 190 | public var Condition: CGConditionalDefine? 191 | public var ThrownExceptions: List? // nil means unknown; empty list means known to not throw. 192 | public var ImplementsInterface: CGTypeReference? 193 | public var ImplementsInterfaceMember: String? 194 | 195 | public init(_ name: String) { 196 | Name = name 197 | } 198 | } 199 | 200 | public class CGRawMemberDefinition: CGMemberDefinition { 201 | public var Lines: List 202 | 203 | public init(_ lines: List) { 204 | super.init("__UNNAMED__") 205 | Lines = lines 206 | } 207 | /*public convenience init(_ lines: String ...) { 208 | init(lines.ToList()) 209 | }*/ 210 | public init(_ lines: String) { 211 | Lines = lines.Replace("\r", "").Split("\n").MutableVersion() 212 | } 213 | } 214 | 215 | public class CGEnumValueDefinition: CGMemberDefinition { 216 | public var Value: CGExpression? 217 | 218 | public init(_ name: String) { 219 | super.init(name) 220 | } 221 | public convenience init(_ name: String, _ value: CGExpression) { 222 | init(name) 223 | Value = value 224 | } 225 | } 226 | 227 | // 228 | // Methods & Co 229 | // 230 | 231 | public enum CGCallingConventionKind { 232 | case CDecl /* C++ and Delphi */ 233 | case Pascal /* C++ and Delphi */ 234 | case StdCall /* C++ and Delphi */ 235 | case FastCall /* C++ */ 236 | case SafeCall /* C++ and Delphi */ 237 | case ClrCall /* VC++ */ 238 | case ThisCall /* VC++ */ 239 | case VectorCall /* VC++ */ 240 | case Register /* C++Builder and Delphi */ 241 | } 242 | 243 | public __abstract class CGMethodLikeMemberDefinition: CGMemberDefinition { 244 | public var Parameters = List() 245 | public var ReturnType: CGTypeReference? 246 | public var Inline = false 247 | public var External = false 248 | public var Empty = false 249 | public var Partial = false /* Oxygene only */ 250 | public var Async = false /* Oxygene only */ 251 | public var Awaitable = false /* C# only */ 252 | public var Throws = false /* Swift and Java only */ 253 | public var Optional = false /* Swift only */ 254 | public var CallingConvention: CGCallingConventionKind? /* Delphi and C++Builder only */ 255 | public var Statements: List 256 | public var LocalVariables: List? // Legacy Delphi only. 257 | public var LocalTypes: List? // Legacy Delphi only. 258 | public var LocalMethods: List? // Pascal only. 259 | public var Handles: CGExpression? // Visual Basic only. 260 | 261 | public init(_ name: String) { 262 | super.init(name) 263 | Statements = List() 264 | } 265 | public init(_ name: String, _ statements: List) { 266 | super.init(name) 267 | Statements = statements 268 | } 269 | public init(_ name: String, _ statements: CGStatement...) { 270 | super.init(name) 271 | Statements = statements.ToList() 272 | } 273 | } 274 | 275 | public class CGMethodDefinition: CGMethodLikeMemberDefinition { 276 | public var GenericParameters: List? 277 | public var Preconditions: List? 278 | public var Postconditions: List? 279 | } 280 | 281 | public class CGConstructorDefinition: CGMethodLikeMemberDefinition { 282 | public var Nullability = CGTypeNullabilityKind.NotNullable /* Swift only. currently. */ 283 | public var Required = false /* Swift only. currently. */ 284 | public var Failable = false /* Swift only. currently. */ 285 | 286 | public init() { 287 | super.init("") 288 | } 289 | public init(_ name: String, _ statements: List) { 290 | var name = name 291 | if name == ".ctor" || name == ".cctor" { 292 | name = "" 293 | } 294 | super.init(name, statements) 295 | } 296 | convenience public init(_ name: String, _ statements: CGStatement...) { 297 | init(name, statements.ToList()) 298 | } 299 | 300 | public var NestedConstrutorCall: CGConstructorCallStatement? { 301 | for s in Statements { 302 | if let ctorCall = s as? CGConstructorCallStatement { 303 | return ctorCall 304 | } 305 | } 306 | return nil 307 | } 308 | } 309 | 310 | public class CGDestructorDefinition: CGMethodLikeMemberDefinition { 311 | } 312 | 313 | public class CGFinalizerDefinition: CGMethodLikeMemberDefinition { 314 | public init() { 315 | super.init("Finalizer") 316 | } 317 | public init(_ name: String, _ statements: List) { 318 | super.init("Finalizer", statements) 319 | } 320 | public init(_ name: String, _ statements: CGStatement...) { 321 | super.init("Finalizer", statements.ToList()) 322 | } 323 | } 324 | 325 | public class CGCustomOperatorDefinition: CGMethodLikeMemberDefinition { 326 | } 327 | 328 | // 329 | // Fields & Co 330 | // 331 | 332 | public __abstract class CGFieldLikeMemberDefinition: CGMemberDefinition { 333 | public var `Type`: CGTypeReference? 334 | 335 | public init(_ name: String) { 336 | super.init(name) 337 | } 338 | public init(_ name: String, _ type: CGTypeReference) { 339 | super.init(name) 340 | `Type` = type 341 | } 342 | } 343 | 344 | public __abstract class CGFieldOrPropertyDefinition: CGFieldLikeMemberDefinition { 345 | public var Initializer: CGExpression? 346 | public var ReadOnly = false 347 | public var WriteOnly = false 348 | public var StorageModifier: CGStorageModifierKind = CGStorageModifierKind.Strong 349 | } 350 | 351 | public class CGFieldDefinition: CGFieldOrPropertyDefinition { 352 | public var Constant = false 353 | public var Volatile = false 354 | public var WithEvents = false // Visual Basic only. 355 | } 356 | 357 | public class CGPropertyDefinition: CGFieldOrPropertyDefinition { 358 | public var Lazy = false 359 | public var Atomic = false 360 | public var Dynamic = false 361 | public var Default = false 362 | public var Parameters = List() 363 | public var GetStatements: List? 364 | public var SetStatements: List? 365 | public var GetExpression: CGExpression? 366 | public var SetExpression: CGExpression? 367 | public var GetterVisibility: CGMemberVisibilityKind? 368 | public var SetterVisibility: CGMemberVisibilityKind? 369 | 370 | public init(_ name: String) { 371 | super.init(name) 372 | } 373 | public init(_ name: String, _ type: CGTypeReference) { 374 | super.init(name, type) 375 | } 376 | public convenience init(_ name: String, _ type: CGTypeReference, _ getStatements: List, _ setStatements: List? = nil) { 377 | init(name, type) 378 | GetStatements = getStatements 379 | SetStatements = setStatements 380 | } 381 | public convenience init(_ name: String, _ type: CGTypeReference, _ getStatements: CGStatement[], _ setStatements: CGStatement[]? = nil) { 382 | init(name, type, getStatements.ToList(), setStatements?.ToList()) 383 | } 384 | public convenience init(_ name: String, _ type: CGTypeReference, _ getExpression: CGExpression, _ setExpression: CGExpression? = nil) { 385 | init(name, type) 386 | GetExpression = getExpression 387 | SetExpression = setExpression 388 | } 389 | 390 | internal var HasGetterMethod: Boolean { 391 | if let getStatements = GetStatements, let type = `Type` { 392 | return true 393 | } else if let getExpression = GetExpression, let type = `Type` { 394 | return true 395 | } 396 | return false 397 | } 398 | 399 | internal var HasSetterMethod: Boolean { 400 | if let setStatements = GetStatements, let type = `Type` { 401 | return true 402 | } else if let setExpression = SetExpression, let type = `Type` { 403 | return true 404 | } 405 | return false 406 | } 407 | 408 | internal func GetterMethodDefinition(`prefix`: String = "get__") -> CGMethodDefinition? { 409 | if let getStatements = GetStatements, let type = `Type` { 410 | let method = CGMethodDefinition(`prefix`+Name, getStatements) 411 | method.ReturnType = type 412 | method.Parameters = Parameters 413 | method.Static = Static 414 | method.Condition = Condition 415 | return method 416 | } else if let getExpression = GetExpression, let type = `Type` { 417 | let method = CGMethodDefinition(`prefix`+Name) 418 | method.ReturnType = type 419 | method.Parameters = Parameters 420 | method.Statements.Add(getExpression.AsReturnStatement()) 421 | method.Static = Static 422 | method.Condition = Condition 423 | return method 424 | } 425 | return nil 426 | } 427 | 428 | public static let MAGIC_VALUE_PARAMETER_NAME = "___value___" 429 | 430 | internal func SetterMethodDefinition(`prefix`: String = "set__") -> CGMethodDefinition? { 431 | if let setStatements = SetStatements, let type = `Type` { 432 | let method = CGMethodDefinition(`prefix`+Name, setStatements) 433 | method.Parameters.Add(Parameters) 434 | method.Parameters.Add(CGParameterDefinition(MAGIC_VALUE_PARAMETER_NAME, type)) 435 | method.Condition = Condition 436 | return method 437 | } else if let setExpression = SetExpression, let type = `Type` { 438 | let method = CGMethodDefinition(`prefix`+Name) 439 | method.Parameters.Add(Parameters) 440 | method.Parameters.Add(CGParameterDefinition(MAGIC_VALUE_PARAMETER_NAME, type)) 441 | method.Statements.Add(CGAssignmentStatement(setExpression, CGLocalVariableAccessExpression(MAGIC_VALUE_PARAMETER_NAME))) 442 | method.Condition = Condition 443 | return method 444 | } 445 | return nil 446 | } 447 | 448 | public var IsShortcutProperty: Boolean { get { return GetStatements == nil && SetStatements == nil && GetExpression == nil && SetExpression == nil } } 449 | } 450 | 451 | public class CGEventDefinition: CGFieldLikeMemberDefinition { 452 | public var AddStatements: List? 453 | public var RemoveStatements: List? 454 | //public var RaiseStatements: List? 455 | 456 | public init(_ name: String, _ type: CGTypeReference) { 457 | super.init(name, type) 458 | } 459 | public convenience init(_ name: String, _ type: CGTypeReference, _ addStatements: List, _ removeStatements: List/*, _ raiseStatements: List? = nil*/) { 460 | init(name, type) 461 | AddStatements = addStatements 462 | RemoveStatements = removeStatements 463 | //RaiseStatements = raiseStatements 464 | } 465 | 466 | internal func AddMethodDefinition() -> CGMethodDefinition? { 467 | if let addStatements = AddStatements, let type = `Type` { 468 | let method = CGMethodDefinition("add__"+Name, addStatements) 469 | method.Parameters.Add(CGParameterDefinition("___value", type)) 470 | method.Condition = Condition 471 | return method 472 | } 473 | return nil 474 | } 475 | 476 | internal func RemoveMethodDefinition() -> CGMethodDefinition? { 477 | if let removeStatements = RemoveStatements, let type = `Type` { 478 | let method = CGMethodDefinition("remove__"+Name, removeStatements) 479 | method.Parameters.Add(CGParameterDefinition("___value", type)) 480 | method.Condition = Condition 481 | return method 482 | } 483 | return nil 484 | } 485 | 486 | /*internal func RaiseMethodDefinition() -> CGMethodDefinition? { 487 | if let raiseStatements = RaiseStatements, type = `Type` { 488 | let method = CGMethodDefinition("raise__"+Name, raisetatements) 489 | //todo: this would need the same parameters as the block, which we don't have 490 | return method 491 | } 492 | return nil 493 | }*/ 494 | } 495 | 496 | public class CGNestedTypeDefinition: CGMemberDefinition { 497 | public var `Type`: CGTypeDefinition 498 | 499 | public init(_ type: CGTypeDefinition) { 500 | super.init(type.Name) 501 | `Type` = type 502 | } 503 | } 504 | 505 | // 506 | // Parameters 507 | // 508 | 509 | public enum CGParameterModifierKind { 510 | case In 511 | case Out 512 | case Var 513 | case Const 514 | case Params 515 | } 516 | 517 | public class CGParameterDefinition : CGEntity { 518 | public var Name: String 519 | public var ExternalName: String? 520 | public var `Type`: CGTypeReference? 521 | public var Modifier: CGParameterModifierKind = .In 522 | public var DefaultValue: CGExpression? 523 | public var Attributes = List() 524 | public var InlineAttributes = true 525 | public var XmlDocumentation: CGXmlDocumentationStatement? 526 | 527 | public init(_ name: String) { 528 | Name = name 529 | } 530 | 531 | public init(_ name: String, _ type: CGTypeReference) { 532 | Name = name 533 | `Type` = type 534 | } 535 | } 536 | 537 | @Obsolete("Use CGParameterDefinition") 538 | public typealias CGAnonymousMethodParameterDefinition = CGParameterDefinition 539 | 540 | public class CGGenericParameterDefinition : CGEntity { 541 | public var Constraints = List() 542 | public var Name: String 543 | public var Variance: CGGenericParameterVarianceKind? 544 | 545 | public init(_ name: String) { 546 | Name = name 547 | } 548 | } 549 | 550 | public enum CGGenericParameterVarianceKind { 551 | case Covariant 552 | case Contravariant 553 | } 554 | 555 | public __abstract class CGGenericConstraint : CGEntity { 556 | } 557 | 558 | public class CGGenericHasConstructorConstraint : CGGenericConstraint { 559 | } 560 | 561 | public class CGGenericIsSpecificTypeConstraint : CGGenericConstraint { 562 | public var `Type`: CGTypeReference 563 | 564 | public init(_ type: CGTypeReference) { 565 | `Type` = type 566 | } 567 | } 568 | 569 | public class CGGenericIsSpecificTypeKindConstraint : CGGenericConstraint { 570 | public var Kind: CGGenericConstraintTypeKind 571 | 572 | public init(_ kind: CGGenericConstraintTypeKind) { 573 | Kind = kind 574 | } 575 | } 576 | 577 | public enum CGGenericConstraintTypeKind { 578 | case Class 579 | case Struct 580 | case Interface 581 | } 582 | 583 | public class CGAttribute: CGEntity, ICGHasCondition { 584 | public var `Type`: CGTypeReference 585 | public var Parameters: List? 586 | public var Comment: CGSingleLineCommentStatement? 587 | public var Condition: CGConditionalDefine? 588 | public var Scope: CGAttributeScopeKind? 589 | 590 | public init(_ type: CGTypeReference) { 591 | `Type` = type 592 | } 593 | public init(_ type: CGTypeReference,_ parameters: List) { 594 | `Type` = type 595 | Parameters = parameters 596 | } 597 | public convenience init(_ type: CGTypeReference,_ parameters: CGCallParameter...) { 598 | init(type, parameters.ToList()) 599 | } 600 | } 601 | 602 | public enum CGAttributeScopeKind { 603 | case Assembly 604 | case Module 605 | case Global // Oxygene only 606 | case Result 607 | case Parameter 608 | case Field 609 | case Getter 610 | case Setter 611 | case Type // C# only 612 | case Method // C# only 613 | case Event // C# only 614 | case Property // C# only 615 | } -------------------------------------------------------------------------------- /TypeReferences.swift: -------------------------------------------------------------------------------- 1 | /* Type References */ 2 | 3 | public enum CGTypeNullabilityKind { 4 | case Unknown 5 | case Default 6 | case NotNullable 7 | case NullableUnwrapped 8 | case NullableNotUnwrapped 9 | } 10 | 11 | public __abstract class CGTypeReference : CGEntity { 12 | public /*fileprivate(set)*/ var Nullability: CGTypeNullabilityKind = .Default 13 | public /*fileprivate(set)*/ var DefaultNullability: CGTypeNullabilityKind = .NotNullable 14 | public /*fileprivate(set)*/ var DefaultValue: CGExpression? 15 | public /*fileprivate(set)*/ var IsClassType = false 16 | 17 | public lazy var NullableUnwrapped: CGTypeReference = ActualNullability == CGTypeNullabilityKind.NullableUnwrapped ? self : self.copyWithNullability(CGTypeNullabilityKind.NullableUnwrapped) 18 | public lazy var NullableNotUnwrapped: CGTypeReference = ActualNullability == CGTypeNullabilityKind.NullableNotUnwrapped ? self : self.copyWithNullability(CGTypeNullabilityKind.NullableNotUnwrapped) 19 | public lazy var NotNullable: CGTypeReference = ActualNullability == CGTypeNullabilityKind.NotNullable ? self : self.copyWithNullability(CGTypeNullabilityKind.NotNullable) 20 | 21 | public var ActualNullability: CGTypeNullabilityKind { 22 | if Nullability == CGTypeNullabilityKind.Default || Nullability == CGTypeNullabilityKind.Unknown { 23 | return DefaultNullability 24 | } 25 | return Nullability 26 | } 27 | 28 | public var IsVoid: Boolean { 29 | return false 30 | } 31 | 32 | public __abstract func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference 33 | } 34 | 35 | public enum CGStorageModifierKind { 36 | case Strong 37 | case Weak 38 | case Unretained 39 | } 40 | 41 | public class CGUnknownTypeReference : CGTypeReference { 42 | public override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 43 | return self 44 | } 45 | 46 | public static lazy var UnknownType = CGUnknownTypeReference() 47 | } 48 | 49 | public class CGNamedTypeReference : CGTypeReference { 50 | public let Name: String 51 | public private(set) var Namespace: CGNamespaceReference? 52 | public var GenericArguments: List? 53 | 54 | public var FullName: String { 55 | if let namespace = Namespace { 56 | return namespace.Name+"."+Name 57 | } 58 | return Name 59 | } 60 | 61 | public init(_ name: String) { 62 | Name = name 63 | IsClassType = true 64 | DefaultNullability = .NullableUnwrapped 65 | } 66 | public convenience init(_ name: String, namespace: CGNamespaceReference) { 67 | init(name) 68 | Namespace = namespace 69 | } 70 | public convenience init(_ name: String, defaultNullability: CGTypeNullabilityKind) { 71 | init(name) 72 | DefaultNullability = defaultNullability 73 | } 74 | public convenience init(_ name: String, defaultNullability: CGTypeNullabilityKind, nullability: CGTypeNullabilityKind) { 75 | init(name) 76 | DefaultNullability = defaultNullability 77 | Nullability = nullability 78 | } 79 | public convenience init(_ name: String, isClassType: Boolean) { 80 | init(name) 81 | IsClassType = isClassType 82 | DefaultNullability = isClassType ? CGTypeNullabilityKind.NullableUnwrapped : CGTypeNullabilityKind.NotNullable 83 | } 84 | public convenience init(_ name: String, namespace: CGNamespaceReference, isClassType: Boolean) { 85 | init(name) 86 | Namespace = namespace 87 | IsClassType = isClassType 88 | DefaultNullability = isClassType ? CGTypeNullabilityKind.NullableUnwrapped : CGTypeNullabilityKind.NotNullable 89 | } 90 | 91 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 92 | let result = CGNamedTypeReference(Name, defaultNullability: DefaultNullability, nullability: nullability) 93 | result.GenericArguments = GenericArguments 94 | 95 | result.Namespace = Namespace 96 | result.DefaultValue = DefaultValue 97 | result.IsClassType = IsClassType 98 | return result 99 | } 100 | 101 | @ToString public func ToString() -> String { 102 | return "<\(Name)>"; 103 | } 104 | } 105 | 106 | __abstract public class CGWrapperTypeReference : CGTypeReference { 107 | public var Type: CGTypeReference 108 | 109 | public init(_ type: CGTypeReference) { 110 | self.Type = type 111 | } 112 | 113 | public init(_ type: CGTypeReference, nullability: CGTypeNullabilityKind) { 114 | self.Type = type 115 | self.Nullability = nullability 116 | } 117 | } 118 | 119 | public class CGOpaqueTypeReference : CGWrapperTypeReference { 120 | public override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 121 | return CGOpaqueTypeReference(Type, nullability: nullability); 122 | } 123 | } 124 | 125 | public class CGExistentialTypeReference : CGWrapperTypeReference { 126 | public override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 127 | return CGExistentialTypeReference(Type, nullability: nullability); 128 | } 129 | } 130 | 131 | public class CGMetaTypeReference : CGWrapperTypeReference { 132 | public override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 133 | return CGMetaTypeReference(Type, nullability: nullability); 134 | } 135 | } 136 | 137 | public class CGPredefinedTypeReference : CGTypeReference { 138 | public var Kind: CGPredefinedTypeKind 139 | 140 | //todo:these should become provate and force use of the static members 141 | private init(_ kind: CGPredefinedTypeKind) { 142 | Kind = kind 143 | switch Kind { 144 | case .Int: fallthrough 145 | case .UInt: fallthrough 146 | case .Int8: fallthrough 147 | case .UInt8: fallthrough 148 | case .Int16: fallthrough 149 | case .UInt16: fallthrough 150 | case .Int32: fallthrough 151 | case .UInt32: fallthrough 152 | case .Int64: fallthrough 153 | case .UInt64: fallthrough 154 | case .IntPtr: fallthrough 155 | case .UIntPtr: 156 | DefaultValue = CGIntegerLiteralExpression.Zero 157 | DefaultNullability = .NotNullable 158 | case .Single: fallthrough 159 | case .Double: 160 | DefaultValue = CGFloatLiteralExpression.Zero 161 | DefaultNullability = .NotNullable 162 | //case .Decimal 163 | case .Boolean: 164 | DefaultValue = CGBooleanLiteralExpression.False 165 | DefaultNullability = .NotNullable 166 | case .String: 167 | DefaultValue = CGStringLiteralExpression.Empty 168 | DefaultNullability = .NullableUnwrapped 169 | IsClassType = true 170 | case .AnsiChar: fallthrough 171 | case .UTF16Char: fallthrough 172 | case .UTF32Char: 173 | DefaultValue = CGCharacterLiteralExpression.Zero 174 | DefaultNullability = .NotNullable 175 | case .Dynamic: fallthrough 176 | case .InstanceType: fallthrough 177 | case .Void: 178 | DefaultValue = CGNilExpression.Nil 179 | DefaultNullability = .NullableUnwrapped 180 | IsClassType = true 181 | case .Object: 182 | DefaultValue = CGNilExpression.Nil 183 | DefaultNullability = .NullableUnwrapped 184 | IsClassType = true 185 | case .Class: 186 | DefaultValue = CGNilExpression.Nil 187 | DefaultNullability = .NullableUnwrapped 188 | IsClassType = true 189 | } 190 | } 191 | private convenience init(_ kind: CGPredefinedTypeKind, defaultNullability: CGTypeNullabilityKind?, nullability: CGTypeNullabilityKind?) { 192 | init(kind) 193 | if let defaultNullability = defaultNullability { 194 | DefaultNullability = defaultNullability 195 | } 196 | if let nullability = nullability { 197 | Nullability = nullability 198 | } 199 | } 200 | private convenience init(_ kind: CGPredefinedTypeKind, defaultValue: CGExpression) { 201 | init(kind) 202 | DefaultValue = defaultValue 203 | } 204 | 205 | override var IsVoid: Boolean { 206 | return Kind == CGPredefinedTypeKind.Void 207 | } 208 | 209 | /*public lazy var NullableUnwrapped: CGPredefinedTypeReference = ActualNullability == CGTypeNullabilityKind.NullableUnwrapped ? self : CGPredefinedTypeReference(Kind, nullability: CGTypeNullabilityKind.NullableUnwrapped) 210 | public lazy var NullableNotUnwrapped: CGPredefinedTypeReference = ActualNullability == CGTypeNullabilityKind.NullableNotUnwrapped ? self : CGPredefinedTypeReference(Kind, nullability: CGTypeNullabilityKind.NullableNotUnwrapped) 211 | public lazy var NotNullable: CGPredefinedTypeReference = ActualNullability == CGTypeNullabilityKind.NotNullable ? self : CGPredefinedTypeReference(Kind, nullability: CGTypeNullabilityKind.NotNullable)*/ 212 | 213 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 214 | let result = CGPredefinedTypeReference(Kind, defaultNullability: nil, nullability: nullability) 215 | 216 | result.DefaultValue = DefaultValue 217 | result.IsClassType = IsClassType 218 | return result 219 | } 220 | 221 | public static lazy var Int = CGPredefinedTypeReference(.Int) 222 | public static lazy var UInt = CGPredefinedTypeReference(.UInt) 223 | public static lazy var Int8 = CGPredefinedTypeReference(.Int8) 224 | public static lazy var UInt8 = CGPredefinedTypeReference(.UInt8) 225 | public static lazy var Int16 = CGPredefinedTypeReference(.Int16) 226 | public static lazy var UInt16 = CGPredefinedTypeReference(.UInt16) 227 | public static lazy var Int32 = CGPredefinedTypeReference(.Int32) 228 | public static lazy var UInt32 = CGPredefinedTypeReference(.UInt32) 229 | public static lazy var Int64 = CGPredefinedTypeReference(.Int64) 230 | public static lazy var UInt64 = CGPredefinedTypeReference(.UInt64) 231 | public static lazy var IntPtr = CGPredefinedTypeReference(.IntPtr) 232 | public static lazy var UIntPtr = CGPredefinedTypeReference(.UIntPtr) 233 | public static lazy var Single = CGPredefinedTypeReference(.Single) 234 | public static lazy var Double = CGPredefinedTypeReference(.Double) 235 | //public static lazy var Decimal = CGPredefinedTypeReference(.Decimal) 236 | public static lazy var Boolean = CGPredefinedTypeReference(.Boolean) 237 | public static lazy var String = CGPredefinedTypeReference(.String) 238 | public static lazy var AnsiChar = CGPredefinedTypeReference(.AnsiChar) 239 | public static lazy var UTF16Char = CGPredefinedTypeReference(.UTF16Char) 240 | public static lazy var UTF32Char = CGPredefinedTypeReference(.UTF32Char) 241 | public static lazy var Dynamic = CGPredefinedTypeReference(.Dynamic) 242 | public static lazy var InstanceType = CGPredefinedTypeReference(.InstanceType) 243 | public static lazy var Void = CGPredefinedTypeReference(.Void) 244 | public static lazy var Object = CGPredefinedTypeReference(.Object) 245 | public static lazy var Class = CGPredefinedTypeReference(.Class) 246 | 247 | @ToString public func ToString() -> String { 248 | return Kind.ToString(); 249 | } 250 | } 251 | 252 | public enum CGPredefinedTypeKind { 253 | case Int 254 | case UInt 255 | case Int8 256 | case UInt8 257 | case Int16 258 | case UInt16 259 | case Int32 260 | case UInt32 261 | case Int64 262 | case UInt64 263 | case IntPtr 264 | case UIntPtr 265 | case Single 266 | case Double 267 | //case Decimal 268 | case Boolean 269 | case String 270 | case AnsiChar 271 | case UTF16Char 272 | case UTF32Char 273 | case Dynamic // aka "id", "Any" 274 | case InstanceType // aka "Self" 275 | case Void 276 | case Object 277 | case Class 278 | } 279 | 280 | public class CGIntegerRangeTypeReference : CGTypeReference { 281 | public let Start: Integer 282 | public let End: Integer 283 | 284 | public init(_ start: Integer, _ end: Integer) { 285 | Start = start 286 | End = end 287 | } 288 | 289 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 290 | let result = CGIntegerRangeTypeReference(Start, End) 291 | result.Nullability = nullability 292 | return result 293 | } 294 | } 295 | 296 | public class CGInlineBlockTypeReference : CGTypeReference { 297 | public let Block: CGBlockTypeDefinition 298 | 299 | public init(_ block: CGBlockTypeDefinition) { 300 | Block = block 301 | DefaultNullability = .NullableUnwrapped 302 | } 303 | 304 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 305 | let result = CGInlineBlockTypeReference(Block) 306 | 307 | result.Nullability = nullability 308 | result.DefaultValue = DefaultValue 309 | result.IsClassType = IsClassType 310 | return result 311 | } 312 | 313 | @ToString public func ToString() -> String { 314 | return "block"; 315 | } 316 | } 317 | 318 | public class CGPointerTypeReference : CGTypeReference { 319 | public let `Type`: CGTypeReference 320 | public private(set) var Reference = false /* C++ only: "&" (true) vs "*" (false) */ 321 | 322 | public init(_ type: CGTypeReference) { 323 | `Type` = type 324 | DefaultNullability = .NullableUnwrapped 325 | } 326 | 327 | public convenience init(_ type: CGTypeReference, reference: Boolean) { /* C++ only */ 328 | init(type) 329 | Reference = reference 330 | } 331 | 332 | public static lazy var VoidPointer = CGPointerTypeReference(CGPredefinedTypeReference.Void) 333 | 334 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 335 | let result = CGPointerTypeReference(`Type`, reference: Reference) 336 | 337 | result.Nullability = nullability 338 | result.DefaultValue = DefaultValue 339 | result.IsClassType = IsClassType 340 | return result 341 | } 342 | 343 | @ToString public func ToString() -> String { 344 | return ""; 345 | } 346 | } 347 | 348 | public class CGConstantTypeReference : CGTypeReference { /* C++ only, currently */ 349 | public let `Type`: CGTypeReference 350 | 351 | public init(_ type: CGTypeReference) { 352 | `Type` = type 353 | DefaultNullability = .NullableUnwrapped 354 | IsClassType = true 355 | } 356 | 357 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 358 | let result = CGConstantTypeReference(`Type`) 359 | 360 | result.Nullability = nullability 361 | result.DefaultValue = DefaultValue 362 | result.IsClassType = IsClassType 363 | return result 364 | } 365 | 366 | @ToString public func ToString() -> String { 367 | return ""; 368 | } 369 | } 370 | 371 | public class CGKindOfTypeReference : CGTypeReference { 372 | public let `Type`: CGTypeReference 373 | 374 | public init(_ type: CGTypeReference) { 375 | `Type` = type 376 | DefaultNullability = .NullableUnwrapped 377 | IsClassType = true 378 | } 379 | 380 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 381 | let result = CGKindOfTypeReference(`Type`) 382 | 383 | result.Nullability = nullability 384 | result.DefaultValue = DefaultValue 385 | result.IsClassType = IsClassType 386 | return result 387 | } 388 | 389 | @ToString public func ToString() -> String { 390 | return ""; 391 | } 392 | } 393 | 394 | public class CGTupleTypeReference : CGTypeReference { 395 | public var Members: List 396 | 397 | public init(_ members: List) { 398 | Members = members 399 | DefaultNullability = .NotNullable 400 | } 401 | public convenience init(_ members: CGTypeReference...) { 402 | init(members.ToList()) 403 | } 404 | 405 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 406 | let result = CGTupleTypeReference(Members) 407 | 408 | result.Nullability = nullability 409 | result.DefaultValue = DefaultValue 410 | result.IsClassType = IsClassType 411 | return result 412 | } 413 | 414 | @ToString public func ToString() -> String { 415 | return ""; 416 | } 417 | } 418 | 419 | public class CGSequenceTypeReference : CGTypeReference { 420 | public var `Type`: CGTypeReference 421 | 422 | public init(_ type: CGTypeReference) { 423 | `Type` = type 424 | DefaultNullability = .NullableNotUnwrapped 425 | IsClassType = true 426 | } 427 | 428 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 429 | let result = CGSequenceTypeReference(`Type`) 430 | 431 | result.Nullability = nullability 432 | result.DefaultValue = DefaultValue 433 | result.IsClassType = IsClassType 434 | return result 435 | } 436 | 437 | @ToString public func ToString() -> String { 438 | return ""; 439 | } 440 | } 441 | 442 | public class CGSetTypeReference : CGTypeReference { 443 | public var `Type`: CGTypeReference 444 | 445 | public init(_ type: CGTypeReference) { 446 | `Type` = type 447 | DefaultNullability = .NotNullable 448 | } 449 | 450 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 451 | let result = CGSetTypeReference(`Type`) 452 | 453 | result.Nullability = nullability 454 | result.DefaultValue = DefaultValue 455 | result.IsClassType = IsClassType 456 | return result 457 | } 458 | } 459 | 460 | /* Arrays */ 461 | 462 | public enum CGArrayKind { 463 | case Static 464 | case Dynamic 465 | case HighLevel /* Swift only */ 466 | } 467 | 468 | public class CGArrayTypeReference : CGTypeReference { 469 | public var `Type`: CGTypeReference 470 | public var Bounds: List? 471 | public var BoundsTypes: List? 472 | public var ArrayKind: CGArrayKind = .Dynamic 473 | 474 | public init(_ type: CGTypeReference, _ bounds: List? = nil) { 475 | `Type` = type 476 | DefaultNullability = .NullableNotUnwrapped 477 | if let bounds = bounds { 478 | Bounds = bounds 479 | } 480 | } 481 | 482 | public init(_ type: CGTypeReference, _ bounds: CGArrayBounds...) { 483 | `Type` = type 484 | DefaultNullability = .NullableNotUnwrapped 485 | Bounds = bounds.ToList() 486 | } 487 | 488 | public init(_ type: CGTypeReference, _ boundsTypes: List) { 489 | `Type` = type 490 | DefaultNullability = .NullableNotUnwrapped 491 | BoundsTypes = boundsTypes 492 | } 493 | 494 | public init(_ type: CGTypeReference, _ boundsTypes: CGTypeReference...) { 495 | `Type` = type 496 | DefaultNullability = .NullableNotUnwrapped 497 | BoundsTypes = boundsTypes.ToList() 498 | } 499 | 500 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 501 | let result = CGArrayTypeReference(`Type`, Bounds) 502 | 503 | result.Nullability = nullability 504 | result.ArrayKind = ArrayKind 505 | result.DefaultValue = DefaultValue 506 | result.IsClassType = IsClassType 507 | return result 508 | } 509 | 510 | @ToString public func ToString() -> String { 511 | return ""; 512 | } 513 | } 514 | 515 | public class CGArrayBounds : CGEntity { 516 | public var Start: CGExpression = 0 517 | public var End: CGExpression? 518 | 519 | public init() { 520 | } 521 | public init(_ start: Int32, end: Int32) { 522 | Start = CGIntegerLiteralExpression(start) 523 | End = CGIntegerLiteralExpression(end) 524 | } 525 | public init(_ start: CGExpression, end: CGExpression?) { 526 | Start = start 527 | End = end 528 | } 529 | } 530 | 531 | /* Dictionaries (Swift only for now) */ 532 | 533 | public class CGDictionaryTypeReference : CGTypeReference { 534 | public var KeyType: CGTypeReference 535 | public var ValueType: CGTypeReference 536 | 537 | public init(_ keyType: CGTypeReference, _ valueType: CGTypeReference) { 538 | KeyType = keyType 539 | ValueType = valueType 540 | DefaultNullability = .NullableNotUnwrapped 541 | IsClassType = true 542 | } 543 | 544 | override func copyWithNullability(_ nullability: CGTypeNullabilityKind) -> CGTypeReference { 545 | let result = CGDictionaryTypeReference(KeyType, ValueType) 546 | 547 | result.Nullability = nullability 548 | result.DefaultValue = DefaultValue 549 | result.IsClassType = IsClassType 550 | return result 551 | } 552 | 553 | @ToString public func ToString() -> String { 554 | return ""; 555 | } 556 | } --------------------------------------------------------------------------------