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.function_;
7 
8 import llvm_hiwrap.types;
9 
10 struct FunctionType {
11     LxType type;
12     alias type this;
13 
14     /// Returns: whether a function type is variadic.
15     bool isVariadic() nothrow {
16         import llvm : LLVMIsFunctionVarArg;
17 
18         return LLVMIsFunctionVarArg(type) != 0;
19     }
20 
21     /// Returns: Obtain the Type this function Type returns.
22     LxType returnType() nothrow {
23         import llvm : LLVMGetReturnType;
24 
25         auto t = LLVMGetReturnType(type);
26         return LxType(t);
27     }
28 
29     /// Returns: an iterator over the parameters.
30     ParametersRange parameters() nothrow {
31         return ParametersRange(this);
32     }
33 }
34 
35 /// The parameters for a function.
36 struct ParametersRange {
37     import llvm;
38 
39     FunctionType type;
40     alias type this;
41 
42     private LLVMTypeRef[] params;
43 
44     /// The number of parameters this function accepts.
45     const size_t length;
46 
47     this(FunctionType t) nothrow {
48         import llvm : LLVMCountParamTypes;
49 
50         auto len = LLVMCountParamTypes(type);
51         if (len >= size_t.max) {
52             length = size_t.max;
53             assert(0, "unreasonable parameter count (>= size_t)");
54         } else {
55             length = len;
56         }
57 
58         params.length = length;
59         LLVMGetParamTypes(type, params.ptr);
60     }
61 
62     LxType opIndex(size_t index) @safe nothrow @nogc {
63         assert(index < length);
64         return LxType(params[index]);
65     }
66 
67     import llvm_hiwrap.util : IndexedRangeX;
68 
69     mixin IndexedRangeX!LxType;
70 }