1 //===-- include/flang/Evaluate/constant.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_CONSTANT_H_
10 #define FORTRAN_EVALUATE_CONSTANT_H_
11 
12 #include "formatting.h"
13 #include "type.h"
14 #include "flang/Common/default-kinds.h"
15 #include "flang/Common/reference.h"
16 #include <map>
17 #include <vector>
18 
19 namespace llvm {
20 class raw_ostream;
21 }
22 
23 namespace Fortran::semantics {
24 class Symbol;
25 }
26 
27 namespace Fortran::evaluate {
28 
29 using semantics::Symbol;
30 using SymbolRef = common::Reference<const Symbol>;
31 
32 // Wraps a constant value in a class templated by its resolved type.
33 // This Constant<> template class should be instantiated only for
34 // concrete intrinsic types and SomeDerived.  There is no instance
35 // Constant<SomeType> since there is no way to constrain each
36 // element of its array to hold the same type.  To represent a generic
37 // constant, use a generic expression like Expr<SomeInteger> or
38 // Expr<SomeType>) to wrap the appropriate instantiation of Constant<>.
39 
40 template <typename> class Constant;
41 
42 // When describing shapes of constants or specifying 1-based subscript
43 // values as indices into constants, use a vector of integers.
44 using ConstantSubscripts = std::vector<ConstantSubscript>;
GetRank(const ConstantSubscripts & s)45 inline int GetRank(const ConstantSubscripts &s) {
46   return static_cast<int>(s.size());
47 }
48 
49 std::size_t TotalElementCount(const ConstantSubscripts &);
50 
51 // Validate dimension re-ordering like ORDER in RESHAPE.
52 // On success, return a vector that can be used as dimOrder in
53 // ConstantBound::IncrementSubscripts().
54 std::optional<std::vector<int>> ValidateDimensionOrder(
55     int rank, const std::vector<int> &order);
56 
57 bool HasNegativeExtent(const ConstantSubscripts &);
58 
59 class ConstantBounds {
60 public:
61   ConstantBounds() = default;
62   explicit ConstantBounds(const ConstantSubscripts &shape);
63   explicit ConstantBounds(ConstantSubscripts &&shape);
64   ~ConstantBounds();
shape()65   const ConstantSubscripts &shape() const { return shape_; }
lbounds()66   const ConstantSubscripts &lbounds() const { return lbounds_; }
67   void set_lbounds(ConstantSubscripts &&);
Rank()68   int Rank() const { return GetRank(shape_); }
69   Constant<SubscriptInteger> SHAPE() const;
70 
71   // If no optional dimension order argument is passed, increments a vector of
72   // subscripts in Fortran array order (first dimension varying most quickly).
73   // Otherwise, increments the vector of subscripts according to the given
74   // dimension order (dimension dimOrder[0] varying most quickly; dimension
75   // indexing is zero based here). Returns false when last element was visited.
76   bool IncrementSubscripts(
77       ConstantSubscripts &, const std::vector<int> *dimOrder = nullptr) const;
78 
79 protected:
80   ConstantSubscript SubscriptsToOffset(const ConstantSubscripts &) const;
81 
82 private:
83   ConstantSubscripts shape_;
84   ConstantSubscripts lbounds_;
85 };
86 
87 // Constant<> is specialized for Character kinds and SomeDerived.
88 // The non-Character intrinsic types, and SomeDerived, share enough
89 // common behavior that they use this common base class.
90 template <typename RESULT, typename ELEMENT = Scalar<RESULT>>
91 class ConstantBase : public ConstantBounds {
92   static_assert(RESULT::category != TypeCategory::Character);
93 
94 public:
95   using Result = RESULT;
96   using Element = ELEMENT;
97 
98   template <typename A>
99   ConstantBase(const A &x, Result res = Result{}) : result_{res}, values_{x} {}
100   ConstantBase(ELEMENT &&x, Result res = Result{})
101       : result_{res}, values_{std::move(x)} {}
102   ConstantBase(
103       std::vector<Element> &&, ConstantSubscripts &&, Result = Result{});
104 
105   DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(ConstantBase)
106   ~ConstantBase();
107 
108   bool operator==(const ConstantBase &) const;
empty()109   bool empty() const { return values_.empty(); }
size()110   std::size_t size() const { return values_.size(); }
values()111   const std::vector<Element> &values() const { return values_; }
result()112   constexpr Result result() const { return result_; }
113 
GetType()114   constexpr DynamicType GetType() const { return result_.GetType(); }
115   llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
116 
117 protected:
118   std::vector<Element> Reshape(const ConstantSubscripts &) const;
119   std::size_t CopyFrom(const ConstantBase &source, std::size_t count,
120       ConstantSubscripts &resultSubscripts, const std::vector<int> *dimOrder);
121 
122   Result result_;
123   std::vector<Element> values_;
124 };
125 
126 template <typename T> class Constant : public ConstantBase<T> {
127 public:
128   using Result = T;
129   using Base = ConstantBase<T>;
130   using Element = Scalar<T>;
131 
132   using Base::Base;
CLASS_BOILERPLATE(Constant)133   CLASS_BOILERPLATE(Constant)
134 
135   std::optional<Scalar<T>> GetScalarValue() const {
136     if (ConstantBounds::Rank() == 0) {
137       return Base::values_.at(0);
138     } else {
139       return std::nullopt;
140     }
141   }
142 
143   // Apply subscripts.
144   Element At(const ConstantSubscripts &) const;
145 
146   Constant Reshape(ConstantSubscripts &&) const;
147   std::size_t CopyFrom(const Constant &source, std::size_t count,
148       ConstantSubscripts &resultSubscripts, const std::vector<int> *dimOrder);
149 };
150 
151 template <int KIND>
152 class Constant<Type<TypeCategory::Character, KIND>> : public ConstantBounds {
153 public:
154   using Result = Type<TypeCategory::Character, KIND>;
155   using Element = Scalar<Result>;
156 
157   CLASS_BOILERPLATE(Constant)
158   explicit Constant(const Scalar<Result> &);
159   explicit Constant(Scalar<Result> &&);
160   Constant(
161       ConstantSubscript length, std::vector<Element> &&, ConstantSubscripts &&);
162   ~Constant();
163 
164   bool operator==(const Constant &that) const {
165     return shape() == that.shape() && values_ == that.values_;
166   }
167   bool empty() const;
168   std::size_t size() const;
169 
LEN()170   ConstantSubscript LEN() const { return length_; }
171 
GetScalarValue()172   std::optional<Scalar<Result>> GetScalarValue() const {
173     if (Rank() == 0) {
174       return values_;
175     } else {
176       return std::nullopt;
177     }
178   }
179 
180   // Apply subscripts
181   Scalar<Result> At(const ConstantSubscripts &) const;
182 
183   Constant Reshape(ConstantSubscripts &&) const;
184   llvm::raw_ostream &AsFortran(llvm::raw_ostream &) const;
GetType()185   static constexpr DynamicType GetType() {
186     return {TypeCategory::Character, KIND};
187   }
188   std::size_t CopyFrom(const Constant &source, std::size_t count,
189       ConstantSubscripts &resultSubscripts, const std::vector<int> *dimOrder);
190 
191 private:
192   Scalar<Result> values_; // one contiguous string
193   ConstantSubscript length_;
194 };
195 
196 class StructureConstructor;
197 using StructureConstructorValues =
198     std::map<SymbolRef, common::CopyableIndirection<Expr<SomeType>>>;
199 
200 template <>
201 class Constant<SomeDerived>
202     : public ConstantBase<SomeDerived, StructureConstructorValues> {
203 public:
204   using Result = SomeDerived;
205   using Element = StructureConstructorValues;
206   using Base = ConstantBase<SomeDerived, StructureConstructorValues>;
207 
208   Constant(const StructureConstructor &);
209   Constant(StructureConstructor &&);
210   Constant(const semantics::DerivedTypeSpec &,
211       std::vector<StructureConstructorValues> &&, ConstantSubscripts &&);
212   Constant(const semantics::DerivedTypeSpec &,
213       std::vector<StructureConstructor> &&, ConstantSubscripts &&);
214   CLASS_BOILERPLATE(Constant)
215 
216   std::optional<StructureConstructor> GetScalarValue() const;
217   StructureConstructor At(const ConstantSubscripts &) const;
218 
219   Constant Reshape(ConstantSubscripts &&) const;
220   std::size_t CopyFrom(const Constant &source, std::size_t count,
221       ConstantSubscripts &resultSubscripts, const std::vector<int> *dimOrder);
222 };
223 
224 FOR_EACH_LENGTHLESS_INTRINSIC_KIND(extern template class ConstantBase, )
225 extern template class ConstantBase<SomeDerived, StructureConstructorValues>;
226 FOR_EACH_INTRINSIC_KIND(extern template class Constant, )
227 
228 #define INSTANTIATE_CONSTANT_TEMPLATES \
229   FOR_EACH_LENGTHLESS_INTRINSIC_KIND(template class ConstantBase, ) \
230   template class ConstantBase<SomeDerived, StructureConstructorValues>; \
231   FOR_EACH_INTRINSIC_KIND(template class Constant, )
232 } // namespace Fortran::evaluate
233 #endif // FORTRAN_EVALUATE_CONSTANT_H_
234