Skip to content

Commit 53ff09c

Browse files
authored
New symbols, plus fixes (progrium#213)
* generate: update symbol and symbol declaration parsing for new symbolsdb * generate: use "Apple Documentation" for modules instead of "Full Documentation" * * update symbolsdb * use conventional generated file banner * protocols live in files with _protocol in the filename * dropped convenience alloc functions * renamed type for protocol wrapper to end with Object instead of Wrapper * ensure protocol wrappers implement the protocol interface * constants with same name as their enum name get K prefix * globbergen finds files by filename now * macos: regenerated * Makefile: update symbolsdb
1 parent 03834f4 commit 53ff09c

File tree

2,911 files changed

+161829
-136244
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

2,911 files changed

+161829
-136244
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ example:
1717
.PHONY: example
1818

1919
generate/symbols.zip:
20-
cd generate && wget https://github.com/mactypes/symbolsdb/releases/download/1.0/symbols.zip
20+
cd generate && wget https://github.com/mactypes/symbolsdb/releases/download/1.1/symbols.zip

generate/class.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (db *Generator) ToClassGen(sym Symbol) *codegen.Class {
4040
DocURL: sym.DocURL(),
4141
}
4242

43-
st, err := sym.Parse()
43+
st, err := sym.Parse(db.Platform)
4444
if err != nil {
4545
panic(err)
4646
}

generate/codegen/codegen.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package codegen
22

33
import "github.com/progrium/macdriver/internal/set"
44

5-
var AutoGeneratedMark = "// AUTO-GENERATED CODE, DO NOT MODIFY\n"
5+
var AutoGeneratedMark = "// Code generated by DarwinKit. DO NOT EDIT.\n"
66

77
// CodeGen is interface for Class/Protocol code Gen
88
type CodeGen interface {

generate/codegen/filewriter.go

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package codegen
33
import (
44
"os"
55
"path/filepath"
6+
"strings"
67

78
"github.com/progrium/macdriver/generate/modules"
89
"github.com/progrium/macdriver/internal/set"
@@ -57,6 +58,9 @@ func (w *FileWriter) WriteCode() {
5758
}
5859

5960
func (w *FileWriter) goFilePath() string {
61+
if strings.Contains(w.Name, "Delegate") {
62+
w.Name = strings.Replace(w.Name, "Protocol", "", -1)
63+
}
6064
name := stringx.CamelToSnake(modules.TrimPrefix(w.Name))
6165
return w.PlatformDir + "/" + w.Module.Package + "/" + name + ".gen.go"
6266
}

generate/codegen/gen_class.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (c *Class) writeGoStruct(w *CodeWriter) {
223223
w.UnIndent()
224224
w.WriteLine("}")
225225
}
226-
if (im.InitMethod || im.ClassMethod) && im.Name != "new" && im.Name != "init" {
226+
if (im.InitMethod || im.ClassMethod) && im.Name != "new" && im.Name != "init" && im.Name != "alloc" {
227227
//add a convenient custom init method function
228228
w.WriteLine("")
229229
if m.DocURL != "" {

generate/codegen/gen_method.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type Method struct {
2626
Variadic bool
2727
Description string
2828
DocURL string
29+
Protocol bool
2930

3031
goFuncName string
3132
identifier string
@@ -72,6 +73,7 @@ func (m *Method) NormalizeInstanceTypeMethod(returnType *typing.ClassType) *Meth
7273
goFuncName: m.goFuncName,
7374
Suffix: m.Suffix,
7475
Variadic: m.Variadic,
76+
Protocol: m.Protocol,
7577
}
7678
return nm
7779
}
@@ -172,7 +174,7 @@ func (m *Method) WriteGoInterfaceCode(currentModule *modules.Module, classType *
172174
func (m *Method) GoFuncDeclare(currentModule *modules.Module, goTypeName string) string {
173175
var paramStrs []string
174176
for _, p := range m.Params {
175-
paramStrs = append(paramStrs, p.GoDeclare(currentModule, false))
177+
paramStrs = append(paramStrs, p.GoDeclare(currentModule, m.Protocol))
176178
}
177179
if m.Variadic {
178180
paramStrs = append(paramStrs, "args ...any")
@@ -216,7 +218,7 @@ func (m *Method) ProtocolGoFuncFieldType(currentModule *modules.Module) string {
216218
paramStrs = append(paramStrs, "args ...any")
217219
}
218220

219-
return "(" + strings.Join(paramStrs, ", ") + ")" + " " + m.ReturnType.GoName(currentModule, false)
221+
return "(" + strings.Join(paramStrs, ", ") + ")" + " " + m.ReturnType.GoName(currentModule, true)
220222
}
221223

222224
// ProtocolGoFuncName return go protocol func name
@@ -292,5 +294,6 @@ func (m *Method) ToProtocolParamAsObjectMethod() *Method {
292294
Description: m.Description,
293295
DocURL: m.DocURL,
294296
Variadic: m.Variadic,
297+
Protocol: m.Protocol,
295298
}
296299
}

generate/codegen/gen_protocol.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,10 @@ func (p *Protocol) writeDelegateStruct(w *CodeWriter) {
226226

227227
func (p *Protocol) writeProtocolWrapperStruct(w *CodeWriter) {
228228
typeName := p.Type.GoWrapperName()
229-
w.WriteLine(fmt.Sprintf("// A concrete type wrapper for the [%s] protocol.", p.Type.GoInterfaceName()))
229+
w.WriteLine("// ensure impl type implements protocol interface")
230+
w.WriteLine(fmt.Sprintf("var _ %s = (*%s)(nil)", p.Type.GoInterfaceName(), typeName))
231+
w.WriteLine("")
232+
w.WriteLine(fmt.Sprintf("// A concrete type for the [%s] protocol.", p.Type.GoInterfaceName()))
230233
w.WriteLine("type " + typeName + " struct {")
231234
w.Indent()
232235
if len(p.Supers) == 0 {
@@ -276,5 +279,8 @@ func (p *Protocol) allMethods() []*Method {
276279

277280
allMethods = append(allMethods, (*Method)(pp.getter()))
278281
}
282+
for idx := range allMethods {
283+
allMethods[idx].Protocol = true
284+
}
279285
return allMethods
280286
}

generate/codegen/modulewriter.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ func (m *ModuleWriter) WriteEnumAliases() {
9898
return
9999
}
100100
armVals := false
101+
imports := set.New[string]()
101102
for _, e := range enums {
103+
imports.AddSet(e.Type.GoImports())
102104
for _, v := range e.Values {
103105
if v.Arm64Value != "" {
104106
armVals = true
@@ -145,6 +147,16 @@ func (m *ModuleWriter) WriteEnumAliases() {
145147
amd64cw.WriteLine(AutoGeneratedMark)
146148
amd64cw.WriteLine("package " + m.Module.Package)
147149

150+
cw.WriteLine("import (")
151+
cw.Indent()
152+
imports.ForEach(func(value string) {
153+
if value != "github.com/progrium/macdriver/macos/objc" {
154+
cw.WriteLine("\"" + value + "\"")
155+
}
156+
})
157+
cw.UnIndent()
158+
cw.WriteLine(")")
159+
148160
for _, ei := range enums {
149161
primitiveType := ei.Type.GoName(&m.Module, false)
150162
if ei.Module.Name == m.Module.Name {
@@ -206,6 +218,9 @@ func (m *ModuleWriter) WriteEnumAliases() {
206218
log.Println("enum ", v.Name, " requires a value")
207219
continue
208220
}
221+
if v.GoName == ei.GoName(&m.Module, false) {
222+
v.GoName = "K" + v.GoName
223+
}
209224
if v.Arm64Value != "" {
210225
if !ei.IsString() {
211226
amd64cw.WriteLine(fmt.Sprintf("\t%s %s = %s", v.GoName, ei.GoName(&m.Module, false), v.Value))
@@ -245,8 +260,8 @@ func (m *ModuleWriter) WriteDocFile() {
245260
cw.WriteLine(AutoGeneratedMark)
246261
cw.WriteLineF("// %s", m.Description)
247262
cw.WriteLine("//")
248-
cw.WriteLine("// [Full Documentation]")
249-
cw.WriteLineF("//\n// [Full Documentation]: %s", m.DocURL)
263+
cw.WriteLine("// [Apple Documentation]")
264+
cw.WriteLineF("//\n// [Apple Documentation]: %s", m.DocURL)
250265
cw.WriteLineF("package %s", m.Module.Package)
251266
}
252267
}

generate/enums.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (db *Generator) ToEnumInfo(fw string, sym Symbol) *codegen.AliasInfo {
1616
}
1717
var enumValues []*codegen.EnumValue
1818
for _, ev := range db.EnumValues(fw) {
19-
st, _ := ev.Parse()
19+
st, _ := ev.Parse(db.Platform)
2020
if st.Variable.Type.Name != sym.Name && ev.Parent != sym.Name {
2121
continue
2222
}
@@ -129,7 +129,7 @@ func (db *Generator) AllowedEnumAlias(s Symbol) bool {
129129
"UInt32",
130130
"UInt64",
131131
}
132-
st, err := s.Parse()
132+
st, err := s.Parse(db.Platform)
133133
if err != nil {
134134
return false
135135
}

generate/generator.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (db *Generator) Generate(platform string, version int, rootDir string, fram
8888
}
8989
protocolGen.Init()
9090
fw := &codegen.FileWriter{
91-
Name: s.Name,
91+
Name: s.Name + "Protocol",
9292
Module: *protocolGen.Type.Module,
9393
PlatformDir: rootDir,
9494
}
@@ -114,7 +114,7 @@ func (db *Generator) Generate(platform string, version int, rootDir string, fram
114114
mw.EnumAliases = append(mw.EnumAliases, db.ToEnumInfo(framework, s))
115115
continue
116116
}
117-
st, err := s.Parse()
117+
st, err := s.Parse(db.Platform)
118118
if err != nil || st.TypeAlias == nil {
119119
log.Printf("skipping '%s', bad decl: %s", s.Name, s.Declaration)
120120
continue
@@ -147,7 +147,7 @@ func (db *Generator) ResolveTypeAlias(typeName string) (declparse.TypeInfo, bool
147147
if s == nil {
148148
return declparse.TypeInfo{}, false
149149
}
150-
st, err := s.Parse()
150+
st, err := s.Parse(db.Platform)
151151
if err != nil || st.TypeAlias == nil {
152152
return declparse.TypeInfo{}, false
153153
}

generate/members.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/progrium/macdriver/generate/codegen"
99
"github.com/progrium/macdriver/generate/declparse"
1010
"github.com/progrium/macdriver/generate/modules"
11+
"github.com/progrium/macdriver/generate/typing"
1112
)
1213

1314
func (db *Generator) shouldSkipType(ti declparse.TypeInfo) bool {
@@ -64,6 +65,7 @@ func (db *Generator) shouldSkipType(ti declparse.TypeInfo) bool {
6465
"CLBeaconIdentityConstraint",
6566
"mach_port_t",
6667
"cpu_type_t",
68+
"ptrdiff_t",
6769
} {
6870
if ti.Name == n {
6971
return true
@@ -93,7 +95,7 @@ func (db *Generator) Members(fw string, sym Symbol, covariantTypes []string) (pr
9395
s.Declaration = strings.ReplaceAll(s.Declaration, fmt.Sprintf("<%s>", ct), "<NSObject>")
9496
}
9597

96-
st, err := s.Parse()
98+
st, err := s.Parse(db.Platform)
9799
if err != nil {
98100
log.Println("Members:", sym.Name, "::", s.Declaration)
99101
panic(err)
@@ -181,6 +183,12 @@ func (db *Generator) Members(fw string, sym Symbol, covariantTypes []string) (pr
181183
if ptyp == nil {
182184
log.Fatalf("Method param type failure: owner=%s arg=%s type=%s methoddecl=%s", sym.Name, arg.Name, arg.Type.Name, s.Declaration)
183185
}
186+
if ptrtyp, ok := ptyp.(*typing.PointerType); ok {
187+
if ptrtyp.Type == nil {
188+
log.Println("using NSObject in place of missing type:", arg.Type.Name)
189+
ptyp = typing.Object
190+
}
191+
}
184192
param := &codegen.Param{
185193
Name: arg.Name,
186194
Type: ptyp,

generate/modules/modules.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ var All = []Module{
119119
{"UIKit", "UIKit", "uikit", "UIKit/UIKit.h", []string{"NS"}},
120120
{"UniformTypeIdentifiers", "Uniform Type Identifiers", "uti", "UniformTypeIdentifiers/UniformTypeIdentifiers.h", []string{"UT"}},
121121
{"WebKit", "WebKit", "webkit", "WebKit/WebKit.h", []string{"WK"}},
122-
{"MediaPlayer", "Media Player", "mediaplayer", "MediaPlayer/MediaPlayer.h", []string{"MP"}},
123122
{"FileProvider", "File Provider", "fileprovider", "FileProvider/FileProvider.h", []string{"NS"}},
124123
{"Quartz", "Quartz", "quartz", "Quartz/Quartz.h", []string{"IK", "kQC", "kQuartz", "QC", "IK_"}},
125124
{"SecurityInterface", "Security Interface", "securityinterface", "SecurityInterface/SecurityInterface.h", []string{"SF"}},
@@ -158,4 +157,5 @@ var All = []Module{
158157
{"MetalPerformanceShadersGraph", "Metal Performance Shaders Graph", "mpsgraph", "MetalPerformanceShadersGraph/MetalPerformanceShadersGraph.h", []string{"MPSGraph"}},
159158
{"MetalPerformanceShaders", "Metal Performance Shaders", "mps", "MetalPerformanceShaders/MetalPerformanceShaders.h", []string{"MPS"}},
160159
{"SystemConfiguration", "System Configuration", "sysconfig", "SystemConfiguration/SystemConfiguration.h", []string{"SC", "kSC"}},
160+
{"MediaPlayer", "Media Player", "mediaplayer", "MediaPlayer/MediaPlayer.h", []string{"MP"}},
161161
}

generate/protocol.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func (db *Generator) ToProtocolGen(fw string, sym Symbol) *codegen.Protocol {
1313
if db.genCache == nil {
1414
db.genCache = make(map[string]codegen.CodeGen)
1515
}
16-
key := fmt.Sprintf("%s.%s", fw, sym.Name)
16+
key := fmt.Sprintf("%s.P%s", fw, sym.Name)
1717
cg, ok := db.genCache[key]
1818
if ok {
1919
if pcg, ok := cg.Copy().(*codegen.Protocol); ok {
@@ -34,8 +34,8 @@ func (db *Generator) ToProtocolGen(fw string, sym Symbol) *codegen.Protocol {
3434
Type: type_,
3535
}
3636

37-
// symbolsdb doesnt have protocol superclasses yet,
38-
// so these are known ones for appkit for now
37+
// symbolsdb should have protocol superclasses now via inheritsFrom,
38+
// but these are known ones for appkit for now
3939
knownSupers := map[string]string{
4040
"NSComboBoxDelegate": "NSTextFieldDelegate",
4141
"NSMatrixDelegate": "NSControlTextEditingDelegate",

generate/symbols.go

+25-11
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,27 @@ var blacklist = []string{
3737
"WebView", // gets picked up instead of WKWebView
3838
}
3939

40+
var pathBlacklist = []string{
41+
"foundation/nshashtable/legacy_hash_table_implementation/nshashtable", // found instead of NSHashTable class
42+
"foundation/nsmaptable/legacy_map_table_implementation/nsmaptable", // same
43+
}
44+
4045
type Symbol struct {
4146
Name string
4247
Path string
4348
Kind string
4449

45-
Description string
46-
Type string
47-
Parent string
48-
Modules []string
49-
Platforms []Platform
50-
Declaration string
51-
Parameters []Parameter
52-
Return string
53-
Deprecated bool
50+
Description string
51+
Type string
52+
Parent string
53+
Modules []string
54+
Platforms []Platform
55+
Declaration string
56+
Declarations map[string]string
57+
Parameters []Parameter
58+
Return string
59+
Deprecated bool
60+
InheritedFrom string
5461
}
5562

5663
type Platform struct {
@@ -125,8 +132,12 @@ func (s Symbol) HasPlatform(name string, version int, deprecated bool) bool {
125132
return false
126133
}
127134

128-
func (s Symbol) Parse() (*declparse.Statement, error) {
129-
p := declparse.NewStringParser(s.Declaration)
135+
func (s Symbol) Parse(platform string) (*declparse.Statement, error) {
136+
decl := s.Declaration
137+
if decl == "" && len(s.Declarations) > 0 {
138+
decl = s.Declarations[platform]
139+
}
140+
p := declparse.NewStringParser(decl)
130141
switch s.Kind {
131142
case "Constant", "Property":
132143
p.Hint = declparse.HintVariable
@@ -177,6 +188,9 @@ func (db *SymbolCache) loadFrom(file *zip.File) (v Symbol, err error) {
177188
if strIn(blacklist, v.Name) {
178189
return v, fmt.Errorf("blacklisted symbol: %s", v.Name)
179190
}
191+
if strIn(pathBlacklist, v.Path) {
192+
return v, fmt.Errorf("blacklisted path: %s", v.Path)
193+
}
180194
if v.Kind != "Property" && v.Kind != "Method" && v.Kind != "Framework" {
181195
db.cache[v.Name] = v
182196
}

0 commit comments

Comments
 (0)