| 1 | ;;; -*-lisp-*- |
| 2 | ;;; |
| 3 | ;;; Method combination implementation |
| 4 | ;;; |
| 5 | ;;; (c) 2009 Straylight/Edgeware |
| 6 | ;;; |
| 7 | |
| 8 | ;;;----- Licensing notice --------------------------------------------------- |
| 9 | ;;; |
| 10 | ;;; This file is part of the Sensible Object Design, an object system for C. |
| 11 | ;;; |
| 12 | ;;; SOD is free software; you can redistribute it and/or modify |
| 13 | ;;; it under the terms of the GNU General Public License as published by |
| 14 | ;;; the Free Software Foundation; either version 2 of the License, or |
| 15 | ;;; (at your option) any later version. |
| 16 | ;;; |
| 17 | ;;; SOD is distributed in the hope that it will be useful, |
| 18 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | ;;; GNU General Public License for more details. |
| 21 | ;;; |
| 22 | ;;; You should have received a copy of the GNU General Public License |
| 23 | ;;; along with SOD; if not, write to the Free Software Foundation, |
| 24 | ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | |
| 26 | (cl:in-package #:sod) |
| 27 | |
| 28 | ;;;-------------------------------------------------------------------------- |
| 29 | ;;; Message classes. |
| 30 | |
| 31 | (export 'basic-message) |
| 32 | (defclass basic-message (sod-message) |
| 33 | ((argument-tail :type list :reader sod-message-argument-tail)) |
| 34 | (:documentation |
| 35 | "Base class for built-in message classes. |
| 36 | |
| 37 | Provides the basic functionality for the built-in method combinations. |
| 38 | This is a separate class so that `special effect' messages can avoid |
| 39 | inheriting its default behaviour. |
| 40 | |
| 41 | The function type protocol is implemented on `basic-message' using slot |
| 42 | reader methods. The actual values are computed on demand.")) |
| 43 | |
| 44 | (define-on-demand-slot basic-message argument-tail (message) |
| 45 | (let ((seq 0)) |
| 46 | (mapcar (lambda (arg) |
| 47 | (if (or (eq arg :ellipsis) (argument-name arg)) arg |
| 48 | (make-argument (make-instance 'temporary-argument |
| 49 | :tag (prog1 seq |
| 50 | (incf seq))) |
| 51 | (argument-type arg)))) |
| 52 | (c-function-arguments (sod-message-type message))))) |
| 53 | |
| 54 | (defmethod sod-message-method-class |
| 55 | ((message basic-message) (class sod-class) pset) |
| 56 | (let ((role (get-property pset :role :keyword nil))) |
| 57 | (case role |
| 58 | ((:before :after) 'daemon-direct-method) |
| 59 | (:around 'delegating-direct-method) |
| 60 | ((nil) (error "How odd: a primary method slipped through the net")) |
| 61 | (t (error "Unknown method role ~A" role))))) |
| 62 | |
| 63 | (export 'simple-message) |
| 64 | (defclass simple-message (basic-message) |
| 65 | () |
| 66 | (:documentation |
| 67 | "Base class for messages with `simple' method combinations. |
| 68 | |
| 69 | A simple method combination is one which has only one method role other |
| 70 | than the `before', `after' and `around' methods provided by |
| 71 | `basic-message'. We call these `primary' methods, and the programmer |
| 72 | designates them by not specifying an explicit role. |
| 73 | |
| 74 | If the programmer doesn't define any primary methods then the effective |
| 75 | method is null -- i.e., the method entry pointer shows up as a null |
| 76 | pointer.")) |
| 77 | |
| 78 | (defmethod sod-message-method-class |
| 79 | ((message simple-message) (class sod-class) pset) |
| 80 | (if (get-property pset :role :keyword nil) |
| 81 | (call-next-method) |
| 82 | (primary-method-class message))) |
| 83 | |
| 84 | (defmethod primary-method-class ((message simple-message)) |
| 85 | 'basic-direct-method) |
| 86 | |
| 87 | ;;;-------------------------------------------------------------------------- |
| 88 | ;;; Direct method classes. |
| 89 | |
| 90 | (export '(basic-direct-method sod-method-role)) |
| 91 | (defclass basic-direct-method (sod-method) |
| 92 | ((role :initarg :role :type symbol :reader sod-method-role) |
| 93 | (function-type :type c-function-type :reader sod-method-function-type)) |
| 94 | (:documentation |
| 95 | "Base class for built-in direct method classes. |
| 96 | |
| 97 | Provides the basic functionality for the built-in direct-method classes. |
| 98 | This is a separate class so that `special effect' methods can avoid |
| 99 | inheriting its default behaviour and slots. |
| 100 | |
| 101 | A basic method can be assigned a `role', which may be set either as an |
| 102 | initarg or using the `:role' property. Roles are used for method |
| 103 | categorization. |
| 104 | |
| 105 | The function type protocol is implemented on `basic-direct-method' using |
| 106 | slot reader methods.")) |
| 107 | |
| 108 | (defmethod shared-initialize :after |
| 109 | ((method basic-direct-method) slot-names &key pset) |
| 110 | (declare (ignore slot-names)) |
| 111 | (default-slot (method 'role) (get-property pset :role :keyword nil))) |
| 112 | |
| 113 | (defun direct-method-suppliedp-struct-tag (direct-method) |
| 114 | (with-slots ((class %class) role message) direct-method |
| 115 | (format nil "~A__~@[~(~A~)_~]suppliedp_~A__~A" |
| 116 | class role |
| 117 | (sod-class-nickname (sod-message-class message)) |
| 118 | (sod-message-name message)))) |
| 119 | |
| 120 | (defun effective-method-keyword-struct-tag (effective-method) |
| 121 | (with-slots ((class %class) message) effective-method |
| 122 | (format nil "~A__keywords_~A__~A" |
| 123 | class |
| 124 | (sod-class-nickname (sod-message-class message)) |
| 125 | (sod-message-name message)))) |
| 126 | |
| 127 | (defun fix-up-keyword-method-args (method args) |
| 128 | "Adjust the ARGS to include METHOD's `suppliedp' and keyword arguments. |
| 129 | |
| 130 | Return the adjusted list. The `suppliedp' argument, if any, is prepended |
| 131 | to the list; the keyword arguments are added to the end. |
| 132 | |
| 133 | (The input ARGS list is not actually modified.)" |
| 134 | (let* ((type (sod-method-type method)) |
| 135 | (keys (c-function-keywords type)) |
| 136 | (tag (direct-method-suppliedp-struct-tag method))) |
| 137 | (append (and keys |
| 138 | (list (make-argument "suppliedp" (c-type (struct tag))))) |
| 139 | args |
| 140 | (mapcar (lambda (key) |
| 141 | (make-argument (argument-name key) |
| 142 | (argument-type key))) |
| 143 | keys)))) |
| 144 | |
| 145 | (define-on-demand-slot basic-direct-method function-type (method) |
| 146 | (let* ((message (sod-method-message method)) |
| 147 | (type (sod-method-type method)) |
| 148 | (method-args (c-function-arguments type))) |
| 149 | (when (keyword-message-p message) |
| 150 | (setf method-args (fix-up-keyword-method-args method method-args))) |
| 151 | (c-type (fun (lisp (c-type-subtype type)) |
| 152 | ("me" (* (class (sod-method-class method)))) |
| 153 | . method-args)))) |
| 154 | |
| 155 | (defmethod sod-method-description ((method basic-direct-method)) |
| 156 | (with-slots (role) method |
| 157 | (if role (string-downcase role) |
| 158 | "primary"))) |
| 159 | |
| 160 | (defmethod sod-method-function-name ((method basic-direct-method)) |
| 161 | (with-slots ((class %class) role message) method |
| 162 | (format nil "~A__~@[~(~A~)_~]method_~A__~A" class role |
| 163 | (sod-class-nickname (sod-message-class message)) |
| 164 | (sod-message-name message)))) |
| 165 | |
| 166 | (export 'daemon-direct-method) |
| 167 | (defclass daemon-direct-method (basic-direct-method) |
| 168 | () |
| 169 | (:documentation |
| 170 | "A daemon direct method is invoked for side effects and cannot override. |
| 171 | |
| 172 | This is the direct method class for `before' and `after' methods, which |
| 173 | cannot choose to override the remaining methods and are not involved in |
| 174 | the computation of the final result. |
| 175 | |
| 176 | In C terms, a daemon method must return `void', and is not passed a |
| 177 | `next_method' pointer.")) |
| 178 | |
| 179 | (defmethod check-method-type ((method daemon-direct-method) |
| 180 | (message sod-message) |
| 181 | (type c-function-type)) |
| 182 | (with-slots ((msgtype %type)) message |
| 183 | (check-method-return-type type c-type-void) |
| 184 | (check-method-argument-lists type msgtype))) |
| 185 | |
| 186 | (export 'delegating-direct-method) |
| 187 | (defclass delegating-direct-method (basic-direct-method) |
| 188 | ((next-method-type :type c-function-type |
| 189 | :reader sod-method-next-method-type)) |
| 190 | (:documentation |
| 191 | "A delegating direct method can choose to override other methods. |
| 192 | |
| 193 | This is the direct method class for `around' and standard-method- |
| 194 | combination primary methods, which are given the choice of computing the |
| 195 | entire method's result or delegating to (usually) less-specific methods. |
| 196 | |
| 197 | In C terms, a delegating method is passed a `next_method' pointer so that |
| 198 | it can delegate part of its behaviour. (A delegating direct method for a |
| 199 | varargs message is also given an additional `va_list' argument, |
| 200 | conventionally named `sod__ap_master', which it is expected to pass on to |
| 201 | its `next_method' function if necessary.) |
| 202 | |
| 203 | The function type protocol is implemented on `delegating-direct-method' |
| 204 | using slot reader methods..")) |
| 205 | |
| 206 | (define-on-demand-slot delegating-direct-method next-method-type (method) |
| 207 | (let* ((message (sod-method-message method)) |
| 208 | (return-type (c-type-subtype (sod-message-type message))) |
| 209 | (msgargs (sod-message-argument-tail message)) |
| 210 | (arguments (cond ((varargs-message-p message) |
| 211 | (cons (make-argument *sod-master-ap* |
| 212 | c-type-va-list) |
| 213 | (butlast msgargs))) |
| 214 | ((keyword-message-p message) |
| 215 | (cons (make-argument *sod-keywords* |
| 216 | (c-type (* (void :const)))) |
| 217 | msgargs)) |
| 218 | (t |
| 219 | msgargs)))) |
| 220 | (c-type (fun (lisp return-type) |
| 221 | ("me" (* (class (sod-method-class method)))) |
| 222 | . arguments)))) |
| 223 | |
| 224 | (define-on-demand-slot delegating-direct-method function-type (method) |
| 225 | (let* ((message (sod-method-message method)) |
| 226 | (type (sod-method-type method)) |
| 227 | (method-args (c-function-arguments type)) |
| 228 | (next-method-arg (make-argument |
| 229 | "next_method" |
| 230 | (make-pointer-type |
| 231 | (commentify-function-type |
| 232 | (sod-method-next-method-type method)))))) |
| 233 | (cond ((varargs-message-p message) |
| 234 | (push (make-argument *sod-master-ap* c-type-va-list) |
| 235 | method-args) |
| 236 | (push next-method-arg method-args)) |
| 237 | ((keyword-message-p message) |
| 238 | (push (make-argument *sod-keywords* (c-type (* (void :const)))) |
| 239 | method-args) |
| 240 | (push next-method-arg method-args) |
| 241 | (setf method-args |
| 242 | (fix-up-keyword-method-args method method-args))) |
| 243 | (t |
| 244 | (push next-method-arg method-args))) |
| 245 | (c-type (fun (lisp (c-type-subtype type)) |
| 246 | ("me" (* (class (sod-method-class method)))) |
| 247 | . method-args)))) |
| 248 | |
| 249 | ;;;-------------------------------------------------------------------------- |
| 250 | ;;; Effective method classes. |
| 251 | |
| 252 | (defmethod sod-message-keyword-argument-lists |
| 253 | ((message sod-message) (class sod-class) direct-methods state) |
| 254 | (and (keyword-message-p message) |
| 255 | (cons (cons (lambda (arg) |
| 256 | (let ((class (sod-message-class message))) |
| 257 | (info-with-location |
| 258 | message "Type `~A' declared in message ~ |
| 259 | definition in `~A' (here)" |
| 260 | (argument-type arg) class) |
| 261 | (report-inheritance-path state class))) |
| 262 | (c-function-keywords (sod-message-type message))) |
| 263 | (mapcar (lambda (method) |
| 264 | (cons (lambda (arg) |
| 265 | (let ((class (sod-method-class method))) |
| 266 | (info-with-location |
| 267 | method "Type `~A' declared in ~A direct ~ |
| 268 | method of `~A' (defined here)" |
| 269 | (argument-type arg) |
| 270 | (sod-method-description method) class) |
| 271 | (report-inheritance-path state class))) |
| 272 | (c-function-keywords (sod-method-type method)))) |
| 273 | direct-methods)))) |
| 274 | |
| 275 | (defmethod sod-message-check-methods |
| 276 | ((message sod-message) (class sod-class) direct-methods) |
| 277 | (compute-effective-method-keyword-arguments message class direct-methods)) |
| 278 | |
| 279 | (defmethod shared-initialize :after |
| 280 | ((method effective-method) slot-names &key direct-methods) |
| 281 | (declare (ignore slot-names)) |
| 282 | |
| 283 | ;; Set the keyword argument list. Blame the class as a whole for mismatch |
| 284 | ;; errors, because they're fundamentally a non-local problem about the |
| 285 | ;; class construction. |
| 286 | (with-slots ((class %class) message keywords) method |
| 287 | (setf keywords |
| 288 | (compute-effective-method-keyword-arguments message |
| 289 | class |
| 290 | direct-methods)))) |
| 291 | |
| 292 | (export '(basic-effective-method |
| 293 | effective-method-around-methods effective-method-before-methods |
| 294 | effective-method-after-methods effective-method-functions)) |
| 295 | (defclass basic-effective-method (effective-method) |
| 296 | ((around-methods :initarg :around-methods :initform nil |
| 297 | :type list :reader effective-method-around-methods) |
| 298 | (before-methods :initarg :before-methods :initform nil |
| 299 | :type list :reader effective-method-before-methods) |
| 300 | (after-methods :initarg :after-methods :initform nil |
| 301 | :type list :reader effective-method-after-methods) |
| 302 | (basic-argument-names :type list |
| 303 | :reader effective-method-basic-argument-names) |
| 304 | (functions :type list :reader effective-method-functions)) |
| 305 | (:documentation |
| 306 | "Base class for built-in effective method classes. |
| 307 | |
| 308 | This class maintains lists of the applicable `before', `after' and |
| 309 | `around' methods and provides behaviour for invoking these methods |
| 310 | correctly. |
| 311 | |
| 312 | The argument names protocol is implemented on `basic-effective-method' |
| 313 | using a slot reader method.")) |
| 314 | |
| 315 | (define-on-demand-slot basic-effective-method basic-argument-names (method) |
| 316 | (let* ((message (effective-method-message method)) |
| 317 | (raw-tail (sod-message-argument-tail message))) |
| 318 | (mapcar #'argument-name (reify-variable-argument-tail raw-tail)))) |
| 319 | |
| 320 | (defmethod effective-method-function-name ((method effective-method)) |
| 321 | (let* ((class (effective-method-class method)) |
| 322 | (message (effective-method-message method)) |
| 323 | (message-class (sod-message-class message))) |
| 324 | (format nil "~A__emethod_~A__~A" |
| 325 | class |
| 326 | (sod-class-nickname message-class) |
| 327 | (sod-message-name message)))) |
| 328 | |
| 329 | (define-on-demand-slot basic-effective-method functions (method) |
| 330 | (compute-method-entry-functions method)) |
| 331 | |
| 332 | (export 'simple-effective-method) |
| 333 | (defclass simple-effective-method (basic-effective-method) |
| 334 | ((primary-methods :initarg :primary-methods :initform nil |
| 335 | :type list :reader effective-method-primary-methods)) |
| 336 | (:documentation |
| 337 | "Effective method counterpart to `simple-message'.")) |
| 338 | |
| 339 | (defmethod shared-initialize :after |
| 340 | ((method simple-effective-method) slot-names &key direct-methods) |
| 341 | (declare (ignore slot-names)) |
| 342 | (categorize (method direct-methods :bind ((role (sod-method-role method)))) |
| 343 | ((primary (null role)) |
| 344 | (before (eq role :before)) |
| 345 | (after (eq role :after)) |
| 346 | (around (eq role :around))) |
| 347 | (with-slots (primary-methods before-methods after-methods around-methods) |
| 348 | method |
| 349 | (setf primary-methods primary |
| 350 | before-methods before |
| 351 | after-methods (reverse after) |
| 352 | around-methods around)))) |
| 353 | |
| 354 | ;;;-------------------------------------------------------------------------- |
| 355 | ;;; Code generation. |
| 356 | |
| 357 | (defmethod shared-initialize :after |
| 358 | ((codegen method-codegen) slot-names &key) |
| 359 | (declare (ignore slot-names)) |
| 360 | (with-slots (message target) codegen |
| 361 | (setf target |
| 362 | (if (eq (c-type-subtype (sod-message-type message)) c-type-void) |
| 363 | :void |
| 364 | :return)))) |
| 365 | |
| 366 | ;;;-------------------------------------------------------------------------- |
| 367 | ;;; Invoking direct methods. |
| 368 | |
| 369 | (export 'basic-effective-method-body) |
| 370 | (defun basic-effective-method-body (codegen target method body) |
| 371 | "Build the common method-invocation structure. |
| 372 | |
| 373 | Writes to CODEGEN some basic method-invocation instructions. It invokes |
| 374 | the `around' methods, from most- to least-specific. If they all delegate, |
| 375 | then the `before' methods are run, most-specific first; next, the |
| 376 | instructions generated by BODY (invoked with a target argument); then, the |
| 377 | `after' methods are run, least-specific first; and, finally, the value |
| 378 | delivered by the BODY is returned to the `around' methods. The result |
| 379 | returned by the outermost `around' method -- or, if there are none, |
| 380 | delivered by the BODY -- is finally delivered to the TARGET." |
| 381 | |
| 382 | (with-slots (message (class %class) |
| 383 | before-methods after-methods around-methods) |
| 384 | method |
| 385 | (let* ((message-type (sod-message-type message)) |
| 386 | (return-type (c-type-subtype message-type)) |
| 387 | (basic-tail (effective-method-basic-argument-names method))) |
| 388 | (flet ((method-kernel (target) |
| 389 | (dolist (before before-methods) |
| 390 | (invoke-method codegen :void basic-tail before)) |
| 391 | (if (null after-methods) |
| 392 | (funcall body target) |
| 393 | (convert-stmts codegen target return-type |
| 394 | (lambda (target) |
| 395 | (funcall body target) |
| 396 | (dolist (after (reverse after-methods)) |
| 397 | (invoke-method codegen :void |
| 398 | basic-tail after))))))) |
| 399 | (invoke-delegation-chain codegen target basic-tail |
| 400 | around-methods #'method-kernel))))) |
| 401 | |
| 402 | ;;;-------------------------------------------------------------------------- |
| 403 | ;;; Method entry points. |
| 404 | |
| 405 | (defparameter *method-entry-inline-threshold* 200 |
| 406 | "Threshold below which effective method bodies are inlined into entries. |
| 407 | |
| 408 | After the effective method body has been computed, we calculate its |
| 409 | metric, multiply by the number of entries we need to generate, and compare |
| 410 | it with this threshold. If the metric is below the threshold then we |
| 411 | fold the method body into the entry functions; otherwise we split the |
| 412 | effective method out into its own function.") |
| 413 | |
| 414 | (defmethod method-entry-function-name |
| 415 | ((method effective-method) (chain-head sod-class) role) |
| 416 | (let* ((class (effective-method-class method)) |
| 417 | (message (effective-method-message method)) |
| 418 | (message-class (sod-message-class message))) |
| 419 | (if (or (not (slot-boundp method 'functions)) |
| 420 | (slot-value method 'functions)) |
| 421 | (format nil "~A__mentry~@[__~(~A~)~]_~A__~A__chain_~A" |
| 422 | class role |
| 423 | (sod-class-nickname message-class) |
| 424 | (sod-message-name message) |
| 425 | (sod-class-nickname chain-head)) |
| 426 | *null-pointer*))) |
| 427 | |
| 428 | (defmethod method-entry-slot-name ((entry method-entry)) |
| 429 | (let* ((method (method-entry-effective-method entry)) |
| 430 | (message (effective-method-message method)) |
| 431 | (name (sod-message-name message)) |
| 432 | (role (method-entry-role entry))) |
| 433 | (method-entry-slot-name-by-role entry role name))) |
| 434 | |
| 435 | (defmethod method-entry-function-type ((entry method-entry)) |
| 436 | (let* ((method (method-entry-effective-method entry)) |
| 437 | (message (effective-method-message method)) |
| 438 | (type (sod-message-type message)) |
| 439 | (keywordsp (keyword-message-p message)) |
| 440 | (raw-tail (append (sod-message-argument-tail message) |
| 441 | (and keywordsp (list :ellipsis)))) |
| 442 | (tail (ecase (method-entry-role entry) |
| 443 | ((nil) raw-tail) |
| 444 | (:valist (reify-variable-argument-tail raw-tail))))) |
| 445 | (c-type (fun (lisp (c-type-subtype type)) |
| 446 | ("me" (* (class (method-entry-chain-tail entry)))) |
| 447 | . tail)))) |
| 448 | |
| 449 | (defgeneric effective-method-keyword-parser-function-name (method) |
| 450 | (:documentation |
| 451 | "Return the name of the keyword-parsing function for an effective METHOD. |
| 452 | |
| 453 | See `make-keyword-parser-function' for details of what this function |
| 454 | actually does.")) |
| 455 | |
| 456 | (defmethod effective-method-keyword-parser-function-name |
| 457 | ((method basic-effective-method)) |
| 458 | (with-slots ((class %class) message) method |
| 459 | (format nil "~A__kwparse_~A__~A" |
| 460 | class |
| 461 | (sod-class-nickname (sod-message-class message)) |
| 462 | (sod-message-name message)))) |
| 463 | |
| 464 | (defun make-keyword-parser-function (codegen method tag set keywords) |
| 465 | "Construct and return a keyword-argument parsing function. |
| 466 | |
| 467 | The function is contributed to the CODEGEN, with the name constructed from |
| 468 | the effective METHOD. It will populate an argument structure with the |
| 469 | given TAG. In case of error, it will mention the name SET in its report. |
| 470 | The KEYWORDS are a list of `argument' objects naming the keywords to be |
| 471 | accepted. |
| 472 | |
| 473 | The generated function has the signature |
| 474 | |
| 475 | void NAME(struct TAG *kw, va_list *ap, struct kwval *v, size_t n) |
| 476 | |
| 477 | It assumes that AP includes the first keyword name. (This makes it |
| 478 | different from the keyword-parsing functions generated by the |
| 479 | `KWSET_PARSEFN' macro, but this interface is slightly more convenient and |
| 480 | we don't need to cope with functions which accept no required |
| 481 | arguments.)" |
| 482 | |
| 483 | ;; Let's start, then. |
| 484 | (codegen-push codegen) |
| 485 | |
| 486 | ;; Set up the local variables we'll need. |
| 487 | (macrolet ((var (name type) |
| 488 | `(ensure-var codegen ,name (c-type ,type)))) |
| 489 | (var "k" const-string) |
| 490 | (var "aap" (* va-list)) |
| 491 | (var "t" (* (struct "kwtab" :const))) |
| 492 | (var "vv" (* (struct "kwval" :const))) |
| 493 | (var "nn" size-t)) |
| 494 | |
| 495 | (flet ((call (target func &rest args) |
| 496 | ;; Call FUNC with ARGS; return result in TARGET. |
| 497 | |
| 498 | (apply #'deliver-call codegen target func args)) |
| 499 | |
| 500 | (convert (target type) |
| 501 | ;; Fetch the object of TYPE pointed to by `v->val', and store it |
| 502 | ;; in TARGET. |
| 503 | |
| 504 | (deliver-expr codegen target |
| 505 | (format nil "*(~A)v->val" |
| 506 | (make-pointer-type (qualify-c-type |
| 507 | type (list :const)))))) |
| 508 | |
| 509 | (namecheck (var name conseq alt) |
| 510 | ;; Return an instruction: if VAR matches the string NAME then do |
| 511 | ;; CONSEQ; otherwise do ALT. |
| 512 | |
| 513 | (make-if-inst (make-call-inst "!strcmp" |
| 514 | var (prin1-to-string name)) |
| 515 | conseq alt))) |
| 516 | |
| 517 | ;; Prepare the main parsing loops. We're going to construct them both at |
| 518 | ;; the same time. They're not quite similar enough for it to be |
| 519 | ;; worthwhile abstracting this further, but carving up the keywords is |
| 520 | ;; too tedious to write out more than once. |
| 521 | (let ((va-act (make-expr-inst (make-call-inst "kw_unknown" set "k"))) |
| 522 | (tab-act (make-expr-inst (make-call-inst "kw_unknown" |
| 523 | set "v->kw"))) |
| 524 | (name (effective-method-keyword-parser-function-name method))) |
| 525 | |
| 526 | ;; Deal with the special `kw.' keywords read via varargs. We're |
| 527 | ;; building the dispatch up backwards, so if we do these first, they |
| 528 | ;; get checked last, which priviliges the function-specific arguments |
| 529 | ;; over these special effects. |
| 530 | (codegen-push codegen) |
| 531 | (call "vv" "va_arg" "*ap" (c-type (* (struct "kwval" :const)))) |
| 532 | (call "nn" "va_arg" "*ap" c-type-size-t) |
| 533 | (call :void name "kw" *null-pointer* "vv" "nn") |
| 534 | (setf va-act (namecheck "k" "kw.tab" |
| 535 | (codegen-pop-block codegen) va-act)) |
| 536 | |
| 537 | (codegen-push codegen) |
| 538 | (call "aap" "va_arg" "*ap" (c-type (* va-list))) |
| 539 | (call :void name "kw" "aap" *null-pointer* 0) |
| 540 | (setf va-act (namecheck "k" "kw.valist" |
| 541 | (codegen-pop-block codegen) va-act)) |
| 542 | |
| 543 | ;; Deal with the special `kw.' keywords read from a table. |
| 544 | (codegen-push codegen) |
| 545 | (deliver-expr codegen "t" |
| 546 | (format nil "(~A)v->val" |
| 547 | (c-type (* (struct "kwtab" :const))))) |
| 548 | (call :void name "kw" *null-pointer* "t->v" "t->n") |
| 549 | (setf tab-act (namecheck "v->kw" "kw.tab" |
| 550 | (codegen-pop-block codegen) tab-act)) |
| 551 | |
| 552 | (codegen-push codegen) |
| 553 | (convert "aap" (c-type (* va-list))) |
| 554 | (call :void name "kw" "aap" *null-pointer* 0) |
| 555 | (setf tab-act (namecheck "v->kw" "kw.valist" |
| 556 | (codegen-pop-block codegen) tab-act)) |
| 557 | |
| 558 | ;; Work through the keywords. We're going to be building up the |
| 559 | ;; conditional dispatch from the end, so reverse the (nicely sorted) |
| 560 | ;; list before processing it. |
| 561 | (dolist (key (reverse keywords)) |
| 562 | (let* ((key-name (argument-name key)) |
| 563 | (key-type (argument-type key))) |
| 564 | |
| 565 | ;; Handle the varargs case. |
| 566 | (codegen-push codegen) |
| 567 | (deliver-expr codegen (format nil "kw->~A__suppliedp" key-name) 1) |
| 568 | (call (format nil "kw->~A" key-name) "va_arg" "*ap" key-type) |
| 569 | (setf va-act (namecheck "k" key-name |
| 570 | (codegen-pop-block codegen) va-act)) |
| 571 | |
| 572 | ;; Handle the table case. |
| 573 | (codegen-push codegen) |
| 574 | (deliver-expr codegen (format nil "kw->~A__suppliedp" key-name) 1) |
| 575 | (convert (format nil "kw->~A" key-name) key-type) |
| 576 | (setf tab-act (namecheck "v->kw" key-name |
| 577 | (codegen-pop-block codegen) tab-act)))) |
| 578 | |
| 579 | ;; Finish up the varargs loop. |
| 580 | (emit-banner codegen "Parse keywords from the variable-length tail.") |
| 581 | (codegen-push codegen) |
| 582 | (call "k" "va_arg" "*ap" c-type-const-string) |
| 583 | (emit-inst codegen (make-if-inst "!k" (make-break-inst))) |
| 584 | (emit-inst codegen va-act) |
| 585 | (let ((loop (make-for-inst nil nil nil (codegen-pop-block codegen)))) |
| 586 | (emit-inst codegen |
| 587 | (make-if-inst "ap" (make-block-inst nil (list loop))))) |
| 588 | |
| 589 | ;; Finish off the table loop. |
| 590 | (emit-banner codegen "Parse keywords from the argument table.") |
| 591 | (codegen-push codegen) |
| 592 | (emit-inst codegen tab-act) |
| 593 | (emit-inst codegen (make-expr-inst "v++")) |
| 594 | (emit-inst codegen (make-expr-inst "n--")) |
| 595 | (emit-inst codegen (make-while-inst "n" (codegen-pop-block codegen))) |
| 596 | |
| 597 | ;; Wrap the whole lot up with a nice bow. |
| 598 | (let ((message (effective-method-message method))) |
| 599 | (codegen-pop-function codegen name |
| 600 | (c-type (fun void |
| 601 | ("kw" (* (struct tag))) |
| 602 | ("ap" (* va-list)) |
| 603 | ("v" (* (struct "kwval" :const))) |
| 604 | ("n" size-t))) |
| 605 | "Keyword parsing for `~A.~A' on class `~A'." |
| 606 | (sod-class-nickname |
| 607 | (sod-message-class message)) |
| 608 | (sod-message-name message) |
| 609 | (effective-method-class method)))))) |
| 610 | |
| 611 | (defmethod make-method-entries ((method basic-effective-method) |
| 612 | (chain-head sod-class) |
| 613 | (chain-tail sod-class)) |
| 614 | (let ((entries nil) |
| 615 | (message (effective-method-message method))) |
| 616 | (flet ((make (role) |
| 617 | (push (make-instance 'method-entry |
| 618 | :method method :role role |
| 619 | :chain-head chain-head |
| 620 | :chain-tail chain-tail) |
| 621 | entries))) |
| 622 | (when (or (varargs-message-p message) |
| 623 | (keyword-message-p message)) |
| 624 | (make :valist)) |
| 625 | (make nil) |
| 626 | entries))) |
| 627 | |
| 628 | (defmethod compute-method-entry-functions ((method basic-effective-method)) |
| 629 | |
| 630 | ;; OK, there's quite a lot of this, so hold tight. |
| 631 | ;; |
| 632 | ;; The first thing we need to do is find all of the related objects. This |
| 633 | ;; is a bit verbose but fairly straightforward. |
| 634 | ;; |
| 635 | ;; Next, we generate the effective method body -- using `compute-effective- |
| 636 | ;; method-body' of all things. This gives us the declarations and body for |
| 637 | ;; an effective method function, but we don't have an actual function yet. |
| 638 | ;; |
| 639 | ;; Now we look at the chains which are actually going to need a method |
| 640 | ;; entry: only those chains whose tail (most specific) class is a |
| 641 | ;; superclass of the class which defined the message need an entry. We |
| 642 | ;; build a list of these tail classes. |
| 643 | ;; |
| 644 | ;; Having done this, we decide whether it's better to generate a standalone |
| 645 | ;; effective-method function and call it from each of the method entries, |
| 646 | ;; or to inline the effective method body into each of the entries. |
| 647 | ;; |
| 648 | ;; Most of the complexity here comes from (a) dealing with the two |
| 649 | ;; different strategies for constructing method entry functions and (b) |
| 650 | ;; (unsurprisingly) the mess involved with dealing with varargs messages. |
| 651 | |
| 652 | (let* ((message (effective-method-message method)) |
| 653 | (class (effective-method-class method)) |
| 654 | (message-class (sod-message-class message)) |
| 655 | (return-type (c-type-subtype (sod-message-type message))) |
| 656 | (codegen (make-instance 'method-codegen |
| 657 | :message message |
| 658 | :class class |
| 659 | :method method)) |
| 660 | |
| 661 | ;; Method entry details. |
| 662 | (chain-tails (remove-if-not (lambda (super) |
| 663 | (sod-subclass-p super message-class)) |
| 664 | (mapcar #'car |
| 665 | (sod-class-chains class)))) |
| 666 | (n-entries (length chain-tails)) |
| 667 | (raw-entry-args (append (sod-message-argument-tail message) |
| 668 | (and (keyword-message-p message) |
| 669 | (list :ellipsis)))) |
| 670 | (entry-args (reify-variable-argument-tail raw-entry-args)) |
| 671 | (parm-n (let ((tail (last (cons (make-argument "me" c-type-void) |
| 672 | raw-entry-args) 2))) |
| 673 | (and tail (eq (cadr tail) :ellipsis) |
| 674 | (argument-name (car tail))))) |
| 675 | (entry-target (codegen-target codegen)) |
| 676 | |
| 677 | ;; Effective method function details. |
| 678 | (emf-name (effective-method-function-name method)) |
| 679 | (ilayout-type (c-type (* (struct (ilayout-struct-tag class))))) |
| 680 | (emf-type (c-type (fun (lisp return-type) |
| 681 | ("sod__obj" (lisp ilayout-type)) |
| 682 | . entry-args)))) |
| 683 | |
| 684 | (flet ((setup-entry (tail) |
| 685 | (let ((head (sod-class-chain-head tail))) |
| 686 | (codegen-push codegen) |
| 687 | (ensure-var codegen "sod__obj" ilayout-type |
| 688 | (make-convert-to-ilayout-inst class |
| 689 | head "me")) |
| 690 | (deliver-call codegen :void "SOD__IGNORE" "sod__obj"))) |
| 691 | (finish-entry (tail) |
| 692 | (let* ((head (sod-class-chain-head tail)) |
| 693 | (role (if parm-n :valist nil)) |
| 694 | (name (method-entry-function-name method head role)) |
| 695 | (type (c-type (fun (lisp return-type) |
| 696 | ("me" (* (class tail))) |
| 697 | . entry-args)))) |
| 698 | (codegen-pop-function codegen name type |
| 699 | "~@(~@[~A ~]entry~) function ~:_~ |
| 700 | for method `~A.~A' ~:_~ |
| 701 | via chain headed by `~A' ~:_~ |
| 702 | defined on `~A'." |
| 703 | (if parm-n "Indirect argument-tail" nil) |
| 704 | (sod-class-nickname message-class) |
| 705 | (sod-message-name message) |
| 706 | head class) |
| 707 | |
| 708 | ;; If this is a varargs or keyword method then we've made the |
| 709 | ;; `:valist' role. Also make the `nil' role. |
| 710 | (when parm-n |
| 711 | (let ((call (apply #'make-call-inst name "me" |
| 712 | (mapcar #'argument-name entry-args))) |
| 713 | (main (method-entry-function-name method head nil)) |
| 714 | (main-type (c-type (fun (lisp return-type) |
| 715 | ("me" (* (class tail))) |
| 716 | . raw-entry-args)))) |
| 717 | (codegen-push codegen) |
| 718 | (ensure-var codegen *sod-ap* c-type-va-list) |
| 719 | (convert-stmts codegen entry-target return-type |
| 720 | (lambda (target) |
| 721 | (deliver-call codegen :void "va_start" |
| 722 | *sod-ap* parm-n) |
| 723 | (deliver-expr codegen target call) |
| 724 | (deliver-call codegen :void "va_end" |
| 725 | *sod-ap*))) |
| 726 | (codegen-pop-function codegen main main-type |
| 727 | "Variable-length argument list ~:_~ |
| 728 | entry function ~:_~ |
| 729 | for method `~A.~A' ~:_~ |
| 730 | via chain headed by `~A' ~:_~ |
| 731 | defined on `~A'." |
| 732 | (sod-class-nickname message-class) |
| 733 | (sod-message-name message) |
| 734 | head class)))))) |
| 735 | |
| 736 | ;; Generate the method body. We'll work out what to do with it later. |
| 737 | (codegen-push codegen) |
| 738 | (let* ((result (if (eq return-type c-type-void) nil |
| 739 | (temporary-var codegen return-type))) |
| 740 | (emf-target (or result :void))) |
| 741 | (compute-effective-method-body method codegen emf-target) |
| 742 | (multiple-value-bind (vars insts) (codegen-pop codegen) |
| 743 | (cond ((or (= n-entries 1) |
| 744 | (<= (* n-entries (reduce #'+ insts :key #'inst-metric)) |
| 745 | *method-entry-inline-threshold*)) |
| 746 | |
| 747 | ;; The effective method body is simple -- or there's only |
| 748 | ;; one of them. We'll inline the method body into the entry |
| 749 | ;; functions. |
| 750 | (dolist (tail chain-tails) |
| 751 | (setup-entry tail) |
| 752 | (dolist (var vars) |
| 753 | (if (typep var 'var-inst) |
| 754 | (ensure-var codegen (inst-name var) |
| 755 | (inst-type var) (inst-init var)) |
| 756 | (emit-decl codegen var))) |
| 757 | (emit-insts codegen insts) |
| 758 | (deliver-expr codegen entry-target result) |
| 759 | (finish-entry tail))) |
| 760 | |
| 761 | (t |
| 762 | |
| 763 | ;; The effective method body is complicated and we'd need |
| 764 | ;; more than one copy. We'll generate an effective method |
| 765 | ;; function and call it a lot. |
| 766 | (codegen-build-function codegen emf-name emf-type vars |
| 767 | (nconc insts (and result |
| 768 | (list (make-return-inst result)))) |
| 769 | "Effective method function ~:_for `~A.~A' ~:_~ |
| 770 | defined on `~A'." |
| 771 | (sod-class-nickname message-class) |
| 772 | (sod-message-name message) |
| 773 | (effective-method-class method)) |
| 774 | |
| 775 | (let ((call (apply #'make-call-inst emf-name "sod__obj" |
| 776 | (mapcar #'argument-name entry-args)))) |
| 777 | (dolist (tail chain-tails) |
| 778 | (setup-entry tail) |
| 779 | (deliver-expr codegen entry-target call) |
| 780 | (finish-entry tail))))))) |
| 781 | |
| 782 | (codegen-functions codegen)))) |
| 783 | |
| 784 | (defmethod compute-effective-method-body :around |
| 785 | ((method basic-effective-method) codegen target) |
| 786 | (let* ((message (effective-method-message method)) |
| 787 | (keywordsp (keyword-message-p message)) |
| 788 | (keywords (effective-method-keywords method)) |
| 789 | (ap-addr (format nil "&~A" *sod-tmp-ap*)) |
| 790 | (set (format nil "\"~A:~A.~A\"" |
| 791 | (sod-class-name (effective-method-class method)) |
| 792 | (sod-class-nickname (sod-message-class message)) |
| 793 | (sod-message-name message)))) |
| 794 | (labels ((call (target func &rest args) |
| 795 | (apply #'deliver-call codegen target func args)) |
| 796 | (parse-keywords (body) |
| 797 | (ensure-var codegen *sod-tmp-ap* c-type-va-list) |
| 798 | (call :void "va_copy" *sod-tmp-ap* *sod-ap*) |
| 799 | (funcall body) |
| 800 | (call :void "va_end" *sod-tmp-ap*))) |
| 801 | (cond ((not keywordsp) |
| 802 | (call-next-method)) |
| 803 | ((null keywords) |
| 804 | (let ((*keyword-struct-disposition* :null)) |
| 805 | (parse-keywords (lambda () |
| 806 | (with-temporary-var |
| 807 | (codegen kw c-type-const-string) |
| 808 | (call kw "va_arg" |
| 809 | *sod-tmp-ap* c-type-const-string) |
| 810 | (call :void "kw_parseempty" set |
| 811 | kw ap-addr *null-pointer* 0)))) |
| 812 | (call-next-method))) |
| 813 | (t |
| 814 | (let* ((name |
| 815 | (effective-method-keyword-parser-function-name method)) |
| 816 | (tag (effective-method-keyword-struct-tag method)) |
| 817 | (kw-addr (format nil "&~A" *sod-keywords*)) |
| 818 | (*keyword-struct-disposition* :local)) |
| 819 | (ensure-var codegen *sod-keywords* (c-type (struct tag))) |
| 820 | (make-keyword-parser-function codegen method tag set keywords) |
| 821 | (emit-insts codegen |
| 822 | (mapcar (lambda (keyword) |
| 823 | (make-set-inst |
| 824 | (format nil "~A.~A__suppliedp" |
| 825 | *sod-keywords* |
| 826 | (argument-name keyword)) |
| 827 | 0)) |
| 828 | keywords)) |
| 829 | (parse-keywords (lambda () |
| 830 | (call :void name kw-addr ap-addr |
| 831 | *null-pointer* 0))) |
| 832 | (call-next-method))))))) |
| 833 | |
| 834 | (defmethod effective-method-live-p ((method simple-effective-method)) |
| 835 | (effective-method-primary-methods method)) |
| 836 | |
| 837 | (defmethod compute-method-entry-functions :around ((method effective-method)) |
| 838 | (if (effective-method-live-p method) |
| 839 | (call-next-method) |
| 840 | nil)) |
| 841 | |
| 842 | (defmethod compute-effective-method-body |
| 843 | ((method simple-effective-method) codegen target) |
| 844 | (basic-effective-method-body codegen target method |
| 845 | (lambda (target) |
| 846 | (simple-method-body method |
| 847 | codegen |
| 848 | target)))) |
| 849 | |
| 850 | ;;;-------------------------------------------------------------------------- |
| 851 | ;;; Standard method combination. |
| 852 | |
| 853 | (export 'standard-message) |
| 854 | (defclass standard-message (simple-message) |
| 855 | () |
| 856 | (:documentation |
| 857 | "Message class for standard method combinations. |
| 858 | |
| 859 | Standard method combination is a simple method combination where the |
| 860 | primary methods are invoked as a delegation chain, from most- to |
| 861 | least-specific.")) |
| 862 | |
| 863 | (export 'standard-effective-method) |
| 864 | (defclass standard-effective-method (simple-effective-method) () |
| 865 | (:documentation "Effective method counterpart to `standard-message'.")) |
| 866 | |
| 867 | (defmethod primary-method-class ((message standard-message)) |
| 868 | 'delegating-direct-method) |
| 869 | |
| 870 | (defmethod sod-message-effective-method-class ((message standard-message)) |
| 871 | 'standard-effective-method) |
| 872 | |
| 873 | (defmethod simple-method-body |
| 874 | ((method standard-effective-method) codegen target) |
| 875 | (invoke-delegation-chain codegen |
| 876 | target |
| 877 | (effective-method-basic-argument-names method) |
| 878 | (effective-method-primary-methods method) |
| 879 | nil)) |
| 880 | |
| 881 | ;;;----- That's all, folks -------------------------------------------------- |