1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
//===-- lib/Semantics/check-select-type.cpp -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "check-select-type.h"
#include "flang/Common/idioms.h"
#include "flang/Common/reference.h"
#include "flang/Evaluate/fold.h"
#include "flang/Evaluate/type.h"
#include "flang/Parser/parse-tree.h"
#include "flang/Semantics/semantics.h"
#include "flang/Semantics/tools.h"
#include <optional>
namespace Fortran::semantics {
class TypeCaseValues {
public:
TypeCaseValues(SemanticsContext &c, const evaluate::DynamicType &t)
: context_{c}, selectorType_{t} {}
void Check(const std::list<parser::SelectTypeConstruct::TypeCase> &cases) {
for (const auto &c : cases) {
AddTypeCase(c);
}
if (!hasErrors_) {
ReportConflictingTypeCases();
}
}
private:
void AddTypeCase(const parser::SelectTypeConstruct::TypeCase &c) {
const auto &stmt{std::get<parser::Statement<parser::TypeGuardStmt>>(c.t)};
const parser::TypeGuardStmt &typeGuardStmt{stmt.statement};
const auto &guard{std::get<parser::TypeGuardStmt::Guard>(typeGuardStmt.t)};
if (std::holds_alternative<parser::Default>(guard.u)) {
typeCases_.emplace_back(stmt, std::nullopt);
} else if (std::optional<evaluate::DynamicType> type{GetGuardType(guard)}) {
if (PassesChecksOnGuard(stmt, *type)) {
typeCases_.emplace_back(stmt, *type);
} else {
hasErrors_ = true;
}
} else {
hasErrors_ = true;
}
}
std::optional<evaluate::DynamicType> GetGuardType(
const parser::TypeGuardStmt::Guard &guard) {
return common::visit(
common::visitors{
[](const parser::Default &)
-> std::optional<evaluate::DynamicType> {
return std::nullopt;
},
[](const parser::TypeSpec &typeSpec) {
return evaluate::DynamicType::From(typeSpec.declTypeSpec);
},
[](const parser::DerivedTypeSpec &spec)
-> std::optional<evaluate::DynamicType> {
if (const auto *derivedTypeSpec{spec.derivedTypeSpec}) {
return evaluate::DynamicType(*derivedTypeSpec);
}
return std::nullopt;
},
},
guard.u);
}
bool PassesChecksOnGuard(const parser::Statement<parser::TypeGuardStmt> &stmt,
const evaluate::DynamicType &guardDynamicType) {
const parser::TypeGuardStmt &typeGuardStmt{stmt.statement};
const auto &guard{std::get<parser::TypeGuardStmt::Guard>(typeGuardStmt.t)};
return common::visit(
common::visitors{
[](const parser::Default &) { return true; },
[&](const parser::TypeSpec &typeSpec) {
const DeclTypeSpec *spec{typeSpec.declTypeSpec};
CHECK(spec);
CHECK(spec->AsIntrinsic() || spec->AsDerived());
bool typeSpecRetVal{false};
if (spec->AsIntrinsic()) {
typeSpecRetVal = true;
if (!selectorType_.IsUnlimitedPolymorphic()) { // C1162
context_.Say(stmt.source,
"If selector is not unlimited polymorphic, "
"an intrinsic type specification must not be specified "
"in the type guard statement"_err_en_US);
typeSpecRetVal = false;
}
if (spec->category() == DeclTypeSpec::Character &&
!guardDynamicType.IsAssumedLengthCharacter()) { // C1160
auto location{parser::FindSourceLocation(typeSpec)};
context_.Say(location.empty() ? stmt.source : location,
"The type specification statement must have "
"LEN type parameter as assumed"_err_en_US);
typeSpecRetVal = false;
}
} else {
const DerivedTypeSpec *derived{spec->AsDerived()};
typeSpecRetVal = PassesDerivedTypeChecks(
*derived, parser::FindSourceLocation(typeSpec));
}
return typeSpecRetVal;
},
[&](const parser::DerivedTypeSpec &x) {
CHECK(x.derivedTypeSpec);
const semantics::DerivedTypeSpec *derived{x.derivedTypeSpec};
return PassesDerivedTypeChecks(
*derived, parser::FindSourceLocation(x));
},
},
guard.u);
}
bool PassesDerivedTypeChecks(const semantics::DerivedTypeSpec &derived,
parser::CharBlock sourceLoc) const {
for (const auto &pair : derived.parameters()) {
if (pair.second.isLen() && !pair.second.isAssumed()) { // C1160
context_.Say(sourceLoc,
"The type specification statement must have "
"LEN type parameter as assumed"_err_en_US);
return false;
}
}
if (!IsExtensibleType(&derived)) { // C1161
context_.Say(sourceLoc,
"The type specification statement must not specify "
"a type with a SEQUENCE attribute or a BIND attribute"_err_en_US);
return false;
}
if (!selectorType_.IsUnlimitedPolymorphic()) { // C1162
if (const semantics::Scope * guardScope{derived.typeSymbol().scope()}) {
if (const auto *selDerivedTypeSpec{
evaluate::GetDerivedTypeSpec(selectorType_)}) {
if (!derived.Match(*selDerivedTypeSpec) &&
!guardScope->FindComponent(selDerivedTypeSpec->name())) {
context_.Say(sourceLoc,
"Type specification '%s' must be an extension"
" of TYPE '%s'"_err_en_US,
derived.AsFortran(), selDerivedTypeSpec->AsFortran());
return false;
}
}
}
}
return true;
}
struct TypeCase {
explicit TypeCase(const parser::Statement<parser::TypeGuardStmt> &s,
std::optional<evaluate::DynamicType> guardTypeDynamic)
: stmt{s} {
SetGuardType(guardTypeDynamic);
}
void SetGuardType(std::optional<evaluate::DynamicType> guardTypeDynamic) {
const auto &guard{GetGuardFromStmt(stmt)};
common::visit(common::visitors{
[&](const parser::Default &) {},
[&](const auto &) { guardType_ = *guardTypeDynamic; },
},
guard.u);
}
bool IsDefault() const {
const auto &guard{GetGuardFromStmt(stmt)};
return std::holds_alternative<parser::Default>(guard.u);
}
bool IsTypeSpec() const {
const auto &guard{GetGuardFromStmt(stmt)};
return std::holds_alternative<parser::TypeSpec>(guard.u);
}
bool IsDerivedTypeSpec() const {
const auto &guard{GetGuardFromStmt(stmt)};
return std::holds_alternative<parser::DerivedTypeSpec>(guard.u);
}
const parser::TypeGuardStmt::Guard &GetGuardFromStmt(
const parser::Statement<parser::TypeGuardStmt> &stmt) const {
const parser::TypeGuardStmt &typeGuardStmt{stmt.statement};
return std::get<parser::TypeGuardStmt::Guard>(typeGuardStmt.t);
}
std::optional<evaluate::DynamicType> guardType() const {
return guardType_;
}
std::string AsFortran() const {
std::string result;
if (this->guardType()) {
auto type{*this->guardType()};
result += type.AsFortran();
} else {
result += "DEFAULT";
}
return result;
}
const parser::Statement<parser::TypeGuardStmt> &stmt;
std::optional<evaluate::DynamicType> guardType_; // is this POD?
};
// Returns true if and only if the values are different
// Does apple to apple comparision, in case of TypeSpec or DerivedTypeSpec
// checks for kinds as well.
static bool TypesAreDifferent(const TypeCase &x, const TypeCase &y) {
if (x.IsDefault()) { // C1164
return !y.IsDefault();
} else if (x.IsTypeSpec() && y.IsTypeSpec()) { // C1163
return !AreTypeKindCompatible(x, y);
} else if (x.IsDerivedTypeSpec() && y.IsDerivedTypeSpec()) { // C1163
return !AreTypeKindCompatible(x, y);
}
return true;
}
static bool AreTypeKindCompatible(const TypeCase &x, const TypeCase &y) {
return (*x.guardType()).IsTkCompatibleWith((*y.guardType()));
}
void ReportConflictingTypeCases() {
for (auto iter{typeCases_.begin()}; iter != typeCases_.end(); ++iter) {
parser::Message *msg{nullptr};
for (auto p{typeCases_.begin()}; p != typeCases_.end(); ++p) {
if (p->stmt.source.begin() < iter->stmt.source.begin() &&
!TypesAreDifferent(*p, *iter)) {
if (!msg) {
msg = &context_.Say(iter->stmt.source,
"Type specification '%s' conflicts with "
"previous type specification"_err_en_US,
iter->AsFortran());
}
msg->Attach(p->stmt.source,
"Conflicting type specification '%s'"_en_US, p->AsFortran());
}
}
}
}
SemanticsContext &context_;
const evaluate::DynamicType &selectorType_;
std::list<TypeCase> typeCases_;
bool hasErrors_{false};
};
void SelectTypeChecker::Enter(const parser::SelectTypeConstruct &construct) {
const auto &selectTypeStmt{
std::get<parser::Statement<parser::SelectTypeStmt>>(construct.t)};
const auto &selectType{selectTypeStmt.statement};
const auto &unResolvedSel{std::get<parser::Selector>(selectType.t)};
if (const auto *selector{GetExprFromSelector(unResolvedSel)}) {
if (IsProcedure(*selector)) {
context_.Say(
selectTypeStmt.source, "Selector may not be a procedure"_err_en_US);
} else if (auto exprType{selector->GetType()}) {
const auto &typeCaseList{
std::get<std::list<parser::SelectTypeConstruct::TypeCase>>(
construct.t)};
TypeCaseValues{context_, *exprType}.Check(typeCaseList);
}
}
}
const SomeExpr *SelectTypeChecker::GetExprFromSelector(
const parser::Selector &selector) {
return common::visit([](const auto &x) { return GetExpr(x); }, selector.u);
}
} // namespace Fortran::semantics
|