Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New symbols, plus fixes #213

Merged
merged 5 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ example:
.PHONY: example

generate/symbols.zip:
cd generate && wget https://github.com/mactypes/symbolsdb/releases/download/1.0/symbols.zip
cd generate && wget https://github.com/mactypes/symbolsdb/releases/download/1.1/symbols.zip
2 changes: 1 addition & 1 deletion generate/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (db *Generator) ToClassGen(sym Symbol) *codegen.Class {
DocURL: sym.DocURL(),
}

st, err := sym.Parse()
st, err := sym.Parse(db.Platform)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion generate/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package codegen

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

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

// CodeGen is interface for Class/Protocol code Gen
type CodeGen interface {
Expand Down
4 changes: 4 additions & 0 deletions generate/codegen/filewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package codegen
import (
"os"
"path/filepath"
"strings"

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

func (w *FileWriter) goFilePath() string {
if strings.Contains(w.Name, "Delegate") {
w.Name = strings.Replace(w.Name, "Protocol", "", -1)
}
name := stringx.CamelToSnake(modules.TrimPrefix(w.Name))
return w.PlatformDir + "/" + w.Module.Package + "/" + name + ".gen.go"
}
Expand Down
2 changes: 1 addition & 1 deletion generate/codegen/gen_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (c *Class) writeGoStruct(w *CodeWriter) {
w.UnIndent()
w.WriteLine("}")
}
if (im.InitMethod || im.ClassMethod) && im.Name != "new" && im.Name != "init" {
if (im.InitMethod || im.ClassMethod) && im.Name != "new" && im.Name != "init" && im.Name != "alloc" {
//add a convenient custom init method function
w.WriteLine("")
if m.DocURL != "" {
Expand Down
7 changes: 5 additions & 2 deletions generate/codegen/gen_method.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Method struct {
Variadic bool
Description string
DocURL string
Protocol bool

goFuncName string
identifier string
Expand Down Expand Up @@ -72,6 +73,7 @@ func (m *Method) NormalizeInstanceTypeMethod(returnType *typing.ClassType) *Meth
goFuncName: m.goFuncName,
Suffix: m.Suffix,
Variadic: m.Variadic,
Protocol: m.Protocol,
}
return nm
}
Expand Down Expand Up @@ -172,7 +174,7 @@ func (m *Method) WriteGoInterfaceCode(currentModule *modules.Module, classType *
func (m *Method) GoFuncDeclare(currentModule *modules.Module, goTypeName string) string {
var paramStrs []string
for _, p := range m.Params {
paramStrs = append(paramStrs, p.GoDeclare(currentModule, false))
paramStrs = append(paramStrs, p.GoDeclare(currentModule, m.Protocol))
}
if m.Variadic {
paramStrs = append(paramStrs, "args ...any")
Expand Down Expand Up @@ -216,7 +218,7 @@ func (m *Method) ProtocolGoFuncFieldType(currentModule *modules.Module) string {
paramStrs = append(paramStrs, "args ...any")
}

return "(" + strings.Join(paramStrs, ", ") + ")" + " " + m.ReturnType.GoName(currentModule, false)
return "(" + strings.Join(paramStrs, ", ") + ")" + " " + m.ReturnType.GoName(currentModule, true)
}

// ProtocolGoFuncName return go protocol func name
Expand Down Expand Up @@ -292,5 +294,6 @@ func (m *Method) ToProtocolParamAsObjectMethod() *Method {
Description: m.Description,
DocURL: m.DocURL,
Variadic: m.Variadic,
Protocol: m.Protocol,
}
}
8 changes: 7 additions & 1 deletion generate/codegen/gen_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ func (p *Protocol) writeDelegateStruct(w *CodeWriter) {

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

allMethods = append(allMethods, (*Method)(pp.getter()))
}
for idx := range allMethods {
allMethods[idx].Protocol = true
}
return allMethods
}
19 changes: 17 additions & 2 deletions generate/codegen/modulewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ func (m *ModuleWriter) WriteEnumAliases() {
return
}
armVals := false
imports := set.New[string]()
for _, e := range enums {
imports.AddSet(e.Type.GoImports())
for _, v := range e.Values {
if v.Arm64Value != "" {
armVals = true
Expand Down Expand Up @@ -145,6 +147,16 @@ func (m *ModuleWriter) WriteEnumAliases() {
amd64cw.WriteLine(AutoGeneratedMark)
amd64cw.WriteLine("package " + m.Module.Package)

cw.WriteLine("import (")
cw.Indent()
imports.ForEach(func(value string) {
if value != "github.com/progrium/macdriver/macos/objc" {
cw.WriteLine("\"" + value + "\"")
}
})
cw.UnIndent()
cw.WriteLine(")")

for _, ei := range enums {
primitiveType := ei.Type.GoName(&m.Module, false)
if ei.Module.Name == m.Module.Name {
Expand Down Expand Up @@ -206,6 +218,9 @@ func (m *ModuleWriter) WriteEnumAliases() {
log.Println("enum ", v.Name, " requires a value")
continue
}
if v.GoName == ei.GoName(&m.Module, false) {
v.GoName = "K" + v.GoName
}
if v.Arm64Value != "" {
if !ei.IsString() {
amd64cw.WriteLine(fmt.Sprintf("\t%s %s = %s", v.GoName, ei.GoName(&m.Module, false), v.Value))
Expand Down Expand Up @@ -245,8 +260,8 @@ func (m *ModuleWriter) WriteDocFile() {
cw.WriteLine(AutoGeneratedMark)
cw.WriteLineF("// %s", m.Description)
cw.WriteLine("//")
cw.WriteLine("// [Full Documentation]")
cw.WriteLineF("//\n// [Full Documentation]: %s", m.DocURL)
cw.WriteLine("// [Apple Documentation]")
cw.WriteLineF("//\n// [Apple Documentation]: %s", m.DocURL)
cw.WriteLineF("package %s", m.Module.Package)
}
}
Expand Down
4 changes: 2 additions & 2 deletions generate/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (db *Generator) ToEnumInfo(fw string, sym Symbol) *codegen.AliasInfo {
}
var enumValues []*codegen.EnumValue
for _, ev := range db.EnumValues(fw) {
st, _ := ev.Parse()
st, _ := ev.Parse(db.Platform)
if st.Variable.Type.Name != sym.Name && ev.Parent != sym.Name {
continue
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func (db *Generator) AllowedEnumAlias(s Symbol) bool {
"UInt32",
"UInt64",
}
st, err := s.Parse()
st, err := s.Parse(db.Platform)
if err != nil {
return false
}
Expand Down
6 changes: 3 additions & 3 deletions generate/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (db *Generator) Generate(platform string, version int, rootDir string, fram
}
protocolGen.Init()
fw := &codegen.FileWriter{
Name: s.Name,
Name: s.Name + "Protocol",
Module: *protocolGen.Type.Module,
PlatformDir: rootDir,
}
Expand All @@ -114,7 +114,7 @@ func (db *Generator) Generate(platform string, version int, rootDir string, fram
mw.EnumAliases = append(mw.EnumAliases, db.ToEnumInfo(framework, s))
continue
}
st, err := s.Parse()
st, err := s.Parse(db.Platform)
if err != nil || st.TypeAlias == nil {
log.Printf("skipping '%s', bad decl: %s", s.Name, s.Declaration)
continue
Expand Down Expand Up @@ -147,7 +147,7 @@ func (db *Generator) ResolveTypeAlias(typeName string) (declparse.TypeInfo, bool
if s == nil {
return declparse.TypeInfo{}, false
}
st, err := s.Parse()
st, err := s.Parse(db.Platform)
if err != nil || st.TypeAlias == nil {
return declparse.TypeInfo{}, false
}
Expand Down
10 changes: 9 additions & 1 deletion generate/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/progrium/macdriver/generate/codegen"
"github.com/progrium/macdriver/generate/declparse"
"github.com/progrium/macdriver/generate/modules"
"github.com/progrium/macdriver/generate/typing"
)

func (db *Generator) shouldSkipType(ti declparse.TypeInfo) bool {
Expand Down Expand Up @@ -64,6 +65,7 @@ func (db *Generator) shouldSkipType(ti declparse.TypeInfo) bool {
"CLBeaconIdentityConstraint",
"mach_port_t",
"cpu_type_t",
"ptrdiff_t",
} {
if ti.Name == n {
return true
Expand Down Expand Up @@ -93,7 +95,7 @@ func (db *Generator) Members(fw string, sym Symbol, covariantTypes []string) (pr
s.Declaration = strings.ReplaceAll(s.Declaration, fmt.Sprintf("<%s>", ct), "<NSObject>")
}

st, err := s.Parse()
st, err := s.Parse(db.Platform)
if err != nil {
log.Println("Members:", sym.Name, "::", s.Declaration)
panic(err)
Expand Down Expand Up @@ -181,6 +183,12 @@ func (db *Generator) Members(fw string, sym Symbol, covariantTypes []string) (pr
if ptyp == nil {
log.Fatalf("Method param type failure: owner=%s arg=%s type=%s methoddecl=%s", sym.Name, arg.Name, arg.Type.Name, s.Declaration)
}
if ptrtyp, ok := ptyp.(*typing.PointerType); ok {
if ptrtyp.Type == nil {
log.Println("using NSObject in place of missing type:", arg.Type.Name)
ptyp = typing.Object
}
}
param := &codegen.Param{
Name: arg.Name,
Type: ptyp,
Expand Down
2 changes: 1 addition & 1 deletion generate/modules/modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ var All = []Module{
{"UIKit", "UIKit", "uikit", "UIKit/UIKit.h", []string{"NS"}},
{"UniformTypeIdentifiers", "Uniform Type Identifiers", "uti", "UniformTypeIdentifiers/UniformTypeIdentifiers.h", []string{"UT"}},
{"WebKit", "WebKit", "webkit", "WebKit/WebKit.h", []string{"WK"}},
{"MediaPlayer", "Media Player", "mediaplayer", "MediaPlayer/MediaPlayer.h", []string{"MP"}},
{"FileProvider", "File Provider", "fileprovider", "FileProvider/FileProvider.h", []string{"NS"}},
{"Quartz", "Quartz", "quartz", "Quartz/Quartz.h", []string{"IK", "kQC", "kQuartz", "QC", "IK_"}},
{"SecurityInterface", "Security Interface", "securityinterface", "SecurityInterface/SecurityInterface.h", []string{"SF"}},
Expand Down Expand Up @@ -158,4 +157,5 @@ var All = []Module{
{"MetalPerformanceShadersGraph", "Metal Performance Shaders Graph", "mpsgraph", "MetalPerformanceShadersGraph/MetalPerformanceShadersGraph.h", []string{"MPSGraph"}},
{"MetalPerformanceShaders", "Metal Performance Shaders", "mps", "MetalPerformanceShaders/MetalPerformanceShaders.h", []string{"MPS"}},
{"SystemConfiguration", "System Configuration", "sysconfig", "SystemConfiguration/SystemConfiguration.h", []string{"SC", "kSC"}},
{"MediaPlayer", "Media Player", "mediaplayer", "MediaPlayer/MediaPlayer.h", []string{"MP"}},
}
6 changes: 3 additions & 3 deletions generate/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (db *Generator) ToProtocolGen(fw string, sym Symbol) *codegen.Protocol {
if db.genCache == nil {
db.genCache = make(map[string]codegen.CodeGen)
}
key := fmt.Sprintf("%s.%s", fw, sym.Name)
key := fmt.Sprintf("%s.P%s", fw, sym.Name)
cg, ok := db.genCache[key]
if ok {
if pcg, ok := cg.Copy().(*codegen.Protocol); ok {
Expand All @@ -34,8 +34,8 @@ func (db *Generator) ToProtocolGen(fw string, sym Symbol) *codegen.Protocol {
Type: type_,
}

// symbolsdb doesnt have protocol superclasses yet,
// so these are known ones for appkit for now
// symbolsdb should have protocol superclasses now via inheritsFrom,
// but these are known ones for appkit for now
knownSupers := map[string]string{
"NSComboBoxDelegate": "NSTextFieldDelegate",
"NSMatrixDelegate": "NSControlTextEditingDelegate",
Expand Down
36 changes: 25 additions & 11 deletions generate/symbols.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,27 @@ var blacklist = []string{
"WebView", // gets picked up instead of WKWebView
}

var pathBlacklist = []string{
"foundation/nshashtable/legacy_hash_table_implementation/nshashtable", // found instead of NSHashTable class
"foundation/nsmaptable/legacy_map_table_implementation/nsmaptable", // same
}

type Symbol struct {
Name string
Path string
Kind string

Description string
Type string
Parent string
Modules []string
Platforms []Platform
Declaration string
Parameters []Parameter
Return string
Deprecated bool
Description string
Type string
Parent string
Modules []string
Platforms []Platform
Declaration string
Declarations map[string]string
Parameters []Parameter
Return string
Deprecated bool
InheritedFrom string
}

type Platform struct {
Expand Down Expand Up @@ -125,8 +132,12 @@ func (s Symbol) HasPlatform(name string, version int, deprecated bool) bool {
return false
}

func (s Symbol) Parse() (*declparse.Statement, error) {
p := declparse.NewStringParser(s.Declaration)
func (s Symbol) Parse(platform string) (*declparse.Statement, error) {
decl := s.Declaration
if decl == "" && len(s.Declarations) > 0 {
decl = s.Declarations[platform]
}
p := declparse.NewStringParser(decl)
switch s.Kind {
case "Constant", "Property":
p.Hint = declparse.HintVariable
Expand Down Expand Up @@ -177,6 +188,9 @@ func (db *SymbolCache) loadFrom(file *zip.File) (v Symbol, err error) {
if strIn(blacklist, v.Name) {
return v, fmt.Errorf("blacklisted symbol: %s", v.Name)
}
if strIn(pathBlacklist, v.Path) {
return v, fmt.Errorf("blacklisted path: %s", v.Path)
}
if v.Kind != "Property" && v.Kind != "Method" && v.Kind != "Framework" {
db.cache[v.Name] = v
}
Expand Down
Loading
Loading