-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
function.cr
86 lines (73 loc) · 2.24 KB
/
function.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require "./value_methods"
struct LLVM::Function
include LLVM::ValueMethods
def basic_blocks
BasicBlockCollection.new self
end
def call_convention
LLVM::CallConvention.new LibLLVM.get_function_call_convention(self)
end
def call_convention=(cc)
LibLLVM.set_function_call_convention(self, cc)
end
def add_attribute(attribute : Attribute, index = AttributeIndex::FunctionIndex)
return if attribute.value == 0
{% if LibLLVM.has_constant?(:AttributeRef) %}
context = LibLLVM.get_module_context(LibLLVM.get_global_parent(self))
attribute.each_kind do |kind|
attribute_ref = LibLLVM.create_enum_attribute(context, kind, 0)
LibLLVM.add_attribute_at_index(self, index, attribute_ref)
end
{% else %}
case index
when AttributeIndex::FunctionIndex
LibLLVM.add_function_attr(self, attribute)
when AttributeIndex::ReturnIndex
raise "Unsupported: can't set attributes on function return type in LLVM < 3.9"
else
LibLLVM.add_attribute(params[index.to_i - 1], attribute)
end
{% end %}
end
def add_target_dependent_attribute(name, value)
LibLLVM.add_target_dependent_function_attr self, name, value
end
def attributes(index = AttributeIndex::FunctionIndex)
{% if LibLLVM.has_constant?(:AttributeRef) %}
attrs = Attribute::None
0.upto(LibLLVM.get_last_enum_attribute_kind) do |kind|
if LibLLVM.get_enum_attribute_at_index(self, index, kind)
attrs |= Attribute.from_kind(kind)
end
end
attrs
{% else %}
case index
when AttributeIndex::FunctionIndex
LibLLVM.get_function_attr(self)
when AttributeIndex::ReturnIndex
raise "Unsupported: can't get attributes from function return type in LLVM < 3.9"
else
LibLLVM.get_attribute(params[index.to_i - 1])
end
{% end %}
end
def function_type
Type.new LibLLVM.get_element_type(LibLLVM.type_of(self))
end
def return_type
function_type.return_type
end
def varargs?
function_type.varargs?
end
def params
ParameterCollection.new self
end
def personality_function=(fn)
LibLLVM.set_personality_fn(self, fn)
end
def delete
LibLLVM.delete_function(self)
end
end