/
function_collection.cr
53 lines (43 loc) · 1.45 KB
/
function_collection.cr
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
struct LLVM::FunctionCollection
def initialize(@mod : Module)
end
def add(name, arg_types : Array(LLVM::Type), ret_type, varargs = false)
# check_types_context(name, arg_types, ret_type)
fun_type = LLVM::Type.function(arg_types, ret_type, varargs)
func = LibLLVM.add_function(@mod, name, fun_type)
Function.new(func)
end
def add(name, arg_types : Array(LLVM::Type), ret_type, varargs = false)
func = add(name, arg_types, ret_type, varargs)
yield func
func
end
def [](name)
func = self[name]?
func || raise "Undefined llvm function: #{name}"
end
def []?(name)
func = LibLLVM.get_named_function(@mod, name)
func ? Function.new(func) : nil
end
def each : Nil
f = LibLLVM.get_first_function(@mod)
while f
yield LLVM::Function.new f
f = LibLLVM.get_next_function(f)
end
end
# The next lines are for ease debugging when a types/values
# are incorrectly used across contexts.
# private def check_types_context(name, arg_types, ret_type)
# ctx = @mod.context
# arg_types.each_with_index do |arg_type, index|
# if arg_type.context != ctx
# Context.wrong(ctx, arg_type.context, "wrong context for function #{name} in #{@mod.name}, index #{index}, type #{arg_type}")
# end
# end
# if ret_type.context != ctx
# Context.wrong(ctx, ret_type.context, "wrong context for function #{name} in #{@mod.name}, return type #{ret_type}")
# end
# end
end