llvmlite 0.44.0rc1__cp310-cp310-macosx_10_15_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of llvmlite might be problematic. Click here for more details.

Files changed (46) hide show
  1. llvmlite/__init__.py +10 -0
  2. llvmlite/_version.py +11 -0
  3. llvmlite/binding/__init__.py +19 -0
  4. llvmlite/binding/analysis.py +69 -0
  5. llvmlite/binding/common.py +34 -0
  6. llvmlite/binding/context.py +39 -0
  7. llvmlite/binding/dylib.py +45 -0
  8. llvmlite/binding/executionengine.py +330 -0
  9. llvmlite/binding/ffi.py +395 -0
  10. llvmlite/binding/initfini.py +73 -0
  11. llvmlite/binding/libllvmlite.dylib +0 -0
  12. llvmlite/binding/linker.py +20 -0
  13. llvmlite/binding/module.py +349 -0
  14. llvmlite/binding/newpassmanagers.py +357 -0
  15. llvmlite/binding/object_file.py +82 -0
  16. llvmlite/binding/options.py +17 -0
  17. llvmlite/binding/orcjit.py +342 -0
  18. llvmlite/binding/passmanagers.py +946 -0
  19. llvmlite/binding/targets.py +520 -0
  20. llvmlite/binding/transforms.py +151 -0
  21. llvmlite/binding/typeref.py +285 -0
  22. llvmlite/binding/value.py +632 -0
  23. llvmlite/ir/__init__.py +11 -0
  24. llvmlite/ir/_utils.py +80 -0
  25. llvmlite/ir/builder.py +1120 -0
  26. llvmlite/ir/context.py +20 -0
  27. llvmlite/ir/instructions.py +920 -0
  28. llvmlite/ir/module.py +246 -0
  29. llvmlite/ir/transforms.py +64 -0
  30. llvmlite/ir/types.py +734 -0
  31. llvmlite/ir/values.py +1217 -0
  32. llvmlite/tests/__init__.py +57 -0
  33. llvmlite/tests/__main__.py +3 -0
  34. llvmlite/tests/customize.py +407 -0
  35. llvmlite/tests/refprune_proto.py +329 -0
  36. llvmlite/tests/test_binding.py +3208 -0
  37. llvmlite/tests/test_ir.py +2986 -0
  38. llvmlite/tests/test_refprune.py +730 -0
  39. llvmlite/tests/test_valuerepr.py +60 -0
  40. llvmlite/utils.py +29 -0
  41. llvmlite-0.44.0rc1.dist-info/LICENSE +24 -0
  42. llvmlite-0.44.0rc1.dist-info/LICENSE.thirdparty +225 -0
  43. llvmlite-0.44.0rc1.dist-info/METADATA +138 -0
  44. llvmlite-0.44.0rc1.dist-info/RECORD +46 -0
  45. llvmlite-0.44.0rc1.dist-info/WHEEL +5 -0
  46. llvmlite-0.44.0rc1.dist-info/top_level.txt +1 -0
