r3002: sbcl additions
[clsql.git] / base / cmucl-compat.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          cmucl-compat.sql
6 ;;;; Purpose:       Compatiblity library for CMUCL functions
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; $Id: cmucl-compat.lisp,v 1.2 2002/10/14 04:09:02 kevin Exp $
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :cl-user)
21
22 (defpackage :cmucl-compat
23   (:use #:common-lisp)
24   (:export
25    #:shrink-vector
26    #:make-sequence-of-type
27    #:result-type-or-lose
28    #:required-argument
29    ))
30 (in-package :cmucl-compat)
31
32 #+cmu
33 (defmacro required-argument ()
34   `(ext:required-argument))
35
36 #-cmu
37 (defun required-argument ()
38   (error "~&A required keyword argument was not supplied"))
39
40 #+cmu
41 (defmacro shrink-vector (vec len)
42   `(lisp::shrink-vector ,vec ,len))
43
44 #+sbcl
45 (defmacro shrink-vector (vec len)
46   `(sb-kernel::shrink-vector ,vec ,len))
47
48 #-(or cmu sbcl)
49 (defmacro shrink-vector (vec len)
50   "Shrinks a vector. Optimized if vector has a fill pointer.
51 Needs to be a macro to overwrite value of VEC."
52   (let ((new-vec (gensym)))
53     `(cond
54       ((adjustable-array-p ,vec)
55        (adjust-array ,vec ,len))
56       ((typep ,vec 'simple-array)
57        (let ((,new-vec (make-array ,len :element-type
58                                    (array-element-type ,vec))))
59          (dotimes (i ,len)
60            (declare (fixnum i))
61            (setf (aref ,new-vec i) (aref ,vec i)))
62          (setq ,vec ,new-vec)))
63       ((typep ,vec 'vector)
64         (setf (fill-pointer ,vec) ,len)
65         ,vec)
66       (t
67        (error "Unable to shrink vector ~S which is type-of ~S" ,vec (type-of ,vec))) 
68        )))
69
70
71
72 #-(or cmu sbcl)
73 (defun make-sequence-of-type (type length)
74   "Returns a sequence of the given TYPE and LENGTH."
75   (declare (fixnum length))
76   (case type
77     (list 
78      (make-list length))
79     ((bit-vector simple-bit-vector) 
80      (make-array length :element-type '(mod 2)))
81     ((string simple-string base-string simple-base-string)
82      (make-string length))
83     (simple-vector 
84      (make-array length))
85     ((array simple-array vector)
86      (if (listp type)
87          (make-array length :element-type (cadr type))
88        (make-array length)))
89     (t
90      (make-sequence-of-type (result-type-or-lose type t) length))))
91
92
93 #+cmu
94 (if (fboundp 'lisp::make-sequence-of-type)
95     (defun make-sequence-of-type (type len)
96       (lisp::make-sequence-of-type type len))
97   (defun make-sequence-of-type (type len)
98     (system::make-sequence-of-type type len)))
99
100 #+sbcl
101 (defun make-sequence-of-type (type len)
102   (sb-impl::make-sequence-of-type type len))
103
104 #-(or cmu sbcl)
105 (defun result-type-or-lose (type nil-ok)
106   (unless (or type nil-ok)
107     (error "NIL output type invalid for this sequence function"))
108   (case type
109     ((list cons)
110      'list)
111     ((string simple-string base-string simple-base-string)
112      'string)
113     (simple-vector
114      'simple-vector)
115     (vector
116      'vector)
117     (t
118      (error "~S is a bad type specifier for sequence functions." type))
119     ))
120
121 #+cmu
122 (defun result-type-or-lose (type nil-ok)
123   (lisp::result-type-or-lose type nil-ok))
124
125 #+sbcl
126 (defun result-type-or-lose (type nil-ok)
127   (sb-impl::result-type-or-lose type nil-ok))