buildCommandEncodeFuncs builds the encode() method for the each API command and the API command call (if they don't return void). encode() will call gapil_encode_type() with the state type before encoding the proto message with gapil_encode_object.
()
| 240 | // encode() will call gapil_encode_type() with the state type before encoding |
| 241 | // the proto message with gapil_encode_object. |
| 242 | func (e *encoder) buildCommandEncodeFuncs() { |
| 243 | for _, api := range e.APIs { |
| 244 | for _, cmd := range api.Functions { |
| 245 | mgCmd := &mangling.Namespace{Name: "cmd", Parent: e.Root} |
| 246 | mgClass := &mangling.Class{Name: cmd.Name(), Parent: mgCmd} |
| 247 | mgEncode := e.mgEncode(mgClass) |
| 248 | |
| 249 | fields := []codegen.Field{ |
| 250 | codegen.Field{Name: "thread", Type: e.T.Uint64}, |
| 251 | } |
| 252 | for _, p := range cmd.FullParameters { |
| 253 | fields = append(fields, codegen.Field{Name: p.Name(), Type: e.T.Target(p.Type)}) |
| 254 | } |
| 255 | |
| 256 | thisTy := e.T.Pointer(e.T.Struct(cmd.Name()+"_params", fields...)) |
| 257 | |
| 258 | e.C.Build(e.M.Function( |
| 259 | e.T.VoidPtr, |
| 260 | e.Mangler(mgEncode), |
| 261 | thisTy, e.T.CtxPtr, e.T.Bool), func(s *compiler.S) { |
| 262 | |
| 263 | this, isGroup := s.Parameter(0), s.Parameter(2) |
| 264 | |
| 265 | e.debug(s, "encoding command: '"+cmd.Name()+"' this: %p, ctx: %p", this, s.Ctx) |
| 266 | |
| 267 | typeID := s.Call(e.entities.funcParams[cmd].encodeType, s.Ctx) |
| 268 | |
| 269 | buf, delBuf := e.newBuf(s) |
| 270 | |
| 271 | threadPtr := this.Index(0, "thread") |
| 272 | e.encodeField(s, threadPtr, buf, serialization.CmdThread, semantic.Uint64Type) |
| 273 | for i, p := range cmd.CallParameters() { |
| 274 | ptr := this.Index(0, p.Name()) |
| 275 | e.encodeField(s, ptr, buf, serialization.CmdFieldStart+serialization.ProtoFieldID(i), p.Type) |
| 276 | } |
| 277 | |
| 278 | bufOffset := buf.Index(0, compiler.BufSize).Load() |
| 279 | bufData := buf.Index(0, compiler.BufData).Load() |
| 280 | out := s.Call(e.callbacks.encodeObject, s.Ctx, isGroup.Cast(e.T.Uint8), typeID, bufOffset, bufData) |
| 281 | |
| 282 | delBuf() |
| 283 | |
| 284 | s.Return(out) |
| 285 | }) |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | // buildCommandCallEncodeFuncs builds the encode() method for the each API |
| 291 | // command call (for commands that don't return void). |
no test coverage detected