From 3b274c0e10f3fc5f0fa4d7d27365e4a13730ba38 Mon Sep 17 00:00:00 2001 From: Miguel de Icaza Date: Wed, 27 Sep 2023 19:15:01 -0400 Subject: [PATCH] Special case some static classes for internals work --- Generator/Generator/ClassGen.swift | 15 +++++++++------ Generator/Generator/MethodGen.swift | 6 +++--- Generator/Generator/UtilityGen.swift | 2 +- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Generator/Generator/ClassGen.swift b/Generator/Generator/ClassGen.swift index 244a092e1..ec33c1510 100644 --- a/Generator/Generator/ClassGen.swift +++ b/Generator/Generator/ClassGen.swift @@ -218,13 +218,13 @@ func generateMethods (_ p: Printer, docClass: DocClass?, methods: [JGodotClassMethod], usedMethods: Set, - isSingleton: Bool) -> [String:(String, JGodotClassMethod)] { + asSingleton: Bool) -> [String:(String, JGodotClassMethod)] { p ("/* Methods */") var virtuals: [String:(String, JGodotClassMethod)] = [:] for method in methods { - if let virtualMethodName = methodGen (p, method: method, className: cdef.name, cdef: cdef, docClass: docClass, usedMethods: usedMethods, kind: .class, isSingleton: isSingleton) { + if let virtualMethodName = methodGen (p, method: method, className: cdef.name, cdef: cdef, docClass: docClass, usedMethods: usedMethods, kind: .class, asSingleton: asSingleton) { virtuals [method.name] = (virtualMethodName, method) } } @@ -266,7 +266,7 @@ func generateProperties (_ p: Printer, _ properties: [JGodotProperty], _ methods: [JGodotClassMethod], _ referencedMethods: inout Set, - isSingleton: Bool) + asSingleton: Bool) { p ("\n/* Properties */\n") @@ -389,7 +389,7 @@ func generateProperties (_ p: Printer, doc (p, cdef, docMember.value) } } - p ("\(isSingleton ? "static" : "final") public var \(godotPropertyToSwift (property.name)): \(type!)"){ + p ("\(asSingleton ? "static" : "final") public var \(godotPropertyToSwift (property.name)): \(type!)"){ p ("get"){ p ("return \(getterName) (\(gettterArgName)\(access))") } @@ -594,7 +594,10 @@ func generateSignalDocAppendix (_ p: Printer, cdef: JGodotExtensionAPIClass, sig func processClass (cdef: JGodotExtensionAPIClass, outputDir: String) { let docClass = loadClassDoc(base: docRoot, name: cdef.name) + + // Determine if it is a singleton, but exclude EditorInterface let isSingleton = jsonApi.singletons.contains (where: { $0.name == cdef.name }) + let asSingleton = isSingleton && cdef.name != "EditorInterface" // Clear the result let p = Printer () @@ -679,10 +682,10 @@ func processClass (cdef: JGodotExtensionAPIClass, outputDir: String) { } if let properties = cdef.properties { - generateProperties (p, cdef: cdef, docClass: docClass, properties, cdef.methods ?? [], &referencedMethods, isSingleton: isSingleton) + generateProperties (p, cdef: cdef, docClass: docClass, properties, cdef.methods ?? [], &referencedMethods, asSingleton: asSingleton) } if let methods = cdef.methods { - virtuals = generateMethods (p, cdef: cdef, docClass: docClass, methods: methods, usedMethods: referencedMethods, isSingleton: isSingleton) + virtuals = generateMethods (p, cdef: cdef, docClass: docClass, methods: methods, usedMethods: referencedMethods, asSingleton: asSingleton) } if let signals = cdef.signals { diff --git a/Generator/Generator/MethodGen.swift b/Generator/Generator/MethodGen.swift index f39344e96..0a3cde47a 100644 --- a/Generator/Generator/MethodGen.swift +++ b/Generator/Generator/MethodGen.swift @@ -68,7 +68,7 @@ func isRefParameterOptional (className: String, method: String, arg: String) -> /// - className: the name of the class where this is being generated /// - usedMethods: a set of methods that have been referenced by properties, to determine whether we make this public or private /// - Returns: nil, or the method we surfaced that needs to have the virtual supporting infrastructured wired up -func methodGen (_ p: Printer, method: MethodDefinition, className: String, cdef: JClassInfo?, docClass: DocClass?, usedMethods: Set, kind: MethodGenType, isSingleton: Bool) -> String? { +func methodGen (_ p: Printer, method: MethodDefinition, className: String, cdef: JClassInfo?, docClass: DocClass?, usedMethods: Set, kind: MethodGenType, asSingleton: Bool) -> String? { var registerVirtualMethodName: String? = nil //let loc = "\(cdef.name).\(method.name)" @@ -87,7 +87,7 @@ func methodGen (_ p: Printer, method: MethodDefinition, className: String, cdef: var finalp: String // Default method name var methodName: String = godotMethodToSwift (method.name) - let instanceOrStatic = method.isStatic || isSingleton ? " static" : "" + let instanceOrStatic = method.isStatic || asSingleton ? " static" : "" var inline = "" if let methodHash = method.hash { assert (!method.isVirtual) @@ -338,7 +338,7 @@ func methodGen (_ p: Printer, method: MethodDefinition, className: String, cdef: switch kind { case .class: - let instanceHandle = method.isStatic ? "nil, " : "UnsafeMutableRawPointer (mutating: \(isSingleton ? "shared." : "")handle), " + let instanceHandle = method.isStatic ? "nil, " : "UnsafeMutableRawPointer (mutating: \(asSingleton ? "shared." : "")handle), " if method.isVararg { p ("gi.object_method_bind_call (\(className).method_\(method.name), \(instanceHandle)\(ptrArgs), Int64 (_args.count), \(ptrResult), nil)") } else { diff --git a/Generator/Generator/UtilityGen.swift b/Generator/Generator/UtilityGen.swift index 528d8e27b..bc20ceeed 100644 --- a/Generator/Generator/UtilityGen.swift +++ b/Generator/Generator/UtilityGen.swift @@ -21,7 +21,7 @@ func generateUtility(values: [JGodotUtilityFunction], outputDir: String) { for method in values { // We ignore the request for virtual methods, should not happen for these - _ = methodGen (p, method: method, className: "Godot", cdef: nil, docClass: docClass, usedMethods: emptyUsedMethods, kind: .utility, isSingleton: false) + _ = methodGen (p, method: method, className: "Godot", cdef: nil, docClass: docClass, usedMethods: emptyUsedMethods, kind: .utility, asSingleton: false) } }