FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lpython/src/libasr/casting_utils.cpp at main · Viocid/lpython · GitHub
Viocid
/
lpython
Public
forked from
lcompilers/lpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lpython
/
src
/
libasr
/
casting_utils.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
149 lines (137 loc) · 7.35 KB
Breadcrumbs
lpython
/
src
/
libasr
/
casting_utils.cpp
Copy path
File metadata and controls
149 lines (137 loc) · 7.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#
include
<
libasr/casting_utils.h
>
#
include
<
libasr/asr_utils.h
>
#
include
<
map
>
namespace
LCompilers
::CastingUtil {
//
Data structure which contains priorities for
//
intrinsic types defined in ASR
const
std::map<
ASR
::ttypeType,
int
>& type2weight = {
{
ASR
::ttypeType::Complex,
4
},
{
ASR
::ttypeType::Real,
3
},
{
ASR
::ttypeType::Integer,
2
},
{
ASR
::ttypeType::Logical,
1
}
};
//
Data structure which contains casting rules for non-equal
//
intrinsic types defined in ASR
const
std::map<std::pair<
ASR
::ttypeType,
ASR
::ttypeType>,
ASR
::cast_kindType>& type_rules = {
{
std::make_pair
(
ASR
::ttypeType::Complex,
ASR
::ttypeType::Real),
ASR
::cast_kindType::ComplexToReal},
{
std::make_pair
(
ASR
::ttypeType::Complex,
ASR
::ttypeType::Logical),
ASR
::cast_kindType::ComplexToLogical},
{
std::make_pair
(
ASR
::ttypeType::Real,
ASR
::ttypeType::Complex),
ASR
::cast_kindType::RealToComplex},
{
std::make_pair
(
ASR
::ttypeType::Real,
ASR
::ttypeType::Integer),
ASR
::cast_kindType::RealToInteger},
{
std::make_pair
(
ASR
::ttypeType::Real,
ASR
::ttypeType::Logical),
ASR
::cast_kindType::RealToLogical},
{
std::make_pair
(
ASR
::ttypeType::Real,
ASR
::ttypeType::UnsignedInteger),
ASR
::cast_kindType::RealToUnsignedInteger},
{
std::make_pair
(
ASR
::ttypeType::Integer,
ASR
::ttypeType::Complex),
ASR
::cast_kindType::IntegerToComplex},
{
std::make_pair
(
ASR
::ttypeType::Integer,
ASR
::ttypeType::Real),
ASR
::cast_kindType::IntegerToReal},
{
std::make_pair
(
ASR
::ttypeType::Integer,
ASR
::ttypeType::Logical),
ASR
::cast_kindType::IntegerToLogical},
{
std::make_pair
(
ASR
::ttypeType::Integer,
ASR
::ttypeType::UnsignedInteger),
ASR
::cast_kindType::IntegerToUnsignedInteger},
{
std::make_pair
(
ASR
::ttypeType::Logical,
ASR
::ttypeType::Real),
ASR
::cast_kindType::LogicalToReal},
{
std::make_pair
(
ASR
::ttypeType::Logical,
ASR
::ttypeType::Integer),
ASR
::cast_kindType::LogicalToInteger},
{
std::make_pair
(
ASR
::ttypeType::UnsignedInteger,
ASR
::ttypeType::Integer),
ASR
::cast_kindType::UnsignedIntegerToInteger},
{
std::make_pair
(
ASR
::ttypeType::UnsignedInteger,
ASR
::ttypeType::Real),
ASR
::cast_kindType::UnsignedIntegerToReal},
{
std::make_pair
(
ASR
::ttypeType::Integer,
ASR
::ttypeType::SymbolicExpression),
ASR
::cast_kindType::IntegerToSymbolicExpression}
};
//
Data structure which contains casting rules for equal intrinsic
//
types but with different kinds.
const
std::map<
ASR
::ttypeType,
ASR
::cast_kindType>& kind_rules = {
{
ASR
::ttypeType::Complex,
ASR
::cast_kindType::ComplexToComplex},
{
ASR
::ttypeType::Real,
ASR
::cast_kindType::RealToReal},
{
ASR
::ttypeType::Integer,
ASR
::cast_kindType::IntegerToInteger},
{
ASR
::ttypeType::UnsignedInteger,
ASR
::cast_kindType::UnsignedIntegerToUnsignedInteger}
};
int
get_type_priority
(
ASR
::ttypeType type) {
if
( type2weight.
find
(type) == type2weight.
end
() ) {
return
-
1
;
}
return
type2weight.
at
(type);
}
int
get_src_dest
(
ASR
::
expr_t
* left_expr,
ASR
::
expr_t
* right_expr,
ASR
::
expr_t
*& src_expr,
ASR
::
expr_t
*& dest_expr,
ASR
::
ttype_t
*& src_type,
ASR
::
ttype_t
*& dest_type,
bool
is_assign,
bool
allow_int_to_float) {
ASR
::
ttype_t
* left_type =
ASRUtils::expr_type
(left_expr);
ASR
::
ttype_t
* right_type =
ASRUtils::expr_type
(right_expr);
left_type =
ASRUtils::type_get_past_pointer
(left_type);
right_type =
ASRUtils::type_get_past_pointer
(right_type);
if
(
ASRUtils::check_equal_type
(left_type, right_type) ||
ASRUtils::is_character
(*left_type) ||
ASRUtils::is_character
(*right_type) ) {
return
2
;
}
if
( is_assign ) {
if
(
ASRUtils::is_real
(*left_type) &&
ASRUtils::is_integer
(*right_type) &&
!allow_int_to_float) {
throw
LCompilersException
(
"
Assigning integer to float is not supported
"
);
}
if
(
ASRUtils::is_complex
(*left_type) && !
ASRUtils::is_complex
(*right_type)) {
throw
LCompilersException
(
"
Assigning non-complex to complex is not supported
"
);
}
dest_expr = left_expr, dest_type = left_type;
src_expr = right_expr, src_type = right_type;
return
1
;
}
int
casted_expr_signal =
2
;
//
TODO: Uncomment the following
//
ASR::ttypeType left_Type = ASRUtils::extract_type(left_type)->type,
//
right_Type = ASRUtils::extract_type(right_type)->type;
ASR
::ttypeType left_Type = left_type->
type
, right_Type = right_type->
type
;
int
left_kind =
ASRUtils::extract_kind_from_ttype_t
(left_type);
int
right_kind =
ASRUtils::extract_kind_from_ttype_t
(right_type);
int
left_priority =
get_type_priority
(left_Type);
int
right_priority =
get_type_priority
(right_Type);
if
( left_priority > right_priority ) {
src_expr = right_expr, src_type = right_type;
dest_expr = left_expr, dest_type = left_type;
casted_expr_signal =
1
;
}
else
if
( left_priority < right_priority ) {
src_expr = left_expr, src_type = left_type;
dest_expr = right_expr, dest_type = right_type;
casted_expr_signal =
0
;
}
else
{
if
( left_kind > right_kind ) {
src_expr = right_expr, src_type = right_type;
dest_expr = left_expr, dest_type = left_type;
casted_expr_signal =
1
;
}
else
if
( left_kind < right_kind ) {
src_expr = left_expr, src_type = left_type;
dest_expr = right_expr, dest_type = right_type;
casted_expr_signal =
0
;
}
else
{
return
2
;
}
}
return
casted_expr_signal;
}
ASR
::
expr_t
*
perform_casting
(
ASR
::
expr_t
* expr,
ASR
::
ttype_t
* dest, Allocator& al,
const
Location& loc) {
ASR
::
ttype_t
* src =
ASRUtils::expr_type
(expr);
//
TODO: Uncomment the following
//
ASR::ttypeType src_type = ASRUtils::extract_type(src)->type;
//
ASR::ttypeType dest_type = ASRUtils::extract_type(dest)->type;
ASR
::ttypeType src_type = src->
type
;
ASR
::ttypeType dest_type = dest->
type
;
ASR
::cast_kindType cast_kind;
if
( src_type == dest_type ) {
if
( kind_rules.
find
(src_type) == kind_rules.
end
() ) {
return
expr;
}
cast_kind = kind_rules.
at
(src_type);
}
else
{
std::pair<
ASR
::ttypeType,
ASR
::ttypeType> cast_key =
std::make_pair
(src_type, dest_type);
if
( type_rules.
find
(cast_key) == type_rules.
end
() ) {
return
expr;
}
cast_kind = type_rules.
at
(cast_key);
}
if
(
ASRUtils::check_equal_type
(src, dest,
true
) ) {
return
expr;
}
//
TODO: Fix loc
if
(
ASRUtils::is_array
(src) ) {
ASR
::
dimension_t
* m_dims =
nullptr
;
size_t
n_dims =
ASRUtils::extract_dimensions_from_ttype
(src, m_dims);
dest =
ASRUtils::make_Array_t_util
(al, loc,
ASRUtils::extract_type
(dest), m_dims, n_dims);
}
else
{
dest =
ASRUtils::extract_type
(dest);
}
return
ASRUtils::EXPR
(
ASRUtils::make_Cast_t_value
(al, loc, expr, cast_kind, dest));
}
}
Back
|
FazBrowse Home
|
New Git URL