Proposal: Virtual Fields
Summary
This proposal introduces virtual fields to XGo. A virtual field looks and behaves like an ordinary struct field at the use site (x.name, x.name = v), but it is backed by methods that follow a naming convention instead of by real storage. The compiler rewrites each access into a call to those methods.
The feature exists so that members of C/C++ types that have no Go equivalent — union members and bit-fields in particular — can still be accessed with plain field syntax from XGo code that imports C/C++ packages through LLGo.
Motivation
LLGo lets XGo programs import C/C++ packages. Most C/C++ data types map cleanly to Go structs, but a few do not:
- Union members. Several members share the same memory. Go has no union type, so the generated binding cannot declare them as fields.
- Bit-fields. Several members share a single storage unit at sub-byte granularity. Go struct fields cannot be narrower than a byte.
Without language support, a binding author has to expose such members as plain methods, and users end up writing v.GetMode() / v.SetMode(3) where the C code says v.mode / v.mode = 3. Virtual fields let the binding keep the C spelling.
The Idea
A type declares a virtual field by providing methods whose names start with XGof_. There are two interchangeable forms, and a field uses one or the other.
Reference form — one method that returns a pointer to the value:
func (p *Value) XGof_ref_<field>() *TReads and writes of <field> both go through the returned pointer.
Getter/setter form — a pair of methods:
func (p *Value) XGof_get_<field>() T
func (p *Value) XGof_set_<field>(v T)Reads call the getter; writes call the setter.
Use the reference form when the member really lives at an addressable location (typical for union members). Use the getter/setter form when there is no address to hand out (typical for bit-fields), or when a read or write needs extra work.
Using Virtual Fields
Suppose a package provides a union and a struct with bit-fields:
union Value {
int i;
float f;
};
struct Header {
unsigned int version : 4;
unsigned int flags : 4;
};A binding for them can provide virtual fields like this:
// union: members are addressable, so use the reference form.
func (p *Value) XGof_ref_i() *c.Int { return (*c.Int)(unsafe.Pointer(p)) }
func (p *Value) XGof_ref_f() *c.Float { return (*c.Float)(unsafe.Pointer(p)) }
// bit-fields: no address to return, so use getter/setter.
func (p *Header) XGof_get_version() c.Uint { /* extract bits */ }
func (p *Header) XGof_set_version(v c.Uint) { /* store bits */ }
func (p *Header) XGof_get_flags() c.Uint { /* extract bits */ }
func (p *Header) XGof_set_flags(v c.Uint) { /* store bits */ }Client code then reads exactly like the C source:
var v Value
v.i = 0x3f800000
echo v.f // reinterprets the same memory as a float
h := &Header{}
h.version = 2
h.flags |= 0x1
echo h.version, h.flagsEverything you would normally do with a field works: reading, assignment, tuple assignment (h.version, h.flags = 1, 3), compound assignment (h.flags |= 0x1), and ++ / --.
Because the XGof_ members are ordinary methods, plain Go code and other tools can still call them directly (v.XGof_ref_i(), h.XGof_get_version()). The virtual field is only a syntactic convenience on top.
How Accesses Are Rewritten
The compiler lowers each virtual-field access to method calls, so there is no runtime cost beyond the call itself, and the generated Go code is plain Go.
| Source | Reference form | Getter/setter form |
|---|---|---|
x.f (read) |
*x.XGof_ref_f() |
x.XGof_get_f() |
x.f = v |
*x.XGof_ref_f() = v |
x.XGof_set_f(v) |
x.f op= v, x.f++ |
*x.XGof_ref_f() op= v |
x.XGof_set_f(x.XGof_get_f() op v) |
&x.f |
x.XGof_ref_f() |
compile error |
In compound assignments and ++/--, the operand x is evaluated only once, exactly as it would be for a real field.
Rules
Naming. The field name is everything after XGof_ref_, XGof_get_ or XGof_set_, taken verbatim. XGof_get_mode defines the field mode; XGof_get_Mode defines Mode. The first segment (ref, get, set) always decides the form, so a field whose name happens to start with get_ or ref_ causes no ambiguity. Since the XGof_ methods are exported, virtual fields are accessible from any package, whatever the case of the field name.
Type consistency. For the getter/setter form, the getter's result type and the setter's parameter type must be identical. A mismatch is a compile error.
One form per field. Declaring both XGof_ref_f and XGof_get_f / XGof_set_f for the same field is a compile error.
Read-only and write-only fields. A getter without a setter gives a read-only field; assigning to it is a compile error. A setter without a getter gives a write-only field; reading it is a compile error.
Address-of. &x.f is only valid for the reference form, where it simply yields the pointer the method returns. For the getter/setter form it is a compile error, the same as taking the address of a C bit-field.
Receivers. The usual Go method rules apply. With a pointer receiver, x.f requires x to be addressable or already a pointer; using a virtual field on a non-addressable value is an error, just as calling a pointer-receiver method would be.
Same namespace as fields and methods. Virtual fields take part in selector resolution together with real fields and methods. A type that has a real field or method f and also a virtual field f gets an ambiguity error at the use site. Virtual fields are promoted through embedded structs like ordinary members, following Go's depth rules.
Not a real field. A virtual field cannot be used as a key in a struct literal (Header{version: 1} is an error), and it is invisible to reflect and to encoders that walk struct fields. Only the XGof_ methods are visible there.
Relationship to C/C++ Bindings
This proposal is the language-side piece. It defines the convention; it does not decide how a given tool generates code. A binding generator such as llcppg can adopt it for union members (reference form) and bit-fields (getter/setter form), so that generated bindings for C unions and bit-fields are used with ordinary field syntax.
Compatibility
The feature only gives meaning to selector expressions that would otherwise fail to compile — x.f where f is neither a field nor a method — so no existing valid program changes behavior. Methods that happen to start with XGof_ remain ordinary methods and can still be called by their full names.
Source: goplus/xgo