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 "type-code.h"
10
11 namespace Fortran::runtime {
12
TypeCode(TypeCategory f,int kind)13 TypeCode::TypeCode(TypeCategory f, int kind) {
14 switch (f) {
15 case TypeCategory::Integer:
16 switch (kind) {
17 case 1:
18 raw_ = CFI_type_int8_t;
19 break;
20 case 2:
21 raw_ = CFI_type_int16_t;
22 break;
23 case 4:
24 raw_ = CFI_type_int32_t;
25 break;
26 case 8:
27 raw_ = CFI_type_int64_t;
28 break;
29 case 16:
30 raw_ = CFI_type_int128_t;
31 break;
32 }
33 break;
34 case TypeCategory::Real:
35 switch (kind) {
36 case 4:
37 raw_ = CFI_type_float;
38 break;
39 case 8:
40 raw_ = CFI_type_double;
41 break;
42 case 10:
43 case 16:
44 raw_ = CFI_type_long_double;
45 break;
46 }
47 break;
48 case TypeCategory::Complex:
49 switch (kind) {
50 case 4:
51 raw_ = CFI_type_float_Complex;
52 break;
53 case 8:
54 raw_ = CFI_type_double_Complex;
55 break;
56 case 10:
57 case 16:
58 raw_ = CFI_type_long_double_Complex;
59 break;
60 }
61 break;
62 case TypeCategory::Character:
63 switch (kind) {
64 case 1:
65 raw_ = CFI_type_char;
66 break;
67 case 2:
68 raw_ = CFI_type_char16_t;
69 break;
70 case 4:
71 raw_ = CFI_type_char32_t;
72 break;
73 }
74 break;
75 case TypeCategory::Logical:
76 switch (kind) {
77 case 1:
78 raw_ = CFI_type_Bool;
79 break;
80 case 2:
81 raw_ = CFI_type_int_fast16_t;
82 break;
83 case 4:
84 raw_ = CFI_type_int_fast32_t;
85 break;
86 case 8:
87 raw_ = CFI_type_int_fast64_t;
88 break;
89 }
90 break;
91 case TypeCategory::Derived:
92 raw_ = CFI_type_struct;
93 break;
94 }
95 }
96
97 std::optional<std::pair<TypeCategory, int>>
GetCategoryAndKind() const98 TypeCode::GetCategoryAndKind() const {
99 switch (raw_) {
100 case CFI_type_int8_t:
101 return std::make_pair(TypeCategory::Integer, 1);
102 case CFI_type_int16_t:
103 return std::make_pair(TypeCategory::Integer, 2);
104 case CFI_type_int32_t:
105 return std::make_pair(TypeCategory::Integer, 4);
106 case CFI_type_int64_t:
107 return std::make_pair(TypeCategory::Integer, 8);
108 case CFI_type_int128_t:
109 return std::make_pair(TypeCategory::Integer, 16);
110 case CFI_type_float:
111 return std::make_pair(TypeCategory::Real, 4);
112 case CFI_type_double:
113 return std::make_pair(TypeCategory::Real, 8);
114 case CFI_type_long_double:
115 #if __x86_64__
116 return std::make_pair(TypeCategory::Real, 10);
117 #else
118 return std::make_pair(TypeCategory::Real, 16);
119 #endif
120 case CFI_type_float_Complex:
121 return std::make_pair(TypeCategory::Complex, 4);
122 case CFI_type_double_Complex:
123 return std::make_pair(TypeCategory::Complex, 8);
124 case CFI_type_long_double_Complex:
125 #if __x86_64__
126 return std::make_pair(TypeCategory::Complex, 10);
127 #else
128 return std::make_pair(TypeCategory::Complex, 16);
129 #endif
130 case CFI_type_char:
131 return std::make_pair(TypeCategory::Character, 1);
132 case CFI_type_char16_t:
133 return std::make_pair(TypeCategory::Character, 2);
134 case CFI_type_char32_t:
135 return std::make_pair(TypeCategory::Character, 4);
136 case CFI_type_Bool:
137 return std::make_pair(TypeCategory::Logical, 1);
138 case CFI_type_int_fast8_t:
139 return std::make_pair(TypeCategory::Logical, 1);
140 case CFI_type_int_fast16_t:
141 return std::make_pair(TypeCategory::Logical, 2);
142 case CFI_type_int_fast32_t:
143 return std::make_pair(TypeCategory::Logical, 4);
144 case CFI_type_int_fast64_t:
145 return std::make_pair(TypeCategory::Logical, 8);
146 case CFI_type_struct:
147 return std::make_pair(TypeCategory::Derived, 0);
148 default:
149 return std::nullopt;
150 }
151 }
152 } // namespace Fortran::runtime
153