Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame^] | 1 | /* Class object implementation */ |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | |
| 5 | #include "PROTO.h" |
| 6 | #include "node.h" |
| 7 | #include "object.h" |
| 8 | #include "stringobject.h" |
| 9 | #include "tupleobject.h" |
| 10 | #include "dictobject.h" |
| 11 | #include "funcobject.h" |
| 12 | #include "classobject.h" |
| 13 | #include "objimpl.h" |
| 14 | |
| 15 | typedef struct { |
| 16 | OB_HEAD |
| 17 | node *cl_tree; /* The entire classdef parse tree */ |
| 18 | object *cl_bases; /* A tuple */ |
| 19 | object *cl_methods; /* A dictionary */ |
| 20 | } classobject; |
| 21 | |
| 22 | object * |
| 23 | newclassobject(tree, bases, methods) |
| 24 | node *tree; |
| 25 | object *bases; /* NULL or tuple of classobjects! */ |
| 26 | object *methods; |
| 27 | { |
| 28 | classobject *op; |
| 29 | op = NEWOBJ(classobject, &Classtype); |
| 30 | if (op == NULL) |
| 31 | return NULL; |
| 32 | op->cl_tree = tree; |
| 33 | if (bases != NULL) |
| 34 | INCREF(bases); |
| 35 | op->cl_bases = bases; |
| 36 | INCREF(methods); |
| 37 | op->cl_methods = methods; |
| 38 | return (object *) op; |
| 39 | } |
| 40 | |
| 41 | /* Class methods */ |
| 42 | |
| 43 | static void |
| 44 | class_dealloc(op) |
| 45 | classobject *op; |
| 46 | { |
| 47 | int i; |
| 48 | if (op->cl_bases != NULL) |
| 49 | DECREF(op->cl_bases); |
| 50 | DECREF(op->cl_methods); |
| 51 | free((ANY *)op); |
| 52 | } |
| 53 | |
| 54 | static object * |
| 55 | class_getattr(op, name) |
| 56 | register classobject *op; |
| 57 | register char *name; |
| 58 | { |
| 59 | register object *v; |
| 60 | v = dictlookup(op->cl_methods, name); |
| 61 | if (v != NULL) { |
| 62 | INCREF(v); |
| 63 | return v; |
| 64 | } |
| 65 | if (op->cl_bases != NULL) { |
| 66 | int n = gettuplesize(op->cl_bases); |
| 67 | int i; |
| 68 | for (i = 0; i < n; i++) { |
| 69 | v = class_getattr(gettupleitem(op->cl_bases, i), name); |
| 70 | if (v != NULL) |
| 71 | return v; |
| 72 | } |
| 73 | } |
| 74 | errno = ESRCH; |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | typeobject Classtype = { |
| 79 | OB_HEAD_INIT(&Typetype) |
| 80 | 0, |
| 81 | "class", |
| 82 | sizeof(classobject), |
| 83 | 0, |
| 84 | class_dealloc, /*tp_dealloc*/ |
| 85 | 0, /*tp_print*/ |
| 86 | class_getattr, /*tp_getattr*/ |
| 87 | 0, /*tp_setattr*/ |
| 88 | 0, /*tp_compare*/ |
| 89 | 0, /*tp_repr*/ |
| 90 | 0, /*tp_as_number*/ |
| 91 | 0, /*tp_as_sequence*/ |
| 92 | 0, /*tp_as_mapping*/ |
| 93 | }; |
| 94 | |
| 95 | |
| 96 | /* We're not done yet: next, we define class member objects... */ |
| 97 | |
| 98 | typedef struct { |
| 99 | OB_HEAD |
| 100 | classobject *cm_class; /* The class object */ |
| 101 | object *cm_attr; /* A dictionary */ |
| 102 | } classmemberobject; |
| 103 | |
| 104 | object * |
| 105 | newclassmemberobject(class) |
| 106 | register object *class; |
| 107 | { |
| 108 | register classmemberobject *cm; |
| 109 | if (!is_classobject(class)) { |
| 110 | errno = EINVAL; |
| 111 | return NULL; |
| 112 | } |
| 113 | cm = NEWOBJ(classmemberobject, &Classmembertype); |
| 114 | if (cm == NULL) |
| 115 | return NULL; |
| 116 | INCREF(class); |
| 117 | cm->cm_class = (classobject *)class; |
| 118 | cm->cm_attr = newdictobject(); |
| 119 | if (cm->cm_attr == NULL) { |
| 120 | DECREF(cm); |
| 121 | return NULL; |
| 122 | } |
| 123 | return (object *)cm; |
| 124 | } |
| 125 | |
| 126 | /* Class member methods */ |
| 127 | |
| 128 | static void |
| 129 | classmember_dealloc(cm) |
| 130 | register classmemberobject *cm; |
| 131 | { |
| 132 | DECREF(cm->cm_class); |
| 133 | if (cm->cm_attr != NULL) |
| 134 | DECREF(cm->cm_attr); |
| 135 | free((ANY *)cm); |
| 136 | } |
| 137 | |
| 138 | static object * |
| 139 | classmember_getattr(cm, name) |
| 140 | register classmemberobject *cm; |
| 141 | register char *name; |
| 142 | { |
| 143 | register object *v = dictlookup(cm->cm_attr, name); |
| 144 | if (v != NULL) { |
| 145 | INCREF(v); |
| 146 | return v; |
| 147 | } |
| 148 | v = class_getattr(cm->cm_class, name); |
| 149 | if (v == NULL) |
| 150 | return v; /* class_getattr() has set errno */ |
| 151 | if (is_funcobject(v)) { |
| 152 | object *w = newclassmethodobject(v, (object *)cm); |
| 153 | DECREF(v); |
| 154 | return w; |
| 155 | } |
| 156 | DECREF(v); |
| 157 | errno = ESRCH; |
| 158 | return NULL; |
| 159 | } |
| 160 | |
| 161 | static int |
| 162 | classmember_setattr(cm, name, v) |
| 163 | classmemberobject *cm; |
| 164 | char *name; |
| 165 | object *v; |
| 166 | { |
| 167 | if (v == NULL) |
| 168 | return dictremove(cm->cm_attr, name); |
| 169 | else |
| 170 | return dictinsert(cm->cm_attr, name, v); |
| 171 | } |
| 172 | |
| 173 | typeobject Classmembertype = { |
| 174 | OB_HEAD_INIT(&Typetype) |
| 175 | 0, |
| 176 | "class member", |
| 177 | sizeof(classmemberobject), |
| 178 | 0, |
| 179 | classmember_dealloc, /*tp_dealloc*/ |
| 180 | 0, /*tp_print*/ |
| 181 | classmember_getattr, /*tp_getattr*/ |
| 182 | classmember_setattr, /*tp_setattr*/ |
| 183 | 0, /*tp_compare*/ |
| 184 | 0, /*tp_repr*/ |
| 185 | 0, /*tp_as_number*/ |
| 186 | 0, /*tp_as_sequence*/ |
| 187 | 0, /*tp_as_mapping*/ |
| 188 | }; |
| 189 | |
| 190 | |
| 191 | /* And finally, here are class method objects */ |
| 192 | /* (Really methods of class members) */ |
| 193 | |
| 194 | typedef struct { |
| 195 | OB_HEAD |
| 196 | object *cm_func; /* The method function */ |
| 197 | object *cm_self; /* The object to which this applies */ |
| 198 | } classmethodobject; |
| 199 | |
| 200 | object * |
| 201 | newclassmethodobject(func, self) |
| 202 | object *func; |
| 203 | object *self; |
| 204 | { |
| 205 | register classmethodobject *cm; |
| 206 | if (!is_funcobject(func)) { |
| 207 | errno = EINVAL; |
| 208 | return NULL; |
| 209 | } |
| 210 | cm = NEWOBJ(classmethodobject, &Classmethodtype); |
| 211 | if (cm == NULL) |
| 212 | return NULL; |
| 213 | INCREF(func); |
| 214 | cm->cm_func = func; |
| 215 | INCREF(self); |
| 216 | cm->cm_self = self; |
| 217 | return (object *)cm; |
| 218 | } |
| 219 | |
| 220 | object * |
| 221 | classmethodgetfunc(cm) |
| 222 | register object *cm; |
| 223 | { |
| 224 | if (!is_classmethodobject(cm)) { |
| 225 | errno = EINVAL; |
| 226 | return NULL; |
| 227 | } |
| 228 | return ((classmethodobject *)cm)->cm_func; |
| 229 | } |
| 230 | |
| 231 | object * |
| 232 | classmethodgetself(cm) |
| 233 | register object *cm; |
| 234 | { |
| 235 | if (!is_classmethodobject(cm)) { |
| 236 | errno = EINVAL; |
| 237 | return NULL; |
| 238 | } |
| 239 | return ((classmethodobject *)cm)->cm_self; |
| 240 | } |
| 241 | |
| 242 | /* Class method methods */ |
| 243 | |
| 244 | static void |
| 245 | classmethod_dealloc(cm) |
| 246 | register classmethodobject *cm; |
| 247 | { |
| 248 | DECREF(cm->cm_func); |
| 249 | DECREF(cm->cm_self); |
| 250 | free((ANY *)cm); |
| 251 | } |
| 252 | |
| 253 | typeobject Classmethodtype = { |
| 254 | OB_HEAD_INIT(&Typetype) |
| 255 | 0, |
| 256 | "class method", |
| 257 | sizeof(classmethodobject), |
| 258 | 0, |
| 259 | classmethod_dealloc, /*tp_dealloc*/ |
| 260 | 0, /*tp_print*/ |
| 261 | 0, /*tp_getattr*/ |
| 262 | 0, /*tp_setattr*/ |
| 263 | 0, /*tp_compare*/ |
| 264 | 0, /*tp_repr*/ |
| 265 | 0, /*tp_as_number*/ |
| 266 | 0, /*tp_as_sequence*/ |
| 267 | 0, /*tp_as_mapping*/ |
| 268 | }; |