home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume10 / comobj.lisp / part03 / co-meta.l next >
Encoding:
Text File  |  1987-07-30  |  11.7 KB  |  419 lines

  1.  
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;
  4. ; File:         co-meta.l
  5. ; RCS:          $Revision: 1.1 $
  6. ; SCCS:         %A% %G% %U%
  7. ; Description:  Metaclass for CommonObjects
  8. ; Author:       James Kempf
  9. ; Created:      March 10, 1987
  10. ; Modified:     March 10, 1987  13:30:58 (Roy D'Souza)
  11. ; Language:     Lisp
  12. ; Package:      COMMON-OBJECTS
  13. ; Status:       Distribution
  14. ;
  15. ; (c) Copyright 1987, HP Labs, all rights reserved.
  16. ;
  17. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  18. ;
  19. ; Copyright (c) 1987 Hewlett-Packard Corporation. All rights reserved.
  20. ;
  21. ; Use and copying of this software and preparation of derivative works based
  22. ; upon this software are permitted.  Any distribution of this software or
  23. ; derivative works must comply with all applicable United States export
  24. ; control laws.
  25. ; This software is made available AS IS, and Hewlett-Packard Corporation makes
  26. ; no warranty about the software, its performance or its conformity to any
  27. ; specification.
  28. ;
  29. ; Suggestions, comments and requests for improvement may be mailed to
  30. ; aiws@hplabs.HP.COM
  31.  
  32. ;;;-*-Mode:LISP; Package:(CO (PCL LISP)); Base:10; Syntax: Common-lisp-*-
  33. ;;;
  34. ;;; *************************************************************************
  35. ;;; Copyright (c) 1985 Xerox Corporation.  All rights reserved.
  36. ;;;
  37. ;;; Use and copying of this software and preparation of derivative works
  38. ;;; based upon this software are permitted.  Any distribution of this
  39. ;;; software or derivative works must comply with all applicable United
  40. ;;; States export control laws.
  41. ;;; 
  42. ;;; This software is made available AS IS, and Xerox Corporation makes no
  43. ;;; warranty about the software, its performance or its conformity to any
  44. ;;; specification.
  45. ;;; 
  46. ;;; Any person obtaining a copy of this software is requested to send their
  47. ;;; name and post office or electronic mail address to:
  48. ;;;   CommonLoops Coordinator
  49. ;;;   Xerox Artifical Intelligence Systems
  50. ;;;   2400 Hanover St.
  51. ;;;   Palo Alto, CA 94303
  52. ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
  53. ;;;
  54. ;;; Suggestions, comments and requests for improvements are also welcome.
  55. ;;; *************************************************************************
  56.  
  57. (in-package 'common-objects :nicknames '(co) :use '(lisp pcl walker))
  58.  
  59. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  60. ;    CommonObjects Class Ndefstruct
  61. ;
  62. ;  Instances are represented as trees of their parent instances just like
  63. ;  in the original CommonObjects implementation except that we do not make
  64. ;  make the single inheritance optimization of in-lining the first parent.
  65. ;  The first slot of every instance is the class object.
  66. ;  The second slot of every instance is named .SELF. and is a pointer to
  67. ;  the acutal object. Then come slots for each of the parent class instances,
  68. ;  then the slots for this class.
  69. ;
  70. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  71.  
  72. (ndefstruct (common-objects-class
  73.           (:class class)
  74.           (:include (essential-class))    
  75.           (:conc-name class-)
  76.             )
  77.  
  78.   (instance-size 1)             ;The total number of slots every instance
  79.                 ;of this class must have.  This includes
  80.                 ;one slot for the pointer to outer self and
  81.                 ;one slot for each of the parent instances.
  82.  
  83.   (local-super-slot-names ())   ;A list of the names of the slots used to
  84.                 ;store the parent instances.  This list
  85.                 ;exactly parallels the local-supers as
  86.                 ;stored in class-local-supers.
  87.  
  88.   (slots ())            ;The slots required by CommonLoops.
  89.  
  90.   (user-visible-slots ())    ;Instance variable names.
  91.  
  92.   (children ())            ;Children of this guy. Not currently used.
  93.  
  94.   (init-keywords                ;Initialization keywords
  95.     () 
  96.   )        
  97.   (init-keywords-check T)       ;Whether to check the initialization 
  98.                 ;keywords
  99. ) ;end ndefstruct for common-objects-class
  100.  
  101. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  102. ;  Establishment of the CommonObjects MetaClass
  103. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  104. (eval-when (load)
  105.  (define-meta-class common-objects-class 
  106.    (lambda (x) (%instance-ref x $CLASS-OBJECT-INDEX))
  107. ))
  108.  
  109. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  110. ;  CommonObjects MetaClass Protocol  
  111. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  112.  
  113. ;;add-class-Add a CommonObjects class. Part of the metaclass protocol.
  114.  
  115. (defmeth add-class ((class common-objects-class)
  116.             new-local-supers
  117.             new-local-slots
  118.             extra
  119.                    )
  120.  
  121.   (let 
  122.     ( 
  123.       (local-super-slot-names
  124.       (mapcar #'(lambda (nls) (local-super-slot-name (class-name nls)))
  125.           new-local-supers
  126.           )
  127.        )
  128.      )
  129.  
  130.     (setf (class-local-super-slot-names class) local-super-slot-names)
  131.  
  132.     (setf (class-user-visible-slots class) new-local-slots)
  133.  
  134.     (setq new-local-slots 
  135.           (mapcar #'(lambda (x) (make-slotd class :name x))
  136.                     (append local-super-slot-names
  137.                         new-local-slots)
  138.           )
  139.     )
  140.  
  141.     (setf (class-instance-size class) (length new-local-slots))
  142.  
  143.     (run-super)
  144.  
  145.   ) ;let
  146.  
  147. ) ;end add-class
  148.  
  149. ;;class-slots-Return the slot names for the parents
  150.  
  151. (defmeth class-slots ((class common-objects-class))
  152.  
  153.   (class-local-slots class)
  154.  
  155. ) ;end class-slots
  156.  
  157. ;;has-slot-p-Return T if class has user visible slot symbol
  158.  
  159. (defmeth has-slot-p ((class common-objects-class) symbol)
  160.  
  161.   (let
  162.     (
  163.       (bool NIL)
  164.     )
  165.  
  166.     (dolist (slotd (class-user-visible-slots class))
  167.       (when  (equal symbol (slot-name-from-slotd slotd))
  168.     (setf bool T)
  169.         (return)
  170.       )
  171.     )
  172.     bool
  173.  
  174.   ) ;end let
  175.  
  176. ) ;end has-slot-p
  177.  
  178. ;;init-keywords-Return the initialization keywords
  179.  
  180. (defmeth init-keywords ((class common-objects-class))
  181.  
  182.   (class-init-keywords class)
  183.  
  184. ) ;init-keywords
  185.  
  186. ;;class-local-super-names-Return the names of the local supers for
  187. ;;  this class.
  188.  
  189. (defmeth class-local-super-names ((class common-objects-class))
  190.  
  191.   (mapcar #'(lambda (x) (class-name x)) (class-local-supers class))
  192.  
  193. ) ;end class-local-super-names
  194.  
  195. ;;compute-class-precedence-list-Calculate class precedence.
  196. ;;  CommonObjects classes don't inherit in the CommonLoops sense.  
  197. ;;  Tell CommonLoops that they only inherit from themselves, 
  198. ;;  the class COMMON-OBJECTS-CLASS itself which they need for 
  199. ;;  GET-SLOT-USING-CLASS and PUT-SLOT-USING-CLASS and default printing
  200. ;;  to work right.
  201.  
  202. (defmeth compute-class-precedence-list ((class common-objects-class))
  203.  
  204.   (list class (class-named 'common-objects-class) (class-named 'object))
  205.  
  206. ) ;end compute-class-precedence-list
  207.  
  208. ;;method-alist-Return the a-list of names v.s. method objects. Only
  209. ;;  methods which are CommonObjects methods are returned. This
  210. ;;  is to accomodate system generated methods, like TYPE-OF, which
  211. ;;  should not be identified as methods on CommonObjects instances.
  212. ;;  This routine is primarily used in parsing.
  213.  
  214. (defmeth method-alist ((class common-objects-class))
  215.   (declare (special *universal-methods*))
  216.  
  217.   (let
  218.     (
  219.       (alist NIL)
  220.     )
  221.  
  222.     ;;First get the direct methods
  223.  
  224.     (dolist (methobj (class-direct-methods class))
  225.  
  226.         (if (eq (class-name (class-of methobj)) 'common-objects-method)
  227.  
  228.           (push 
  229.         (list (unkeyword-standin (method-name methobj)) methobj)
  230.         alist
  231.       )
  232.         ) ;if
  233.     )
  234.  
  235.     ;;Now check if any of the universal methods need to be added
  236.  
  237.     (dolist (univmeth *universal-methods*)
  238.  
  239.       (if (not (assoc univmeth alist))
  240.         (push
  241.           (list 
  242.         univmeth 
  243.         (find-method 
  244.           (discriminator-named (keyword-standin univmeth))
  245.               '(common-objects-class)
  246.           NIL
  247.           T
  248.             )
  249.           )
  250.           alist
  251.         )
  252.  
  253.       ) ;if
  254.  
  255.     ) ;dolist            
  256.  
  257.     alist
  258.  
  259.   ) ;end let
  260.  
  261. ) ;end method-alist
  262.  
  263. ;;check-init-keywords-Check if the initialization keywords are
  264. ;;  correct
  265.  
  266. (defmeth check-init-keywords ((class common-objects-class) keylist)
  267.  
  268.   (let
  269.     (
  270.       (legalkeys (class-init-keywords class))
  271.     )
  272.     
  273.     (do
  274.       (
  275.         (key (car keylist) (cddr key) )
  276.       )
  277.       ( (null key) )
  278.  
  279.       (if (not (and (keywordp (car key)) (>= (length key) 2)))
  280.         (error "MAKE-INSTANCE: For type ~S, keylist must have alternating keys and values. List:~S~%"
  281.          (class-name class) (car keylist)
  282.         )
  283.       )
  284.  
  285.       (when (not (member (car key) legalkeys))
  286.         (error "MAKE-INSTANCE: For type ~S, ~S is not a legal initialization keyword.~%"
  287.          (class-name class) (car key)
  288.         )
  289.       )
  290.     ) ;dolist
  291.  
  292.   ) ;let
  293.  
  294. ) ;end check-init-keywords
  295.  
  296. ;;optimize-get-slot-Optimize a get slot by returning
  297. ;;  the right code. CommonObjects instances are statically
  298. ;;  allocated, so "hard" indicies can be used for them.
  299. ;;  Stolen from the protocol for BASIC-CLASS.
  300.  
  301. ;(defmeth optimize-get-slot ((method common-objects-method)
  302. ;                     (class common-objects-class)
  303. ;                     form)
  304. ;  (declare (ignore method)) ; rds 3/9
  305. (defmeth optimize-get-slot ((class common-objects-class) form)
  306.     `(%instance-ref ,(second form) ,(slot-index class (second (third form))))
  307.  
  308.  
  309.  
  310. ) ;end optimize-get-slot
  311.  
  312. ;;pcl::optimize-setf-of-get-slot-Optimize a setf of a slot
  313. ;;  by returning the right code. Again, "hard" indicies
  314. ;;  can be used since in-line allocation is the rule.
  315. ;;  Stolen from the protocol for BASIC-CLASS.
  316.  
  317. ;(defmeth pcl::optimize-setf-of-get-slot ((method common-objects-method)
  318. ;                         (class common-objects-class)
  319. ;                         form)
  320. ;  (declare (ignore method))
  321. (defmeth pcl::optimize-setf-of-get-slot ((class common-objects-class)
  322.                                          form)
  323.     `(setf 
  324.       (%instance-ref , (nth 1 form) ,(slot-index class (second (nth 2 form))))
  325.            ,(nth 3 form)
  326.      )
  327.  
  328. ) ;end optimize-setf-of-get-slot
  329.  
  330. ;;slot-index-Calculate the slot index for the indicated slot
  331.  
  332. (defmeth slot-index ((class common-objects-class) slotname)
  333.  
  334.   ;;Treat .SELF. as a special case
  335.  
  336.   (if (eq slotname '.self.)
  337.     $SELF-INDEX
  338.  
  339.     (calculate-slot-index 
  340.       slotname
  341.       (class-local-super-slot-names class) 
  342.       (class-user-visible-slots class)
  343.     )
  344.  
  345.   ) ;if
  346.  
  347. ) ;end slot-index
  348.  
  349. ;;get-slot-using-class-Generic version for all CommonObjects classes.
  350. ;;  Normally, this will be optimized out by the optimization method
  351. ;;  but just in case.
  352.  
  353. (defmeth get-slot-using-class ((class common-objects-class) object slot-name)
  354.  
  355.   (%instance-ref object (slot-index class slot-name))
  356.  
  357. ) ;get-slot-using-class 
  358.  
  359. ;;put-slot-using-class-Generic version for all CommonObjects classes.
  360. ;;  A bug in the default code-walker makes this necessary, although
  361. ;;  ultimately a custom walking function for CommonObjects methods
  362. ;;  might make the optimization work. Note that the code walker
  363. ;;  bug is fixed in the specialized walker method WALK-METHOD-BODY-INTERNAL
  364. ;;  for CommonObjects methods.
  365.  
  366. (defmeth pcl::put-slot-using-class 
  367.   ((class common-objects-class) object slot-name new-value)
  368.  
  369.   (setf 
  370.     (%instance-ref object (slot-index class slot-name) )
  371.     new-value
  372.   )
  373.   
  374. ) ;put-slot-using-class
  375.  
  376.  
  377. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  378. ;  CommonObjects MetaClass Utility Functions
  379. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  380.  
  381. ;;defined-classes-List the defined CommonObjects classes
  382.  
  383. (defun defined-classes ()
  384.  
  385.   (let 
  386.     (
  387.       (defined-types NIL)
  388.       (class (class-named 'common-objects-class))
  389.     )
  390.  
  391.     (maphash 
  392.     #'(lambda (key val) 
  393.         (when (and val (eq (class-of val) class))
  394.           (setf defined-types (cons key defined-types))
  395.             )
  396.       )
  397.           pcl::*class-name-hash-table*
  398.     )
  399.     defined-types
  400.   )
  401. ) ;end defined-classes
  402.  
  403. ;;slot-name-from-slotd-Return the name of the slot, given the SLOTD.
  404.  
  405. (defun slot-name-from-slotd (slotd)
  406.   slotd
  407.  
  408. ) ;slot-name-from-slotd
  409.  
  410. ;;method-name-Return the method name, given the method object
  411.  
  412. (defun method-name (methobj)
  413.  
  414.   (discriminator-name (method-discriminator methobj))
  415. )
  416.  
  417.