]> asedeno.scripts.mit.edu Git - cl-protobufs.git/blob - serialize.lisp
595df40c863a5ed0de8d1a0c65be1e9f831ea20d
[cl-protobufs.git] / serialize.lisp
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;;                                                                  ;;;
3 ;;; Free Software published under an MIT-like license. See LICENSE   ;;;
4 ;;;                                                                  ;;;
5 ;;; Copyright (c) 2012 Google, Inc.  All rights reserved.            ;;;
6 ;;;                                                                  ;;;
7 ;;; Original author: Scott McKay                                     ;;;
8 ;;;                                                                  ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10
11 (in-package "PROTO-IMPL")
12
13
14 ;;; Protobuf serialization from Lisp objects
15
16 ;;; Serialization
17
18 (defun serialize-object-to-file (filename object type &key visited)
19   "Serializes the object 'object' of type 'type' into the file 'filename'
20    using the wire format.
21    'object' and 'type' are the same as for 'serialize-object-to-bytes'."
22   (with-open-file (stream filename
23                    :direction :output
24                    :element-type '(unsigned-byte 8))
25     (serialize-object-to-stream object type :stream stream :visited visited)))
26
27 (defun serialize-object-to-stream (object type &key (stream *standard-output*) visited)
28   "Serializes the object 'object' of type 'type' onto the stream 'stream'
29    using the wire format.
30    'object' and 'type' are the same as for 'serialize-object-to-bytes'."
31   (let ((buffer (serialize-object-to-bytes object type :visited visited)))
32     (write-sequence buffer stream)
33     buffer))
34
35 (defun serialize-object-to-bytes (object type &key visited)
36   "Serializes the object 'object' of type 'type' into a new byte vector
37    using the wire format.
38    'type' is the Lisp name of a Protobufs message (usually the name of a 
39    Lisp class) or a 'protobuf-message'.
40    'visited' is a hash table used to cache object sizes. If it is supplied, it will be
41    cleared before it is used; otherwise, a fresh table will be created.
42    The return value is the buffer containing the serialized object. If the stream is
43    nil, the buffer is not actually written to anywhere."
44   (let* ((visited (let ((v (or visited (make-hash-table))))
45                     (clrhash v)
46                     v))
47          (size    (object-size object type visited))
48          (buffer  (make-byte-vector size)))
49     (serialize-object object type buffer 0 visited)
50     buffer))
51
52 ;; Serialize the object using the given protobuf type
53
54
55 ;; Allow clients to add their own methods
56 ;; This is how we address the problem of cycles, e.g. -- if you have an object
57 ;; that may contain cycles, serialize the cyclic object using a "handle"
58 (defgeneric serialize-object (object type buffer &optional start visited)
59   (:documentation
60    "Serializes the object 'object' of type 'type' into the byte array 'buffer'
61     using the wire format.
62     'type' is the Lisp name of a Protobufs message (usually the name of a 
63     Lisp class) or a 'protobuf-message'.
64     The object is serialized into the byte array given by 'buffer' starting
65     at the fixnum index 'index' using the wire format.
66     'visited' is a hash table used to cache object sizes.
67     The return value is the buffer containing the serialized object."))
68
69 (defmethod serialize-object (object type buffer &optional start visited)
70   (let ((message (find-message-for-class type)))
71     (assert message ()
72             "There is no Protobuf message having the type ~S" type)
73     (serialize-object object message buffer start visited)))
74
75 ;; 'visited' is used to cache object sizes
76 ;; If it's passed in explicitly, it is assumed to already have the sizes within it
77 ;; The default method uses metadata from the protobuf "schema" for the message
78 (defmethod serialize-object (object (message protobuf-message) buffer &optional start visited)
79   (declare (type (simple-array (unsigned-byte 8)) buffer))
80   (let ((visited (or visited (make-hash-table)))
81         (index   (or start 0)))
82     (declare (type fixnum index))
83     (macrolet ((read-slot (object slot reader)
84                  ;; Don't do a boundp check, we assume the object is fully populated
85                  ;; Unpopulated slots should be "nullable" and will contain nil when empty
86                  `(if ,reader
87                     (funcall ,reader ,object)
88                     (slot-value ,object ,slot))))
89       (labels ((do-field (object trace field)
90                  ;; We don't do cycle detection here
91                  ;; If the client needs it, he can define his own 'serialize-object'
92                  ;; method to clean things up first
93                  (let* ((type   (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
94                         (slot   (proto-value field))
95                         (reader (proto-reader field))
96                         msg)
97                    (when (or slot reader)
98                      (cond ((eq (proto-required field) :repeated)
99                             (cond ((and (proto-packed field) (packed-type-p type))
100                                    ;; This is where we handle packed primitive types
101                                    ;; Packed enums get handled below
102                                    (let ((tag (make-tag type (proto-index field))))
103                                      (setq index (serialize-packed (read-slot object slot reader)
104                                                                    type tag buffer index))))
105                                   ((keywordp type)
106                                    (let ((tag (make-tag type (proto-index field))))
107                                      (map () #'(lambda (v)
108                                                  (setq index (serialize-prim v type tag buffer index)))
109                                              (read-slot object slot reader))))
110                                   ((typep (setq msg (and type (or (find-message trace type)
111                                                                   (find-enum trace type)
112                                                                   (find-type-alias trace type))))
113                                           'protobuf-message)
114                                    (if (eq (proto-message-type msg) :group)
115                                      (map () #'(lambda (v)
116                                                  ;; To serialize a group, we encode a start tag,
117                                                  ;; serialize the fields, then encode an end tag
118                                                  (let ((tag1 (make-tag $wire-type-start-group (proto-index field)))
119                                                        (tag2 (make-tag $wire-type-end-group   (proto-index field))))
120                                                    (setq index (encode-uint32 tag1 buffer index))
121                                                    (map () (curry #'do-field v msg)
122                                                            (proto-fields msg))
123                                                    (setq index (encode-uint32 tag2 buffer index))))
124                                              (if slot (read-slot object slot reader) (list object)))
125                                      (map () #'(lambda (v)
126                                                  ;; To serialize an embedded message, first say that it's
127                                                  ;; a string, then encode its size, then serialize its fields
128                                                  (let ((tag (make-tag $wire-type-string (proto-index field)))
129                                                        (len (object-size v msg visited)))
130                                                    (setq index (encode-uint32 tag buffer index))
131                                                    (setq index (encode-uint32 len buffer index)))
132                                                  (map () (curry #'do-field v msg)
133                                                          (proto-fields msg)))
134                                              (if slot (read-slot object slot reader) (list object)))))
135                                   ((typep msg 'protobuf-enum)
136                                    (let ((tag (make-tag $wire-type-varint (proto-index field))))
137                                      ;; 'proto-packed-p' of enum types returns nil,
138                                      ;; so packed enum fields won't be handled above
139                                      (if (proto-packed field)
140                                        (setq index (serialize-packed-enum (read-slot object slot reader)
141                                                                           (proto-values msg) tag buffer index))
142                                        (map () #'(lambda (v)
143                                                    (setq index (serialize-enum v (proto-values msg) tag buffer index)))
144                                                (read-slot object slot reader)))))
145                                   ((typep msg 'protobuf-type-alias)
146                                    (let* ((type (proto-proto-type msg))
147                                           (tag  (make-tag type (proto-index field))))
148                                      (map () #'(lambda (v)
149                                                  (let ((v (funcall (proto-serializer msg) v)))
150                                                    (setq index (serialize-prim v type tag buffer index))))
151                                              (read-slot object slot reader))))))
152                            (t
153                             (cond ((eq type :bool)
154                                    ;; We have to handle optional boolean fields specially
155                                    ;; because "false" and nil are the same value in Lisp
156                                    (let ((v (cond ((or (eq (proto-required field) :required)
157                                                        (null slot))
158                                                    (read-slot object slot reader))
159                                                   ((slot-boundp object slot)
160                                                    (read-slot object slot reader))
161                                                   (t :unbound))))
162                                      (unless (eq v :unbound)
163                                        (let ((tag (make-tag :bool (proto-index field))))
164                                          (setq index (serialize-prim v type tag buffer index))))))
165                                   ((keywordp type)
166                                    (let ((v (read-slot object slot reader)))
167                                      (when v
168                                        (let ((tag (make-tag type (proto-index field))))
169                                          (setq index (serialize-prim v type tag buffer index))))))
170                                   ((typep (setq msg (and type (or (find-message trace type)
171                                                                   (find-enum trace type)
172                                                                   (find-type-alias trace type))))
173                                           'protobuf-message)
174                                    (let ((v (if slot (read-slot object slot reader) object)))
175                                      (when v
176                                        (if (eq (proto-message-type msg) :group)
177                                          (let ((tag1 (make-tag $wire-type-start-group (proto-index field)))
178                                                (tag2 (make-tag $wire-type-end-group   (proto-index field))))
179                                            (setq index (encode-uint32 tag1 buffer index))
180                                            (map () (curry #'do-field v msg)
181                                                    (proto-fields msg))
182                                            (setq index (encode-uint32 tag2 buffer index)))
183                                          (let ((tag (make-tag $wire-type-string (proto-index field)))
184                                                (len (object-size v msg visited)))
185                                            (setq index (encode-uint32 tag buffer index))
186                                            (setq index (encode-uint32 len buffer index))
187                                            (map () (curry #'do-field v msg)
188                                                    (proto-fields msg)))))))
189                                   ((typep msg 'protobuf-enum)
190                                    (let ((v (read-slot object slot reader)))
191                                      (when v
192                                        (let ((tag (make-tag $wire-type-varint (proto-index field))))
193                                          (setq index (serialize-enum v (proto-values msg) tag buffer index))))))
194                                   ((typep msg 'protobuf-type-alias)
195                                    (let ((v (read-slot object slot reader)))
196                                      (when v
197                                        (let* ((v    (funcall (proto-serializer msg) v))
198                                               (type (proto-proto-type msg))
199                                               (tag  (make-tag type (proto-index field))))
200                                          (setq index (serialize-prim v type tag buffer index)))))))))))))
201         (declare (dynamic-extent #'do-field))
202         (map () (curry #'do-field object message) (proto-fields message))))
203     (values buffer index)))
204
205
206 ;;; Deserialization
207
208 (defun deserialize-object-from-file (type filename)
209   "Deserializes an object of the given type 'type' from the given file
210    as a Protobuf object."
211   (with-open-file (stream filename
212                    :direction :input
213                    :element-type '(unsigned-byte 8))
214     (deserialize-object-from-stream type :stream stream)))
215
216 (defun deserialize-object-from-stream (type &key (stream *standard-input*))
217   "Deserializes an object of the given type 'type' from the given stream
218    as a Protobuf object."
219   (let* ((size    (file-length stream))
220          (buffer  (make-byte-vector size)))
221     (read-sequence buffer stream)
222     (deserialize-object type buffer 0 size)))
223
224 (defun deserialize-object-from-bytes (type buffer)
225   "Deserializes an object of the given type 'type' from the given stream
226    as a Protobuf object.
227    'type' is the Lisp name of a Protobufs message (usually the name of a 
228    Lisp class) or a 'protobuf-message'.
229    The return value is the object."
230   (deserialize-object type buffer))
231
232 ;; Allow clients to add their own methods
233 ;; This is you might preserve object identity, e.g.
234 (defgeneric deserialize-object (type buffer &optional start end end-tag)
235   (:documentation
236    "Deserializes an object of the given type 'type' as a Protobufs object.
237     'type' is the Lisp name of a Protobufs message (usually the name of a 
238     Lisp class) or a 'protobuf-message'.
239     The encoded bytes are in the byte array given by 'buffer' starting at
240     the fixnum index 'start' up to the end of the buffer, given by 'end'.
241     'start' defaults to 0, 'end' defaults to the length of the buffer.
242     'end-tag' is used internally to handle the (deprecated) \"group\" feature.
243     The return values are the object and the index at which deserialization stopped.."))
244
245 (defmethod deserialize-object (type buffer &optional start end (end-tag 0))
246   (let ((message (find-message-for-class type)))
247     (assert message ()
248             "There is no Protobuf message having the type ~S" type)
249     (deserialize-object message buffer start end end-tag)))
250
251 ;; The default method uses metadata from the protobuf "schema" for the message
252 (defmethod deserialize-object ((message protobuf-message) buffer &optional start end (end-tag 0))
253   (declare (type (simple-array (unsigned-byte 8)) buffer))
254   (let ((index   (or start 0))
255         (length  (or end (length buffer))))
256     (declare (type fixnum index length))
257     (macrolet ((read-slot (object slot reader)
258                  `(if ,reader
259                     (funcall ,reader ,object)
260                     (slot-value ,object ,slot)))
261                (write-slot (object slot writer value)
262                  (with-gensyms (vval)
263                    `(let ((,vval ,value))
264                       (if ,writer
265                         (funcall ,writer ,object ,vval)
266                         (setf (slot-value ,object ,slot) ,vval)))))
267                (push-slot (object slot reader writer value)
268                  (with-gensyms (vvals)
269                    `(let ((,vvals (read-slot ,object ,slot ,reader)))
270                       (if (i= (length ,vvals) 0)
271                         ;; We need the initial value to be a stretchy vector,
272                         ;; so scribble over it just to make sure
273                         (let ((,vvals (make-array 1
274                                         :fill-pointer t :adjustable t
275                                         :initial-contents (list ,value))))
276                           (write-slot ,object ,slot ,writer ,vvals))
277                         (vector-push-extend ,value ,vvals))))))
278       (labels ((deserialize (type trace end end-tag)
279                  (declare (type fixnum end end-tag))
280                  (let* ((message (find-message trace type))
281                         (object  (and message
282                                       (make-instance (or (proto-alias-for message) (proto-class message)))))
283                         ;; All the slots into which we store a repeated element
284                         ;; These will be reversed at the end of deserialization
285                         (rslots ()))
286                    (loop
287                      (multiple-value-bind (tag idx)
288                          (if (i< index end) (decode-uint32 buffer index) (values 0 index))
289                        ;; We're done if we've gotten to the end index or
290                        ;; we see an end tag that matches a previous group's start tag
291                        ;; Note that the default end tag is 0, which is also an end of
292                        ;; message marker (there can never be "real" zero tags because
293                        ;; field indices start at 1)
294                        (setq index idx)
295                        (when (i= tag end-tag)
296                          ;; Reverse the repeated slots
297                          (dolist (field rslots)
298                            (let ((slot   (proto-value field))
299                                  (reader (proto-reader field))
300                                  (writer (proto-writer field)))
301                              (write-slot object slot writer
302                                          (nreverse (read-slot object slot reader)))))
303                          (return-from deserialize
304                            (values object index)))
305                        (let* ((fidx  (ilogand (iash tag -3) #x1FFFFFFF))
306                               (field (find fidx (proto-fields message) :key #'proto-index))
307                               (type  (and field (if (eq (proto-class field) 'boolean) :bool (proto-class field))))
308                               ;; It's OK for this to be null
309                               ;; That means we're parsing some version of a message
310                               ;; that has the field, but our current message does not
311                               ;; We still have to deserialize everything, though
312                               (slot   (and field (proto-value field)))
313                               (reader (and field (proto-reader field)))
314                               (writer (and field (proto-writer field)))
315                               msg)
316                          (if (null field)
317                            ;; If there's no field descriptor for this index, just skip
318                            ;; the next element in the buffer having the given wire type
319                            (setq index (skip-element buffer index tag))
320                            ;;--- Check for mismatched wire type, running past end of buffer, etc
321                            (cond ((and field (eq (proto-required field) :repeated))
322                                   (let ((vectorp (vector-field-p field)))
323                                     (cond ((and (proto-packed field) (packed-type-p type))
324                                            (multiple-value-bind (values idx)
325                                                (deserialize-packed type buffer index)
326                                              (setq index idx)
327                                              (if vectorp
328                                                (let ((values (make-array (length values)
329                                                                :fill-pointer t :adjustable t
330                                                                :initial-contents values)))
331                                                  (write-slot object slot writer values))
332                                                (write-slot object slot writer values))))
333                                           ((keywordp type)
334                                            (multiple-value-bind (val idx)
335                                                (deserialize-prim type buffer index)
336                                              (setq index idx)
337                                              (cond (vectorp
338                                                     (push-slot object slot reader writer val))
339                                                    (t
340                                                     (pushnew field rslots)
341                                                     ;; This "push" could type-check the entire list if
342                                                     ;; there's a parameterized list type in effect,
343                                                     ;; so you'll want to avoid using such types
344                                                     ;; We'll reverse the slots at the last minute
345                                                     (write-slot object slot writer
346                                                                 (cons val (read-slot object slot reader)))))))
347                                           ((typep (setq msg (and type (or (find-message trace type)
348                                                                           (find-enum trace type)
349                                                                           (find-type-alias trace type))))
350                                                   'protobuf-message)
351                                            (if (eq (proto-message-type msg) :group)
352                                              (let* ((etag (make-tag $wire-type-end-group fidx))
353                                                     (obj  (deserialize type msg length etag)))
354                                                (cond (vectorp
355                                                       (push-slot object slot reader writer obj))
356                                                      (t
357                                                       (pushnew field rslots)
358                                                       (write-slot object slot writer
359                                                                   (cons obj (read-slot object slot reader))))))
360                                              (multiple-value-bind (len idx)
361                                                  (decode-uint32 buffer index)
362                                                (setq index idx)
363                                                (let ((obj (deserialize type msg (+ index len) 0)))
364                                                  (cond (vectorp
365                                                         (push-slot object slot reader writer obj))
366                                                        (t
367                                                         (pushnew field rslots)
368                                                         (write-slot object slot writer
369                                                                     (cons obj (read-slot object slot reader)))))))))
370                                           ((typep msg 'protobuf-enum)
371                                            (if (proto-packed field)
372                                              (multiple-value-bind (values idx)
373                                                  (deserialize-packed-enum (proto-values msg) buffer index)
374                                                (setq index idx)
375                                                (if vectorp
376                                                  (let ((values (make-array (length values)
377                                                                  :fill-pointer t :adjustable t
378                                                                  :initial-contents values)))
379                                                    (write-slot object slot writer values))
380                                                  (write-slot object slot writer values)))
381                                              (multiple-value-bind (val idx)
382                                                  (deserialize-enum (proto-values msg) buffer index)
383                                                (setq index idx)
384                                                (cond (vectorp
385                                                       (push-slot object slot reader writer val))
386                                                      (t
387                                                       (pushnew field rslots)
388                                                       (write-slot object slot writer
389                                                                   (cons val (read-slot object slot reader))))))))
390                                           ((typep msg 'protobuf-type-alias)
391                                            (let ((type (proto-proto-type msg)))
392                                              (multiple-value-bind (val idx)
393                                                  (deserialize-prim type buffer index)
394                                                (setq index idx)
395                                                (cond (vectorp
396                                                       (push-slot object slot reader writer
397                                                                  (funcall (proto-deserializer msg) val)))
398                                                      (t
399                                                       (pushnew field rslots)
400                                                       (write-slot object slot writer
401                                                                   (cons (funcall (proto-deserializer msg) val)
402                                                                         (read-slot object slot reader)))))))))))
403                                  (t
404                                   (cond ((keywordp type)
405                                          (multiple-value-bind (val idx)
406                                              (deserialize-prim type buffer index)
407                                            (setq index idx)
408                                            (write-slot object slot writer val)))
409                                         ((typep (setq msg (and type (or (find-message trace type)
410                                                                         (find-enum trace type)
411                                                                         (find-type-alias trace type))))
412                                                 'protobuf-message)
413                                          ;;--- If there's already a value in the slot, merge messages
414                                          (if (eq (proto-message-type msg) :group)
415                                            (let* ((etag (make-tag $wire-type-end-group fidx))
416                                                   (obj  (deserialize type msg length etag)))
417                                              (write-slot object slot writer obj))
418                                            (multiple-value-bind (len idx)
419                                                (decode-uint32 buffer index)
420                                              (setq index idx)
421                                              (let ((obj (deserialize type msg (+ index len) 0)))
422                                                (write-slot object slot writer obj)))))
423                                         ((typep msg 'protobuf-enum)
424                                          (multiple-value-bind (val idx)
425                                              (deserialize-enum (proto-values msg) buffer index)
426                                            (setq index idx)
427                                            (write-slot object slot writer val)))
428                                         ((typep msg 'protobuf-type-alias)
429                                          (let ((type (proto-proto-type msg)))
430                                            (multiple-value-bind (val idx)
431                                                (deserialize-prim type buffer index)
432                                              (setq index idx)
433                                              (write-slot object slot writer
434                                                          (funcall (proto-deserializer msg) val)))))))))))))))
435         (declare (dynamic-extent #'deserialize))
436         (deserialize (proto-class message) message length end-tag)))))
437
438
439 ;;; Object sizes
440
441 ;; Allow clients to add their own methods
442 ;; This is how we address the problem of cycles, e.g. -- if you have an object
443 ;; that may contain cycles, return the size of the "handle" to the object
444 (defgeneric object-size (object type &optional visited)
445   (:documentation
446    "Computes the size in bytes of the object 'object' of type 'type'.
447     'type' is the Lisp name of a Protobufs message (usually the name of a 
448     Lisp class) or a 'protobuf-message'.
449     'visited' is a hash table used to cache object sizes.
450     The return value is the size of the object in bytes."))
451
452 (defmethod object-size (object type &optional visited)
453   (let ((message (find-message-for-class type)))
454     (assert message ()
455             "There is no Protobuf message having the type ~S" type)
456     (object-size object message visited)))
457
458 ;; 'visited' is used to cache object sizes
459 ;; The default method uses metadata from the protobuf "schema" for the message
460 (defmethod object-size (object (message protobuf-message) &optional visited)
461   (let ((size (and visited (gethash object visited))))
462     (when size
463       (return-from object-size size)))
464   (let ((size 0))
465     (declare (type fixnum size))
466     (macrolet ((read-slot (object slot reader)
467                  ;; Don't do a boundp check, we assume the object is fully populated
468                  ;; Unpopulated slots should be "nullable" and will contain nil when empty
469                  `(if ,reader
470                     (funcall ,reader ,object)
471                     (slot-value ,object ,slot))))
472       (labels ((do-field (object trace field)
473                  ;; We don't do cycle detection here
474                  ;; If the client needs it, he can define his own 'object-size'
475                  ;; method to clean things up first
476                  (let* ((type   (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
477                         (slot   (proto-value field))
478                         (reader (proto-reader field))
479                         msg)
480                    (when (or slot reader)
481                      (cond ((eq (proto-required field) :repeated)
482                             (cond ((and (proto-packed field) (packed-type-p type))
483                                    (let ((tag (make-tag type (proto-index field))))
484                                      (iincf size (packed-size (read-slot object slot reader) type tag))))
485                                   ((keywordp type)
486                                    (let ((tag (make-tag type (proto-index field))))
487                                      (map () #'(lambda (v)
488                                                  (iincf size (prim-size v type tag)))
489                                              (read-slot object slot reader))))
490                                   ((typep (setq msg (and type (or (find-message trace type)
491                                                                   (find-enum trace type)
492                                                                   (find-type-alias trace type))))
493                                           'protobuf-message)
494                                    (if (eq (proto-message-type msg) :group)
495                                      (map () #'(lambda (v)
496                                                  (let ((tag1 (make-tag $wire-type-start-group (proto-index field)))
497                                                        (tag2 (make-tag $wire-type-end-group   (proto-index field))))
498                                                    (iincf size (length32 tag1))
499                                                    (map () (curry #'do-field v msg)
500                                                            (proto-fields msg))
501                                                    (iincf size (length32 tag2))))
502                                              (if slot (read-slot object slot reader) (list object)))
503                                      (map () #'(lambda (v)
504                                                  (let ((tag (make-tag $wire-type-string (proto-index field)))
505                                                        (len (object-size v msg visited)))
506                                                    (iincf size (length32 tag))
507                                                    (iincf size (length32 len))
508                                                    (map () (curry #'do-field v msg)
509                                                            (proto-fields msg))))
510                                              (if slot (read-slot object slot reader) (list object)))))
511                                   ((typep msg 'protobuf-enum)
512                                    (let ((tag (make-tag $wire-type-varint (proto-index field))))
513                                      (if (proto-packed field)
514                                        (iincf size (packed-enum-size (read-slot object slot reader) type tag))
515                                        (map () #'(lambda (v)
516                                                    (iincf size (enum-size v (proto-values msg) tag)))
517                                                (read-slot object slot reader)))))
518                                   ((typep msg 'protobuf-type-alias)
519                                    (let* ((type (proto-proto-type msg))
520                                           (tag  (make-tag type (proto-index field))))
521                                      (map () #'(lambda (v)
522                                                  (let ((v (funcall (proto-serializer msg) v)))
523                                                    (iincf size (prim-size v type tag))))
524                                              (read-slot object slot reader))))))
525                            (t
526                             (cond ((eq type :bool)
527                                    (let ((v (cond ((or (eq (proto-required field) :required)
528                                                        (null slot))
529                                                    (read-slot object slot reader))
530                                                   ((slot-boundp object slot)
531                                                    (read-slot object slot reader))
532                                                   (t :unbound))))
533                                      (unless (eq v :unbound)
534                                        (let ((tag (make-tag :bool (proto-index field))))
535                                          (iincf size (prim-size v type tag))))))
536                                   ((keywordp type)
537                                    (let ((v (read-slot object slot reader)))
538                                      (when v
539                                        (let ((tag (make-tag type (proto-index field))))
540                                          (iincf size (prim-size v type tag))))))
541                                   ((typep (setq msg (and type (or (find-message trace type)
542                                                                   (find-enum trace type)
543                                                                   (find-type-alias trace type))))
544                                           'protobuf-message)
545                                    (let ((v (if slot (read-slot object slot reader) object)))
546                                      (when v
547                                        (if (eq (proto-message-type msg) :group)
548                                          (let ((tag1 (make-tag $wire-type-start-group (proto-index field)))
549                                              (tag2 (make-tag $wire-type-end-group   (proto-index field))))
550                                            (iincf size (length32 tag1))
551                                            (map () (curry #'do-field v msg)
552                                                    (proto-fields msg))
553                                            (iincf size (length32 tag2)))
554                                          (let ((tag (make-tag $wire-type-string (proto-index field)))
555                                                (len (object-size v msg visited)))
556                                            (iincf size (length32 tag))
557                                            (iincf size (length32 len))
558                                            (map () (curry #'do-field v msg)
559                                                 (proto-fields msg)))))))
560                                   ((typep msg 'protobuf-enum)
561                                    (let ((v (read-slot object slot reader)))
562                                      (when v
563                                        (let ((tag (make-tag $wire-type-varint (proto-index field))))
564                                          (iincf size (enum-size (read-slot object slot reader) (proto-values msg) tag))))))
565                                   ((typep msg 'protobuf-type-alias)
566                                    (let ((v (read-slot object slot reader)))
567                                      (when v
568                                        (let* ((v    (funcall (proto-serializer msg) v))
569                                               (type (proto-proto-type msg))
570                                               (tag  (make-tag type (proto-index field))))
571                                          (iincf size (prim-size v type tag)))))))))))))
572         (declare (dynamic-extent #'do-field))
573         (map () (curry #'do-field object message) (proto-fields message))
574         (when visited
575           (setf (gethash object visited) size))   ;cache the size
576         size))))
577
578 \f
579 ;;; Compile-time generation of serializers
580 ;;; Type-checking is done at the top-level methods specialized on 'symbol',
581 ;;; so we turn off all type checking at the level of these functions
582
583 ;; Note well: keep this in sync with the main 'serialize-object' method above
584 (defun generate-serializer (message)
585   "Generate a 'serialize-object' method for the given message."
586   (with-gensyms (vobj vbuf vidx vval vclass)
587     (when (null (proto-fields message))
588       (return-from generate-serializer
589         `(defmethod serialize-object
590            (,vobj (,vclass (eql ,message)) ,vbuf &optional (,vidx 0) visited)
591          (declare #.$optimize-serialization)
592          (declare (ignorable ,vobj ,vclass visited)
593                   (type (simple-array (unsigned-byte 8)) ,vbuf)
594                   (type fixnum ,vidx))
595          (values ,vbuf ,vidx))))
596     (with-collectors ((serializers collect-serializer))
597       (dolist (field (proto-fields message))
598         (let* ((class  (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
599                (msg    (and class (not (keywordp class))
600                             (or (find-message message class)
601                                 (find-enum message class)
602                                 (find-type-alias message class))))
603                (reader (cond ((proto-reader field)
604                               `(,(proto-reader field) ,vobj))
605                              ((proto-value field)
606                               `(slot-value ,vobj ',(proto-value field)))))
607                (index  (proto-index field)))
608           (when reader
609             (cond ((eq (proto-required field) :repeated)
610                    (let ((iterator (if (vector-field-p field) 'dovector 'dolist)))
611                      (cond ((and (proto-packed field) (packed-type-p class))
612                             (collect-serializer
613                              (let ((tag (make-tag class index)))
614                                `(setq ,vidx (serialize-packed ,reader ,class ,tag ,vbuf ,vidx)))))
615                            ((keywordp class)
616                             (collect-serializer
617                              (let ((tag (make-tag class index)))
618                                `(,iterator (,vval ,reader)
619                                   (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))
620                            ((typep msg 'protobuf-message)
621                             (collect-serializer
622                              (if (eq (proto-message-type msg) :group)
623                                (let ((tag1 (make-tag $wire-type-start-group index))
624                                      (tag2 (make-tag $wire-type-end-group   index)))
625                                  `(,iterator (,vval ,reader)
626                                     (let ((len (or (and visited (gethash ,vval visited))
627                                                    (object-size ,vval ,msg visited))))
628                                       (setq ,vidx (encode-uint32 ,tag1 ,vbuf ,vidx))
629                                       (serialize-object ,vval ,msg ,vbuf ,vidx visited)
630                                       (iincf ,vidx len)
631                                       (setq ,vidx (encode-uint32 ,tag2 ,vbuf ,vidx)))))
632                                (let ((tag (make-tag $wire-type-string index)))
633                                  `(,iterator (,vval ,reader)
634                                     (let ((len (or (and visited (gethash ,vval visited))
635                                                    (object-size ,vval ,msg visited))))
636                                       (setq ,vidx (encode-uint32 ,tag ,vbuf ,vidx))
637                                       (setq ,vidx (encode-uint32 len ,vbuf ,vidx))
638                                       (serialize-object ,vval ,msg ,vbuf ,vidx visited)
639                                       (iincf ,vidx len)))))))
640                            ((typep msg 'protobuf-enum)
641                             (collect-serializer
642                              (let ((tag (make-tag $wire-type-varint index)))
643                                (if (proto-packed field)
644                                  `(setq ,vidx (serialize-packed-enum ,reader '(,@(proto-values msg)) ,tag ,vbuf ,vidx))
645
646                                  `(,iterator (,vval ,reader)
647                                     (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx)))))))
648                            ((typep msg 'protobuf-type-alias)
649                             (collect-serializer
650                              (let* ((class (proto-proto-type msg))
651                                     (tag   (make-tag class (proto-index field))))
652                                `(,iterator (,vval ,reader)
653                                   (let ((,vval (funcall #',(proto-serializer msg) ,vval)))
654                                     (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))))))
655                   (t
656                    (cond ((keywordp class)
657                           (collect-serializer
658                            (let ((tag (make-tag class index)))
659                              (if (eq class :bool)
660                                (if (or (eq (proto-required field) :required)
661                                        (null (proto-value field)))
662                                  `(let ((,vval ,reader))
663                                     (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx)))
664                                  `(let ((,vval (cond ((slot-boundp ,vobj ',(proto-value field))
665                                                       ,reader)
666                                                      (t :unbound))))
667                                     (unless (eq ,vval :unbound)
668                                       (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx)))))
669                                `(let ((,vval ,reader))
670                                   (when ,vval
671                                     (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))))
672                          ((typep msg 'protobuf-message)
673                           (collect-serializer
674                            (if (eq (proto-message-type msg) :group)
675                              (let ((tag1 (make-tag $wire-type-start-group index))
676                                    (tag2 (make-tag $wire-type-end-group   index)))
677                                `(let ((,vval ,reader))
678                                   (when ,vval
679                                     (let ((len (or (and visited (gethash ,vval visited))
680                                                    (object-size ,vval ,msg visited))))
681                                       (setq ,vidx (encode-uint32 ,tag1 ,vbuf ,vidx))
682                                       (serialize-object ,vval ,msg ,vbuf ,vidx visited)
683                                       (iincf ,vidx len)
684                                       (setq ,vidx (encode-uint32 ,tag2 ,vbuf ,vidx))))))
685                              (let ((tag (make-tag $wire-type-string index)))
686                                `(let ((,vval ,reader))
687                                   (when ,vval
688                                     (let ((len (or (and visited (gethash ,vval visited))
689                                                    (object-size ,vval ,msg visited))))
690                                       (setq ,vidx (encode-uint32 ,tag ,vbuf ,vidx))
691                                       (setq ,vidx (encode-uint32 len ,vbuf ,vidx))
692                                       (serialize-object ,vval ,msg ,vbuf ,vidx visited)
693                                       (iincf ,vidx len))))))))
694                          ((typep msg 'protobuf-enum)
695                           (collect-serializer
696                            (let ((tag (make-tag $wire-type-varint index)))
697                              `(let ((,vval ,reader))
698                                 (when ,vval
699                                   (setq ,vidx (serialize-enum ,vval '(,@(proto-values msg)) ,tag ,vbuf ,vidx)))))))
700                          ((typep msg 'protobuf-type-alias)
701                           (collect-serializer
702                            (let* ((class (proto-proto-type msg))
703                                   (tag   (make-tag class (proto-index field))))
704                              `(let ((,vval ,reader))
705                                 (when ,vval
706                                   (let ((,vval (funcall #',(proto-serializer msg) ,vval)))
707                                     (setq ,vidx (serialize-prim ,vval ,class ,tag ,vbuf ,vidx))))))))))))))
708       `(defmethod serialize-object
709            (,vobj (,vclass (eql ,message)) ,vbuf &optional (,vidx 0) visited)
710          (declare #.$optimize-serialization)
711          (declare (ignorable visited)
712                   (type (simple-array (unsigned-byte 8)) ,vbuf)
713                   (type fixnum ,vidx))
714          ,@serializers
715          (values ,vbuf ,vidx)))))
716
717 ;; Note well: keep this in sync with the main 'deserialize-object' method above
718 (defun generate-deserializer (message)
719   "Generate a 'deserialize-object' method for the given message."
720   (with-gensyms (vclass vbuf vidx vlen vendtag vobj vval)
721     (when (null (proto-fields message))
722       (return-from generate-deserializer
723         `(defmethod deserialize-object
724              ((,vclass (eql ,message)) ,vbuf &optional ,vidx ,vlen (,vendtag 0))
725            (declare #.$optimize-serialization)
726            (declare (ignorable ,vclass ,vbuf ,vlen ,vendtag)
727                     (type (simple-array (unsigned-byte 8)) ,vbuf))
728            (let ((,vidx (or ,vidx 0)))
729              (declare (type fixnum ,vidx))
730              (let ((,vobj (make-instance ',(or (proto-alias-for message) (proto-class message)))))
731                (values ,vobj ,vidx))))))
732     (with-collectors ((deserializers collect-deserializer)
733                       ;; For tracking repeated slots that will need to be reversed
734                       (rslots collect-rslot))
735       (flet ((read-slot (object field)
736                (cond ((proto-reader field)
737                       `(,(proto-reader field) ,object))
738                      ((proto-value field)
739                       `(slot-value ,object ',(proto-value field)))))
740              (write-slot (object field value)
741                (cond ((proto-writer field)
742                       `(,(proto-writer field) ,object ,value))
743                      ((proto-value field)
744                       `(setf (slot-value ,object ',(proto-value field)) ,value)))))
745         (dolist (field (proto-fields message))
746           (let* ((class  (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
747                  (msg    (and class (not (keywordp class))
748                               (or (find-message message class)
749                                   (find-enum message class)
750                                   (find-type-alias message class))))
751                  (index  (proto-index field)))
752             (cond ((eq (proto-required field) :repeated)
753                    (cond ((and (proto-packed field) (packed-type-p class))
754                           (collect-deserializer
755                            `((,(make-tag class index))
756                              (multiple-value-bind (,vval idx)
757                                  (deserialize-packed ,class ,vbuf ,vidx)
758                                (setq ,vidx idx)
759                                ,@(when (vector-field-p field)
760                                    `((setq ,vval (make-array (length ,vval)
761                                                    :fill-pointer t :adjustable t
762                                                    :initial-contents ,vval))))
763                                ,(write-slot vobj field vval)))))
764                          ((keywordp class)
765                           (let ((temp (gensym (string (proto-value field)))))
766                             (collect-rslot (list field temp))
767                             (collect-deserializer
768                              `((,(make-tag class index))
769                                (multiple-value-bind (,vval idx)
770                                    (deserialize-prim ,class ,vbuf ,vidx)
771                                  (setq ,vidx idx)
772                                  (push ,vval ,temp))))))
773                          ((typep msg 'protobuf-message)
774                           (let ((temp (gensym (string (proto-value field)))))
775                             (collect-rslot (list field temp))
776                             (collect-deserializer
777                              (if (eq (proto-message-type msg) :group)
778                                `((,(make-tag $wire-type-start-group index))
779                                  (multiple-value-bind (,vval idx)
780                                      (deserialize-object ,msg ,vbuf ,vidx ,vlen
781                                                          ,(make-tag $wire-type-end-group index))
782                                    (setq ,vidx idx)
783                                    (push ,vval ,temp)))
784                                `((,(make-tag $wire-type-string index))
785                                  (multiple-value-bind (len idx)
786                                      (decode-uint32 ,vbuf ,vidx)
787                                    (setq ,vidx idx)
788                                    (multiple-value-bind (,vval idx)
789                                        (deserialize-object ,msg ,vbuf ,vidx (i+ ,vidx len) 0)
790                                      (setq ,vidx idx)
791                                      (push ,vval ,temp))))))))
792                          ((typep msg 'protobuf-enum)
793                           (if (proto-packed field)
794                             (collect-deserializer
795                              `((,(make-tag $wire-type-varint index))
796                                (multiple-value-bind (,vval idx)
797                                    (deserialize-packed-enum '(,@(proto-values msg)) ,vbuf ,vidx)
798                                  (setq ,vidx idx)
799                                  ,(write-slot vobj field vval))))
800                             (let ((temp (gensym (string (proto-value field)))))
801                               (collect-rslot (list field temp))
802                               (collect-deserializer
803                                `((,(make-tag $wire-type-varint index))
804                                  (multiple-value-bind (,vval idx)
805                                      (deserialize-enum '(,@(proto-values msg)) ,vbuf ,vidx)
806                                    (setq ,vidx idx)
807                                    (push ,vval ,temp)))))))
808                          ((typep msg 'protobuf-type-alias)
809                           (let ((class (proto-proto-type msg))
810                                 (temp  (gensym (string (proto-value field)))))
811                             (collect-rslot (list field temp))
812                             (collect-deserializer
813                              `((,(make-tag class index))
814                                (multiple-value-bind (,vval idx)
815                                    (deserialize-prim ,class ,vbuf ,vidx)
816                                  (setq ,vidx idx)
817                                  (push (funcall #',(proto-deserializer msg) ,vval) ,temp))))))))
818                   (t
819                    (cond ((keywordp class)
820                           (collect-deserializer
821                            `((,(make-tag class index))
822                              (multiple-value-bind (,vval idx)
823                                  (deserialize-prim ,class ,vbuf ,vidx)
824                                (setq ,vidx idx)
825                                ,(write-slot vobj field vval)))))
826                          ((typep msg 'protobuf-message)
827                           (collect-deserializer
828                            (if (eq (proto-message-type msg) :group)
829                              `((,(make-tag $wire-type-start-group index))
830                                (multiple-value-bind (,vval idx)
831                                    (deserialize-object ,msg ,vbuf ,vidx  ,vlen
832                                                        ,(make-tag $wire-type-end-group index))
833                                  (setq ,vidx idx)
834                                  ,(write-slot vobj field vval)))
835                              `((,(make-tag $wire-type-string index))
836                                (multiple-value-bind (len idx)
837                                    (decode-uint32 ,vbuf ,vidx)
838                                  (setq ,vidx idx)
839                                  (multiple-value-bind (,vval idx)
840                                      (deserialize-object ,msg ,vbuf ,vidx (i+ ,vidx len) 0)
841                                    (setq ,vidx idx)
842                                    ,(write-slot vobj field vval)))))))
843                          ((typep msg 'protobuf-enum)
844                           (collect-deserializer
845                            `((,(make-tag $wire-type-varint index))
846                              (multiple-value-bind (,vval idx)
847                                  (deserialize-enum '(,@(proto-values msg)) ,vbuf ,vidx)
848                                (setq ,vidx idx)
849                                ,(write-slot vobj field vval)))))
850                          ((typep msg 'protobuf-type-alias)
851                           (let ((class (proto-proto-type msg)))
852                             (collect-deserializer
853                              `((,(make-tag class index))
854                                (multiple-value-bind (,vval idx)
855                                      (deserialize-prim ,class ,vbuf ,vidx)
856                                    (let ((,vval (funcall #',(proto-deserializer msg) ,vval)))
857                                      (setq ,vidx idx)
858                                      ,(write-slot vobj field vval)))))))))))))
859       (let* ((rslots  (delete-duplicates rslots :key #'first))
860              (rfields (mapcar #'first  rslots))
861              (rtemps  (mapcar #'second rslots)))
862         `(defmethod deserialize-object
863              ((,vclass (eql ,message)) ,vbuf &optional ,vidx ,vlen (,vendtag 0))
864            (declare #.$optimize-serialization)
865            (declare (type (simple-array (unsigned-byte 8)) ,vbuf))
866            (let ((,vidx (or ,vidx 0))
867                  (,vlen (or ,vlen (length ,vbuf))))
868              (declare (type fixnum ,vidx ,vlen))
869              (let ((,vobj (make-instance ',(or (proto-alias-for message) (proto-class message))))
870                    ;; Bind the temporary variables that hold repeated slots
871                    ,@rtemps)
872                (loop
873                  (multiple-value-bind (tag idx)
874                      (if (i< ,vidx ,vlen) (decode-uint32 ,vbuf ,vidx) (values 0 ,vidx))
875                    (setq ,vidx idx)
876                    (when (i= tag ,vendtag)
877                      ;; Set the (un)reversed values of the repeated slots
878                      ,@(loop for field in rfields
879                              for temp in rtemps
880                              as slot = (proto-value field)
881                              as writer = (proto-writer field)
882                              collect (cond ((vector-field-p field)
883                                             (if writer
884                                               `(funcall ,writer ,vobj (make-array (length ,temp)
885                                                                         :fill-pointer t :adjustable t
886                                                                         :initial-contents (nreverse ,temp)))
887                                               `(setf (slot-value ,vobj ',slot) (make-array (length ,temp)
888                                                                                  :fill-pointer t :adjustable t
889                                                                                  :initial-contents (nreverse ,temp)))))
890                                            (t
891                                             (if writer
892                                               `(funcall ,writer ,vobj (nreverse ,temp))
893                                               `(setf (slot-value ,vobj ',slot) (nreverse ,temp))))))
894                      (return-from deserialize-object
895                        (values ,vobj ,vidx)))
896                    (case tag
897                      ,@deserializers
898                      (otherwise
899                       (setq ,vidx (skip-element ,vbuf ,vidx tag)))))))))))))
900
901 ;; Note well: keep this in sync with the main 'object-size' method above
902 (defun generate-object-size (message)
903   "Generate an 'object-size' method for the given message."
904   (with-gensyms (vobj vsize vval vclass)
905     (when (null (proto-fields message))
906       (return-from generate-object-size
907         `(defmethod object-size
908              (,vobj (,vclass (eql ,message)) &optional visited)
909          (declare #.$optimize-serialization)
910          (declare (ignorable ,vobj visited))
911          0)))
912     (with-collectors ((sizers collect-sizer))
913       (dolist (field (proto-fields message))
914         (let* ((class  (if (eq (proto-class field) 'boolean) :bool (proto-class field)))
915                (msg    (and class (not (keywordp class))
916                             (or (find-message message class)
917                                 (find-enum message class)
918                                 (find-type-alias message class))))
919                (reader (cond ((proto-reader field)
920                               `(,(proto-reader field) ,vobj))
921                              ((proto-value field)
922                               `(slot-value ,vobj ',(proto-value field)))))
923                (index  (proto-index field)))
924           (when reader
925             (cond ((eq (proto-required field) :repeated)
926                    (let ((iterator (if (vector-field-p field) 'dovector 'dolist)))
927                      (cond ((and (proto-packed field) (packed-type-p class))
928                             (collect-sizer
929                              (let ((tag (make-tag class index)))
930                                `(iincf ,vsize (packed-size ,reader ,class ,tag)))))
931                            ((keywordp class)
932                             (collect-sizer
933                              (let ((tag (make-tag class index)))
934                                `(,iterator (,vval ,reader)
935                                   (iincf ,vsize (prim-size ,vval ,class ,tag))))))
936                            ((typep msg 'protobuf-message)
937                             (collect-sizer
938                              (if (eq (proto-message-type msg) :group)
939                                (let ((tag1 (make-tag $wire-type-start-group index))
940                                      (tag2 (make-tag $wire-type-end-group   index)))
941                                  `(,iterator (,vval ,reader)
942                                     (let ((len (or (and visited (gethash ,vval visited))
943                                                    (object-size ,vval ,msg visited))))
944                                       (iincf ,vsize (length32 ,tag1))
945                                       (iincf ,vsize len)
946                                       (iincf ,vsize ,tag2))))
947                                (let ((tag (make-tag $wire-type-string index)))
948                                  `(,iterator (,vval ,reader)
949                                     (let ((len (or (and visited (gethash ,vval visited))
950                                                    (object-size ,vval ,msg visited))))
951                                       (iincf ,vsize (length32 ,tag))
952                                       (iincf ,vsize (length32 len))
953                                       (iincf ,vsize len)))))))
954                            ((typep msg 'protobuf-enum)
955                             (let ((tag (make-tag $wire-type-varint index)))
956                               (collect-sizer
957                                (if (proto-packed field)
958                                  `(iincf ,vsize (packed-enum-size ,reader '(,@(proto-values msg)) ,tag))
959                                  `(,iterator (,vval ,reader)
960                                     (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag)))))))
961                            ((typep msg 'protobuf-type-alias)
962                             (collect-sizer
963                              (let* ((class (proto-proto-type msg))
964                                     (tag   (make-tag class index)))
965                                `(,iterator (,vval ,reader)
966                                   (let ((,vval (funcall #',(proto-serializer msg) ,vval)))
967                                     (iincf ,vsize (prim-size ,vval ,class ,tag))))))))))
968                   (t
969                    (cond ((keywordp class)
970                           (let ((tag (make-tag class index)))
971                             (collect-sizer
972                              (if (eq class :bool)
973                                (if (or (eq (proto-required field) :required)
974                                        (null (proto-value field)))
975                                  `(let ((,vval ,reader))
976                                     (declare (ignorable ,vval))
977                                     (iincf ,vsize (prim-size ,vval ,class ,tag)))
978                                  `(let ((,vval (cond ((slot-boundp ,vobj ',(proto-value field))
979                                                       ,reader)
980                                                      (t :unbound))))
981                                     (unless (eq ,vval :unbound)
982                                       (iincf ,vsize (prim-size ,vval ,class ,tag)))))
983                                `(let ((,vval ,reader))
984                                   (when ,vval
985                                     (iincf ,vsize (prim-size ,vval ,class ,tag))))))))
986                          ((typep msg 'protobuf-message)
987                           (collect-sizer
988                            (if (eq (proto-message-type msg) :group)
989                              (let ((tag1 (make-tag $wire-type-start-group index))
990                                    (tag2 (make-tag $wire-type-end-group   index)))
991                                `(let ((,vval ,reader))
992                                   (when ,vval
993                                     (let ((len (or (and visited (gethash ,vval visited))
994                                                    (object-size ,vval ,msg visited))))
995                                       (iincf ,vsize (length32 ,tag1))
996                                       (iincf ,vsize len)
997                                       (iincf ,vsize (length32 ,tag2))))))
998                              (let ((tag (make-tag $wire-type-string index)))
999                                `(let ((,vval ,reader))
1000                                   (when ,vval
1001                                     (let ((len (or (and visited (gethash ,vval visited))
1002                                                    (object-size ,vval ,msg visited))))
1003                                       (iincf ,vsize (length32 ,tag))
1004                                       (iincf ,vsize (length32 len))
1005                                       (iincf ,vsize len))))))))
1006                          ((typep msg 'protobuf-enum)
1007                           (let ((tag (make-tag $wire-type-varint index)))
1008                             (collect-sizer
1009                              `(let ((,vval ,reader))
1010                                 (when ,vval
1011                                   (iincf ,vsize (enum-size ,vval '(,@(proto-values msg)) ,tag)))))))
1012                          ((typep msg 'protobuf-type-alias)
1013                           (collect-sizer
1014                            (let* ((class (proto-proto-type msg))
1015                                   (tag   (make-tag class index)))
1016                              `(let ((,vval ,reader))
1017                                 (when ,vval
1018                                   (let ((,vval (funcall #',(proto-serializer msg) ,vval)))
1019                                     (iincf ,vsize (prim-size ,vval ,class ,tag))))))))))))))
1020       `(defmethod object-size
1021            (,vobj (,vclass (eql ,message)) &optional visited)
1022          (declare #.$optimize-serialization)
1023          (declare (ignorable visited))
1024          (let ((,vsize (and visited (gethash ,vobj visited))))
1025            (when ,vsize
1026              (return-from object-size ,vsize)))
1027          (let ((,vsize 0))
1028            (declare (type fixnum ,vsize))
1029            ,@sizers
1030            (when visited
1031              (setf (gethash ,vobj visited) ,vsize))
1032            ,vsize)))))