| 1 |
//===-- lib/Semantics/check-case.cpp --------------------------------------===// |
| 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 |
#include "check-case.h" |
| 10 |
#include "flang/Common/idioms.h" |
| 11 |
#include "flang/Common/reference.h" |
| 12 |
#include "flang/Common/template.h" |
| 13 |
#include "flang/Evaluate/fold.h" |
| 14 |
#include "flang/Evaluate/type.h" |
| 15 |
#include "flang/Parser/parse-tree.h" |
| 16 |
#include "flang/Semantics/semantics.h" |
| 17 |
#include "flang/Semantics/tools.h" |
| 18 |
#include <tuple> |
| 19 |
|
| 20 |
namespace Fortran::semantics { |
| 21 |
|
| 22 |
template <typename T> class CaseValues { |
| 23 |
public: |
| 24 |
CaseValues(SemanticsContext &c, const evaluate::DynamicType &t) |
| 25 |
: context_{c}, caseExprType_{t} {} |
| 26 |
|
| 27 |
void Check(const std::list<parser::CaseConstruct::Case> &cases) { |
| 28 |
for (const parser::CaseConstruct::Case &c : cases) { |
| 29 |
AddCase(c); |
| 30 |
} |
| 31 |
if (!hasErrors_) { |
| 32 |
cases_.sort(Comparator{}); |
| 33 |
if (!AreCasesDisjoint()) { // C1149 |
| 34 |
ReportConflictingCases(); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
private: |
| 40 |
using Value = evaluate::Scalar<T>; |
| 41 |
|
| 42 |
void AddCase(const parser::CaseConstruct::Case &c) { |
| 43 |
const auto &stmt{std::get<parser::Statement<parser::CaseStmt>>(c.t)}; |
| 44 |
const parser::CaseStmt &caseStmt{stmt.statement}; |
| 45 |
const auto &selector{std::get<parser::CaseSelector>(caseStmt.t)}; |
| 46 |
common::visit( |
| 47 |
common::visitors{ |
| 48 |
[&](const std::list<parser::CaseValueRange> &ranges) { |
| 49 |
for (const auto &range : ranges) { |
| 50 |
auto pair{ComputeBounds(range)}; |
| 51 |
if (pair.first && pair.second && *pair.first > *pair.second) { |
| 52 |
if (context_.ShouldWarn(common::UsageWarning::EmptyCase)) { |
| 53 |
context_.Say(stmt.source, |
| 54 |
"CASE has lower bound greater than upper bound"_warn_en_US); |
| 55 |
} |
| 56 |
} else { |
| 57 |
if constexpr (T::category == TypeCategory::Logical) { // C1148 |
| 58 |
if ((pair.first || pair.second) && |
| 59 |
(!pair.first || !pair.second || |
| 60 |
*pair.first != *pair.second)) { |
| 61 |
context_.Say(stmt.source, |
| 62 |
"CASE range is not allowed for LOGICAL"_err_en_US); |
| 63 |
} |
| 64 |
} |
| 65 |
cases_.emplace_back(stmt); |
| 66 |
cases_.back().lower = std::move(pair.first); |
| 67 |
cases_.back().upper = std::move(pair.second); |
| 68 |
} |
| 69 |
} |
| 70 |
}, |
| 71 |
[&](const parser::Default &) { cases_.emplace_front(stmt); }, |
| 72 |
}, |
| 73 |
selector.u); |
| 74 |
} |
| 75 |
|
| 76 |
std::optional<Value> GetValue(const parser::CaseValue &caseValue) { |
| 77 |
const parser::Expr &expr{caseValue.thing.thing.value()}; |
| 78 |
auto *x{expr.typedExpr.get()}; |
| 79 |
if (x && x->v) { // C1147 |
| 80 |
auto type{x->v->GetType()}; |
| 81 |
if (type && type->category() == caseExprType_.category() && |
| 82 |
(type->category() != TypeCategory::Character || |
| 83 |
type->kind() == caseExprType_.kind())) { |
| 84 |
parser::Messages buffer; // discarded folding messages |
| 85 |
parser::ContextualMessages foldingMessages{expr.source, &buffer}; |
| 86 |
evaluate::FoldingContext foldingContext{ |
| 87 |
context_.foldingContext(), foldingMessages}; |
| 88 |
auto folded{evaluate::Fold(foldingContext, SomeExpr{*x->v})}; |
| 89 |
if (auto converted{evaluate::Fold(foldingContext, |
| 90 |
evaluate::ConvertToType(T::GetType(), SomeExpr{folded}))}) { |
| 91 |
if (auto value{evaluate::GetScalarConstantValue<T>(*converted)}) { |
| 92 |
auto back{evaluate::Fold(foldingContext, |
| 93 |
evaluate::ConvertToType(*type, SomeExpr{*converted}))}; |
| 94 |
if (back == folded) { |
| 95 |
x->v = converted; |
| 96 |
return value; |
| 97 |
} else { |
| 98 |
if (context_.ShouldWarn(common::UsageWarning::CaseOverflow)) { |
| 99 |
context_.Say(expr.source, |
| 100 |
"CASE value (%s) overflows type (%s) of SELECT CASE expression"_warn_en_US, |
| 101 |
folded.AsFortran(), caseExprType_.AsFortran()); |
| 102 |
} |
| 103 |
hasErrors_ = true; |
| 104 |
return std::nullopt; |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
context_.Say(expr.source, |
| 109 |
"CASE value (%s) must be a constant scalar"_err_en_US, |
| 110 |
x->v->AsFortran()); |
| 111 |
} else { |
| 112 |
std::string typeStr{type ? type->AsFortran() : "typeless"s}; |
| 113 |
context_.Say(expr.source, |
| 114 |
"CASE value has type '%s' which is not compatible with the SELECT CASE expression's type '%s'"_err_en_US, |
| 115 |
typeStr, caseExprType_.AsFortran()); |
| 116 |
} |
| 117 |
hasErrors_ = true; |
| 118 |
} |
| 119 |
return std::nullopt; |
| 120 |
} |
| 121 |
|
| 122 |
using PairOfValues = std::pair<std::optional<Value>, std::optional<Value>>; |
| 123 |
PairOfValues ComputeBounds(const parser::CaseValueRange &range) { |
| 124 |
return common::visit( |
| 125 |
common::visitors{ |
| 126 |
[&](const parser::CaseValue &x) { |
| 127 |
auto value{GetValue(x)}; |
| 128 |
return PairOfValues{value, value}; |
| 129 |
}, |
| 130 |
[&](const parser::CaseValueRange::Range &x) { |
| 131 |
std::optional<Value> lo, hi; |
| 132 |
if (x.lower) { |
| 133 |
lo = GetValue(*x.lower); |
| 134 |
} |
| 135 |
if (x.upper) { |
| 136 |
hi = GetValue(*x.upper); |
| 137 |
} |
| 138 |
if ((x.lower && !lo) || (x.upper && !hi)) { |
| 139 |
return PairOfValues{}; // error case |
| 140 |
} |
| 141 |
return PairOfValues{std::move(lo), std::move(hi)}; |
| 142 |
}, |
| 143 |
}, |
| 144 |
range.u); |
| 145 |
} |
| 146 |
|
| 147 |
struct Case { |
| 148 |
explicit Case(const parser::Statement<parser::CaseStmt> &s) : stmt{s} {} |
| 149 |
bool IsDefault() const { return !lower && !upper; } |
| 150 |
std::string AsFortran() const { |
| 151 |
std::string result; |
| 152 |
{ |
| 153 |
llvm::raw_string_ostream bs{result}; |
| 154 |
if (lower) { |
| 155 |
evaluate::Constant<T>{*lower}.AsFortran(bs << '('); |
| 156 |
if (!upper) { |
| 157 |
bs << ':'; |
| 158 |
} else if (*lower != *upper) { |
| 159 |
evaluate::Constant<T>{*upper}.AsFortran(bs << ':'); |
| 160 |
} |
| 161 |
bs << ')'; |
| 162 |
} else if (upper) { |
| 163 |
evaluate::Constant<T>{*upper}.AsFortran(bs << "(:") << ')'; |
| 164 |
} else { |
| 165 |
bs << "DEFAULT"; |
| 166 |
} |
| 167 |
} |
| 168 |
return result; |
| 169 |
} |
| 170 |
|
| 171 |
const parser::Statement<parser::CaseStmt> &stmt; |
| 172 |
std::optional<Value> lower, upper; |
| 173 |
}; |
| 174 |
|
| 175 |
// Defines a comparator for use with std::list<>::sort(). |
| 176 |
// Returns true if and only if the highest value in range x is less |
| 177 |
// than the least value in range y. The DEFAULT case is arbitrarily |
| 178 |
// defined to be less than all others. When two ranges overlap, |
| 179 |
// neither is less than the other. |
| 180 |
struct Comparator { |
| 181 |
bool operator()(const Case &x, const Case &y) const { |
| 182 |
if (x.IsDefault()) { |
| 183 |
return !y.IsDefault(); |
| 184 |
} else { |
| 185 |
return x.upper && y.lower && *x.upper < *y.lower; |
| 186 |
} |
| 187 |
} |
| 188 |
}; |
| 189 |
|
| 190 |
bool AreCasesDisjoint() const { |
| 191 |
auto endIter{cases_.end()}; |
| 192 |
for (auto iter{cases_.begin()}; iter != endIter; ++iter) { |
| 193 |
auto next{iter}; |
| 194 |
if (++next != endIter && !Comparator{}(*iter, *next)) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
} |
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
// This has quadratic time, but only runs in error cases |
| 202 |
void ReportConflictingCases() { |
| 203 |
for (auto iter{cases_.begin()}; iter != cases_.end(); ++iter) { |
| 204 |
parser::Message *msg{nullptr}; |
| 205 |
for (auto p{cases_.begin()}; p != cases_.end(); ++p) { |
| 206 |
if (p->stmt.source.begin() < iter->stmt.source.begin() && |
| 207 |
!Comparator{}(*p, *iter) && !Comparator{}(*iter, *p)) { |
| 208 |
if (!msg) { |
| 209 |
msg = &context_.Say(iter->stmt.source, |
| 210 |
"CASE %s conflicts with previous cases"_err_en_US, |
| 211 |
iter->AsFortran()); |
| 212 |
} |
| 213 |
msg->Attach( |
| 214 |
p->stmt.source, "Conflicting CASE %s"_en_US, p->AsFortran()); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
SemanticsContext &context_; |
| 221 |
const evaluate::DynamicType &caseExprType_; |
| 222 |
std::list<Case> cases_; |
| 223 |
bool hasErrors_{false}; |
| 224 |
}; |
| 225 |
|
| 226 |
template <TypeCategory CAT> struct TypeVisitor { |
| 227 |
using Result = bool; |
| 228 |
using Types = evaluate::CategoryTypes<CAT>; |
| 229 |
template <typename T> Result Test() { |
| 230 |
if (T::kind == exprType.kind()) { |
| 231 |
CaseValues<T>(context, exprType).Check(caseList); |
| 232 |
return true; |
| 233 |
} else { |
| 234 |
return false; |
| 235 |
} |
| 236 |
} |
| 237 |
SemanticsContext &context; |
| 238 |
const evaluate::DynamicType &exprType; |
| 239 |
const std::list<parser::CaseConstruct::Case> &caseList; |
| 240 |
}; |
| 241 |
|
| 242 |
void CaseChecker::Enter(const parser::CaseConstruct &construct) { |
| 243 |
const auto &selectCaseStmt{ |
| 244 |
std::get<parser::Statement<parser::SelectCaseStmt>>(construct.t)}; |
| 245 |
const auto &selectCase{selectCaseStmt.statement}; |
| 246 |
const auto &selectExpr{ |
| 247 |
std::get<parser::Scalar<parser::Expr>>(selectCase.t).thing}; |
| 248 |
const auto *x{GetExpr(context_, selectExpr)}; |
| 249 |
if (!x) { |
| 250 |
return; // expression semantics failed |
| 251 |
} |
| 252 |
if (auto exprType{x->GetType()}) { |
| 253 |
const auto &caseList{ |
| 254 |
std::get<std::list<parser::CaseConstruct::Case>>(construct.t)}; |
| 255 |
switch (exprType->category()) { |
| 256 |
case TypeCategory::Integer: |
| 257 |
common::SearchTypes( |
| 258 |
TypeVisitor<TypeCategory::Integer>{context_, *exprType, caseList}); |
| 259 |
return; |
| 260 |
case TypeCategory::Logical: |
| 261 |
CaseValues<evaluate::Type<TypeCategory::Logical, 1>>{context_, *exprType} |
| 262 |
.Check(caseList); |
| 263 |
return; |
| 264 |
case TypeCategory::Character: |
| 265 |
common::SearchTypes( |
| 266 |
TypeVisitor<TypeCategory::Character>{context_, *exprType, caseList}); |
| 267 |
return; |
| 268 |
default: |
| 269 |
break; |
| 270 |
} |
| 271 |
} |
| 272 |
context_.Say(selectExpr.source, |
| 273 |
"SELECT CASE expression must be integer, logical, or character"_err_en_US); |
| 274 |
} |
| 275 |
} // namespace Fortran::semantics |