1//===-- runtime/type-code.cpp ---------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "flang/Runtime/type-code.h"
10
11namespace Fortran::runtime {
12
13RT_OFFLOAD_API_GROUP_BEGIN
14
15RT_API_ATTRS TypeCode::TypeCode(TypeCategory f, int kind) {
16 switch (f) {
17 case TypeCategory::Integer:
18 switch (kind) {
19 case 1:
20 raw_ = CFI_type_int8_t;
21 break;
22 case 2:
23 raw_ = CFI_type_int16_t;
24 break;
25 case 4:
26 raw_ = CFI_type_int32_t;
27 break;
28 case 8:
29 raw_ = CFI_type_int64_t;
30 break;
31 case 16:
32 raw_ = CFI_type_int128_t;
33 break;
34 }
35 break;
36 case TypeCategory::Real:
37 switch (kind) {
38 case 2:
39 raw_ = CFI_type_half_float;
40 break;
41 case 3:
42 raw_ = CFI_type_bfloat;
43 break;
44 case 4:
45 raw_ = CFI_type_float;
46 break;
47 case 8:
48 raw_ = CFI_type_double;
49 break;
50 case 10:
51 raw_ = CFI_type_extended_double;
52 break;
53 case 16:
54 raw_ = CFI_type_float128;
55 break;
56 }
57 break;
58 case TypeCategory::Complex:
59 switch (kind) {
60 case 2:
61 raw_ = CFI_type_half_float_Complex;
62 break;
63 case 3:
64 raw_ = CFI_type_bfloat_Complex;
65 break;
66 case 4:
67 raw_ = CFI_type_float_Complex;
68 break;
69 case 8:
70 raw_ = CFI_type_double_Complex;
71 break;
72 case 10:
73 raw_ = CFI_type_extended_double_Complex;
74 break;
75 case 16:
76 raw_ = CFI_type_long_double_Complex;
77 break;
78 }
79 break;
80 case TypeCategory::Character:
81 switch (kind) {
82 case 1:
83 raw_ = CFI_type_char;
84 break;
85 case 2:
86 raw_ = CFI_type_char16_t;
87 break;
88 case 4:
89 raw_ = CFI_type_char32_t;
90 break;
91 }
92 break;
93 case TypeCategory::Logical:
94 switch (kind) {
95 case 1:
96 raw_ = CFI_type_Bool;
97 break;
98 case 2:
99 raw_ = CFI_type_int_least16_t;
100 break;
101 case 4:
102 raw_ = CFI_type_int_least32_t;
103 break;
104 case 8:
105 raw_ = CFI_type_int_least64_t;
106 break;
107 }
108 break;
109 case TypeCategory::Derived:
110 raw_ = CFI_type_struct;
111 break;
112 }
113}
114
115RT_API_ATTRS Fortran::common::optional<std::pair<TypeCategory, int>>
116TypeCode::GetCategoryAndKind() const {
117 switch (raw_) {
118 case CFI_type_signed_char:
119 return std::make_pair(TypeCategory::Character, sizeof(signed char));
120 case CFI_type_short:
121 return std::make_pair(TypeCategory::Integer, sizeof(short));
122 case CFI_type_int:
123 return std::make_pair(TypeCategory::Integer, sizeof(int));
124 case CFI_type_long:
125 return std::make_pair(TypeCategory::Integer, sizeof(long));
126 case CFI_type_long_long:
127 return std::make_pair(TypeCategory::Integer, sizeof(long long));
128 case CFI_type_size_t:
129 return std::make_pair(TypeCategory::Integer, sizeof(std::size_t));
130 case CFI_type_int8_t:
131 return std::make_pair(TypeCategory::Integer, 1);
132 case CFI_type_int16_t:
133 return std::make_pair(TypeCategory::Integer, 2);
134 case CFI_type_int32_t:
135 return std::make_pair(TypeCategory::Integer, 4);
136 case CFI_type_int64_t:
137 return std::make_pair(TypeCategory::Integer, 8);
138 case CFI_type_int128_t:
139 return std::make_pair(TypeCategory::Integer, 16);
140 case CFI_type_int_least8_t:
141 return std::make_pair(TypeCategory::Logical, 1);
142 case CFI_type_int_least16_t:
143 return std::make_pair(TypeCategory::Logical, 2);
144 case CFI_type_int_least32_t:
145 return std::make_pair(TypeCategory::Logical, 4);
146 case CFI_type_int_least64_t:
147 return std::make_pair(TypeCategory::Logical, 8);
148 case CFI_type_int_least128_t:
149 return std::make_pair(TypeCategory::Integer, 16);
150 case CFI_type_int_fast8_t:
151 return std::make_pair(TypeCategory::Integer, sizeof(std::int_fast8_t));
152 case CFI_type_int_fast16_t:
153 return std::make_pair(TypeCategory::Integer, sizeof(std::int_fast16_t));
154 case CFI_type_int_fast32_t:
155 return std::make_pair(TypeCategory::Integer, sizeof(std::int_fast32_t));
156 case CFI_type_int_fast64_t:
157 return std::make_pair(TypeCategory::Integer, sizeof(std::int_fast64_t));
158 case CFI_type_int_fast128_t:
159 return std::make_pair(TypeCategory::Integer, 16);
160 case CFI_type_intmax_t:
161 return std::make_pair(TypeCategory::Integer, sizeof(std::intmax_t));
162 case CFI_type_intptr_t:
163 return std::make_pair(TypeCategory::Integer, sizeof(std::intptr_t));
164 case CFI_type_ptrdiff_t:
165 return std::make_pair(TypeCategory::Integer, sizeof(std::ptrdiff_t));
166 case CFI_type_half_float:
167 return std::make_pair(TypeCategory::Real, 2);
168 case CFI_type_bfloat:
169 return std::make_pair(TypeCategory::Real, 3);
170 case CFI_type_float:
171 return std::make_pair(TypeCategory::Real, 4);
172 case CFI_type_double:
173 return std::make_pair(TypeCategory::Real, 8);
174 case CFI_type_extended_double:
175 return std::make_pair(TypeCategory::Real, 10);
176 case CFI_type_long_double:
177 return std::make_pair(TypeCategory::Real, 16);
178 case CFI_type_float128:
179 return std::make_pair(TypeCategory::Real, 16);
180 case CFI_type_half_float_Complex:
181 return std::make_pair(TypeCategory::Complex, 2);
182 case CFI_type_bfloat_Complex:
183 return std::make_pair(TypeCategory::Complex, 3);
184 case CFI_type_float_Complex:
185 return std::make_pair(TypeCategory::Complex, 4);
186 case CFI_type_double_Complex:
187 return std::make_pair(TypeCategory::Complex, 8);
188 case CFI_type_extended_double_Complex:
189 return std::make_pair(TypeCategory::Complex, 10);
190 case CFI_type_long_double_Complex:
191 return std::make_pair(TypeCategory::Complex, 16);
192 case CFI_type_float128_Complex:
193 return std::make_pair(TypeCategory::Complex, 16);
194 case CFI_type_Bool:
195 return std::make_pair(TypeCategory::Logical, 1);
196 case CFI_type_char:
197 return std::make_pair(TypeCategory::Character, 1);
198 case CFI_type_cptr:
199 return std::make_pair(TypeCategory::Integer, sizeof(void *));
200 case CFI_type_struct:
201 return std::make_pair(TypeCategory::Derived, 0);
202 case CFI_type_char16_t:
203 return std::make_pair(TypeCategory::Character, 2);
204 case CFI_type_char32_t:
205 return std::make_pair(TypeCategory::Character, 4);
206 default:
207 return Fortran::common::nullopt;
208 }
209}
210
211RT_OFFLOAD_API_GROUP_END
212
213} // namespace Fortran::runtime
214

source code of flang/runtime/type-code.cpp