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 | @public
@typechecked
def same_type(lhs: astx.DataType | None, rhs: astx.DataType | None) -> bool:
"""
title: Return whether two AST types share the same class.
parameters:
lhs:
type: astx.DataType | None
rhs:
type: astx.DataType | None
returns:
type: bool
"""
if lhs is None or rhs is None:
return False
if isinstance(lhs, astx.UnionType) and isinstance(rhs, astx.UnionType):
if lhs.alias_name != rhs.alias_name:
return False
if len(lhs.members) != len(rhs.members):
return False
return all(
same_type(left_member, right_member)
for left_member, right_member in zip(lhs.members, rhs.members)
)
if isinstance(lhs, astx.TemplateTypeVar) and isinstance(
rhs,
astx.TemplateTypeVar,
):
return lhs.name == rhs.name and same_type(lhs.bound, rhs.bound)
if isinstance(lhs, astx.GeneratorType) and isinstance(
rhs,
astx.GeneratorType,
):
return same_type(lhs.yield_type, rhs.yield_type)
if isinstance(lhs, astx.StructType) and isinstance(rhs, astx.StructType):
lhs_identity = lhs.qualified_name or lhs.name
rhs_identity = rhs.qualified_name or rhs.name
return lhs_identity == rhs_identity
if isinstance(lhs, astx.ClassType) and isinstance(rhs, astx.ClassType):
lhs_identity = lhs.qualified_name or lhs.name
rhs_identity = rhs.qualified_name or rhs.name
return lhs_identity == rhs_identity
if isinstance(lhs, astx.NamespaceType) and isinstance(
rhs,
astx.NamespaceType,
):
return (
lhs.namespace_key == rhs.namespace_key
and lhs.namespace_kind is rhs.namespace_kind
)
if isinstance(lhs, astx.PointerType) and isinstance(rhs, astx.PointerType):
if lhs.pointee_type is None or rhs.pointee_type is None:
return lhs.pointee_type is None and rhs.pointee_type is None
return same_type(lhs.pointee_type, rhs.pointee_type)
if isinstance(lhs, astx.ListType) and isinstance(rhs, astx.ListType):
if len(lhs.element_types) != len(rhs.element_types):
return False
return all(
same_type(
cast(astx.DataType, left_member),
cast(astx.DataType, right_member),
)
for left_member, right_member in zip(
lhs.element_types,
rhs.element_types,
)
)
if isinstance(lhs, astx.TupleType) and isinstance(rhs, astx.TupleType):
if len(lhs.element_types) != len(rhs.element_types):
return False
return all(
same_type(
cast(astx.DataType, left_member),
cast(astx.DataType, right_member),
)
for left_member, right_member in zip(
lhs.element_types,
rhs.element_types,
)
)
if isinstance(lhs, astx.SetType) and isinstance(rhs, astx.SetType):
return same_type(lhs.element_type, rhs.element_type)
if isinstance(lhs, astx.DictType) and isinstance(rhs, astx.DictType):
return same_type(lhs.key_type, rhs.key_type) and same_type(
lhs.value_type,
rhs.value_type,
)
if isinstance(lhs, astx.OpaqueHandleType) and isinstance(
rhs,
astx.OpaqueHandleType,
):
return lhs.handle_name == rhs.handle_name
if isinstance(lhs, astx.BufferViewType) and isinstance(
rhs,
astx.BufferViewType,
):
if lhs.element_type is None or rhs.element_type is None:
return True
return same_type(lhs.element_type, rhs.element_type)
if isinstance(lhs, astx.TensorType) and isinstance(
rhs,
astx.TensorType,
):
if lhs.element_type is None or rhs.element_type is None:
return True
return same_type(lhs.element_type, rhs.element_type)
return lhs.__class__ is rhs.__class__
|