[flang] Use ConstantSubscript as the one name for int64_t
authorpeter klausler <pklausler@nvidia.com>
Fri, 7 Jun 2019 16:57:10 +0000 (09:57 -0700)
committerpeter klausler <pklausler@nvidia.com>
Fri, 7 Jun 2019 16:57:52 +0000 (09:57 -0700)
Original-commit: flang-compiler/f18@13870c00179a56e9a2b865c64cac1453dcbdc8e2
Reviewed-on: https://github.com/flang-compiler/f18/pull/484

flang/lib/common/default-kinds.h
flang/lib/evaluate/common.cc
flang/lib/evaluate/common.h
flang/lib/evaluate/constant.cc
flang/lib/evaluate/constant.h
flang/lib/evaluate/fold.cc
flang/lib/evaluate/formatting.cc
flang/lib/evaluate/variable.cc

index 8b4ab24..04f6118 100644 (file)
 #include "Fortran.h"
 #include <cstdint>
 
+namespace Fortran::common {
+
+// All address calculations in generated code are 64-bit safe.
+// Compile-time folding of bounds, subscripts, and lengths
+// consequently uses 64-bit signed integers.  The name reflects
+// this usage as a subscript into a constant array.
+using ConstantSubscript = std::int64_t;
+
 // Represent the default values of the kind parameters of the
 // various intrinsic types.  Most of these can be configured by
 // means of the compiler command line; subscriptIntegerKind,
 // however, is fixed at 8 because all address calculations are
 // 64-bit safe.
-namespace Fortran::common {
-
-using SubscriptCIntType = std::int64_t;
-
 class IntrinsicTypeDefaultKinds {
 public:
   IntrinsicTypeDefaultKinds();
index f2296a0..f3d3354 100644 (file)
@@ -35,14 +35,14 @@ void RealFlagWarnings(
   }
 }
 
-common::SubscriptCIntType &FoldingContext::StartImpliedDo(
-    parser::CharBlock name, common::SubscriptCIntType n) {
+ConstantSubscript &FoldingContext::StartImpliedDo(
+    parser::CharBlock name, ConstantSubscript n) {
   auto pair{impliedDos_.insert(std::make_pair(name, n))};
   CHECK(pair.second);
   return pair.first->second;
 }
 
-std::optional<common::SubscriptCIntType> FoldingContext::GetImpliedDo(
+std::optional<ConstantSubscript> FoldingContext::GetImpliedDo(
     parser::CharBlock name) const {
   if (auto iter{impliedDos_.find(name)}; iter != impliedDos_.cend()) {
     return {iter->second};
index 3f5b7e0..8b2780b 100644 (file)
@@ -33,6 +33,7 @@ class DerivedTypeSpec;
 
 namespace Fortran::evaluate {
 
+using common::ConstantSubscript;
 using common::RelationalOperator;
 
 // Integers are always ordered; reals may not be.
@@ -229,13 +230,11 @@ public:
     return hostIntrinsicsLibrary_;
   }
 
-  common::SubscriptCIntType &StartImpliedDo(
-      parser::CharBlock, common::SubscriptCIntType = 1);
-  std::optional<common::SubscriptCIntType> GetImpliedDo(
-      parser::CharBlock) const;
+  ConstantSubscript &StartImpliedDo(parser::CharBlock, ConstantSubscript = 1);
+  std::optional<ConstantSubscript> GetImpliedDo(parser::CharBlock) const;
   void EndImpliedDo(parser::CharBlock);
 
-  std::map<parser::CharBlock, common::SubscriptCIntType> &impliedDos() {
+  std::map<parser::CharBlock, ConstantSubscript> &impliedDos() {
     return impliedDos_;
   }
 
@@ -250,7 +249,7 @@ private:
   bool flushSubnormalsToZero_{false};
   bool bigEndian_{false};
   const semantics::DerivedTypeSpec *pdtInstance_{nullptr};
-  std::map<parser::CharBlock, common::SubscriptCIntType> impliedDos_;
+  std::map<parser::CharBlock, ConstantSubscript> impliedDos_;
   HostIntrinsicProceduresLibrary hostIntrinsicsLibrary_;
 };
 
index a00a950..f14d0fc 100644 (file)
@@ -109,23 +109,23 @@ auto Constant<T>::Reshape(ConstantSubscripts &&dims) const -> Constant {
 template<int KIND>
 Constant<Type<TypeCategory::Character, KIND>>::Constant(
     const Scalar<Result> &str)
-  : values_{str}, length_{static_cast<LengthCIntType>(values_.size())} {}
+  : values_{str}, length_{static_cast<ConstantSubscript>(values_.size())} {}
 
 template<int KIND>
 Constant<Type<TypeCategory::Character, KIND>>::Constant(Scalar<Result> &&str)
-  : values_{std::move(str)}, length_{
-                                 static_cast<LengthCIntType>(values_.size())} {}
+  : values_{std::move(str)}, length_{static_cast<ConstantSubscript>(
+                                 values_.size())} {}
 
 template<int KIND>
-Constant<Type<TypeCategory::Character, KIND>>::Constant(LengthCIntType len,
+Constant<Type<TypeCategory::Character, KIND>>::Constant(ConstantSubscript len,
     std::vector<Scalar<Result>> &&strings, ConstantSubscripts &&dims)
   : length_{len}, shape_{std::move(dims)} {
   CHECK(strings.size() == TotalElementCount(shape_));
   values_.assign(strings.size() * length_,
       static_cast<typename Scalar<Result>::value_type>(' '));
-  LengthCIntType at{0};
+  ConstantSubscript at{0};
   for (const auto &str : strings) {
-    auto strLen{static_cast<LengthCIntType>(str.size())};
+    auto strLen{static_cast<ConstantSubscript>(str.size())};
     if (strLen > length_) {
       values_.replace(at, length_, str.substr(0, length_));
     } else {
@@ -133,7 +133,7 @@ Constant<Type<TypeCategory::Character, KIND>>::Constant(LengthCIntType len,
     }
     at += length_;
   }
-  CHECK(at == static_cast<LengthCIntType>(values_.size()));
+  CHECK(at == static_cast<ConstantSubscript>(values_.size()));
 }
 
 template<int KIND> Constant<Type<TypeCategory::Character, KIND>>::~Constant() {}
@@ -148,7 +148,7 @@ std::size_t Constant<Type<TypeCategory::Character, KIND>>::size() const {
   if (length_ == 0) {
     return TotalElementCount(shape_);
   } else {
-    return static_cast<LengthCIntType>(values_.size()) / length_;
+    return static_cast<ConstantSubscript>(values_.size()) / length_;
   }
 }
 
@@ -165,7 +165,8 @@ auto Constant<Type<TypeCategory::Character, KIND>>::Reshape(
   std::size_t n{TotalElementCount(dims)};
   CHECK(!empty() || n == 0);
   std::vector<Element> elements;
-  LengthCIntType at{0}, limit{static_cast<LengthCIntType>(values_.size())};
+  ConstantSubscript at{0},
+      limit{static_cast<ConstantSubscript>(values_.size())};
   while (n-- > 0) {
     elements.push_back(values_.substr(at, length_));
     at += length_;
index c5c9cc0..f140fc4 100644 (file)
@@ -36,14 +36,11 @@ template<typename> class Constant;
 
 // When describing shapes of constants or specifying 1-based subscript
 // values as indices into constants, use a vector of integers.
-using ConstantSubscript = common::SubscriptCIntType;
 using ConstantSubscripts = std::vector<ConstantSubscript>;
 inline int GetRank(const ConstantSubscripts &s) {
   return static_cast<int>(s.size());
 }
 
-using LengthCIntType = common::SubscriptCIntType;
-
 std::size_t TotalElementCount(const ConstantSubscripts &);
 
 inline ConstantSubscripts InitialSubscripts(int rank) {
@@ -130,7 +127,7 @@ public:
   CLASS_BOILERPLATE(Constant)
   explicit Constant(const Scalar<Result> &);
   explicit Constant(Scalar<Result> &&);
-  Constant(LengthCIntType, std::vector<Element> &&, ConstantSubscripts &&);
+  Constant(ConstantSubscript, std::vector<Element> &&, ConstantSubscripts &&);
   ~Constant();
 
   int Rank() const { return GetRank(shape_); }
@@ -141,7 +138,7 @@ public:
   std::size_t size() const;
   const ConstantSubscripts &shape() const { return shape_; }
 
-  LengthCIntType LEN() const { return length_; }
+  ConstantSubscript LEN() const { return length_; }
 
   std::optional<Scalar<Result>> GetScalarValue() const {
     if (shape_.empty()) {
@@ -163,7 +160,7 @@ public:
 
 private:
   Scalar<Result> values_;  // one contiguous string
-  LengthCIntType length_;
+  ConstantSubscript length_;
   ConstantSubscripts shape_;
 };
 
index 3662975..9b01fbd 100644 (file)
@@ -267,7 +267,7 @@ static inline Expr<TR> FoldElementalIntrinsicHelper(FoldingContext &context,
     }
     // Build and return constant result
     if constexpr (TR::category == TypeCategory::Character) {
-      auto len{static_cast<LengthCIntType>(
+      auto len{static_cast<ConstantSubscript>(
           results.size() ? results[0].length() : 0)};
       return Expr<TR>{Constant<TR>{len, std::move(results), std::move(shape)}};
     } else {
@@ -1290,8 +1290,7 @@ Expr<T> FoldOperation(FoldingContext &context, Designator<T> &&designator) {
 
 Expr<ImpliedDoIndex::Result> FoldOperation(
     FoldingContext &context, ImpliedDoIndex &&iDo) {
-  if (std::optional<common::SubscriptCIntType> value{
-          context.GetImpliedDo(iDo.name)}) {
+  if (std::optional<ConstantSubscript> value{context.GetImpliedDo(iDo.name)}) {
     return Expr<ImpliedDoIndex::Result>{*value};
   } else {
     return Expr<ImpliedDoIndex::Result>{std::move(iDo)};
@@ -1311,7 +1310,7 @@ public:
             std::move(elements_), ConstantSubscripts{n}}};
       } else if constexpr (T::category == TypeCategory::Character) {
         auto length{Fold(context_, common::Clone(array.LEN()))};
-        if (std::optional<LengthCIntType> lengthValue{ToInt64(length)}) {
+        if (std::optional<ConstantSubscript> lengthValue{ToInt64(length)}) {
           return Expr<T>{Constant<T>{
               *lengthValue, std::move(elements_), ConstantSubscripts{n}}};
         }
@@ -1352,14 +1351,14 @@ private:
         Fold(context_, Expr<SubscriptInteger>{iDo.upper()})};
     Expr<SubscriptInteger> stride{
         Fold(context_, Expr<SubscriptInteger>{iDo.stride()})};
-    std::optional<common::SubscriptCIntType> start{ToInt64(lower)},
-        end{ToInt64(upper)}, step{ToInt64(stride)};
+    std::optional<ConstantSubscript> start{ToInt64(lower)}, end{ToInt64(upper)},
+        step{ToInt64(stride)};
     if (start.has_value() && end.has_value() && step.has_value()) {
       if (*step == 0) {
         return false;
       }
       bool result{true};
-      common::SubscriptCIntType &j{context_.StartImpliedDo(iDo.name(), *start)};
+      ConstantSubscript &j{context_.StartImpliedDo(iDo.name(), *start)};
       if (*step > 0) {
         for (; j <= *end; j += *step) {
           result &= FoldArray(iDo.values());
@@ -2123,14 +2122,14 @@ Expr<Type<TypeCategory::Character, KIND>> FoldOperation(
   }
   using Result = Type<TypeCategory::Character, KIND>;
   if (auto folded{OperandsAreConstants(x)}) {
-    auto oldLength{static_cast<LengthCIntType>(folded->first.size())};
+    auto oldLength{static_cast<ConstantSubscript>(folded->first.size())};
     auto newLength{folded->second.ToInt64()};
     if (newLength < oldLength) {
       folded->first.erase(newLength);
     } else {
       folded->first.append(newLength - oldLength, ' ');
     }
-    CHECK(static_cast<LengthCIntType>(folded->first.size()) == newLength);
+    CHECK(static_cast<ConstantSubscript>(folded->first.size()) == newLength);
     return Expr<Result>{Constant<Result>{std::move(folded->first)}};
   }
   return Expr<Result>{std::move(x)};
index 7116442..58db40a 100644 (file)
@@ -84,8 +84,8 @@ std::ostream &Constant<Type<TypeCategory::Character, KIND>>::AsFortran(
   if (Rank() > 0) {
     o << '[' << GetType().AsFortran(std::to_string(length_)) << "::";
   }
-  auto total{static_cast<LengthCIntType>(size())};
-  for (LengthCIntType j{0}; j < total; ++j) {
+  auto total{static_cast<ConstantSubscript>(size())};
+  for (ConstantSubscript j{0}; j < total; ++j) {
     Scalar<Result> value{values_.substr(j * length_, length_)};
     if (j > 0) {
       o << ',';
index 9013fbd..5bb3fbe 100644 (file)
@@ -188,7 +188,7 @@ std::optional<Expr<SomeCharacter>> Substring::Fold(FoldingContext &context) {
   upper_.value() = evaluate::Fold(context, std::move(upper_.value().value()));
   if (std::optional<ConstantSubscript> ubi{ToInt64(upper_.value().value())}) {
     auto *literal{std::get_if<StaticDataObject::Pointer>(&parent_)};
-    std::optional<LengthCIntType> length;
+    std::optional<ConstantSubscript> length;
     if (literal != nullptr) {
       length = (*literal)->data().size();
     } else if (const Symbol * symbol{GetLastSymbol()}) {
@@ -223,7 +223,7 @@ std::optional<Expr<SomeCharacter>> Substring::Fold(FoldingContext &context) {
       }
       parent_ = newStaticData;
       lower_ = AsExpr(Constant<SubscriptInteger>{1});
-      LengthCIntType length = newStaticData->data().size();
+      ConstantSubscript length = newStaticData->data().size();
       upper_ = AsExpr(Constant<SubscriptInteger>{length});
       switch (width) {
       case 1: