naming

package
v0.11.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package naming converts ObjC identifiers to idiomatic Go names.

ObjC uses a keyword-argument selector syntax ("objectAtIndex:") that must be flattened into a single Go method name ("ObjectAtIndex"). This package provides the mapping functions used consistently across all emitters:

  • MethodName converts an ObjC selector string to an exported Go method name, handling common suffixes ("UsingBlock:" → drop "Block") and acronym casing.
  • ParamName lowercases the first letter of a parameter name and escapes any identifier that collides with a Go keyword or common builtin.
  • PackageName lowercases the framework name to produce the Go package name ("Foundation" → "foundation").
  • BridgeFuncName produces the C symbol for a bridge function ("foundation_NSArray_objectAtIndex_").
  • ProtocolGoTypeName disambiguates protocol names from same-named classes (NSObject class vs NSObjectProtocol interface).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BlockTypeName

func BlockTypeName(className, selector string) string

BlockTypeName produces a named Go type for an ObjC block used in a framework. Example: ("Foundation", "NSArray", "enumerateObjectsUsingBlock:") → "NSArrayEnumerationBlock"

func BridgeFuncName

func BridgeFuncName(framework, className, selector string, isClassMethod bool) string

BridgeFuncName produces the C bridge function name for a given class+selector. Class methods get a "_cls" suffix to avoid conflicts with identically-named instance methods (e.g. +[NSDate timeIntervalSinceReferenceDate] vs -[NSDate timeIntervalSinceReferenceDate]).

("Foundation", "NSArray", "objectAtIndex:", false)              → "foundation_NSArray_objectAtIndex"
("Foundation", "NSBundle", "URLForAuxiliaryExecutable:", false) → "foundation_NSBundle_urlForAuxiliaryExecutable"
("Foundation", "NSDate", "timeIntervalSinceReferenceDate", true) → "foundation_NSDate_timeIntervalSinceReferenceDate_cls"

func FindSiblingMethod

func FindSiblingMethod(methods []macosplatformmetadata.Method, selector string, isClassMethod bool) (macosplatformmetadata.Method, bool)

FindSiblingMethod returns the first method in methods that matches both selector and isClassMethod. Used to locate the zero-arg counterpart when classifying a one-arg method under disambiguation rules 1 and 2.

func FunctionBridgeID

func FunctionBridgeID(framework, funcName string) string

FunctionBridgeID returns the structured-path ID comment for a free C function binding.

("CoreFoundation", "CFArrayCreateMutable") → "ID: objc-sym CoreFoundation.CFArrayCreateMutable"

func GoTypeName

func GoTypeName(name string) string

GoTypeName ensures a type name is exported (first letter uppercase).

func IsIBActionSender

func IsIBActionSender(objcType string) bool

IsIBActionSender reports whether an ObjC type string is the bare `id` sender type used exclusively in Interface Builder action wiring.

func MethodBridgeID

func MethodBridgeID(framework, class, selector string, isClassMethod bool) string

MethodBridgeID returns the structured-path ID comment for an ObjC method binding.

("Foundation", "NSArray", "objectAtIndex:", false) → "ID: objc-sym Foundation.NSArray.-[objectAtIndex:]"
("Foundation", "NSDate", "date", true)             → "ID: objc-sym Foundation.NSDate.+[date]"

func MethodName

func MethodName(selector string) string

MethodName converts an ObjC selector to an exported Go method name.

"objectAtIndex:"               → "ObjectAtIndex"
"writeToURL:error:"            → "WriteToURL"  (NSError** arg elided by scanner)
"initWithContentsOfURL:encoding:error:" → "InitWithContentsOfURL"
"enumerateObjectsUsingBlock:"  → "EnumerateObjectsUsing"
"isKindOfClass:"               → "IsKindOfClass"
"count"                        → "Count"

func PackageName

func PackageName(framework string) string

PackageName converts a framework name to a Go package name (lowercase).

"Foundation" → "foundation"
"CoreML"     → "coreml"

func ParamName

func ParamName(objcName string) string

ParamName sanitises an ObjC parameter name for use as a Go argument name. It lowercases the first letter and escapes Go reserved words.

func ProtocolGoTypeName

func ProtocolGoTypeName(protoName string, classNameOwner map[string]string) string

ProtocolGoTypeName returns the Go interface type name for an ObjC protocol, disambiguating from a class of the same name by appending "Protocol".

Apple's ObjC root NSObject exists both as a class (objc/NSObject.h) and as a protocol (the implicit parent of all ObjC protocols). Mac SDK headers use the same identifier `NSObject` for both. The generated Go binding produces a `type NSObject struct` for the class, so the protocol must use a distinct type name (`NSObjectProtocol`) to be embeddable as an interface — matching the convention Apple's own Swift bindings adopted for the same reason.

classNameOwner is the registry of class names → owning framework (the same map the mapper uses for cross-framework class references). When the protocol's bare Go name appears there, we apply the suffix; otherwise we return the bare name unchanged.

func ResolveGoMethodName

func ResolveGoMethodName(gn string, method macosplatformmetadata.Method, classMethods []macosplatformmetadata.Method, goNameCount map[string]int) (resolved string, skip bool)

ResolveGoMethodName resolves the final Go name for a method when its base name (from MethodName) collides with another method on the same class (goNameCount[gn] > 1).

Three rules in priority order:

  1. Zero-arg priority: the zero-arg form (-[Foo bar]) owns the clean base name. The one-arg overload (-[Foo bar:]) is resolved by rules 2 or 3. Rationale: in Cocoa the zero-arg form is always the direct programmatic call; any n-arg overload is a specialisation.

  2. IBAction omission: a one-arg method whose sole argument is the bare `id` type AND whose zero-arg sibling exists is a Cocoa IBAction overload used only by Interface Builder — never from code. Returns skip=true so the caller omits the method entirely. Example: -[WKWebView goBack:(id)sender] vs -[WKWebView goBack]

  3. Last resort — genuine selector collision: two ObjC selectors produce the same Go name (e.g. transformContextForBox: and transformContext:forBox:). The colon count is appended for a deterministic, stable suffix.

Class methods are excluded from rules 1–2 because they occupy a separate Go namespace (ClassName-prefixed package-level functions). The rare collision between two class methods falls directly to rule 3.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL