-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
macdriver: refactor generation in preparation for supporting more typ…
…es (#117)
- Loading branch information
Showing
8 changed files
with
269 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package gen | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/progrium/macschema/schema" | ||
) | ||
|
||
func Convert(desc PackageDescription, imports []PackageContents, schemas ...*schema.Schema) (*GoPackage, error) { | ||
pkg := &GoPackage{ | ||
PackageDescription: desc, | ||
} | ||
consumedImports := map[Import]bool{ | ||
{Path: "unsafe"}: true, | ||
{Path: "github.com/progrium/macdriver/objc"}: true, | ||
} | ||
for _, s := range schemas { | ||
if s.Class != nil { | ||
classDef, err := processClassSchema(pkg, s, imports, consumedImports) | ||
if err != nil { | ||
return pkg, fmt.Errorf("issue with class %s failed to parse declaration %+v: %w", s.Class.Name, s.Class, err) | ||
} | ||
pkg.Classes = append(pkg.Classes, classDef) | ||
} else if s.Struct != nil { | ||
return pkg, fmt.Errorf("%v: structs are not yet supported", s.Struct.Name) | ||
} else { | ||
return pkg, fmt.Errorf("invalid schema kind %v", s.Kind) | ||
} | ||
} | ||
for imp := range consumedImports { | ||
pkg.Imports = append(pkg.Imports, imp) | ||
} | ||
return pkg, nil | ||
} | ||
|
||
// processClassSchema converts a schema.Class into a ClassDef | ||
func processClassSchema(pkg *GoPackage, s *schema.Schema, imports []PackageContents, consumedImports map[Import]bool) (ClassDef, error) { | ||
cb := classBuilder{ | ||
Class: *s.Class, | ||
Imports: imports, | ||
consumedImports: consumedImports, | ||
} | ||
classDef := ClassDef{ | ||
Name: cb.Class.Name, | ||
Base: "objc.Object", | ||
} | ||
decl, err := parseClassDeclaration(cb.Class.Declaration) | ||
if err != nil { | ||
return classDef, err | ||
} | ||
|
||
if decl.Base != "NSObject" { | ||
if cls := cb.mapClass(decl.Base); cls != nil { | ||
classDef.Base = cls.GoType | ||
} | ||
} | ||
|
||
cb.EachTypeMethod(func(m schema.Method) { | ||
defer ignoreIfUnimplemented(fmt.Sprintf("%s.%s", s.Class.Name, m.Name)) | ||
|
||
msg := cb.msgSend(m, true) | ||
wrapper := MethodDef{ | ||
Name: fmt.Sprintf("%s_%s", cb.Class.Name, selectorNameToGoIdent(m.Name)), | ||
WrappedFunc: cb.cgoWrapperFunc(m, true), | ||
} | ||
|
||
pkg.ClassMsgSendWrappers = append(pkg.ClassMsgSendWrappers, msg) | ||
pkg.CGoWrapperFuncs = append(pkg.CGoWrapperFuncs, wrapper) | ||
}) | ||
cb.EachInstanceMethod(func(m schema.Method) { | ||
defer ignoreIfUnimplemented(fmt.Sprintf("%s.%s", s.Class.Name, m.Name)) | ||
|
||
method := cb.instanceMethod(m) | ||
msg := cb.msgSend(m, false) | ||
|
||
classDef.InstanceMethods = append(classDef.InstanceMethods, method) | ||
pkg.MsgSendWrappers = append(pkg.MsgSendWrappers, msg) | ||
}) | ||
|
||
return classDef, nil | ||
} |
Oops, something went wrong.