FLANG
common.h
1//===-- include/flang/Evaluate/common.h -------------------------*- C++ -*-===//
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#ifndef FORTRAN_EVALUATE_COMMON_H_
10#define FORTRAN_EVALUATE_COMMON_H_
11
12#include "flang/Common/api-attrs.h"
13#include "flang/Common/enum-set.h"
14#include "flang/Common/idioms.h"
15#include "flang/Common/indirection.h"
16#include "flang/Common/restorer.h"
17#include "flang/Common/target-rounding.h"
18#include "flang/Parser/char-block.h"
19#include "flang/Parser/message.h"
21#include "flang/Support/Fortran-features.h"
22#include "flang/Support/Fortran.h"
23#include "flang/Support/default-kinds.h"
24#include <cinttypes>
25#include <map>
26#include <set>
27#include <string>
28
29namespace Fortran::semantics {
30class DerivedTypeSpec;
31}
32
33namespace Fortran::evaluate {
34class IntrinsicProcTable;
36
37namespace value {
38class CharacterValue;
39}
40
41using common::ConstantSubscript;
42using common::RealFlag;
43using common::RealFlags;
44using common::RelationalOperator;
45
46// Integers are always ordered; reals may not be.
47ENUM_CLASS(Ordering, Less, Equal, Greater)
48ENUM_CLASS(Relation, Less, Equal, Greater, Unordered)
49
50template <typename A>
51static constexpr Ordering Compare(const A &x, const A &y) {
52 if (x < y) {
53 return Ordering::Less;
54 } else if (x > y) {
55 return Ordering::Greater;
56 } else {
57 return Ordering::Equal;
58 }
59}
60
61template <typename CH>
62static constexpr Ordering Compare(
63 const std::basic_string<CH> &x, const std::basic_string<CH> &y) {
64 std::size_t xLen{x.size()}, yLen{y.size()};
65 using String = std::basic_string<CH>;
66 // Fortran CHARACTER comparison is defined with blank padding
67 // to extend a shorter operand.
68 if (xLen < yLen) {
69 return Compare(String{x}.append(yLen - xLen, CH{' '}), y);
70 } else if (xLen > yLen) {
71 return Compare(x, String{y}.append(xLen - yLen, CH{' '}));
72 } else if (x < y) {
73 return Ordering::Less;
74 } else if (x > y) {
75 return Ordering::Greater;
76 } else {
77 return Ordering::Equal;
78 }
79}
80
81Ordering Compare(
83
84static constexpr Ordering Reverse(Ordering ordering) {
85 if (ordering == Ordering::Less) {
86 return Ordering::Greater;
87 } else if (ordering == Ordering::Greater) {
88 return Ordering::Less;
89 } else {
90 return Ordering::Equal;
91 }
92}
93
94static constexpr Relation RelationFromOrdering(Ordering ordering) {
95 if (ordering == Ordering::Less) {
96 return Relation::Less;
97 } else if (ordering == Ordering::Greater) {
98 return Relation::Greater;
99 } else {
100 return Relation::Equal;
101 }
102}
103
104static constexpr Relation Reverse(Relation relation) {
105 if (relation == Relation::Less) {
106 return Relation::Greater;
107 } else if (relation == Relation::Greater) {
108 return Relation::Less;
109 } else {
110 return relation;
111 }
112}
113
114static constexpr bool Satisfies(RelationalOperator op, Ordering order) {
115 switch (order) {
116 case Ordering::Less:
117 return op == RelationalOperator::LT || op == RelationalOperator::LE ||
118 op == RelationalOperator::NE;
119 case Ordering::Equal:
120 return op == RelationalOperator::LE || op == RelationalOperator::EQ ||
121 op == RelationalOperator::GE;
122 case Ordering::Greater:
123 return op == RelationalOperator::NE || op == RelationalOperator::GE ||
124 op == RelationalOperator::GT;
125 }
126 return false; // silence g++ warning
127}
128
129static constexpr bool Satisfies(RelationalOperator op, Relation relation) {
130 switch (relation) {
131 case Relation::Less:
132 return Satisfies(op, Ordering::Less);
133 case Relation::Equal:
134 return Satisfies(op, Ordering::Equal);
135 case Relation::Greater:
136 return Satisfies(op, Ordering::Greater);
137 case Relation::Unordered:
138 return op == RelationalOperator::NE;
139 }
140 return false; // silence g++ warning
141}
142
143template <typename A> struct ValueWithRealFlags {
144 A AccumulateFlags(RealFlags &f) {
145 f |= flags;
146 return value;
147 }
148 A value;
149 RealFlags flags{};
150};
151
152#if FLANG_BIG_ENDIAN
153constexpr bool isHostLittleEndian{false};
154#elif FLANG_LITTLE_ENDIAN
155constexpr bool isHostLittleEndian{true};
156#else
157#error host endianness is not known
158#endif
159
160// HostUnsignedInt<BITS> finds the smallest native unsigned integer type
161// whose size is >= BITS.
162template <bool LE8, bool LE16, bool LE32, bool LE64> struct SmallestUInt {};
163template <> struct SmallestUInt<true, true, true, true> {
164 using type = std::uint8_t;
165};
166template <> struct SmallestUInt<false, true, true, true> {
167 using type = std::uint16_t;
168};
169template <> struct SmallestUInt<false, false, true, true> {
170 using type = std::uint32_t;
171};
172template <> struct SmallestUInt<false, false, false, true> {
173 using type = std::uint64_t;
174};
175template <int BITS>
176using HostUnsignedInt =
178
179// Many classes in this library follow a common paradigm.
180// - There is no default constructor (Class() {}), usually to prevent the
181// need for std::monostate as a default constituent in a std::variant<>.
182// - There are full copy and move semantics for construction and assignment.
183// - Discriminated unions have a std::variant<> member "u" and support
184// explicit copy and move constructors as well as comparison for equality.
185#define DECLARE_CONSTRUCTORS_AND_ASSIGNMENTS(t) \
186 t(const t &); \
187 t(t &&); \
188 t &operator=(const t &); \
189 t &operator=(t &&);
190#define DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(t) \
191 t(const t &) = default; \
192 t(t &&) = default; \
193 t &operator=(const t &) = default; \
194 t &operator=(t &&) = default;
195#define DEFINE_DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(t) \
196 t::t(const t &) = default; \
197 t::t(t &&) = default; \
198 t &t::operator=(const t &) = default; \
199 t &t::operator=(t &&) = default;
200#define CONSTEXPR_CONSTRUCTORS_AND_ASSIGNMENTS(t) \
201 constexpr t(const t &) = default; \
202 constexpr t(t &&) = default; \
203 constexpr t &operator=(const t &) = default; \
204 constexpr t &operator=(t &&) = default;
205
206#define CLASS_BOILERPLATE(t) \
207 t() = delete; \
208 DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(t)
209
210#define UNION_CONSTRUCTORS(t) \
211 template <typename _A> explicit t(const _A &x) : u{x} {} \
212 template <typename _A, typename = common::NoLvalue<_A>> \
213 explicit t(_A &&x) : u(std::move(x)) {}
214
215#define EVALUATE_UNION_CLASS_BOILERPLATE(t) \
216 CLASS_BOILERPLATE(t) \
217 UNION_CONSTRUCTORS(t) \
218 bool operator==(const t &) const;
219
220// Forward definition of Expr<> so that it can be indirectly used in its own
221// definition
222template <typename A> class Expr;
223
224class FoldingContext {
225public:
226 FoldingContext(const common::IntrinsicTypeDefaultKinds &d,
227 const IntrinsicProcTable &t, const TargetCharacteristics &c,
229 std::set<std::string> &tempNames,
230 common::FPMaxminBehavior fpMaxminBehavior =
231 common::FPMaxminBehavior::Legacy)
232 : defaults_{d}, intrinsics_{t}, targetCharacteristics_{c},
233 languageFeatures_{lfc}, tempNames_{tempNames},
234 fpMaxminBehavior_{fpMaxminBehavior} {}
235 FoldingContext(const parser::ContextualMessages &m,
236 const common::IntrinsicTypeDefaultKinds &d, const IntrinsicProcTable &t,
238 std::set<std::string> &tempNames,
239 common::FPMaxminBehavior fpMaxminBehavior =
240 common::FPMaxminBehavior::Legacy)
241 : messages_{m}, defaults_{d}, intrinsics_{t}, targetCharacteristics_{c},
242 languageFeatures_{lfc}, tempNames_{tempNames},
243 fpMaxminBehavior_{fpMaxminBehavior} {}
244 FoldingContext(const FoldingContext &that)
245 : messages_{that.messages_}, defaults_{that.defaults_},
246 intrinsics_{that.intrinsics_},
247 targetCharacteristics_{that.targetCharacteristics_},
248 pdtInstance_{that.pdtInstance_},
249 analyzingPDTComponentKindSelector_{
250 that.analyzingPDTComponentKindSelector_},
251 inConstantContext_{that.inConstantContext_},
252 impliedDos_{that.impliedDos_},
253 languageFeatures_{that.languageFeatures_}, tempNames_{that.tempNames_},
254 fpMaxminBehavior_{that.fpMaxminBehavior_} {}
255 FoldingContext(
256 const FoldingContext &that, const parser::ContextualMessages &m)
257 : messages_{m}, defaults_{that.defaults_}, intrinsics_{that.intrinsics_},
258 targetCharacteristics_{that.targetCharacteristics_},
259 pdtInstance_{that.pdtInstance_},
260 analyzingPDTComponentKindSelector_{
261 that.analyzingPDTComponentKindSelector_},
262 inConstantContext_{that.inConstantContext_},
263 impliedDos_{that.impliedDos_},
264 languageFeatures_{that.languageFeatures_}, tempNames_{that.tempNames_},
265 fpMaxminBehavior_{that.fpMaxminBehavior_} {}
266
267 parser::ContextualMessages &messages() { return messages_; }
268 const parser::ContextualMessages &messages() const { return messages_; }
269 const common::IntrinsicTypeDefaultKinds &defaults() const {
270 return defaults_;
271 }
272 const semantics::DerivedTypeSpec *pdtInstance() const { return pdtInstance_; }
273 bool analyzingPDTComponentKindSelector() const {
274 return analyzingPDTComponentKindSelector_;
275 }
276 const IntrinsicProcTable &intrinsics() const { return intrinsics_; }
277 const TargetCharacteristics &targetCharacteristics() const {
278 return targetCharacteristics_;
279 }
280 const common::LanguageFeatureControl &languageFeatures() const {
281 return languageFeatures_;
282 }
283 common::FPMaxminBehavior fpMaxminBehavior() const {
284 return fpMaxminBehavior_;
285 }
286 template <typename... A>
287 parser::Message *Warn(common::LanguageFeature feature, A &&...args) {
288 return messages_.Warn(
289 IsInModuleFile(), languageFeatures_, feature, std::forward<A>(args)...);
290 }
291 template <typename... A>
292 parser::Message *Warn(common::UsageWarning warning, A &&...args) {
293 return messages_.Warn(
294 IsInModuleFile(), languageFeatures_, warning, std::forward<A>(args)...);
295 }
296 std::optional<parser::CharBlock> moduleFileName() const {
297 return moduleFileName_;
298 }
299 FoldingContext &set_moduleFileName(std::optional<parser::CharBlock> n) {
300 moduleFileName_ = n;
301 return *this;
302 }
303 bool IsInModuleFile() const { return moduleFileName_.has_value(); }
304
305 ConstantSubscript &StartImpliedDo(parser::CharBlock, ConstantSubscript = 1);
306 std::optional<ConstantSubscript> GetImpliedDo(parser::CharBlock) const;
307 void EndImpliedDo(parser::CharBlock);
308
309 std::map<parser::CharBlock, ConstantSubscript> &impliedDos() {
310 return impliedDos_;
311 }
312
314 const semantics::DerivedTypeSpec &spec) {
315 return common::ScopedSet(pdtInstance_, &spec);
316 }
318 return common::ScopedSet(pdtInstance_, nullptr);
319 }
320
321 common::Restorer<bool> AnalyzingPDTComponentKindSelector() {
322 return common::ScopedSet(analyzingPDTComponentKindSelector_, true);
323 }
324
325 bool inConstantContext() const { return inConstantContext_; }
326 common::Restorer<bool> WithConstantContext() {
327 return common::ScopedSet(inConstantContext_, true);
328 }
329
330 common::Restorer<std::string> SetRealFlagWarningContext(std::string str) {
331 return common::ScopedSet(realFlagWarningContext_, str);
332 }
333
334 parser::CharBlock SaveTempName(std::string &&name) {
335 return {*tempNames_.emplace(std::move(name)).first};
336 }
337
338 void RealFlagWarnings(const RealFlags &, const char *op);
339
340private:
342 const common::IntrinsicTypeDefaultKinds &defaults_;
343 const IntrinsicProcTable &intrinsics_;
344 const TargetCharacteristics &targetCharacteristics_;
345 const semantics::DerivedTypeSpec *pdtInstance_{nullptr};
346 bool analyzingPDTComponentKindSelector_{false};
347 // True while folding an expression that is required to be constant (e.g. a
348 // named-constant or type-parameter initializer). Used to promote certain
349 // deferred-to-runtime conditions into compile-time diagnostics.
350 bool inConstantContext_{false};
351 std::optional<parser::CharBlock> moduleFileName_;
352 std::map<parser::CharBlock, ConstantSubscript> impliedDos_;
353 const common::LanguageFeatureControl &languageFeatures_;
354 std::set<std::string> &tempNames_;
355 std::string realFlagWarningContext_;
356 common::FPMaxminBehavior fpMaxminBehavior_{common::FPMaxminBehavior::Legacy};
357};
358
359} // namespace Fortran::evaluate
360#endif // FORTRAN_EVALUATE_COMMON_H_
Definition default-kinds.h:26
Definition Fortran-features.h:102
Definition restorer.h:24
Definition common.h:222
Definition character-value.h:34
Definition char-block.h:27
Definition message.h:399
Definition message.h:202
FPMaxminBehavior
Definition FPMaxminBehavior.h:29
Definition call.h:34
Definition common.h:162