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