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

fix: use of @optional directive in protocols and interfaces #47

Merged
merged 3 commits into from
Apr 15, 2024
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions types/objc/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (c *Category) dump(verbose, addrs bool) string {
} else {
cat = fmt.Sprintf("@interface %s(%s)%s", className, c.Name, protos)
}
cat += "\n"

if len(c.ClassMethods) > 0 {
s := bytes.NewBufferString("/* class methods */\n")
Expand All @@ -80,14 +81,12 @@ func (c *Category) dump(verbose, addrs bool) string {
}
}
cMethods = s.String()
if cMethods != "" {
cMethods += "\n"
}
}
if len(c.InstanceMethods) > 0 {
var s *bytes.Buffer
if len(c.ClassMethods) > 0 {
s = bytes.NewBufferString("\n/* instance methods */\n")
} else {
s = bytes.NewBufferString("/* instance methods */\n")
}
s := bytes.NewBufferString("/* instance methods */\n")
for _, meth := range c.InstanceMethods {
if !addrs && strings.HasPrefix(meth.Name, ".cxx_") {
continue
Expand All @@ -103,13 +102,20 @@ func (c *Category) dump(verbose, addrs bool) string {
}
}
iMethods = s.String()
if iMethods != "" {
iMethods += "\n"
}
}

return fmt.Sprintf(
"%s\n%s%s@end\n",
"%s\n"+
"%s"+
"%s"+
"@end\n",
cat,
cMethods,
iMethods)
iMethods,
)
}

