+(defun compiler-layout-ready-p (name)
+ (let ((layout (info :type :compiler-layout name)))
+ (and layout (typep (layout-info layout) 'defstruct-description))))
+
+(sb!xc:defmacro %make-structure-instance-macro (dd slot-specs &rest slot-vars)
+ (if (compiler-layout-ready-p (dd-name dd))
+ `(truly-the ,(dd-name dd)
+ (%make-structure-instance ,dd ,slot-specs ,@slot-vars))
+ ;; Non-toplevel defstructs don't have a layout at compile time,
+ ;; so we need to construct the actual function at runtime -- but
+ ;; we cache it at the call site, so that we don't perform quite
+ ;; so horribly.
+ `(let* ((cell (load-time-value (list nil)))
+ (fun (car cell)))
+ (if (functionp fun)
+ (funcall fun ,@slot-vars)
+ (funcall (setf (car cell)
+ (%make-structure-instance-allocator ,dd ,slot-specs))
+ ,@slot-vars)))))
+
+(declaim (ftype (sfunction (defstruct-description list) function)
+ %make-structure-instance-allocator))
+(defun %make-structure-instance-allocator (dd slot-specs)
+ (let ((vars (make-gensym-list (length slot-specs))))
+ (values (compile nil `(lambda (,@vars)
+ (%make-structure-instance-macro ,dd ',slot-specs ,@vars))))))
+
+(defun %make-funcallable-structure-instance-allocator (dd slot-specs)
+ (when slot-specs
+ (bug "funcallable-structure-instance allocation with slots unimplemented"))
+ (let ((name (dd-name dd))
+ (length (dd-length dd))
+ (nobject (gensym "OBJECT")))
+ (values
+ (compile nil `(lambda ()
+ (let ((,nobject (%make-funcallable-instance ,length)))
+ (setf (%funcallable-instance-layout ,nobject)
+ (%delayed-get-compiler-layout ,name))
+ ,nobject))))))
+