File size: 3,093 Bytes
d69fc90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
(in-package :cl-user)
(defpackage cl-annot.std
  (:nicknames :annot.std)
  (:use :cl
        :annot.util
        :annot.helper)
  (:export :export*
           :ignore*
           :ignorable*
		   :dynamic-extent*
		   :declaration*
		   :special*
           :type*
           :ftype*
           :optimize*
           :inline*
           :notinline*))
(in-package :annot.std)

(defannotation export* (definition-form)
    (:alias export)
  "Export the definition symbol of DEFINITION-FORM."
  (let ((name (definition-form-symbol definition-form)))
    (if name
        `(progn
           (export ',name)
           ,definition-form)
        definition-form)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; simple one-or-more variables
;;;; 

(defun %declare-list-or-symbol (vars sym)
  (if (listp vars)
      `(declare (,sym ,@vars))
      `(declare (,sym ,vars))))

(defannotation ignore* (vars) (:alias ignore :inline t)
  "Shorthand for (DECLARE (IGNORE ...))."
  (%declare-list-or-symbol vars 'ignore))

(defannotation ignorable* (vars) (:alias ignorable :inline t)
  "Shorthand for (DECLARE (IGNORABLE ...))."
  (%declare-list-or-symbol vars 'ignorable))

(defannotation dynamic-extent* (vars) (:alias dynamic-extent :inline t)
  "Shorthand for (DECLARE (DYNAMIC-EXTENT ...))."
  (%declare-list-or-symbol vars 'dynamic-extent))

(defannotation declaration* (vars) (:alias declaration :inline t)
  "Shorthand for (DECLARE (DECLARATION ...))."
  (%declare-list-or-symbol vars 'declaration))

(defannotation special* (vars) (:alias special :inline t)
  "Shorthand for (DECLARE (SPECIAL ...))."
  (%declare-list-or-symbol vars 'special))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; others
;;;;

(defannotation type* (typespec name)
    (:alias type :arity 2 :inline t)
  "Shorthand for (DECLARE (TYPE ...))."
  (if (consp name)
      ;; TODO
      ()
      `(declare (type ,typespec ,name))))

(defannotation ftype* (typespec name)
    (:alias ftype :arity 2 :inline t)
  "Shorthand for (DECLARE (FTYPE ...))."
  (if (consp name)
      ;; TODO
      ()
      `(declare (ftype ,typespec ,name))))

(defannotation optimize* (quality)
    (:alias optimize :inline t)
  "Shorthand for (DECLARE (OPTIMIZE ...))."
  `(declare (optimize ,quality)))

(defannotation inline* (name)
    (:alias inline :inline t)
  "Shorthand for (DECLARE (INLINE ...))."
  (let ((symbol (definition-form-symbol name))
        (type (definition-form-type name)))
    (if (and symbol
             (member type
                     '(defun defmethod)))
        `(progn
           (declaim (inline ,symbol))
           ,name)
        `(declare (inline ,name)))))

(defannotation notinline* (name)
    (:alias notinline :inline t)
  "Shorthand for (DECLARE (NOTINLINE ...))."
  (let ((symbol (definition-form-symbol name))
        (type (definition-form-type name)))
    (if (and symbol
             (member type
                     '(defun defmethod)))
        `(progn
           (declaim (notinline ,symbol))
           ,name)
        `(declare (notinline ,name)))))