func (c *Category) String() string {
Expand Down
54 changes: 27 additions & 27 deletions types/objc/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (c *Class) dump(verbose, addrs bool) string {
for _, prot := range c.Protocols {
subProts = append(subProts, prot.Name)
}
class += fmt.Sprintf("<%s>", strings.Join(subProts, ", "))
class += fmt.Sprintf(" <%s>", strings.Join(subProts, ", "))
}
if len(c.Ivars) > 0 {
class += fmt.Sprintf(" {")
Expand All @@ -171,44 +171,37 @@ func (c *Class) dump(verbose, addrs bool) string {
s := bytes.NewBufferString("")
w := tabwriter.NewWriter(s, 0, 0, 1, ' ', 0)
if addrs {
fmt.Fprintf(w, "\n /* instance variables */\t// +size offset\n")
fmt.Fprintf(w, "\n /* instance variables */\t// +size offset\n")
} else {
fmt.Fprintf(w, "\n /* instance variables */\n")
fmt.Fprintf(w, "\n /* instance variables */\n")
}
for _, ivar := range c.Ivars {
if verbose {
if addrs {
fmt.Fprintf(w, " %s\n", ivar.WithAddrs())
fmt.Fprintf(w, " %s\n", ivar.WithAddrs())
} else {
fmt.Fprintf(w, " %s\n", ivar.Verbose())
fmt.Fprintf(w, " %s\n", ivar.Verbose())
}
} else {
fmt.Fprintf(w, " %s\n", &ivar)
fmt.Fprintf(w, " %s\n", &ivar)
}
}
w.Flush()
s.WriteString("}\n\n")
s.WriteString("}")
iVars = s.String()
} else {
iVars = fmt.Sprintf("\n")
}
if len(c.Props) > 0 {
if len(c.Ivars) == 0 {
props += "\n"
}
for _, prop := range c.Props {
if verbose {
attrs, optional := prop.Attributes()
var optionalStr string
if optional {
optionalStr = "@optional\n"
}
props += fmt.Sprintf("%s@property %s%s%s;\n", optionalStr, attrs, prop.Type(), prop.Name)
attrs, _ := prop.Attributes()
props += fmt.Sprintf("@property %s%s%s;\n", attrs, prop.Type(), prop.Name)
} else {
props += fmt.Sprintf("@property (%s) %s;\n", prop.EncodedAttributes, prop.Name)
}
}
props += "\n"
if props != "" {
props += "\n"
}
}
if len(c.ClassMethods) > 0 {
s := bytes.NewBufferString("/* class methods */\n")
Expand All @@ -229,14 +222,12 @@ func (c *Class) dump(verbose, addrs bool) string {
}
w.Flush()
cMethods = s.String()
if cMethods != "" {
cMethods += "\n"
}
}
if len(c.InstanceMethods) > 0 {
var s *bytes.Buffer
if len(c.ClassMethods) > 0 {
s = bytes.NewBufferString("\n/* instance methods */\n")
} else {
s = bytes.NewBufferString("/* instance methods */\n")
}
s := bytes.NewBufferString("/* instance methods */\n")
for _, meth := range c.InstanceMethods {
if !addrs && strings.HasPrefix(meth.Name, ".cxx_") {
continue
Expand All @@ -252,15 +243,24 @@ func (c *Class) dump(verbose, addrs bool) string {
}
}
iMethods = s.String()
if iMethods != "" {
iMethods += "\n"
}
}

return fmt.Sprintf(
"%s%s%s%s%s@end\n",
"%s"+
"%s\n\n"+
"%s"+
"%s"+
"%s"+
"@end\n",
class,
iVars,
props,
cMethods,
iMethods)
iMethods,
)
}

// IsSwift returns true if the class is a Swift class.
Expand Down
55 changes: 40 additions & 15 deletions types/objc/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,40 +53,37 @@ type Protocol struct {

func (p *Protocol) dump(verbose, addrs bool) string {
var props string
var optProps string
var cMethods string
var iMethods string
var optMethods string

protocol := fmt.Sprintf("@protocol %s ", p.Name)

protocol := fmt.Sprintf("@protocol %s", p.Name)
if len(p.Prots) > 0 {
var subProts []string
for _, prot := range p.Prots {
subProts = append(subProts, prot.Name)
}
protocol += fmt.Sprintf("<%s>", strings.Join(subProts, ", "))
protocol += fmt.Sprintf(" <%s>", strings.Join(subProts, ", "))
}
if addrs {
protocol += fmt.Sprintf(" // %#x", p.Ptr)
}
if len(p.InstanceProperties) > 0 {
props += "\n"
for _, prop := range p.InstanceProperties {
if verbose {
attrs, optional := prop.Attributes()
var optionalStr string
if optional {
optionalStr = "@optional\n"
if attrs, optional := prop.Attributes(); !optional {
props += fmt.Sprintf("@property %s%s%s;\n", attrs, prop.Type(), prop.Name)
}
props += fmt.Sprintf("%s@property %s%s%s;\n", optionalStr, attrs, prop.Type(), prop.Name)
} else {
props += fmt.Sprintf("@property (%s) %s;\n", prop.EncodedAttributes, prop.Name)
}
}
props += "\n"
if props != "" {
props += "\n"
}
}
if len(p.ClassMethods) > 0 {
cMethods = "/* class methods */\n"
for _, meth := range p.ClassMethods {
if verbose {
rtype, args := decodeMethodTypes(meth.Types)
Expand All @@ -95,9 +92,11 @@ func (p *Protocol) dump(verbose, addrs bool) string {
cMethods += fmt.Sprintf("+[%s %s];\n", p.Name, meth.Name)
}
}
if cMethods != "" {
cMethods = "/* class methods */\n" + cMethods + "\n"
}
}
if len(p.InstanceMethods) > 0 {
iMethods = "/* instance methods */\n"
for _, meth := range p.InstanceMethods {
if verbose {
rtype, args := decodeMethodTypes(meth.Types)
Expand All @@ -106,9 +105,25 @@ func (p *Protocol) dump(verbose, addrs bool) string {
iMethods += fmt.Sprintf("-[%s %s];\n", p.Name, meth.Name)
}
}
if iMethods != "" {
iMethods = "/* required instance methods */\n" + iMethods + "\n"
}
}
if len(p.InstanceProperties) > 0 {
for _, prop := range p.InstanceProperties {
if verbose {
if attrs, optional := prop.Attributes(); optional {
optProps += fmt.Sprintf("@property %s%s%s;\n", attrs, prop.Type(), prop.Name)
}
} else {
// optProps += fmt.Sprintf("@property (%s) %s;\n", prop.EncodedAttributes, prop.Name)
}
}
if optProps != "" {
optProps += "\n"
}
}
if len(p.OptionalInstanceMethods) > 0 {
optMethods = "@optional\n/* instance methods */\n"
for _, meth := range p.OptionalInstanceMethods {
if verbose {
rtype, args := decodeMethodTypes(meth.Types)
Expand All @@ -117,15 +132,25 @@ func (p *Protocol) dump(verbose, addrs bool) string {
optMethods += fmt.Sprintf("-[%s %s];\n", p.Name, meth.Name)
}
}
if optMethods != "" {
optMethods = "/* optional instance methods */\n" + optMethods + "\n"
}
}
return fmt.Sprintf(
"%s\n"+
"%s%s%s%s"+
"%s\n\n"+
"@required\n\n"+
"%s"+
"%s"+
"%s"+
"@optional\n\n"+
"%s"+
"%s"+
"@end\n",
protocol,
props,
cMethods,
iMethods,
optProps,
optMethods,
)
}
Expand Down