-
Notifications
You must be signed in to change notification settings - Fork 8
/
primitive_cast.cpp
76 lines (75 loc) · 2.99 KB
/
primitive_cast.cpp
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
#include "primitive_cast.h"
#include "string_utils.h"
compiler::instruction_ptr compiler::cast_primitive(type_base_ptr from_type, type_base_ptr to_type)
{
string to = to_type->id;
if (to == type_int->id)
return cast_primitive0<int>(from_type);
else if (to == type_float->id)
return cast_primitive0<float>(from_type);
else if (to == type_double->id)
return cast_primitive0<double>(from_type);
else if (to == type_long_double->id)
return cast_primitive0<long double>(from_type);
else if (to == type_long->id)
return cast_primitive0<long>(from_type);
else if (to == type_int64->id)
return cast_primitive0<long long>(from_type);
else if (to == type_bool->id)
return cast_primitive0<bool>(from_type);
else if (to == type_char->id)
return cast_primitive0<char>(from_type);
else if (to == type_short->id)
return cast_primitive0<short>(from_type);
else if (to == type_byte->id)
return cast_primitive0<unsigned char>(from_type);
else if (to == type_ushort->id)
return cast_primitive0<unsigned short>(from_type);
else if (to == type_uint->id)
return cast_primitive0<unsigned int>(from_type);
else if (to == type_ulong->id)
return cast_primitive0<unsigned long>(from_type);
else if (to == type_uint64->id)
return cast_primitive0<unsigned long long>(from_type);
else if (to_type->is_pointer())
{
if (from_type->is_integral())
return cast_primitive0<void*>(from_type);
// no need to convert void*
return instruction_ptr();
}
else
throw std::runtime_error(string_format("primitive cast does not support type %s", to.c_str()));
}
namespace compiler
{
template<>
instruction_ptr cast_primitive0<void*>(type_base_ptr from_type)
{
string from = from_type->id;
if (from == type_int->id)
return instruction_ptr(new instruction_primitive_cast<int, void*>());
else if (from == type_long->id)
return instruction_ptr(new instruction_primitive_cast<long, void*>());
else if (from == type_int64->id)
return instruction_ptr(new instruction_primitive_cast<long long, void*>());
else if (from == type_bool->id)
return instruction_ptr(new instruction_primitive_cast<bool, void*>());
else if (from == type_char->id)
return instruction_ptr(new instruction_primitive_cast<char, void*>());
else if (from == type_short->id)
return instruction_ptr(new instruction_primitive_cast<short, void*>());
else if (from == type_byte->id)
return instruction_ptr(new instruction_primitive_cast<unsigned char, void*>());
else if (from == type_ushort->id)
return instruction_ptr(new instruction_primitive_cast<unsigned short, void*>());
else if (from == type_uint->id)
return instruction_ptr(new instruction_primitive_cast<unsigned int, void*>());
else if (from == type_ulong->id)
return instruction_ptr(new instruction_primitive_cast<unsigned long, void*>());
else if (from == type_uint64->id)
return instruction_ptr(new instruction_primitive_cast<unsigned long long, void*>());
else
throw std::runtime_error(string_format("primitive cast does not support type %s", from.c_str()));
}
}