[flang] Make implicit conversion explicit in assignment
authorTim Keith <tkeith@nvidia.com>
Sat, 9 May 2020 16:10:08 +0000 (09:10 -0700)
committerTim Keith <tkeith@nvidia.com>
Sat, 9 May 2020 16:11:00 +0000 (09:11 -0700)
When intrinsic types are assigned there are some implicit conversions
that take place. This change make them explicit in the types
representation of assignments.

Differential Revision: https://reviews.llvm.org/D79637

flang/lib/Semantics/expression.cpp

index 9306f70..cc7faef 100644 (file)
@@ -184,6 +184,8 @@ private:
   std::optional<ActualArgument> AnalyzeExpr(const parser::Expr &);
   bool AreConformable() const;
   const Symbol *FindBoundOp(parser::CharBlock, int passIndex);
+  void AddAssignmentConversion(
+      const DynamicType &lhsType, const DynamicType &rhsType);
   bool OkLogicalIntegerAssignment(TypeCategory lhs, TypeCategory rhs);
   std::optional<DynamicType> GetType(std::size_t) const;
   int GetRank(std::size_t) const;
@@ -2809,6 +2811,9 @@ std::optional<ProcedureRef> ArgumentAnalyzer::TryDefinedAssignment() {
   Tristate isDefined{
       semantics::IsDefinedAssignment(lhsType, lhsRank, rhsType, rhsRank)};
   if (isDefined == Tristate::No) {
+    if (lhsType && rhsType) {
+      AddAssignmentConversion(*lhsType, *rhsType);
+    }
     return std::nullopt; // user-defined assignment not allowed for these args
   }
   auto restorer{context_.GetContextualMessages().SetLocation(source_)};
@@ -2939,6 +2944,19 @@ const Symbol *ArgumentAnalyzer::FindBoundOp(
   return result;
 }
 
+// If there is an implicit conversion between intrinsic types, make it explicit
+void ArgumentAnalyzer::AddAssignmentConversion(
+    const DynamicType &lhsType, const DynamicType &rhsType) {
+  if (lhsType.category() == rhsType.category() &&
+      lhsType.kind() == rhsType.kind()) {
+    // no conversion necessary
+  } else if (auto rhsExpr{evaluate::ConvertToType(lhsType, MoveExpr(1))}) {
+    actuals_[1] = ActualArgument{*rhsExpr};
+  } else {
+    actuals_[1] = std::nullopt;
+  }
+}
+
 std::optional<DynamicType> ArgumentAnalyzer::GetType(std::size_t i) const {
   return i < actuals_.size() ? actuals_[i].value().GetType() : std::nullopt;
 }