FLANG
intrinsics.h
1//===-- include/flang/Evaluate/intrinsics.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_INTRINSICS_H_
10#define FORTRAN_EVALUATE_INTRINSICS_H_
11
12#include "call.h"
13#include "characteristics.h"
14#include "flang/Parser/char-block.h"
15#include "flang/Parser/message.h"
16#include "flang/Support/default-kinds.h"
17#include <memory>
18#include <optional>
19#include <string>
20
21namespace llvm {
22class raw_ostream;
23}
24
25namespace Fortran::semantics {
26class Scope;
27}
28
29namespace Fortran::evaluate {
30
31class FoldingContext;
32
33// Utility for checking for missing, excess, and duplicated arguments,
34// and rearranging the actual arguments into dummy argument order.
35bool CheckAndRearrangeArguments(ActualArguments &, parser::ContextualMessages &,
36 const char *const dummyKeywords[] /* null terminated */,
37 std::size_t trailingOptionals = 0);
38
40 std::string name;
41 bool isSubroutineCall{false};
42};
43
44struct SpecificCall {
45 SpecificCall(SpecificIntrinsic &&si, ActualArguments &&as)
46 : specificIntrinsic{std::move(si)}, arguments{std::move(as)} {}
47 SpecificIntrinsic specificIntrinsic;
48 ActualArguments arguments;
49};
50
51struct SpecificIntrinsicFunctionInterface : public characteristics::Procedure {
52 SpecificIntrinsicFunctionInterface(
53 characteristics::Procedure &&p, std::string n, bool isRestrictedSpecific)
54 : characteristics::Procedure{std::move(p)}, genericName{n},
55 isRestrictedSpecific{isRestrictedSpecific} {}
56 std::string genericName;
57 bool isRestrictedSpecific;
58 // N.B. If there are multiple arguments, they all have the same type.
59 // All argument and result types are intrinsic types with default kinds.
60};
61
62// Generic intrinsic classes from table 16.1
63ENUM_CLASS(IntrinsicClass, atomicSubroutine, collectiveSubroutine,
64 elementalFunction, elementalSubroutine, inquiryFunction, pureSubroutine,
65 impureFunction, impureSubroutine, transformationalFunction, noClass)
66
67class IntrinsicProcTable {
68private:
69 class Implementation;
70
71 IntrinsicProcTable() = default;
72
73public:
74 ~IntrinsicProcTable();
75 IntrinsicProcTable(IntrinsicProcTable &&) = default;
76
77 static IntrinsicProcTable Configure(
79
80 // Make *this aware of the __Fortran_builtins module to expose TEAM_TYPE &c.
81 void SupplyBuiltins(const semantics::Scope &) const;
82
83 // Check whether a name should be allowed to appear on an INTRINSIC
84 // statement.
85 bool IsIntrinsic(const std::string &) const;
86 bool IsIntrinsicFunction(const std::string &) const;
87 bool IsIntrinsicSubroutine(const std::string &) const;
88 bool IsDualIntrinsic(const std::string &) const;
89
90 // Inquiry intrinsics are defined in section 16.7, table 16.1
91 IntrinsicClass GetIntrinsicClass(const std::string &) const;
92
93 // Return the generic name of a specific intrinsic name.
94 // The name provided is returned if it is a generic intrinsic name or is
95 // not known to be an intrinsic.
96 std::string GetGenericIntrinsicName(const std::string &) const;
97
98 // Probe the intrinsics for a match against a specific call.
99 // On success, the actual arguments are transferred to the result
100 // in dummy argument order; on failure, the actual arguments remain
101 // untouched.
102 // For MIN and MAX, only a1 and a2 actual arguments are transferred in dummy
103 // order on success and the other arguments are transferred afterwards
104 // without being sorted.
105 std::optional<SpecificCall> Probe(
106 const CallCharacteristics &, ActualArguments &, FoldingContext &) const;
107
108 // Probe the intrinsics with the name of a potential specific intrinsic.
109 std::optional<SpecificIntrinsicFunctionInterface> IsSpecificIntrinsicFunction(
110 const std::string &) const;
111
112 // Illegal name for an intrinsic used to avoid cascading error messages when
113 // constant folding.
114 static const inline std::string InvalidName{
115 "(invalid intrinsic function call)"};
116
117 // Name of intrinsics used in various locations.
118 static inline const char *const BuiltinIntName{"__builtin_int"};
119
120 llvm::raw_ostream &Dump(llvm::raw_ostream &) const;
121
122private:
123 std::unique_ptr<Implementation> impl_;
124};
125
126// Check if an intrinsic explicitly allows its INTENT(OUT) arguments to be
127// allocatable coarrays.
128bool AcceptsIntentOutAllocatableCoarray(const std::string &);
129} // namespace Fortran::evaluate
130#endif // FORTRAN_EVALUATE_INTRINSICS_H_
Definition default-kinds.h:26
Definition common.h:217
Definition scope.h:68
Definition call.h:34
Definition intrinsics.h:39
Definition characteristics.h:367