1 /**
2 Copyright: Copyright (c) 2017, Joakim Brännström. All rights reserved.
3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 */
6 module llvm_hiwrap.type.struct_;
7 
8 import llvm_hiwrap.types;
9 
10 struct StructType {
11     import llvm;
12 
13     LxType type;
14     alias type this;
15 
16     @property const(char)[] name() {
17         import std.string : fromStringz;
18 
19         auto s = LLVMGetStructName(type);
20         return s.fromStringz;
21     }
22 
23     @property auto elements() {
24         return ElementsRange(this);
25     }
26 
27     /// Returns: Determine whether a structure is packed.
28     bool isPacked() nothrow {
29         return LLVMIsPackedStruct(type) != 0;
30     }
31 
32     /// Returns: Determine whether a structure is opaque.
33     bool isOpaque() nothrow {
34         return LLVMIsOpaqueStruct(type) != 0;
35     }
36 }
37 
38 /// The elements defined in a struct.
39 struct ElementsRange {
40     import llvm;
41 
42     StructType type;
43     alias type this;
44 
45     // The number of elements in the struct.
46     const size_t length;
47 
48     this(StructType t) nothrow {
49         auto len = LLVMCountStructElementTypes(type);
50         if (len >= size_t.max) {
51             length = size_t.max;
52             assert(0, "unreasonable parameter count (>= size_t)");
53         } else {
54             length = len;
55         }
56     }
57 
58     /// Returns: The type of the element at a given index in the structure.
59     LxType opIndex(size_t index) nothrow {
60         assert(index < length);
61         assert(index < uint.max);
62 
63         auto t = LLVMStructGetTypeAtIndex(type, cast(uint) index);
64         return LxType(t);
65     }
66 
67     import llvm_hiwrap.util : IndexedRangeX;
68 
69     mixin IndexedRangeX!LxType;
70 }