| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include "drgnpy.h" |
| 5 |
#include "../bitops.h" |
| 6 |
#include "../util.h" |
| 7 |
|
| 8 |
static PyObject *collections_abc_Set; |
| 9 |
|
| 10 |
PyObject *TypeKindSet_wrap(uint64_t mask) |
| 11 |
{ |
| 12 |
TypeKindSet *res = call_tp_alloc(TypeKindSet); |
| 13 |
if (res) |
| 14 |
res->kinds = mask; |
| 15 |
return (PyObject *)res; |
| 16 |
} |
| 17 |
|
| 18 |
int init_type_kind_set(void) |
| 19 |
{ |
| 20 |
_cleanup_pydecref_ PyObject *collections_abc = |
| 21 |
PyImport_ImportModule("collections.abc"); |
| 22 |
if (!collections_abc) |
| 23 |
return -1; |
| 24 |
collections_abc_Set = PyObject_GetAttrString(collections_abc, "Set"); |
| 25 |
if (!collections_abc_Set) |
| 26 |
return -1; |
| 27 |
_cleanup_pydecref_ PyObject *res = |
| 28 |
PyObject_CallMethod(collections_abc_Set, "register", "O", |
| 29 |
&TypeKindSet_type); |
| 30 |
if (!res) |
| 31 |
return -1; |
| 32 |
return 0; |
| 33 |
} |
| 34 |
|
| 35 |
static inline const char *type_kind_to_str(enum drgn_type_kind kind) |
| 36 |
{ |
| 37 |
SWITCH_ENUM(kind) { |
| 38 |
case DRGN_TYPE_VOID: |
| 39 |
return "TypeKind.VOID"; |
| 40 |
case DRGN_TYPE_INT: |
| 41 |
return "TypeKind.INT"; |
| 42 |
case DRGN_TYPE_BOOL: |
| 43 |
return "TypeKind.BOOL"; |
| 44 |
case DRGN_TYPE_FLOAT: |
| 45 |
return "TypeKind.FLOAT"; |
| 46 |
case DRGN_TYPE_STRUCT: |
| 47 |
return "TypeKind.STRUCT"; |
| 48 |
case DRGN_TYPE_UNION: |
| 49 |
return "TypeKind.UNION"; |
| 50 |
case DRGN_TYPE_CLASS: |
| 51 |
return "TypeKind.CLASS"; |
| 52 |
case DRGN_TYPE_ENUM: |
| 53 |
return "TypeKind.ENUM"; |
| 54 |
case DRGN_TYPE_TYPEDEF: |
| 55 |
return "TypeKind.TYPEDEF"; |
| 56 |
case DRGN_TYPE_POINTER: |
| 57 |
return "TypeKind.POINTER"; |
| 58 |
case DRGN_TYPE_ARRAY: |
| 59 |
return "TypeKind.ARRAY"; |
| 60 |
case DRGN_TYPE_FUNCTION: |
| 61 |
return "TypeKind.FUNCTION"; |
| 62 |
default: |
| 63 |
UNREACHABLE(); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
static PyObject *TypeKindSet_repr(TypeKindSet *self) |
| 68 |
{ |
| 69 |
_cleanup_pydecref_ PyObject *parts = PyList_New(0); |
| 70 |
if (!parts) |
| 71 |
return NULL; |
| 72 |
if (append_string(parts, "TypeKindSet(")) |
| 73 |
return NULL; |
| 74 |
bool first = true; |
| 75 |
unsigned int kind; |
| 76 |
uint64_t kinds = self->kinds; |
| 77 |
for_each_bit(kind, kinds) { |
| 78 |
if (append_format(parts, "%s%s", first ? "{" : ", ", |
| 79 |
type_kind_to_str(kind))) |
| 80 |
return NULL; |
| 81 |
first = false; |
| 82 |
} |
| 83 |
if (append_string(parts, first ? ")" : "})")) |
| 84 |
return NULL; |
| 85 |
return join_strings(parts); |
| 86 |
} |
| 87 |
|
| 88 |
static int TypeKind_value(PyObject *obj) |
| 89 |
{ |
| 90 |
_cleanup_pydecref_ PyObject *value_obj = |
| 91 |
PyObject_GetAttrString(obj, "value"); |
| 92 |
if (!value_obj) |
| 93 |
return -1; |
| 94 |
long value = PyLong_AsLong(value_obj); |
| 95 |
if ((value < 0 && !PyErr_Occurred()) || value >= 64) { |
| 96 |
PyErr_BadArgument(); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
return value; |
| 100 |
} |
| 101 |
|
| 102 |
static int TypeKindSet_mask_from_iterable(PyObject *iterable, uint64_t *ret) |
| 103 |
{ |
| 104 |
if (PyObject_TypeCheck(iterable, &TypeKindSet_type)) { |
| 105 |
*ret = ((TypeKindSet *)iterable)->kinds; |
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
int non_typekind = 0; |
| 110 |
uint64_t mask = 0; |
| 111 |
if (iterable) { |
| 112 |
_cleanup_pydecref_ PyObject *it = PyObject_GetIter(iterable); |
| 113 |
if (!it) |
| 114 |
return -1; |
| 115 |
|
| 116 |
for (;;) { |
| 117 |
_cleanup_pydecref_ PyObject *item = PyIter_Next(it); |
| 118 |
if (!item) |
| 119 |
break; |
| 120 |
if (PyObject_TypeCheck(item, |
| 121 |
(PyTypeObject *)TypeKind_class)) { |
| 122 |
int value = TypeKind_value(item); |
| 123 |
if (value < 0) |
| 124 |
return -1; |
| 125 |
mask |= 1 << value; |
| 126 |
} else { |
| 127 |
non_typekind = 1; |
| 128 |
} |
| 129 |
} |
| 130 |
if (PyErr_Occurred()) |
| 131 |
return -1; |
| 132 |
} |
| 133 |
*ret = mask; |
| 134 |
return non_typekind; |
| 135 |
} |
| 136 |
|
| 137 |
static TypeKindSet *TypeKindSet_new(PyTypeObject *subtype, PyObject *args, |
| 138 |
PyObject *kwds) |
| 139 |
{ |
| 140 |
static char *keywords[] = { "", NULL }; |
| 141 |
PyObject *iterable = NULL; |
| 142 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:TypeKindSet", keywords, |
| 143 |
&iterable)) |
| 144 |
return NULL; |
| 145 |
uint64_t kinds = 0; |
| 146 |
if (iterable) { |
| 147 |
int r = TypeKindSet_mask_from_iterable(iterable, &kinds); |
| 148 |
if (r < 0) |
| 149 |
return NULL; |
| 150 |
if (r > 0) { |
| 151 |
PyErr_SetString(PyExc_TypeError, |
| 152 |
"TypeKindSet elements must be TypeKind"); |
| 153 |
return NULL; |
| 154 |
} |
| 155 |
} |
| 156 |
TypeKindSet *res = (TypeKindSet *)subtype->tp_alloc(subtype, 0); |
| 157 |
res->kinds = kinds; |
| 158 |
return res; |
| 159 |
} |
| 160 |
|
| 161 |
static Py_ssize_t TypeKindSet_length(TypeKindSet *self) |
| 162 |
{ |
| 163 |
return popcount(self->kinds); |
| 164 |
} |
| 165 |
|
| 166 |
static int TypeKindSet_contains(TypeKindSet *self, PyObject *other) |
| 167 |
{ |
| 168 |
if (!PyObject_TypeCheck(other, (PyTypeObject *)TypeKind_class)) |
| 169 |
return 0; |
| 170 |
int value = TypeKind_value(other); |
| 171 |
if (value < 0) |
| 172 |
return value; |
| 173 |
return !!(self->kinds & (1 << value)); |
| 174 |
} |
| 175 |
|
| 176 |
static Py_ssize_t TypeKindSet_hash(TypeKindSet *self) |
| 177 |
{ |
| 178 |
return self->kinds; |
| 179 |
} |
| 180 |
|
| 181 |
static PyObject *TypeKindSet_richcompare(TypeKindSet *self, PyObject *other, |
| 182 |
int op) |
| 183 |
{ |
| 184 |
if (!PyObject_IsInstance(other, collections_abc_Set)) |
| 185 |
Py_RETURN_NOTIMPLEMENTED; |
| 186 |
|
| 187 |
uint64_t other_kinds; |
| 188 |
int other_non_typekind = |
| 189 |
TypeKindSet_mask_from_iterable(other, &other_kinds); |
| 190 |
if (other_non_typekind < 0) |
| 191 |
return NULL; |
| 192 |
|
| 193 |
switch (op) { |
| 194 |
case Py_EQ: |
| 195 |
Py_RETURN_BOOL(self->kinds == other_kinds && !other_non_typekind); |
| 196 |
case Py_NE: |
| 197 |
Py_RETURN_BOOL(self->kinds != other_kinds || other_non_typekind); |
| 198 |
case Py_LT: |
| 199 |
Py_RETURN_BOOL((self->kinds != other_kinds || other_non_typekind) |
| 200 |
&& (other_kinds | self->kinds) == other_kinds); |
| 201 |
case Py_GT: |
| 202 |
Py_RETURN_BOOL(self->kinds != other_kinds |
| 203 |
&& (self->kinds | other_kinds) == self->kinds |
| 204 |
&& !other_non_typekind); |
| 205 |
case Py_LE: |
| 206 |
Py_RETURN_BOOL((other_kinds | self->kinds) == other_kinds); |
| 207 |
case Py_GE: |
| 208 |
Py_RETURN_BOOL((self->kinds | other_kinds) == self->kinds |
| 209 |
&& !other_non_typekind); |
| 210 |
default: |
| 211 |
Py_UNREACHABLE(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
static TypeKindSetIterator *TypeKindSet_iter(TypeKindSet *self) |
| 216 |
{ |
| 217 |
TypeKindSetIterator *it = call_tp_alloc(TypeKindSetIterator); |
| 218 |
if (!it) |
| 219 |
return NULL; |
| 220 |
it->mask = self->kinds; |
| 221 |
return it; |
| 222 |
} |
| 223 |
|
| 224 |
static PyObject *TypeKindSet_isdisjoint(TypeKindSet *self, PyObject *other) |
| 225 |
{ |
| 226 |
uint64_t other_kinds; |
| 227 |
if (TypeKindSet_mask_from_iterable(other, &other_kinds) < 0) |
| 228 |
return NULL; |
| 229 |
// Non-TypeKind elements in other cannot be in self, so they don't |
| 230 |
// affect the answer and can be ignored. |
| 231 |
Py_RETURN_BOOL((self->kinds ^ other_kinds) |
| 232 |
== (self->kinds | other_kinds)); |
| 233 |
} |
| 234 |
|
| 235 |
static PyObject *TypeKindSet_sub(PyObject *left, PyObject *right) |
| 236 |
{ |
| 237 |
uint64_t left_kinds; |
| 238 |
int left_r = TypeKindSet_mask_from_iterable(left, &left_kinds); |
| 239 |
if (left_r < 0) |
| 240 |
return NULL; |
| 241 |
if (left_r > 0) |
| 242 |
Py_RETURN_NOTIMPLEMENTED; |
| 243 |
|
| 244 |
uint64_t right_kinds; |
| 245 |
if (TypeKindSet_mask_from_iterable(right, &right_kinds) < 0) |
| 246 |
return NULL; |
| 247 |
// If right has non-TypeKind elements, then left is a TypeKindSet and |
| 248 |
// removing non-TypeKind elements has no effect, so they can be ignored. |
| 249 |
return TypeKindSet_wrap(left_kinds & ~right_kinds); |
| 250 |
} |
| 251 |
|
| 252 |
static PyObject *TypeKindSet_and(PyObject *left, PyObject *right) |
| 253 |
{ |
| 254 |
uint64_t left_kinds, right_kinds; |
| 255 |
if (TypeKindSet_mask_from_iterable(left, &left_kinds) < 0 |
| 256 |
|| TypeKindSet_mask_from_iterable(right, &right_kinds) < 0) |
| 257 |
return NULL; |
| 258 |
// At least one of the operands is a TypeKindSet, so non-TypeKind |
| 259 |
// elements cannot be in both and can be ignored. |
| 260 |
return TypeKindSet_wrap(left_kinds & right_kinds); |
| 261 |
} |
| 262 |
|
| 263 |
#define TypeKindSet_OR_OP(name, op) \ |
| 264 |
static PyObject *TypeKindSet_##name(PyObject *left, PyObject *right) \ |
| 265 |
{ \ |
| 266 |
/* Both operands must only contain TypeKind elements. */ \ |
| 267 |
uint64_t left_kinds; \ |
| 268 |
int left_r = TypeKindSet_mask_from_iterable(left, &left_kinds); \ |
| 269 |
if (left_r < 0) \ |
| 270 |
return NULL; \ |
| 271 |
if (left_r > 0) \ |
| 272 |
Py_RETURN_NOTIMPLEMENTED; \ |
| 273 |
\ |
| 274 |
uint64_t right_kinds; \ |
| 275 |
int right_r = TypeKindSet_mask_from_iterable(right, &right_kinds); \ |
| 276 |
if (right_r < 0) \ |
| 277 |
return NULL; \ |
| 278 |
if (right_r > 0) \ |
| 279 |
Py_RETURN_NOTIMPLEMENTED; \ |
| 280 |
\ |
| 281 |
return TypeKindSet_wrap(left_kinds op right_kinds); \ |
| 282 |
} |
| 283 |
TypeKindSet_OR_OP(xor, ^) |
| 284 |
TypeKindSet_OR_OP(or, |) |
| 285 |
#undef TypeKindSet_OR_OP |
| 286 |
|
| 287 |
static PyNumberMethods TypeKindSet_as_number = { |
| 288 |
.nb_subtract = TypeKindSet_sub, |
| 289 |
.nb_and = TypeKindSet_and, |
| 290 |
.nb_xor = TypeKindSet_xor, |
| 291 |
.nb_or = TypeKindSet_or, |
| 292 |
}; |
| 293 |
|
| 294 |
static PySequenceMethods TypeKindSet_as_sequence = { |
| 295 |
.sq_length = (lenfunc)TypeKindSet_length, |
| 296 |
.sq_contains = (objobjproc)TypeKindSet_contains, |
| 297 |
}; |
| 298 |
|
| 299 |
static PyMethodDef TypeKindSet_methods[] = { |
| 300 |
{"isdisjoint", (PyCFunction)TypeKindSet_isdisjoint, METH_O}, |
| 301 |
{}, |
| 302 |
}; |
| 303 |
|
| 304 |
PyTypeObject TypeKindSet_type = { |
| 305 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 306 |
.tp_name = "_drgn.TypeKindSet", |
| 307 |
.tp_basicsize = sizeof(TypeKindSet), |
| 308 |
.tp_repr = (reprfunc)TypeKindSet_repr, |
| 309 |
.tp_as_number = &TypeKindSet_as_number, |
| 310 |
.tp_as_sequence = &TypeKindSet_as_sequence, |
| 311 |
.tp_hash = (hashfunc)TypeKindSet_hash, |
| 312 |
// Doesn't reference any objects, no GC needed. |
| 313 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 314 |
.tp_doc = drgn_TypeKindSet_DOC, |
| 315 |
.tp_richcompare = (richcmpfunc)TypeKindSet_richcompare, |
| 316 |
.tp_iter = (getiterfunc)TypeKindSet_iter, |
| 317 |
.tp_methods = TypeKindSet_methods, |
| 318 |
.tp_new = (newfunc)TypeKindSet_new, |
| 319 |
}; |
| 320 |
|
| 321 |
static PyObject *TypeKindSetIterator_next(TypeKindSetIterator *self) |
| 322 |
{ |
| 323 |
if (self->mask == 0) |
| 324 |
return NULL; |
| 325 |
unsigned int i = ctz(self->mask); |
| 326 |
self->mask &= self->mask - 1; |
| 327 |
return PyObject_CallFunction(TypeKind_class, "I", i); |
| 328 |
} |
| 329 |
|
| 330 |
static PyObject *TypeKindSetIterator_length_hint(TypeKindSetIterator *self) |
| 331 |
{ |
| 332 |
return PyLong_FromUnsignedLong(popcount(self->mask)); |
| 333 |
} |
| 334 |
|
| 335 |
static PyMethodDef TypeKindSetIterator_methods[] = { |
| 336 |
{"__length_hint__", (PyCFunction)TypeKindSetIterator_length_hint, |
| 337 |
METH_NOARGS}, |
| 338 |
{}, |
| 339 |
}; |
| 340 |
|
| 341 |
PyTypeObject TypeKindSetIterator_type = { |
| 342 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 343 |
.tp_name = "_drgn._TypeKindSetIterator", |
| 344 |
.tp_basicsize = sizeof(TypeKindSetIterator), |
| 345 |
// Doesn't reference any objects, no GC needed. |
| 346 |
.tp_flags = Py_TPFLAGS_DEFAULT, |
| 347 |
.tp_iter = PyObject_SelfIter, |
| 348 |
.tp_iternext = (iternextfunc)TypeKindSetIterator_next, |
| 349 |
.tp_methods = TypeKindSetIterator_methods, |
| 350 |
}; |