CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 1 | //===-- lib/Semantics/pointer-assignment.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 "pointer-assignment.h" |
| 10 | #include "flang/Common/idioms.h" |
| 11 | #include "flang/Common/restorer.h" |
| 12 | #include "flang/Evaluate/characteristics.h" |
| 13 | #include "flang/Evaluate/expression.h" |
| 14 | #include "flang/Evaluate/fold.h" |
| 15 | #include "flang/Evaluate/tools.h" |
| 16 | #include "flang/Parser/message.h" |
| 17 | #include "flang/Parser/parse-tree-visitor.h" |
| 18 | #include "flang/Parser/parse-tree.h" |
| 19 | #include "flang/Semantics/expression.h" |
| 20 | #include "flang/Semantics/symbol.h" |
| 21 | #include "flang/Semantics/tools.h" |
Caroline Concatto | 8670e49 | 2020-02-28 15:11:03 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 23 | #include <optional> |
| 24 | #include <set> |
| 25 | #include <string> |
| 26 | #include <type_traits> |
| 27 | |
| 28 | // Semantic checks for pointer assignment. |
| 29 | |
| 30 | namespace Fortran::semantics { |
| 31 | |
| 32 | using namespace parser::literals; |
| 33 | using evaluate::characteristics::DummyDataObject; |
| 34 | using evaluate::characteristics::FunctionResult; |
| 35 | using evaluate::characteristics::Procedure; |
| 36 | using evaluate::characteristics::TypeAndShape; |
| 37 | using parser::MessageFixedText; |
| 38 | using parser::MessageFormattedText; |
| 39 | |
| 40 | class PointerAssignmentChecker { |
| 41 | public: |
| 42 | PointerAssignmentChecker(evaluate::FoldingContext &context, |
| 43 | parser::CharBlock source, const std::string &description) |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 44 | : context_{context}, source_{source}, description_{description} {} |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 45 | PointerAssignmentChecker(evaluate::FoldingContext &context, const Symbol &lhs) |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 46 | : context_{context}, source_{lhs.name()}, |
| 47 | description_{"pointer '"s + lhs.name().ToString() + '\''}, lhs_{&lhs}, |
| 48 | procedure_{Procedure::Characterize(lhs, context.intrinsics())} { |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 49 | set_lhsType(TypeAndShape::Characterize(lhs, context)); |
| 50 | set_isContiguous(lhs.attrs().test(Attr::CONTIGUOUS)); |
| 51 | set_isVolatile(lhs.attrs().test(Attr::VOLATILE)); |
| 52 | } |
| 53 | PointerAssignmentChecker &set_lhsType(std::optional<TypeAndShape> &&); |
| 54 | PointerAssignmentChecker &set_isContiguous(bool); |
| 55 | PointerAssignmentChecker &set_isVolatile(bool); |
| 56 | PointerAssignmentChecker &set_isBoundsRemapping(bool); |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 57 | bool Check(const SomeExpr &); |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 58 | |
| 59 | private: |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 60 | template <typename T> bool Check(const T &); |
| 61 | template <typename T> bool Check(const evaluate::Expr<T> &); |
| 62 | template <typename T> bool Check(const evaluate::FunctionRef<T> &); |
| 63 | template <typename T> bool Check(const evaluate::Designator<T> &); |
| 64 | bool Check(const evaluate::NullPointer &); |
| 65 | bool Check(const evaluate::ProcedureDesignator &); |
| 66 | bool Check(const evaluate::ProcedureRef &); |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 67 | // Target is a procedure |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 68 | bool Check( |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 69 | parser::CharBlock rhsName, bool isCall, const Procedure * = nullptr); |
| 70 | bool LhsOkForUnlimitedPoly() const; |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 71 | template <typename... A> parser::Message *Say(A &&...); |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 72 | |
| 73 | evaluate::FoldingContext &context_; |
| 74 | const parser::CharBlock source_; |
| 75 | const std::string description_; |
| 76 | const Symbol *lhs_{nullptr}; |
| 77 | std::optional<TypeAndShape> lhsType_; |
| 78 | std::optional<Procedure> procedure_; |
| 79 | bool isContiguous_{false}; |
| 80 | bool isVolatile_{false}; |
| 81 | bool isBoundsRemapping_{false}; |
| 82 | }; |
| 83 | |
| 84 | PointerAssignmentChecker &PointerAssignmentChecker::set_lhsType( |
| 85 | std::optional<TypeAndShape> &&lhsType) { |
| 86 | lhsType_ = std::move(lhsType); |
| 87 | return *this; |
| 88 | } |
| 89 | |
| 90 | PointerAssignmentChecker &PointerAssignmentChecker::set_isContiguous( |
| 91 | bool isContiguous) { |
| 92 | isContiguous_ = isContiguous; |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | PointerAssignmentChecker &PointerAssignmentChecker::set_isVolatile( |
| 97 | bool isVolatile) { |
| 98 | isVolatile_ = isVolatile; |
| 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | PointerAssignmentChecker &PointerAssignmentChecker::set_isBoundsRemapping( |
| 103 | bool isBoundsRemapping) { |
| 104 | isBoundsRemapping_ = isBoundsRemapping; |
| 105 | return *this; |
| 106 | } |
| 107 | |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 108 | template <typename T> bool PointerAssignmentChecker::Check(const T &) { |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 109 | // Catch-all case for really bad target expression |
| 110 | Say("Target associated with %s must be a designator or a call to a" |
| 111 | " pointer-valued function"_err_en_US, |
| 112 | description_); |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 113 | return false; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 114 | } |
| 115 | |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 116 | template <typename T> |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 117 | bool PointerAssignmentChecker::Check(const evaluate::Expr<T> &x) { |
| 118 | return std::visit([&](const auto &x) { return Check(x); }, x.u); |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 119 | } |
| 120 | |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 121 | bool PointerAssignmentChecker::Check(const SomeExpr &rhs) { |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 122 | if (HasVectorSubscript(rhs)) { // C1025 |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 123 | Say("An array section with a vector subscript may not be a pointer target"_err_en_US); |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 124 | return false; |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 125 | } else if (ExtractCoarrayRef(rhs)) { // C1026 |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 126 | Say("A coindexed object may not be a pointer target"_err_en_US); |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 127 | return false; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 128 | } else { |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 129 | return std::visit([&](const auto &x) { return Check(x); }, rhs.u); |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 133 | bool PointerAssignmentChecker::Check(const evaluate::NullPointer &) { |
| 134 | return true; // P => NULL() without MOLD=; always OK |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 135 | } |
| 136 | |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 137 | template <typename T> |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 138 | bool PointerAssignmentChecker::Check(const evaluate::FunctionRef<T> &f) { |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 139 | std::string funcName; |
| 140 | const auto *symbol{f.proc().GetSymbol()}; |
| 141 | if (symbol) { |
| 142 | funcName = symbol->name().ToString(); |
| 143 | } else if (const auto *intrinsic{f.proc().GetSpecificIntrinsic()}) { |
| 144 | funcName = intrinsic->name; |
| 145 | } |
| 146 | auto proc{Procedure::Characterize(f.proc(), context_.intrinsics())}; |
| 147 | if (!proc) { |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 148 | return false; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 149 | } |
| 150 | std::optional<MessageFixedText> msg; |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 151 | const auto &funcResult{proc->functionResult}; // C1025 |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 152 | if (!funcResult) { |
| 153 | msg = "%s is associated with the non-existent result of reference to" |
| 154 | " procedure"_err_en_US; |
| 155 | } else if (procedure_) { |
| 156 | // Shouldn't be here in this function unless lhs is an object pointer. |
| 157 | msg = "Procedure %s is associated with the result of a reference to" |
| 158 | " function '%s' that does not return a procedure pointer"_err_en_US; |
| 159 | } else if (funcResult->IsProcedurePointer()) { |
| 160 | msg = "Object %s is associated with the result of a reference to" |
| 161 | " function '%s' that is a procedure pointer"_err_en_US; |
| 162 | } else if (!funcResult->attrs.test(FunctionResult::Attr::Pointer)) { |
| 163 | msg = "%s is associated with the result of a reference to function '%s'" |
| 164 | " that is a not a pointer"_err_en_US; |
| 165 | } else if (isContiguous_ && |
| 166 | !funcResult->attrs.test(FunctionResult::Attr::Contiguous)) { |
| 167 | msg = "CONTIGUOUS %s is associated with the result of reference to" |
| 168 | " function '%s' that is not contiguous"_err_en_US; |
| 169 | } else if (lhsType_) { |
| 170 | const auto *frTypeAndShape{funcResult->GetTypeAndShape()}; |
| 171 | CHECK(frTypeAndShape); |
| 172 | if (!lhsType_->IsCompatibleWith(context_.messages(), *frTypeAndShape)) { |
| 173 | msg = "%s is associated with the result of a reference to function '%s'" |
| 174 | " whose pointer result has an incompatible type or shape"_err_en_US; |
| 175 | } |
| 176 | } |
| 177 | if (msg) { |
| 178 | auto restorer{common::ScopedSet(lhs_, symbol)}; |
| 179 | Say(*msg, description_, funcName); |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 180 | return false; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 181 | } |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 182 | return true; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 183 | } |
| 184 | |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 185 | template <typename T> |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 186 | bool PointerAssignmentChecker::Check(const evaluate::Designator<T> &d) { |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 187 | const Symbol *last{d.GetLastSymbol()}; |
| 188 | const Symbol *base{d.GetBaseObject().symbol()}; |
| 189 | if (!last || !base) { |
| 190 | // P => "character literal"(1:3) |
| 191 | context_.messages().Say("Pointer target is not a named entity"_err_en_US); |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 192 | return false; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 193 | } |
| 194 | std::optional<std::variant<MessageFixedText, MessageFormattedText>> msg; |
| 195 | if (procedure_) { |
| 196 | // Shouldn't be here in this function unless lhs is an object pointer. |
| 197 | msg = "In assignment to procedure %s, the target is not a procedure or" |
| 198 | " procedure pointer"_err_en_US; |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 199 | } else if (!evaluate::GetLastTarget(GetSymbolVector(d))) { // C1025 |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 200 | msg = "In assignment to object %s, the target '%s' is not an object with" |
| 201 | " POINTER or TARGET attributes"_err_en_US; |
| 202 | } else if (auto rhsType{TypeAndShape::Characterize(d, context_)}) { |
| 203 | if (!lhsType_) { |
| 204 | msg = "%s associated with object '%s' with incompatible type or" |
| 205 | " shape"_err_en_US; |
| 206 | } else if (rhsType->corank() > 0 && |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 207 | (isVolatile_ != last->attrs().test(Attr::VOLATILE))) { // C1020 |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 208 | // TODO: what if A is VOLATILE in A%B%C? need a better test here |
| 209 | if (isVolatile_) { |
| 210 | msg = "Pointer may not be VOLATILE when target is a" |
| 211 | " non-VOLATILE coarray"_err_en_US; |
| 212 | } else { |
| 213 | msg = "Pointer must be VOLATILE when target is a" |
| 214 | " VOLATILE coarray"_err_en_US; |
| 215 | } |
| 216 | } else if (rhsType->type().IsUnlimitedPolymorphic()) { |
| 217 | if (!LhsOkForUnlimitedPoly()) { |
| 218 | msg = "Pointer type must be unlimited polymorphic or non-extensible" |
| 219 | " derived type when target is unlimited polymorphic"_err_en_US; |
| 220 | } |
| 221 | } else { |
| 222 | if (!lhsType_->type().IsTypeCompatibleWith(rhsType->type())) { |
| 223 | msg = MessageFormattedText{ |
| 224 | "Target type %s is not compatible with pointer type %s"_err_en_US, |
| 225 | rhsType->type().AsFortran(), lhsType_->type().AsFortran()}; |
| 226 | |
| 227 | } else if (!isBoundsRemapping_) { |
| 228 | std::size_t lhsRank{lhsType_->shape().size()}; |
| 229 | std::size_t rhsRank{rhsType->shape().size()}; |
| 230 | if (lhsRank != rhsRank) { |
| 231 | msg = MessageFormattedText{ |
| 232 | "Pointer has rank %d but target has rank %d"_err_en_US, lhsRank, |
| 233 | rhsRank}; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | if (msg) { |
| 239 | auto restorer{common::ScopedSet(lhs_, last)}; |
| 240 | if (auto *m{std::get_if<MessageFixedText>(&*msg)}) { |
Caroline Concatto | 8670e49 | 2020-02-28 15:11:03 | [diff] [blame] | 241 | std::string buf; |
| 242 | llvm::raw_string_ostream ss{buf}; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 243 | d.AsFortran(ss); |
| 244 | Say(*m, description_, ss.str()); |
| 245 | } else { |
| 246 | Say(std::get<MessageFormattedText>(*msg)); |
| 247 | } |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 248 | return false; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 249 | } |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 250 | return true; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | // Compare procedure characteristics for equality except that lhs may be |
| 254 | // Pure or Elemental when rhs is not. |
| 255 | static bool CharacteristicsMatch(const Procedure &lhs, const Procedure &rhs) { |
| 256 | using Attr = Procedure::Attr; |
| 257 | auto lhsAttrs{rhs.attrs}; |
| 258 | lhsAttrs.set( |
| 259 | Attr::Pure, lhs.attrs.test(Attr::Pure) | rhs.attrs.test(Attr::Pure)); |
| 260 | lhsAttrs.set(Attr::Elemental, |
| 261 | lhs.attrs.test(Attr::Elemental) | rhs.attrs.test(Attr::Elemental)); |
| 262 | return lhsAttrs == rhs.attrs && lhs.functionResult == rhs.functionResult && |
| 263 | lhs.dummyArguments == rhs.dummyArguments; |
| 264 | } |
| 265 | |
| 266 | // Common handling for procedure pointer right-hand sides |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 267 | bool PointerAssignmentChecker::Check( |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 268 | parser::CharBlock rhsName, bool isCall, const Procedure *rhsProcedure) { |
| 269 | std::optional<MessageFixedText> msg; |
| 270 | if (!procedure_) { |
| 271 | msg = "In assignment to object %s, the target '%s' is a procedure" |
| 272 | " designator"_err_en_US; |
| 273 | } else if (!rhsProcedure) { |
| 274 | msg = "In assignment to procedure %s, the characteristics of the target" |
| 275 | " procedure '%s' could not be determined"_err_en_US; |
| 276 | } else if (CharacteristicsMatch(*procedure_, *rhsProcedure)) { |
| 277 | // OK |
| 278 | } else if (isCall) { |
| 279 | msg = "Procedure %s associated with result of reference to function '%s'" |
| 280 | " that is an incompatible procedure pointer"_err_en_US; |
| 281 | } else if (procedure_->IsPure() && !rhsProcedure->IsPure()) { |
| 282 | msg = "PURE procedure %s may not be associated with non-PURE" |
| 283 | " procedure designator '%s'"_err_en_US; |
| 284 | } else if (procedure_->IsElemental() && !rhsProcedure->IsElemental()) { |
| 285 | msg = "ELEMENTAL procedure %s may not be associated with non-ELEMENTAL" |
| 286 | " procedure designator '%s'"_err_en_US; |
| 287 | } else if (procedure_->IsFunction() && !rhsProcedure->IsFunction()) { |
| 288 | msg = "Function %s may not be associated with subroutine" |
| 289 | " designator '%s'"_err_en_US; |
| 290 | } else if (!procedure_->IsFunction() && rhsProcedure->IsFunction()) { |
| 291 | msg = "Subroutine %s may not be associated with function" |
| 292 | " designator '%s'"_err_en_US; |
| 293 | } else if (procedure_->HasExplicitInterface() && |
| 294 | !rhsProcedure->HasExplicitInterface()) { |
| 295 | msg = "Procedure %s with explicit interface may not be associated with" |
| 296 | " procedure designator '%s' with implicit interface"_err_en_US; |
| 297 | } else if (!procedure_->HasExplicitInterface() && |
| 298 | rhsProcedure->HasExplicitInterface()) { |
| 299 | msg = "Procedure %s with implicit interface may not be associated with" |
| 300 | " procedure designator '%s' with explicit interface"_err_en_US; |
| 301 | } else { |
| 302 | msg = "Procedure %s associated with incompatible procedure" |
| 303 | " designator '%s'"_err_en_US; |
| 304 | } |
| 305 | if (msg) { |
| 306 | Say(std::move(*msg), description_, rhsName); |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 307 | return false; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 308 | } |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 309 | return true; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 310 | } |
| 311 | |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 312 | bool PointerAssignmentChecker::Check(const evaluate::ProcedureDesignator &d) { |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 313 | if (auto chars{Procedure::Characterize(d, context_.intrinsics())}) { |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 314 | return Check(d.GetName(), false, &*chars); |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 315 | } else { |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 316 | return Check(d.GetName(), false); |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 320 | bool PointerAssignmentChecker::Check(const evaluate::ProcedureRef &ref) { |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 321 | const Procedure *procedure{nullptr}; |
| 322 | auto chars{Procedure::Characterize(ref, context_.intrinsics())}; |
| 323 | if (chars) { |
| 324 | procedure = &*chars; |
| 325 | if (chars->functionResult) { |
| 326 | if (const auto *proc{chars->functionResult->IsProcedurePointer()}) { |
| 327 | procedure = proc; |
| 328 | } |
| 329 | } |
| 330 | } |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 331 | return Check(ref.proc().GetName(), true, procedure); |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | // The target can be unlimited polymorphic if the pointer is, or if it is |
| 335 | // a non-extensible derived type. |
| 336 | bool PointerAssignmentChecker::LhsOkForUnlimitedPoly() const { |
| 337 | const auto &type{lhsType_->type()}; |
| 338 | if (type.category() != TypeCategory::Derived || type.IsAssumedType()) { |
| 339 | return false; |
| 340 | } else if (type.IsUnlimitedPolymorphic()) { |
| 341 | return true; |
| 342 | } else { |
| 343 | return !IsExtensibleType(&type.GetDerivedTypeSpec()); |
| 344 | } |
| 345 | } |
| 346 | |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 347 | template <typename... A> |
peter klausler | 0e9e06a | 2020-08-06 23:56:14 | [diff] [blame] | 348 | parser::Message *PointerAssignmentChecker::Say(A &&...x) { |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 349 | auto *msg{context_.messages().Say(std::forward<A>(x)...)}; |
| 350 | if (lhs_) { |
| 351 | return evaluate::AttachDeclaration(msg, *lhs_); |
| 352 | } else if (!source_.empty()) { |
| 353 | msg->Attach(source_, "Declaration of %s"_en_US, description_); |
| 354 | } |
| 355 | return msg; |
| 356 | } |
| 357 | |
| 358 | // Verify that any bounds on the LHS of a pointer assignment are valid. |
| 359 | // Return true if it is a bound-remapping so we can perform further checks. |
| 360 | static bool CheckPointerBounds( |
| 361 | evaluate::FoldingContext &context, const evaluate::Assignment &assignment) { |
| 362 | auto &messages{context.messages()}; |
| 363 | const SomeExpr &lhs{assignment.lhs}; |
| 364 | const SomeExpr &rhs{assignment.rhs}; |
| 365 | bool isBoundsRemapping{false}; |
| 366 | std::size_t numBounds{std::visit( |
| 367 | common::visitors{ |
| 368 | [&](const evaluate::Assignment::BoundsSpec &bounds) { |
| 369 | return bounds.size(); |
| 370 | }, |
| 371 | [&](const evaluate::Assignment::BoundsRemapping &bounds) { |
| 372 | isBoundsRemapping = true; |
| 373 | evaluate::ExtentExpr lhsSizeExpr{1}; |
| 374 | for (const auto &bound : bounds) { |
| 375 | lhsSizeExpr = std::move(lhsSizeExpr) * |
| 376 | (common::Clone(bound.second) - common::Clone(bound.first) + |
| 377 | evaluate::ExtentExpr{1}); |
| 378 | } |
| 379 | if (std::optional<std::int64_t> lhsSize{evaluate::ToInt64( |
| 380 | evaluate::Fold(context, std::move(lhsSizeExpr)))}) { |
| 381 | if (auto shape{evaluate::GetShape(context, rhs)}) { |
| 382 | if (std::optional<std::int64_t> rhsSize{ |
| 383 | evaluate::ToInt64(evaluate::Fold( |
| 384 | context, evaluate::GetSize(std::move(*shape))))}) { |
| 385 | if (*lhsSize > *rhsSize) { |
| 386 | messages.Say( |
| 387 | "Pointer bounds require %d elements but target has" |
| 388 | " only %d"_err_en_US, |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 389 | *lhsSize, *rhsSize); // 10.2.2.3(9) |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | return bounds.size(); |
| 395 | }, |
| 396 | [](const auto &) -> std::size_t { |
| 397 | DIE("not valid for pointer assignment"); |
| 398 | }, |
| 399 | }, |
| 400 | assignment.u)}; |
| 401 | if (numBounds > 0) { |
| 402 | if (lhs.Rank() != static_cast<int>(numBounds)) { |
| 403 | messages.Say("Pointer '%s' has rank %d but the number of bounds specified" |
| 404 | " is %d"_err_en_US, |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 405 | lhs.AsFortran(), lhs.Rank(), numBounds); // C1018 |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | if (isBoundsRemapping && rhs.Rank() != 1 && |
| 409 | !evaluate::IsSimplyContiguous(rhs, context.intrinsics())) { |
| 410 | messages.Say("Pointer bounds remapping target must have rank 1 or be" |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 411 | " simply contiguous"_err_en_US); // 10.2.2.3(9) |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 412 | } |
| 413 | return isBoundsRemapping; |
| 414 | } |
| 415 | |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 416 | bool CheckPointerAssignment( |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 417 | evaluate::FoldingContext &context, const evaluate::Assignment &assignment) { |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 418 | return CheckPointerAssignment(context, assignment.lhs, assignment.rhs, |
| 419 | CheckPointerBounds(context, assignment)); |
| 420 | } |
| 421 | |
| 422 | bool CheckPointerAssignment(evaluate::FoldingContext &context, |
| 423 | const SomeExpr &lhs, const SomeExpr &rhs, bool isBoundsRemapping) { |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 424 | const Symbol *pointer{GetLastSymbol(lhs)}; |
| 425 | if (!pointer) { |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 426 | return false; // error was reported |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 427 | } |
| 428 | if (!IsPointer(*pointer)) { |
| 429 | evaluate::SayWithDeclaration(context.messages(), *pointer, |
| 430 | "'%s' is not a pointer"_err_en_US, pointer->name()); |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 431 | return false; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 432 | } |
| 433 | if (pointer->has<ProcEntityDetails>() && evaluate::ExtractCoarrayRef(lhs)) { |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 434 | context.messages().Say( // C1027 |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 435 | "Procedure pointer may not be a coindexed object"_err_en_US); |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 436 | return false; |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 437 | } |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 438 | return PointerAssignmentChecker{context, *pointer} |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 439 | .set_isBoundsRemapping(isBoundsRemapping) |
| 440 | .Check(rhs); |
| 441 | } |
| 442 | |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 443 | bool CheckPointerAssignment( |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 444 | evaluate::FoldingContext &context, const Symbol &lhs, const SomeExpr &rhs) { |
| 445 | CHECK(IsPointer(lhs)); |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 446 | return PointerAssignmentChecker{context, lhs}.Check(rhs); |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 447 | } |
| 448 | |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 449 | bool CheckPointerAssignment(evaluate::FoldingContext &context, |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 450 | parser::CharBlock source, const std::string &description, |
| 451 | const DummyDataObject &lhs, const SomeExpr &rhs) { |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 452 | return PointerAssignmentChecker{context, source, description} |
CarolineConcatto | 64ab330 | 2020-02-25 15:11:52 | [diff] [blame] | 453 | .set_lhsType(common::Clone(lhs.type)) |
| 454 | .set_isContiguous(lhs.attrs.test(DummyDataObject::Attr::Contiguous)) |
| 455 | .set_isVolatile(lhs.attrs.test(DummyDataObject::Attr::Volatile)) |
| 456 | .Check(rhs); |
| 457 | } |
| 458 | |
peter klausler | 4171f80 | 2020-06-19 00:17:04 | [diff] [blame] | 459 | bool CheckInitialTarget(evaluate::FoldingContext &context, |
| 460 | const SomeExpr &pointer, const SomeExpr &init) { |
| 461 | return evaluate::IsInitialDataTarget(init, &context.messages()) && |
| 462 | CheckPointerAssignment(context, pointer, init); |
| 463 | } |
| 464 | |
Tim Keith | 1f87900 | 2020-03-29 04:00:16 | [diff] [blame] | 465 | } // namespace Fortran::semantics |