-
-
-(deftype-method transalte-type-spec static (type-spec)
- (translate-type-spec (second type-spec)))
-
-(deftype-method size-of static (type-spec)
- (size-of type-spec))
-
-(deftype-method translate-to-alien static (type-spec expr &optional copy)
- (declare (ignore copy))
- (translate-to-alien (second type-spec) expr nil))
-
-(deftype-method translate-from-alien static (type-spec alien &optional alloc)
- (declare (ignore alloc))
- (translate-from-alien (second type-spec) alien nil))
-
-(deftype-method cleanup-alien static (type-spec alien &optional copied)
- (declare (ignore copied))
- (cleanup-alien type-spec alien nil))
-
-
-
-;;;; Enum and flags type
-
-(defun map-mappings (args op)
- (let ((current-value 0))
- (map
- 'list
- #'(lambda (mapping)
- (destructuring-bind (symbol &optional (value current-value))
- (mklist mapping)
- (setf current-value (1+ value))
- (case op
- (:enum-int (list symbol value))
- (:flags-int (list symbol (ash 1 value)))
- (:int-enum (list value symbol))
- (:int-flags (list (ash 1 value) symbol))
- (:symbols symbol))))
- (if (integerp (first args))
- (rest args)
- args))))
-
-
-(lisp:deftype enum (&rest args)
- `(member ,@(map-mappings args :symbols)))
-
-(deftype-method translate-type-spec enum (type-spec)
- (let ((args (cdr (type-expand-to 'enum type-spec))))
- (if (integerp (first args))
- (translate-type-spec `(signed ,(first args)))
- (translate-type-spec 'signed))))
-
-(deftype-method size-of enum (type-spec)
- (let ((args (cdr (type-expand-to 'enum type-spec))))
- (if (integerp (first args))
- (size-of `(signed ,(first args)))
- (size-of 'signed))))
-
-(deftype-method translate-to-alien enum (type-spec expr &optional copy)
- (declare (ignore copy))
- (let ((args (cdr (type-expand-to 'enum type-spec))))
- `(let ((expr ,expr))
- (if (integerp expr)
- expr
- (ecase expr
- ,@(map-mappings args :enum-int))))))
-
-(deftype-method translate-from-alien enum (type-spec expr &optional alloc)
- (declare (ignore alloc))
- (destructuring-bind (name &rest args) (type-expand-to 'enum type-spec)
- (declare (ignore name))
- `(ecase ,expr
- ,@(map-mappings args :int-enum))))
-
-
-(lisp:deftype flags (&rest args)
- `(or
- null
- (cons
- (member ,@(map-mappings args :symbols))
- list)))
-
-(deftype-method translate-type-spec flags (type-spec)
- (let ((args (cdr (type-expand-to 'flags type-spec))))
- (if (integerp (first args))
- (translate-type-spec `(signed ,(first args)))
- (translate-type-spec 'signed))))
-
-(deftype-method size-of flags (type-spec)
- (let ((args (cdr (type-expand-to 'flags type-spec))))
- (if (integerp (first args))
- (size-of `(signed ,(first args)))
- (size-of 'signed))))
-
-(deftype-method translate-to-alien flags (type-spec expr &optional copy)
- (declare (ignore copy))
- (destructuring-bind (name &rest args) (type-expand-to 'flags type-spec)
- (declare (ignore name))
- (let ((mappings (map-mappings args :flags-int)))
- `(let ((value 0))
- (dolist (flag ,expr value)
- (setq value (logior value (second (assoc flag ',mappings)))))))))
-
-(deftype-method translate-from-alien flags (type-spec expr &optional alloc)
- (declare (ignore alloc))
- (destructuring-bind (name &rest args) (type-expand-to 'flags type-spec)
- (declare (ignore name))
- (let ((mappings (map-mappings args :int-flags)))
- `(let ((result nil))
- (dolist (mapping ',mappings result)
- (unless (zerop (logand ,expr (first mapping)))
- (push (second mapping) result)))))))