FLANG
call.h
1//===-- include/flang/Evaluate/call.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_CALL_H_
10#define FORTRAN_EVALUATE_CALL_H_
11
12#include "common.h"
13#include "constant.h"
14#include "formatting.h"
15#include "type.h"
16#include "flang/Common/indirection.h"
17#include "flang/Common/reference.h"
18#include "flang/Parser/char-block.h"
19#include "flang/Semantics/attr.h"
20#include "flang/Support/Fortran.h"
21#include <optional>
22#include <vector>
23
24namespace llvm {
25class raw_ostream;
26}
27
28namespace Fortran::semantics {
29class Symbol;
30}
31
32// Mutually referential data structures are represented here with forward
33// declarations of hitherto undefined class types and a level of indirection.
35class Component;
36class IntrinsicProcTable;
37} // namespace Fortran::evaluate
38namespace Fortran::evaluate::characteristics {
39struct DummyArgument;
40struct Procedure;
41} // namespace Fortran::evaluate::characteristics
42
44 true>;
45extern template class Fortran::common::Indirection<
47
48namespace Fortran::evaluate {
49
50using semantics::Symbol;
51using SymbolRef = common::Reference<const Symbol>;
52
53class ActualArgument {
54public:
55 ENUM_CLASS(Attr, PassedObject, PercentVal, PercentRef);
57
58 // Dummy arguments that are TYPE(*) can be forwarded as actual arguments.
59 // Since that's the only thing one may do with them in Fortran, they're
60 // represented in expressions as a special case of an actual argument.
61 class AssumedType {
62 public:
63 explicit AssumedType(const Symbol &);
64 DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(AssumedType)
65 const Symbol &symbol() const { return symbol_; }
66 int Rank() const;
67 bool operator==(const AssumedType &that) const {
68 return &*symbol_ == &*that.symbol_;
69 }
70 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
71
72 private:
73 SymbolRef symbol_;
74 };
75
76 // F2023 R1526 conditional-arg: runtime selection of actual arguments.
77 // Recursive structure mirroring the parser: each ConditionalArg holds
78 // a condition, a consequent, and a tail that is either another
79 // ConditionalArg (continuing the chain) or a terminal Consequent.
80 // std::nullopt represents .NIL. (absent optional argument).
81 class ConditionalArg {
82 public:
83 using Consequent =
84 std::optional<common::CopyableIndirection<Expr<SomeType>>>;
85 using ConditionalArgPartOrConsequent =
86 std::variant<common::CopyableIndirection<ConditionalArg>, Consequent>;
87
88 ConditionalArg(Expr<Type<TypeCategory::Logical>> &&condition,
89 Consequent &&consequent, ConditionalArgPartOrConsequent &&tail);
90 DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(ConditionalArg)
91
93 return condition_.value();
94 }
95 const Expr<Type<TypeCategory::Logical>> &condition() const {
96 return condition_.value();
97 }
98 const Consequent &consequent() const { return consequent_; }
99 Consequent &consequent() { return consequent_; }
100 ConditionalArgPartOrConsequent &tail() { return tail_; }
101 const ConditionalArgPartOrConsequent &tail() const { return tail_; }
102
103 // Dispatch on the tail: calls onConditionalArg(const ConditionalArg &)
104 // if the tail continues the chain, or onConsequent(const Consequent &)
105 // if the tail is the terminal consequent.
106 template <typename F, typename G>
107 auto VisitTail(F onConditionalArg, G onConsequent) const {
108 return common::visit(
110 [&](const common::CopyableIndirection<ConditionalArg> &inner) {
111 return onConditionalArg(inner.value());
112 },
113 [&](const Consequent &cons) { return onConsequent(cons); },
114 },
115 tail_);
116 }
117
118 template <typename F, typename G>
119 auto VisitTail(F onConditionalArg, G onConsequent) {
120 return common::visit(
122 [&](common::CopyableIndirection<ConditionalArg> &inner) {
123 return onConditionalArg(inner.value());
124 },
125 [&](Consequent &cons) { return onConsequent(cons); },
126 },
127 tail_);
128 }
129
130 // Apply a callback to every Consequent in the chain (including .NIL.
131 // entries). This encapsulates the recurring "process consequent, then
132 // VisitTail with recursion" pattern.
133 template <typename F> void ForEachConsequent(F f) const {
134 f(consequent_);
135 VisitTail(
136 [&](const ConditionalArg &inner) { inner.ForEachConsequent(f); },
137 [&](const Consequent &cons) { f(cons); });
138 }
139 template <typename F> void ForEachConsequent(F f) {
140 f(consequent_);
141 VisitTail([&](ConditionalArg &inner) { inner.ForEachConsequent(f); },
142 [&](Consequent &cons) { f(cons); });
143 }
144
145 // Returns the first non-.NIL. consequent expression, or nullptr.
146 const Expr<SomeType> *FirstNonNilConsequent() const;
147 bool HasNilConsequent() const;
148 // True if any consequent-arg is polymorphic (F2023 15.5.2.3 p4).
149 bool IsPolymorphic() const;
150
151 bool operator==(const ConditionalArg &) const;
152 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
153
154 private:
155 common::CopyableIndirection<Expr<Type<TypeCategory::Logical>>> condition_;
156 Consequent consequent_;
157 ConditionalArgPartOrConsequent tail_;
158 };
159
160 DECLARE_CONSTRUCTORS_AND_ASSIGNMENTS(ActualArgument)
161 explicit ActualArgument(Expr<SomeType> &&);
162 explicit ActualArgument(common::CopyableIndirection<Expr<SomeType>> &&);
163 explicit ActualArgument(AssumedType);
164 explicit ActualArgument(common::Label);
165 explicit ActualArgument(ConditionalArg &&);
166 ~ActualArgument();
167 ActualArgument &operator=(Expr<SomeType> &&);
168
169 Expr<SomeType> *UnwrapExpr() {
170 if (auto *p{
171 std::get_if<common::CopyableIndirection<Expr<SomeType>>>(&u_)}) {
172 return &p->value();
173 } else {
174 return nullptr;
175 }
176 }
177 const Expr<SomeType> *UnwrapExpr() const {
178 if (const auto *p{
179 std::get_if<common::CopyableIndirection<Expr<SomeType>>>(&u_)}) {
180 return &p->value();
181 } else {
182 return nullptr;
183 }
184 }
185
186 const Symbol *GetAssumedTypeDummy() const {
187 if (const AssumedType * aType{std::get_if<AssumedType>(&u_)}) {
188 return &aType->symbol();
189 } else {
190 return nullptr;
191 }
192 }
193
194 common::Label GetLabel() const { return std::get<common::Label>(u_); }
195
196 // For a conditional argument, the type of its first consequent-arg; see
197 // ConditionalArg::IsPolymorphic().
198 std::optional<DynamicType> GetType() const;
199 int Rank() const;
200 bool operator==(const ActualArgument &) const;
201 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
202 std::string AsFortran() const;
203
204 std::optional<parser::CharBlock> keyword() const { return keyword_; }
205 ActualArgument &set_keyword(parser::CharBlock x) {
206 keyword_ = x;
207 return *this;
208 }
209 bool isAlternateReturn() const {
210 return std::holds_alternative<common::Label>(u_);
211 }
212 bool isConditionalArg() const {
213 return std::holds_alternative<ConditionalArg>(u_);
214 }
215 const ConditionalArg *GetConditionalArg() const {
216 return std::get_if<ConditionalArg>(&u_);
217 }
218 ConditionalArg *GetConditionalArg() {
219 return std::get_if<ConditionalArg>(&u_);
220 }
221 const Expr<SomeType> *GetConditionalArgExpr() const {
222 const auto *condArg{GetConditionalArg()};
223 return condArg ? condArg->FirstNonNilConsequent() : nullptr;
224 }
225 // Returns the expression from a direct Expr argument or, failing that,
226 // the first non-NIL consequent from a ConditionalArg.
227 const Expr<SomeType> *GetArgExpr() const {
228 if (const auto *expr{UnwrapExpr()}) {
229 return expr;
230 }
231 return GetConditionalArgExpr();
232 }
233 bool isPassedObject() const { return attrs_.test(Attr::PassedObject); }
234 ActualArgument &set_isPassedObject(bool yes = true) {
235 if (yes) {
236 attrs_ = attrs_ + Attr::PassedObject;
237 } else {
238 attrs_ = attrs_ - Attr::PassedObject;
239 }
240 return *this;
241 }
242
243 bool Matches(const characteristics::DummyArgument &) const;
244 common::Intent dummyIntent() const { return dummyIntent_; }
245 ActualArgument &set_dummyIntent(common::Intent intent) {
246 dummyIntent_ = intent;
247 return *this;
248 }
249 std::optional<parser::CharBlock> sourceLocation() const {
250 return sourceLocation_;
251 }
252 ActualArgument &set_sourceLocation(std::optional<parser::CharBlock> at) {
253 sourceLocation_ = at;
254 return *this;
255 }
256
257 // Wrap this argument in parentheses
258 void Parenthesize();
259
260 // Legacy %VAL.
261 bool isPercentVal() const { return attrs_.test(Attr::PercentVal); };
262 ActualArgument &set_isPercentVal() {
263 attrs_ = attrs_ + Attr::PercentVal;
264 return *this;
265 }
266 // Legacy %REF.
267 bool isPercentRef() const { return attrs_.test(Attr::PercentRef); };
268 ActualArgument &set_isPercentRef() {
269 attrs_ = attrs_ + Attr::PercentRef;
270 return *this;
271 }
272
273private:
274 // Subtlety: There is a distinction that must be maintained here between an
275 // actual argument expression that is a variable and one that is not,
276 // e.g. between X and (X). The parser attempts to parse each argument
277 // first as a variable, then as an expression, and the distinction appears
278 // in the parse tree.
279 std::variant<common::CopyableIndirection<Expr<SomeType>>, AssumedType,
280 common::Label, ConditionalArg>
281 u_;
282 std::optional<parser::CharBlock> keyword_;
283 Attrs attrs_;
284 common::Intent dummyIntent_{common::Intent::Default};
285 std::optional<parser::CharBlock> sourceLocation_;
286};
287
288using ActualArguments = std::vector<std::optional<ActualArgument>>;
289
290// Intrinsics are identified by their names and the characteristics
291// of their arguments, at least for now.
292using IntrinsicProcedure = std::string;
293
294struct SpecificIntrinsic {
295 SpecificIntrinsic(IntrinsicProcedure, characteristics::Procedure &&);
296 DECLARE_CONSTRUCTORS_AND_ASSIGNMENTS(SpecificIntrinsic)
297 ~SpecificIntrinsic();
298 bool operator==(const SpecificIntrinsic &) const;
299 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
300
301 IntrinsicProcedure name;
302 bool isRestrictedSpecific{false}; // if true, can only call it, not pass it
303 common::CopyableIndirection<characteristics::Procedure> characteristics;
304};
305
306struct ProcedureDesignator {
307 static int kind() {
308 DIE("This class has no kind");
309 return 0;
310 }
311
312 EVALUATE_UNION_CLASS_BOILERPLATE(ProcedureDesignator)
313 explicit ProcedureDesignator(SpecificIntrinsic &&i) : u{std::move(i)} {}
314 explicit ProcedureDesignator(const Symbol &n) : u{n} {}
315 explicit ProcedureDesignator(Component &&);
316
317 // Exactly one of these will return a non-null pointer.
318 const SpecificIntrinsic *GetSpecificIntrinsic() const;
319 const Symbol *GetSymbol() const; // symbol or component symbol
320 const SymbolRef *UnwrapSymbolRef() const; // null if intrinsic or component
321
322 // For references to NOPASS components and bindings only.
323 // References to PASS components and bindings are represented
324 // with the symbol below and the base object DataRef in the
325 // passed-object ActualArgument.
326 // Always null when the procedure is intrinsic.
327 const Component *GetComponent() const;
328
329 const Symbol *GetInterfaceSymbol() const;
330
331 std::string GetName() const;
332 std::optional<parser::CharBlock> genericName() const { return genericName_; }
333 ProcedureDesignator &set_genericName(parser::CharBlock name) {
334 genericName_ = name;
335 return *this;
336 }
337 std::optional<DynamicType> GetType() const;
338 int Rank() const;
339 bool IsElemental() const;
340 bool IsPure() const;
341 bool IsSimple() const;
342 std::optional<Expr<SubscriptInteger>> LEN() const;
343 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
344
345 std::variant<SpecificIntrinsic, SymbolRef,
346 common::CopyableIndirection<Component>>
347 u;
348
349private:
350 // Diagnostic metadata only; equality and lowering use the resolved specific.
351 std::optional<parser::CharBlock> genericName_;
352};
353
354using Chevrons = std::vector<Expr<SomeType>>;
355
356class ProcedureRef {
357public:
358 static int kind() {
359 DIE("This class has no kind");
360 return 0;
361 }
362
363 CLASS_BOILERPLATE(ProcedureRef)
364 ProcedureRef(ProcedureDesignator &&p, ActualArguments &&a,
365 bool hasAlternateReturns = false)
366 : proc_{std::move(p)}, arguments_{std::move(a)},
367 hasAlternateReturns_{hasAlternateReturns} {}
368 ~ProcedureRef();
369 static void Deleter(ProcedureRef *);
370
371 ProcedureDesignator &proc() { return proc_; }
372 const ProcedureDesignator &proc() const { return proc_; }
373 ActualArguments &arguments() { return arguments_; }
374 const ActualArguments &arguments() const { return arguments_; }
375 // CALL subr <<< kernel launch >>> (...); not function
376 Chevrons &chevrons() { return chevrons_; }
377 const Chevrons &chevrons() const { return chevrons_; }
378 void set_chevrons(Chevrons &&chevrons) { chevrons_ = std::move(chevrons); }
379
380 std::optional<Expr<SubscriptInteger>> LEN() const;
381 int Rank() const;
382 static constexpr int Corank() { return 0; } // TODO
383 bool IsElemental() const { return proc_.IsElemental(); }
384 bool hasAlternateReturns() const { return hasAlternateReturns_; }
385
386 bool hasNoInline() const { return noInline_; }
387 void setNoInline(bool ni) { noInline_ = ni; }
388 bool hasAlwaysInline() const { return alwaysInline_; }
389 void setAlwaysInline(bool ai) { alwaysInline_ = ai; }
390 bool hasInlineHint() const { return inlineHint_; }
391 void setInlineHint(bool ih) { inlineHint_ = ih; }
392
393 Expr<SomeType> *UnwrapArgExpr(int n) {
394 if (static_cast<std::size_t>(n) < arguments_.size() && arguments_[n]) {
395 return arguments_[n]->UnwrapExpr();
396 } else {
397 return nullptr;
398 }
399 }
400 const Expr<SomeType> *UnwrapArgExpr(int n) const {
401 if (static_cast<std::size_t>(n) < arguments_.size() && arguments_[n]) {
402 return arguments_[n]->UnwrapExpr();
403 } else {
404 return nullptr;
405 }
406 }
407
408 bool operator==(const ProcedureRef &) const;
409 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
410
411protected:
413 ActualArguments arguments_;
414 Chevrons chevrons_;
415 bool hasAlternateReturns_;
416 bool noInline_{false};
417 bool alwaysInline_{false};
418 bool inlineHint_{false};
419};
420
421template <typename A> class FunctionRef : public ProcedureRef {
422public:
423 constexpr int kind() const { return kind_; }
424
425 using Result = A;
426 CLASS_BOILERPLATE(FunctionRef)
427
428 explicit FunctionRef(int kind, ProcedureRef &&pr)
429 : ProcedureRef{std::move(pr)}, kind_{kind} {
430 CHECK_KIND(kind, A);
431 }
432 FunctionRef(int kind, ProcedureDesignator &&p, ActualArguments &&a)
433 : ProcedureRef{std::move(p), std::move(a)}, kind_{kind} {
434 CHECK_KIND(kind, A);
435 }
436
437 std::optional<DynamicType> GetType() const {
438 if constexpr (IsLengthlessIntrinsicType<A>) {
439 return DynamicType{A::category, kind_};
440 } else if (auto type{proc_.GetType()}) {
441 // TODO: Non constant explicit length parameters of PDTs result should
442 // likely be dropped too. This is not as easy as for characters since some
443 // long lived DerivedTypeSpec pointer would need to be created here. It is
444 // not clear if this is causing any issue so far since the storage size of
445 // PDTs is independent of length parameters.
446 return type->DropNonConstantCharacterLength();
447 } else {
448 return std::nullopt;
449 }
450 }
451
452private:
453 int kind_;
454};
455} // namespace Fortran::evaluate
456#endif // FORTRAN_EVALUATE_CALL_H_
Definition enum-set.h:28
Definition indirection.h:31
Definition variable.h:73
Definition type.h:101
Definition common.h:222
Definition type.h:61
Definition char-block.h:27
Definition symbol.h:905
Definition call.h:34
Definition idioms.h:60
Definition characteristics.h:350