FLANG
variable.h
1//===-- include/flang/Evaluate/variable.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_VARIABLE_H_
10#define FORTRAN_EVALUATE_VARIABLE_H_
11
12// Defines data structures to represent data access and function calls
13// for use in expressions and assignment statements. Both copy and move
14// semantics are supported. The representation adheres closely to the
15// Fortran 2018 language standard (q.v.) and uses strong typing to ensure
16// that only admissable combinations can be constructed.
17
18#include "call.h"
19#include "common.h"
20#include "formatting.h"
21#include "static-data.h"
22#include "type.h"
23#include "flang/Common/idioms.h"
24#include "flang/Common/reference.h"
25#include "flang/Common/template.h"
26#include "flang/Parser/char-block.h"
27#include <optional>
28#include <variant>
29#include <vector>
30
31namespace llvm {
32class raw_ostream;
33}
34
35namespace Fortran::semantics {
36class Symbol;
37}
38
39namespace Fortran::evaluate {
40
41using semantics::Symbol;
42using SymbolRef = common::Reference<const Symbol>;
43using SymbolVector = std::vector<SymbolRef>;
44
45// Forward declarations
46struct DataRef;
47
48// Reference a base object in memory. This can be a Fortran symbol,
49// static data (e.g., CHARACTER literal), or compiler-created temporary.
50struct BaseObject {
51 EVALUATE_UNION_CLASS_BOILERPLATE(BaseObject)
52 int Rank() const;
53 int Corank() const;
54 std::optional<Expr<SubscriptInteger>> LEN() const;
55 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
56 const Symbol *symbol() const {
57 if (const auto *result{std::get_if<SymbolRef>(&u)}) {
58 return &result->get();
59 } else {
60 return nullptr;
61 }
62 }
63 std::variant<SymbolRef, StaticDataObject::Pointer> u;
64};
65
66// R913 structure-component & C920: Defined to be a multi-part
67// data-ref whose last part has no subscripts (or image-selector, although
68// that isn't explicit in the document). Pointer and allocatable components
69// are not explicitly indirected in this representation.
70// Complex components (%RE, %IM) are isolated below in ComplexPart.
71// (Type parameter inquiries look like component references but are distinct
72// constructs and not represented by this class.)
73class Component {
74public:
75 CLASS_BOILERPLATE(Component)
76 Component(const DataRef &b, const Symbol &c) : base_{b}, symbol_{c} {}
77 Component(DataRef &&b, const Symbol &c) : base_{std::move(b)}, symbol_{c} {}
78 Component(common::CopyableIndirection<DataRef> &&b, const Symbol &c)
79 : base_{std::move(b)}, symbol_{c} {}
80
81 const DataRef &base() const { return base_.value(); }
82 DataRef &base() { return base_.value(); }
83 const SymbolRef &symbol() const { return symbol_; }
84 SymbolRef &symbol() { return symbol_; }
85
86 int Rank() const;
87 int Corank() const;
88 const Symbol &GetFirstSymbol() const;
89 const Symbol &GetLastSymbol() const { return symbol_; }
90 std::optional<Expr<SubscriptInteger>> LEN() const;
91 bool operator==(const Component &) const;
92 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
93
94private:
95 common::CopyableIndirection<DataRef> base_;
96 SymbolRef symbol_;
97};
98
99// A NamedEntity is either a whole Symbol or a component in an instance
100// of a derived type. It may be a descriptor.
101class NamedEntity {
102public:
103 CLASS_BOILERPLATE(NamedEntity)
104 explicit NamedEntity(const Symbol &symbol) : u_{symbol} {}
105 explicit NamedEntity(Component &&c) : u_{std::move(c)} {}
106
107 bool IsSymbol() const { return std::holds_alternative<SymbolRef>(u_); }
108 const Symbol &GetFirstSymbol() const;
109 const Symbol &GetLastSymbol() const;
110 const Component &GetComponent() const { return std::get<Component>(u_); }
111 Component &GetComponent() { return std::get<Component>(u_); }
112 const SymbolRef *UnwrapSymbolRef() const; // null if a Component
113 SymbolRef *UnwrapSymbolRef();
114 const Component *UnwrapComponent() const; // null if not a Component
115 Component *UnwrapComponent();
116
117 int Rank() const;
118 int Corank() const;
119 std::optional<Expr<SubscriptInteger>> LEN() const;
120 bool operator==(const NamedEntity &) const;
121 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
122
123private:
124 std::variant<SymbolRef, Component> u_;
125};
126
127// R916 type-param-inquiry
128// N.B. x%LEN for CHARACTER is rewritten in semantics to LEN(x), which is
129// then handled via LEN() member functions in the various classes;
130// it becomes a DescriptorInquiry with Field::Len for assumed-length
131// CHARACTER objects.
132// x%KIND for intrinsic types is similarly rewritten in semantics to
133// KIND(x), which is then folded to a constant value.
134// "Bare" type parameter references within a derived type definition do
135// not have base objects.
136class TypeParamInquiry {
137public:
138 using Result = SubscriptInteger;
139 CLASS_BOILERPLATE(TypeParamInquiry)
140 TypeParamInquiry(NamedEntity &&x, const Symbol &param)
141 : base_{std::move(x)}, parameter_{param} {}
142 TypeParamInquiry(std::optional<NamedEntity> &&x, const Symbol &param)
143 : base_{std::move(x)}, parameter_{param} {}
144
145 const std::optional<NamedEntity> &base() const { return base_; }
146 std::optional<NamedEntity> &base() { return base_; }
147 const Symbol &parameter() const { return parameter_; }
148
149 static constexpr int Rank() { return 0; } // always scalar
150 static constexpr int Corank() { return 0; }
151 bool operator==(const TypeParamInquiry &) const;
152 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
153
154private:
155 std::optional<NamedEntity> base_;
156 SymbolRef parameter_;
157};
158
159// R921 subscript-triplet
160class Triplet {
161public:
162 Triplet();
163 DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(Triplet)
164 Triplet(std::optional<Expr<SubscriptInteger>> &&,
165 std::optional<Expr<SubscriptInteger>> &&,
166 std::optional<Expr<SubscriptInteger>> &&);
167
168 std::optional<Expr<SubscriptInteger>> lower() const;
169 const Expr<SubscriptInteger> *GetLower() const {
170 return lower_.has_value() ? &lower_->value() : nullptr;
171 }
172 Triplet &set_lower(Expr<SubscriptInteger> &&);
173 std::optional<Expr<SubscriptInteger>> upper() const;
174 const Expr<SubscriptInteger> *GetUpper() const {
175 return upper_.has_value() ? &upper_->value() : nullptr;
176 }
177 Triplet &set_upper(Expr<SubscriptInteger> &&);
178 Expr<SubscriptInteger> stride() const; // N.B. result is not optional<>
179 const Expr<SubscriptInteger> &GetStride() const { return stride_.value(); }
180 Triplet &set_stride(Expr<SubscriptInteger> &&);
181
182 bool operator==(const Triplet &) const;
183 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
184
185private:
186 std::optional<IndirectSubscriptIntegerExpr> lower_, upper_;
187 IndirectSubscriptIntegerExpr stride_;
188};
189
190// R919 subscript when rank 0, R923 vector-subscript when rank 1
191struct Subscript {
192 EVALUATE_UNION_CLASS_BOILERPLATE(Subscript)
193 explicit Subscript(Expr<SubscriptInteger> &&s)
194 : u{IndirectSubscriptIntegerExpr::Make(std::move(s))} {}
195 int Rank() const;
196 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
197 std::variant<IndirectSubscriptIntegerExpr, Triplet> u;
198};
199
200// R917 array-element, R918 array-section; however, the case of an
201// array-section that is a complex-part-designator is represented here
202// as a ComplexPart instead. C919 & C925 require that at most one set of
203// subscripts have rank greater than 0, but that is not explicit in
204// these types.
205class ArrayRef {
206public:
207 CLASS_BOILERPLATE(ArrayRef)
208 ArrayRef(const Symbol &symbol, std::vector<Subscript> &&ss)
209 : base_{symbol}, subscript_(std::move(ss)) {}
210 ArrayRef(Component &&c, std::vector<Subscript> &&ss)
211 : base_{std::move(c)}, subscript_(std::move(ss)) {}
212 ArrayRef(NamedEntity &&base, std::vector<Subscript> &&ss)
213 : base_{std::move(base)}, subscript_(std::move(ss)) {}
214
215 NamedEntity &base() { return base_; }
216 const NamedEntity &base() const { return base_; }
217 std::vector<Subscript> &subscript() { return subscript_; }
218 const std::vector<Subscript> &subscript() const { return subscript_; }
219
220 int size() const { return static_cast<int>(subscript_.size()); }
221 Subscript &at(int n) { return subscript_.at(n); }
222 const Subscript &at(int n) const { return subscript_.at(n); }
223 template <typename A> common::IfNoLvalue<Subscript &, A> emplace_back(A &&x) {
224 return subscript_.emplace_back(std::move(x));
225 }
226
227 int Rank() const;
228 int Corank() const;
229 const Symbol &GetFirstSymbol() const;
230 const Symbol &GetLastSymbol() const;
231 std::optional<Expr<SubscriptInteger>> LEN() const;
232 bool operator==(const ArrayRef &) const;
233 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
234
235private:
236 NamedEntity base_;
237 std::vector<Subscript> subscript_;
238};
239
240// A coindexed data-ref. The base is represented as a general
241// DataRef, but the base may not contain a CoarrayRef and may
242// have rank > 0 only in an uppermost ArrayRef.
243class CoarrayRef {
244public:
245 CLASS_BOILERPLATE(CoarrayRef)
246 CoarrayRef(DataRef &&, std::vector<Expr<SubscriptInteger>> &&);
247
248 const DataRef &base() const { return base_.value(); }
249 DataRef &base() { return base_.value(); }
250 const std::vector<Expr<SubscriptInteger>> &cosubscript() const {
251 return cosubscript_;
252 }
253 std::vector<Expr<SubscriptInteger>> &cosubscript() { return cosubscript_; }
254
255 // These integral expressions for STAT= and TEAM= must be variables
256 // (i.e., Designator or pointer-valued FunctionRef).
257 std::optional<Expr<SomeInteger>> stat() const;
258 CoarrayRef &set_stat(Expr<SomeInteger> &&);
259 // When team() is Expr<SomeInteger>, it's TEAM_NUMBER=; otherwise,
260 // it's TEAM=.
261 std::optional<Expr<SomeType>> team() const;
262 CoarrayRef &set_team(Expr<SomeType> &&);
263
264 int Rank() const;
265 int Corank() const { return 0; }
266 const Symbol &GetFirstSymbol() const;
267 const Symbol &GetLastSymbol() const;
268 std::optional<Expr<SubscriptInteger>> LEN() const;
269 bool operator==(const CoarrayRef &) const;
270 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
271
272private:
273 common::CopyableIndirection<DataRef> base_;
274 std::vector<Expr<SubscriptInteger>> cosubscript_;
275 std::optional<common::CopyableIndirection<Expr<SomeInteger>>> stat_;
276 std::optional<common::CopyableIndirection<Expr<SomeType>>> team_;
277};
278
279// R911 data-ref is defined syntactically as a series of part-refs, which
280// would be far too expressive if the constraints were ignored. Here, the
281// possible outcomes are spelled out. Note that a data-ref cannot include
282// a terminal substring range or complex component designator; use
283// R901 designator for that.
284struct DataRef {
285 EVALUATE_UNION_CLASS_BOILERPLATE(DataRef)
286 int Rank() const;
287 int Corank() const;
288 const Symbol &GetFirstSymbol() const;
289 const Symbol &GetLastSymbol() const;
290 std::optional<Expr<SubscriptInteger>> LEN() const;
291 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
292
293 std::variant<SymbolRef, Component, ArrayRef, CoarrayRef> u;
294};
295
296// R908 substring, R909 parent-string, R910 substring-range.
297// The base object of a substring can be a literal.
298// In the F2018 standard, substrings of array sections are parsed as
299// variants of sections instead.
300class Substring {
301 using Parent = std::variant<DataRef, StaticDataObject::Pointer>;
302
303public:
304 CLASS_BOILERPLATE(Substring)
305 Substring(DataRef &&parent, std::optional<Expr<SubscriptInteger>> &&lower,
306 std::optional<Expr<SubscriptInteger>> &&upper)
307 : parent_{std::move(parent)} {
308 SetBounds(lower, upper);
309 }
310 Substring(StaticDataObject::Pointer &&parent,
311 std::optional<Expr<SubscriptInteger>> &&lower,
312 std::optional<Expr<SubscriptInteger>> &&upper)
313 : parent_{std::move(parent)} {
314 SetBounds(lower, upper);
315 }
316
317 Expr<SubscriptInteger> lower() const;
318 const Expr<SubscriptInteger> *GetLower() const {
319 return lower_.has_value() ? &lower_->value() : nullptr;
320 }
321 Substring &set_lower(Expr<SubscriptInteger> &&);
322 std::optional<Expr<SubscriptInteger>> upper() const;
323 const Expr<SubscriptInteger> *GetUpper() const {
324 return upper_.has_value() ? &upper_->value() : nullptr;
325 }
326 Substring &set_upper(Expr<SubscriptInteger> &&);
327 const Parent &parent() const { return parent_; }
328 Parent &parent() { return parent_; }
329
330 int Rank() const;
331 int Corank() const;
332 template <typename A> const A *GetParentIf() const {
333 return std::get_if<A>(&parent_);
334 }
335 BaseObject GetBaseObject() const;
336 const Symbol *GetLastSymbol() const;
337 std::optional<Expr<SubscriptInteger>> LEN() const;
338 bool operator==(const Substring &) const;
339 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
340
341 std::optional<Expr<SomeCharacter>> Fold(FoldingContext &);
342
343private:
344 void SetBounds(std::optional<Expr<SubscriptInteger>> &,
345 std::optional<Expr<SubscriptInteger>> &);
346 Parent parent_;
347 std::optional<IndirectSubscriptIntegerExpr> lower_, upper_;
348};
349
350// R915 complex-part-designator
351// In the F2018 standard, complex parts of array sections are parsed as
352// variants of sections instead.
353class ComplexPart {
354public:
355 ENUM_CLASS(Part, RE, IM)
356 CLASS_BOILERPLATE(ComplexPart)
357 ComplexPart(DataRef &&z, Part p) : complex_{std::move(z)}, part_{p} {}
358 DataRef &complex() { return complex_; }
359 const DataRef &complex() const { return complex_; }
360 Part part() const { return part_; }
361 int Rank() const;
362 int Corank() const;
363 const Symbol &GetFirstSymbol() const { return complex_.GetFirstSymbol(); }
364 const Symbol &GetLastSymbol() const { return complex_.GetLastSymbol(); }
365 bool operator==(const ComplexPart &) const;
366 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
367
368private:
369 DataRef complex_;
370 Part part_;
371};
372
373// R901 designator is the most general data reference object, apart from
374// calls to pointer-valued functions. Its variant holds everything that
375// a DataRef can, and possibly also a substring reference or a
376// complex component (%RE/%IM) reference.
377template <typename T> class Designator {
378 using DataRefs = std::decay_t<decltype(DataRef::u)>;
379 using MaybeSubstring =
380 std::conditional_t<T::category == TypeCategory::Character,
381 std::variant<Substring>, std::variant<>>;
382 using MaybeComplexPart = std::conditional_t<T::category == TypeCategory::Real,
383 std::variant<ComplexPart>, std::variant<>>;
384 using Variant =
385 common::CombineVariants<DataRefs, MaybeSubstring, MaybeComplexPart>;
386
387public:
388 using Result = T;
389 static_assert(
390 IsSpecificIntrinsicType<Result> || std::is_same_v<Result, SomeDerived>);
391 EVALUATE_UNION_CLASS_BOILERPLATE(Designator)
392 Designator(const DataRef &that) : u{common::CopyVariant<Variant>(that.u)} {}
393 Designator(DataRef &&that)
394 : u{common::MoveVariant<Variant>(std::move(that.u))} {}
395
396 std::optional<DynamicType> GetType() const;
397 int Rank() const;
398 int Corank() const;
399 BaseObject GetBaseObject() const;
400 const Symbol *GetLastSymbol() const;
401 std::optional<Expr<SubscriptInteger>> LEN() const;
402 llvm::raw_ostream &AsFortran(llvm::raw_ostream &o) const;
403
404 Variant u;
405};
406
407FOR_EACH_CHARACTER_KIND(extern template class Designator, )
408
409class DescriptorInquiry {
410public:
411 using Result = SubscriptInteger;
412 ENUM_CLASS(Field, LowerBound, Extent, Stride, Rank, Len)
413
414 CLASS_BOILERPLATE(DescriptorInquiry)
415 DescriptorInquiry(const NamedEntity &, Field, int = 0);
416 DescriptorInquiry(NamedEntity &&, Field, int = 0);
417
418 NamedEntity &base() { return base_; }
419 const NamedEntity &base() const { return base_; }
420 Field field() const { return field_; }
421 int dimension() const { return dimension_; }
422
423 static constexpr int Rank() { return 0; } // always scalar
424 static constexpr int Corank() { return 0; }
425 bool operator==(const DescriptorInquiry &) const;
426 llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
427
428private:
429 NamedEntity base_;
430 Field field_;
431 int dimension_{0}; // zero-based
432};
433
434#define INSTANTIATE_VARIABLE_TEMPLATES \
435 FOR_EACH_SPECIFIC_TYPE(template class Designator, )
436} // namespace Fortran::evaluate
437#endif // FORTRAN_EVALUATE_VARIABLE_H_
Definition variable.h:73
Definition variable.h:377
Definition common.h:214
Definition common.h:216
Definition variable.h:101
Definition symbol.h:778
Definition call.h:34
Definition variable.h:50
Definition variable.h:284
Definition variable.h:191