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