FLANG
idioms.h
1//===-- include/flang/Common/idioms.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_COMMON_IDIOMS_H_
10#define FORTRAN_COMMON_IDIOMS_H_
11
12// Defines anything that might ever be useful in more than one source file
13// or that is too weird or too specific to the host C++ compiler to be
14// exposed elsewhere.
15
16#ifndef __cplusplus
17#error this is a C++ program
18#endif
19#if __cplusplus < 201703L
20#error this is a C++17 program
21#endif
22#if !__clang__ && defined __GNUC__ && __GNUC__ < 7
23#error g++ >= 7.2 is required
24#endif
25
26#include "enum-class.h"
27#include "variant.h"
28#include "visit.h"
29#include <functional>
30#include <list>
31#include <memory>
32#include <optional>
33#include <string>
34#include <tuple>
35#include <type_traits>
36
37#if __GNUC__ == 7
38// Avoid a deduction bug in GNU 7.x headers by forcing the answer.
39namespace std {
40template <typename A>
41struct is_trivially_copy_constructible<list<A>> : false_type {};
42template <typename A>
43struct is_trivially_copy_constructible<optional<list<A>>> : false_type {};
44} // namespace std
45#endif
46
47// enable "this is a std::string"s with the 's' suffix
48using namespace std::literals::string_literals;
49
50namespace Fortran::common {
51
52// Helper templates for combining a list of lambdas into an anonymous
53// struct for use with common::visit() on a std::variant<> sum type.
54// E.g.: common::visit(visitors{
55// [&](const firstType &x) { ... },
56// [&](const secondType &x) { ... },
57// ...
58// [&](const auto &catchAll) { ... }}, variantObject);
59
60template <typename... LAMBDAS> struct visitors : LAMBDAS... {
61 using LAMBDAS::operator()...;
62};
63
64template <typename... LAMBDAS> visitors(LAMBDAS... x) -> visitors<LAMBDAS...>;
65
66// Calls std::fprintf(stderr, ...), then abort().
67[[noreturn]] void die(const char *, ...);
68
69#define DIE(x) Fortran::common::die(x " at " __FILE__ "(%d)", __LINE__)
70
71// For switch statement default: labels.
72#define CRASH_NO_CASE DIE("no case")
73
74// clang-format off
75// For switch statements whose cases have return statements for
76// all possibilities. Clang emits warnings if the default: is
77// present, gcc emits warnings if it is absent.
78#if __clang__
79#define SWITCH_COVERS_ALL_CASES
80#else
81#define SWITCH_COVERS_ALL_CASES default: CRASH_NO_CASE;
82#endif
83// clang-format on
84
85// For cheap assertions that should be applied in production.
86// To disable, compile with '-DCHECK=(void)'
87#ifndef CHECK
88#define CHECK(x) ((x) || (DIE("CHECK(" #x ") failed"), false))
89#endif
90
91// Same as above, but with a custom error message.
92#ifndef CHECK_MSG
93#define CHECK_MSG(x, y) ((x) || (DIE("CHECK(" #x ") failed: " #y), false))
94#endif
95
96// User-defined type traits that default to false:
97// Invoke CLASS_TRAIT(traitName) to define a trait, then put
98// using traitName = std::true_type; (or false_type)
99// into the appropriate class definitions. You can then use
100// typename std::enable_if_t<traitName<...>, ...>
101// in template specialization definitions.
102#define CLASS_TRAIT(T) \
103 namespace class_trait_ns_##T { \
104 template <typename A> std::true_type test(typename A::T *); \
105 template <typename A> std::false_type test(...); \
106 template <typename A> \
107 constexpr bool has_trait{decltype(test<A>(nullptr))::value}; \
108 template <typename A> constexpr bool trait_value() { \
109 if constexpr (has_trait<A>) { \
110 using U = typename A::T; \
111 return U::value; \
112 } else { \
113 return false; \
114 } \
115 } \
116 } \
117 template <typename A> constexpr bool T{class_trait_ns_##T::trait_value<A>()};
118
119// Check that a pointer is non-null and dereference it
120#define DEREF(p) Fortran::common::Deref(p, __FILE__, __LINE__)
121
122template <typename T> constexpr T &Deref(T *p, const char *file, int line) {
123 if (!p) {
124 Fortran::common::die("nullptr dereference at %s(%d)", file, line);
125 }
126 return *p;
127}
128
129template <typename T>
130constexpr T &Deref(const std::unique_ptr<T> &p, const char *file, int line) {
131 if (!p) {
132 Fortran::common::die("nullptr dereference at %s(%d)", file, line);
133 }
134 return *p;
135}
136
137// Given a const reference to a value, return a copy of the value.
138template <typename A> A Clone(const A &x) { return x; }
139
140// C++ does a weird and dangerous thing when deducing template type parameters
141// from function arguments: lvalue references are allowed to match rvalue
142// reference arguments. Template function declarations like
143// template<typename A> int foo(A &&);
144// need to be protected against this C++ language feature when functions
145// may modify such arguments. Use these type functions to invoke SFINAE
146// on a result type via
147// template<typename A> common::IfNoLvalue<int, A> foo(A &&);
148// or, for constructors,
149// template<typename A, typename = common::NoLvalue<A>> int foo(A &&);
150// This works with parameter packs too.
151template <typename A, typename... B>
152using IfNoLvalue = std::enable_if_t<(... && !std::is_lvalue_reference_v<B>), A>;
153template <typename... RVREF> using NoLvalue = IfNoLvalue<void, RVREF...>;
154} // namespace Fortran::common
155#endif // FORTRAN_COMMON_IDIOMS_H_
Definition bit-population-count.h:20
Definition idioms.h:60