llvmlite/ir/types.py ADDED
@@ -0,0 +1,734 @@
1
+ """
2
+ Classes that are LLVM types
3
+ """
4
+
5
+ import struct
6
+
7
+ from llvmlite.ir._utils import _StrCaching
8
+
9
+ # FIXME: Remove me once typed pointers are no longer supported.
10
+ from llvmlite import opaque_pointers_enabled
11
+
12
+
13
+ def _wrapname(x):
14
+ return '"{0}"'.format(x.replace('\\', '\\5c').replace('"', '\\22'))
15
+
16
+
17
+ class Type(_StrCaching):
18
+ """
19
+ The base class for all LLVM types.
20
+ """
21
+ is_pointer = False
22
+ null = 'zeroinitializer'
23
+
24
+ def __repr__(self):
25
+ return "<%s %s>" % (type(self), str(self))
26
+
27
+ def _to_string(self):
28
+ raise NotImplementedError
29
+
30
+ def as_pointer(self, addrspace=0):
31
+ return PointerType(self, addrspace)
32
+
33
+ def __ne__(self, other):
34
+ return not (self == other)
35
+
36
+ def _get_ll_global_value_type(self, target_data, context=None):
37
+ """
38
+ Convert this type object to an LLVM type.
39
+ """
40
+ from llvmlite.ir import Module, GlobalVariable
41
+ from llvmlite.binding import parse_assembly
42
+
43
+ if context is None:
44
+ m = Module()
45
+ else:
46
+ m = Module(context=context)
47
+ foo = GlobalVariable(m, self, name="foo")
48
+ with parse_assembly(str(m)) as llmod:
49
+ return llmod.get_global_variable(foo.name).global_value_type
50
+
51
+ def get_abi_size(self, target_data, context=None):
52
+ """
53
+ Get the ABI size of this type according to data layout *target_data*.
54
+ """
55
+ llty = self._get_ll_global_value_type(target_data, context)
56
+ return target_data.get_abi_size(llty)
57
+
58
+ def get_element_offset(self, target_data, ndx, context=None):
59
+ llty = self._get_ll_global_value_type(target_data, context)
60
+ return target_data.get_element_offset(llty, ndx)
61
+
62
+ def get_abi_alignment(self, target_data, context=None):
63
+ """
64
+ Get the minimum ABI alignment of this type according to data layout
65
+ *target_data*.
66
+ """
67
+ llty = self._get_ll_global_value_type(target_data, context)
68
+ return target_data.get_abi_alignment(llty)
69
+
70
+ def format_constant(self, value):
71
+ """
72
+ Format constant *value* of this type. This method may be overriden
73
+ by subclasses.
74
+ """
75
+ return str(value)
76
+
77
+ def wrap_constant_value(self, value):
78
+ """
79
+ Wrap constant *value* if necessary. This method may be overriden
80
+ by subclasses (especially aggregate types).
81
+ """
82
+ return value
83
+
84
+ def __call__(self, value):
85
+ """
86
+ Create a LLVM constant of this type with the given Python value.
87
+ """
88
+ from llvmlite.ir import Constant
89
+ return Constant(self, value)
90
+
91
+
92
+ class MetaDataType(Type):
93
+
94
+ def _to_string(self):
95
+ return "metadata"
96
+
97
+ def as_pointer(self):
98
+ raise TypeError
99
+
100
+ def __eq__(self, other):
101
+ return isinstance(other, MetaDataType)
102
+
103
+ def __hash__(self):
104
+ return hash(MetaDataType)
105
+
106
+
107
+ class LabelType(Type):
108
+ """
109
+ The label type is the type of e.g. basic blocks.
110
+ """
111
+
112
+ def _to_string(self):
113
+ return "label"
114
+
115
+
116
+ class PointerType(Type):
117
+ """
118
+ The type of all pointer values.
119
+ By default (without specialisation) represents an opaque pointer.
120
+ """
121
+ is_opaque = True
122
+ is_pointer = True
123
+ null = 'null'
124
+
125
+ # Factory to create typed or opaque pointers based on `pointee'.
126
+ def __new__(cls, pointee=None, addrspace=0):
127
+ if cls is PointerType and pointee is not None:
128
+ return super().__new__(_TypedPointerType)
129
+ return super(PointerType, cls).__new__(cls)
130
+
131
+ def __init__(self, addrspace=0):
132
+ self.addrspace = addrspace
133
+
134
+ def _to_string(self):
135
+ if self.addrspace != 0:
136
+ return "ptr addrspace({0})".format(self.addrspace)
137
+ else:
138
+ return "ptr"
139
+
140
+ def __hash__(self):
141
+ return hash(PointerType)
142
+
143
+ @classmethod
144
+ def from_llvm(cls, typeref, ir_ctx):
145
+ """
146
+ Create from a llvmlite.binding.TypeRef
147
+ """
148
+ if not opaque_pointers_enabled:
149
+ return _TypedPointerType.from_llvm(typeref, ir_ctx)
150
+ return cls()
151
+
152
+
153
+ class _TypedPointerType(PointerType):
154
+ """
155
+ The type of typed pointer values. To be removed eventually.
156
+ """
157
+
158
+ def __init__(self, pointee, addrspace=0):
159
+ super(_TypedPointerType, self).__init__(addrspace)
160
+ assert pointee is not None
161
+ assert not isinstance(pointee, VoidType)
162
+ self.pointee = pointee
163
+ self.is_opaque = False
164
+
165
+ def _to_string(self):
166
+ if not opaque_pointers_enabled:
167
+ return "{0}*".format(self.pointee) if self.addrspace == 0 else \
168
+ "{0} addrspace({1})*".format(self.pointee, self.addrspace)
169
+ return super(_TypedPointerType, self)._to_string()
170
+
171
+ # This implements ``isOpaqueOrPointeeTypeEquals''.
172
+ def __eq__(self, other):
173
+ if isinstance(other, _TypedPointerType):
174
+ return (self.pointee, self.addrspace) == (other.pointee,
175
+ other.addrspace)
176
+ return isinstance(other, PointerType)
177
+
178
+ def __hash__(self):
179
+ return hash(_TypedPointerType)
180
+
181
+ def gep(self, i):
182
+ """
183
+ Resolve the type of the i-th element (for getelementptr lookups).
184
+ """
185
+ if not isinstance(i.type, IntType):
186
+ raise TypeError(i.type)
187
+ return self.pointee
188
+
189
+ @property
190
+ def intrinsic_name(self):
191
+ return 'p%d%s' % (self.addrspace, self.pointee.intrinsic_name)
192
+
193
+ @classmethod
194
+ def from_llvm(cls, typeref, ir_ctx):
195
+ """
196
+ Create from a llvmlite.binding.TypeRef
197
+ """
198
+ assert not opaque_pointers_enabled
199
+ # opaque pointer will change this
200
+ [pointee] = typeref.elements
201
+ # addrspace is not handled
202
+ return cls(pointee.as_ir(ir_ctx=ir_ctx))
203
+
204
+
205
+ class VoidType(Type):
206
+ """
207
+ The type for empty values (e.g. a function returning no value).
208
+ """
209
+
210
+ def _to_string(self):
211
+ return 'void'
212
+
213
+ def __eq__(self, other):
214
+ return isinstance(other, VoidType)
215
+
216
+ def __hash__(self):
217
+ return hash(VoidType)
218
+
219
+ @classmethod
220
+ def from_llvm(cls, typeref, ir_ctx):
221
+ """
222
+ Create from a llvmlite.binding.TypeRef
223
+ """
224
+ return cls()
225
+
226
+
227
+ class FunctionType(Type):
228
+ """
229
+ The type for functions.
230
+ """
231
+
232
+ def __init__(self, return_type, args, var_arg=False):
233
+ self.return_type = return_type
234
+ self.args = tuple(args)
235
+ self.var_arg = var_arg
236
+
237
+ def _to_string(self):
238
+ if self.args:
239
+ strargs = ', '.join([str(a) for a in self.args])
240
+ if self.var_arg:
241
+ return '{0} ({1}, ...)'.format(self.return_type, strargs)
242
+ else:
243
+ return '{0} ({1})'.format(self.return_type, strargs)
244
+ elif self.var_arg:
245
+ return '{0} (...)'.format(self.return_type)
246
+ else:
247
+ return '{0} ()'.format(self.return_type)
248
+
249
+ def __eq__(self, other):
250
+ if isinstance(other, FunctionType):
251
+ return (self.return_type == other.return_type and
252
+ self.args == other.args and self.var_arg == other.var_arg)
253
+ else:
254
+ return False
255
+
256
+ def __hash__(self):
257
+ return hash(FunctionType)
258
+
259
+ @classmethod
260
+ def from_llvm(cls, typeref, ir_ctx):
261
+ """
262
+ Create from a llvmlite.binding.TypeRef
263
+ """
264
+ params = tuple(x.as_ir(ir_ctx=ir_ctx)
265
+ for x in typeref.get_function_parameters())
266
+ ret = typeref.get_function_return().as_ir(ir_ctx=ir_ctx)
267
+ is_vararg = typeref.is_function_vararg
268
+ return cls(ret, params, is_vararg)
269
+
270
+
271
+ class IntType(Type):
272
+ """
273
+ The type for integers.
274
+ """
275
+ null = '0'
276
+ _instance_cache = {}
277
+ width: int
278
+
279
+ def __new__(cls, bits):
280
+ # Cache all common integer types
281
+ if 0 <= bits <= 128:
282
+ try:
283
+ return cls._instance_cache[bits]
284
+ except KeyError:
285
+ inst = cls._instance_cache[bits] = cls.__new(bits)
286
+ return inst
287
+ return cls.__new(bits)
288
+
289
+ @classmethod
290
+ def __new(cls, bits):
291
+ assert isinstance(bits, int) and bits >= 0
292
+ self = super(IntType, cls).__new__(cls)
293
+ self.width = bits
294
+ return self
295
+
296
+ def __getnewargs__(self):
297
+ return self.width,
298
+
299
+ def __copy__(self):
300
+ return self
301
+
302
+ def _to_string(self):
303
+ return 'i%u' % (self.width,)
304
+
305
+ def __eq__(self, other):
306
+ if isinstance(other, IntType):
307
+ return self.width == other.width
308
+ else:
309
+ return False
310
+
311
+ def __hash__(self):
312
+ return hash(IntType)
313
+
314
+ def format_constant(self, val):
315
+ if isinstance(val, bool):
316
+ return str(val).lower()
317
+ else:
318
+ return str(val)
319
+
320
+ def wrap_constant_value(self, val):
321
+ if val is None:
322
+ return 0
323
+ return val
324
+
325
+ @property
326
+ def intrinsic_name(self):
327
+ return str(self)
328
+
329
+ @classmethod
330
+ def from_llvm(cls, typeref, ir_ctx):
331
+ """
332
+ Create from a llvmlite.binding.TypeRef
333
+ """
334
+ return IntType(typeref.type_width)
335
+
336
+
337
+ def _as_float(value):
338
+ """
339
+ Truncate to single-precision float.
340
+ """
341
+ return struct.unpack('f', struct.pack('f', value))[0]
342
+
343
+
344
+ def _as_half(value):
345
+ """
346
+ Truncate to half-precision float.
347
+ """
348
+ try:
349
+ return struct.unpack('e', struct.pack('e', value))[0]
350
+ except struct.error:
351
+ # 'e' only added in Python 3.6+
352
+ return _as_float(value)
353
+
354
+
355
+ def _format_float_as_hex(value, packfmt, unpackfmt, numdigits):
356
+ raw = struct.pack(packfmt, float(value))
357
+ intrep = struct.unpack(unpackfmt, raw)[0]
358
+ out = '{{0:#{0}x}}'.format(numdigits).format(intrep)
359
+ return out
360
+
361
+
362
+ def _format_double(value):
363
+ """
364
+ Format *value* as a hexadecimal string of its IEEE double precision
365
+ representation.
366
+ """
367
+ return _format_float_as_hex(value, 'd', 'Q', 16)
368
+
369
+
370
+ class _BaseFloatType(Type):
371
+
372
+ def __new__(cls):
373
+ return cls._instance_cache
374
+
375
+ def __eq__(self, other):
376
+ return isinstance(other, type(self))
377
+
378
+ def __hash__(self):
379
+ return hash(type(self))
380
+
381
+ @classmethod
382
+ def _create_instance(cls):
383
+ cls._instance_cache = super(_BaseFloatType, cls).__new__(cls)
384
+
385
+ @classmethod
386
+ def from_llvm(cls, typeref, ir_ctx):
387
+ """
388
+ Create from a llvmlite.binding.TypeRef
389
+ """
390
+ return cls()
391
+
392
+
393
+ class HalfType(_BaseFloatType):
394
+ """
395
+ The type for single-precision floats.
396
+ """
397
+ null = '0.0'
398
+ intrinsic_name = 'f16'
399
+
400
+ def __str__(self):
401
+ return 'half'
402
+
403
+ def format_constant(self, value):
404
+ return _format_double(_as_half(value))
405
+
406
+
407
+ class FloatType(_BaseFloatType):
408
+ """
409
+ The type for single-precision floats.
410
+ """
411
+ null = '0.0'
412
+ intrinsic_name = 'f32'
413
+
414
+ def __str__(self):
415
+ return 'float'
416
+
417
+ def format_constant(self, value):
418
+ return _format_double(_as_float(value))
419
+
420
+
421
+ class DoubleType(_BaseFloatType):
422
+ """
423
+ The type for double-precision floats.
424
+ """
425
+ null = '0.0'
426
+ intrinsic_name = 'f64'
427
+
428
+ def __str__(self):
429
+ return 'double'
430
+
431
+ def format_constant(self, value):
432
+ return _format_double(value)
433
+
434
+
435
+ for _cls in (HalfType, FloatType, DoubleType):
436
+ _cls._create_instance()
437
+
438
+
439
+ class _Repeat(object):
440
+ def __init__(self, value, size):
441
+ self.value = value
442
+ self.size = size
443
+
444
+ def __len__(self):
445
+ return self.size
446
+
447
+ def __getitem__(self, item):
448
+ if 0 <= item < self.size:
449
+ return self.value
450
+ else:
451
+ raise IndexError(item)
452
+
453
+
454
+ class VectorType(Type):
455
+ """
456
+ The type for vectors of primitive data items (e.g. "<f32 x 4>").
457
+ """
458
+
459
+ def __init__(self, element, count):
460
+ self.element = element
461
+ self.count = count
462
+
463
+ @property
464
+ def elements(self):
465
+ return _Repeat(self.element, self.count)
466
+
467
+ def __len__(self):
468
+ return self.count
469
+
470
+ def _to_string(self):
471
+ return "<%d x %s>" % (self.count, self.element)
472
+
473
+ def __eq__(self, other):
474
+ if isinstance(other, VectorType):
475
+ return self.element == other.element and self.count == other.count
476
+
477
+ def __hash__(self):
478
+ # TODO: why does this not take self.element/self.count into account?
479
+ return hash(VectorType)
480
+
481
+ def __copy__(self):
482
+ return self
483
+
484
+ def format_constant(self, value):
485
+ itemstring = ", " .join(["{0} {1}".format(x.type, x.get_reference())
486
+ for x in value])
487
+ return "<{0}>".format(itemstring)
488
+
489
+ def wrap_constant_value(self, values):
490
+ from . import Value, Constant
491
+ if not isinstance(values, (list, tuple)):
492
+ if isinstance(values, Constant):
493
+ if values.type != self.element:
494
+ raise TypeError("expected {} for {}".format(
495
+ self.element, values.type))
496
+ return (values, ) * self.count
497
+ return (Constant(self.element, values), ) * self.count
498
+ if len(values) != len(self):
499
+ raise ValueError("wrong constant size for %s: got %d elements"
500
+ % (self, len(values)))
501
+ return [Constant(ty, val) if not isinstance(val, Value) else val
502
+ for ty, val in zip(self.elements, values)]
503
+
504
+ @classmethod
505
+ def from_llvm(cls, typeref, ir_ctx):
506
+ """
507
+ Create from a llvmlite.binding.TypeRef
508
+ """
509
+ [elemtyperef] = typeref.elements
510
+ elemty = elemtyperef.as_ir(ir_ctx=ir_ctx)
511
+ count = typeref.element_count
512
+ return cls(elemty, count)
513
+
514
+
515
+ class Aggregate(Type):
516
+ """
517
+ Base class for aggregate types.
518
+ See http://llvm.org/docs/LangRef.html#t-aggregate
519
+ """
520
+
521
+ def wrap_constant_value(self, values):
522
+ from . import Value, Constant
523
+
524
+ if not isinstance(values, (list, tuple)):
525
+ return values
526
+ if len(values) != len(self):
527
+ raise ValueError("wrong constant size for %s: got %d elements"
528
+ % (self, len(values)))
529
+ return [Constant(ty, val) if not isinstance(val, Value) else val
530
+ for ty, val in zip(self.elements, values)]
531
+
532
+
533
+ class ArrayType(Aggregate):
534
+ """
535
+ The type for fixed-size homogenous arrays (e.g. "[f32 x 3]").
536
+ """
537
+
538
+ def __init__(self, element, count):
539
+ self.element = element
540
+ self.count = count
541
+
542
+ @property
543
+ def elements(self):
544
+ return _Repeat(self.element, self.count)
545
+
546
+ def __len__(self):
547
+ return self.count
548
+
549
+ def _to_string(self):
550
+ return "[%d x %s]" % (self.count, self.element)
551
+
552
+ def __eq__(self, other):
553
+ if isinstance(other, ArrayType):
554
+ return self.element == other.element and self.count == other.count
555
+
556
+ def __hash__(self):
557
+ return hash(ArrayType)
558
+
559
+ def gep(self, i):
560
+ """
561
+ Resolve the type of the i-th element (for getelementptr lookups).
562
+ """
563
+ if not isinstance(i.type, IntType):
564
+ raise TypeError(i.type)
565
+ return self.element
566
+
567
+ def format_constant(self, value):
568
+ itemstring = ", " .join(["{0} {1}".format(x.type, x.get_reference())
569
+ for x in value])
570
+ return "[{0}]".format(itemstring)
571
+
572
+ @classmethod
573
+ def from_llvm(cls, typeref, ir_ctx):
574
+ """
575
+ Create from a llvmlite.binding.TypeRef
576
+ """
577
+ [elemtyperef] = typeref.elements
578
+ elemty = elemtyperef.as_ir(ir_ctx=ir_ctx)
579
+ count = typeref.element_count
580
+ return cls(elemty, count)
581
+
582
+
583
+ class BaseStructType(Aggregate):
584
+ """
585
+ The base type for heterogenous struct types.
586
+ """
587
+ _packed = False
588
+
589
+ @property
590
+ def packed(self):
591
+ """
592
+ A boolean attribute that indicates whether the structure uses
593
+ packed layout.
594
+ """
595
+ return self._packed
596
+
597
+ @packed.setter
598
+ def packed(self, val):
599
+ self._packed = bool(val)
600
+
601
+ def __len__(self):
602
+ assert self.elements is not None
603
+ return len(self.elements)
604
+
605
+ def __iter__(self):
606
+ assert self.elements is not None
607
+ return iter(self.elements)
608
+
609
+ @property
610
+ def is_opaque(self):
611
+ return self.elements is None
612
+
613
+ def structure_repr(self):
614
+ """
615
+ Return the LLVM IR for the structure representation
616
+ """
617
+ ret = '{%s}' % ', '.join([str(x) for x in self.elements])
618
+ return self._wrap_packed(ret)
619
+
620
+ def format_constant(self, value):
621
+ itemstring = ", " .join(["{0} {1}".format(x.type, x.get_reference())
622
+ for x in value])
623
+ ret = "{{{0}}}".format(itemstring)
624
+ return self._wrap_packed(ret)
625
+
626
+ def gep(self, i):
627
+ """
628
+ Resolve the type of the i-th element (for getelementptr lookups).
629
+
630
+ *i* needs to be a LLVM constant, so that the type can be determined
631
+ at compile-time.
632
+ """
633
+ if not isinstance(i.type, IntType):
634
+ raise TypeError(i.type)
635
+ return self.elements[i.constant]
636
+
637
+ def _wrap_packed(self, textrepr):
638
+ """
639
+ Internal helper to wrap textual repr of struct type into packed struct
640
+ """
641
+ if self.packed:
642
+ return '<{}>'.format(textrepr)
643
+ else:
644
+ return textrepr
645
+
646
+ @classmethod
647
+ def from_llvm(cls, typeref, ir_ctx):
648
+ """
649
+ Create from a llvmlite.binding.TypeRef
650
+ """
651
+ if typeref.is_literal_struct:
652
+ elems = [el.as_ir(ir_ctx=ir_ctx) for el in typeref.elements]
653
+ return cls(elems, typeref.is_packed_struct)
654
+ else:
655
+ return ir_ctx.get_identified_type(typeref.name)
656
+
657
+
658
+ class LiteralStructType(BaseStructType):
659
+ """
660
+ The type of "literal" structs, i.e. structs with a literally-defined
661
+ type (by contrast with IdentifiedStructType).
662
+ """
663
+
664
+ null = 'zeroinitializer'
665
+
666
+ def __init__(self, elems, packed=False):
667
+ """
668
+ *elems* is a sequence of types to be used as members.
669
+ *packed* controls the use of packed layout.
670
+ """
671
+ self.elements = tuple(elems)
672
+ self.packed = packed
673
+
674
+ def _to_string(self):
675
+ return self.structure_repr()
676
+
677
+ def __eq__(self, other):
678
+ if isinstance(other, LiteralStructType):
679
+ return (self.elements == other.elements
680
+ and self.packed == other.packed)
681
+
682
+ def __hash__(self):
683
+ return hash(LiteralStructType)
684
+
685
+
686
+ class IdentifiedStructType(BaseStructType):
687
+ """
688
+ A type which is a named alias for another struct type, akin to a typedef.
689
+ While literal struct types can be structurally equal (see
690
+ LiteralStructType), identified struct types are compared by name.
691
+
692
+ Do not use this directly.
693
+ """
694
+ null = 'zeroinitializer'
695
+
696
+ def __init__(self, context, name, packed=False):
697
+ """
698
+ *context* is a llvmlite.ir.Context.
699
+ *name* is the identifier for the new struct type.
700
+ *packed* controls the use of packed layout.
701
+ """
702
+ assert name
703
+ self.context = context
704
+ self.name = name
705
+ self.elements = None
706
+ self.packed = packed
707
+
708
+ def _to_string(self):
709
+ return "%{name}".format(name=_wrapname(self.name))
710
+
711
+ def get_declaration(self):
712
+ """
713
+ Returns the string for the declaration of the type
714
+ """
715
+ if self.is_opaque:
716
+ out = "{strrep} = type opaque".format(strrep=str(self))
717
+ else:
718
+ out = "{strrep} = type {struct}".format(
719
+ strrep=str(self), struct=self.structure_repr())
720
+ return out
721
+
722
+ def __eq__(self, other):
723
+ if isinstance(other, IdentifiedStructType):
724
+ return (self.name == other.name
725
+ and self.packed == other.packed)
726
+
727
+ def __hash__(self):
728
+ return hash(IdentifiedStructType)
729
+
730
+ def set_body(self, *elems):
731
+ if not self.is_opaque:
732
+ raise RuntimeError("{name} is already defined".format(
733
+ name=self.name))
734
+ self.elements = tuple(elems)