Debian Codegraph @

src/cmp/cmptype-prop.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; Copyright (c) 2009, Juan Jose Garcia-Ripoll.
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8
9 ;;;;
10 ;;;; CMPTYPE-PROP -- Type propagation basic routines and database
11 ;;;;
12
13 (in-package "COMPILER")
14
15 (defun infer-arg-and-return-types (fname forms &optional (env *cmp-env*))
16 (let ((found (gethash fname *p0-dispatch-table*))
17 arg-types
18 (return-type '(VALUES &REST T)))
19 (cond (found
20 (multiple-value-setq (arg-types return-type)
21 (apply found fname (mapcar #'location-primary-type forms))))
22 ((multiple-value-setq (arg-types found)
23 (get-arg-types fname env))
24 (setf return-type (or (get-return-type fname) return-type))))
25 (values arg-types return-type found)))
26
27 (defun enforce-types (fname arg-types arguments)
28 (do* ((types arg-types (rest types))
29 (args arguments (rest args))
30 (i 1 (1+ i))
31 (in-optionals nil))
32 ((endp types)
33 (when types
34 (cmpwarn "Too many arguments passed to ~A" fname)))
35 (let ((expected-type (first types)))
36 (cond ((member expected-type '(* &rest &key &allow-other-keys) :test #'eq)
37 (return))
38 ((eq expected-type '&optional)
39 (when in-optionals
40 (cmpwarn "Syntax error in type proclamation for function ~A.~&~A"
41 fname arg-types))
42 (setf in-optionals t
43 types (rest types)))
44 ((endp args)
45 (unless in-optionals
46 (cmpwarn "Too few arguments for proclaimed function ~A" fname))
47 (return))
48 (t
49 (let* ((value (first args))
50 (actual-type (location-primary-type value))
51 (intersection (type-and actual-type expected-type)))
52 (unless intersection
53 (cmpwarn-style "The argument ~d of function ~a has type~&~4T~A~&instead of expected~&~4T~A"
54 i fname actual-type expected-type))
55 (when (zerop (cmp-env-optimization 'safety))
56 (setf (c1form-type value) intersection))))))))
57
58 (defun propagate-types (fname forms)
59 (multiple-value-bind (arg-types return-type found)
60 (infer-arg-and-return-types fname forms)
61 (when found
62 (enforce-types fname arg-types forms))
63 return-type))
64
65 (defmacro def-type-propagator (fname lambda-list &body body)
66 (unless (member '&rest lambda-list)
67 (let ((var (gensym)))
68 (setf lambda-list (append lambda-list (list '&rest var))
69 body (list* `(declare (ignorable ,var)) body))))
70 `(setf (gethash ',fname *p0-dispatch-table*)
71 #'(lambda ,lambda-list (declare (ignorable ,(first lambda-list))) ,@body)))
72
73 (defun copy-type-propagator (orig dest-list)
74 (loop with function = (gethash orig *p0-dispatch-table*)
75 for name in dest-list
76 when function
77 do (setf (gethash name *p0-dispatch-table*) function)))
78
79