blob: 8cf46f5a5cda7de90bf12c6d02e4277626897e1f [file] [log] [blame]
CarolineConcatto64ab3302020-02-25 15:11:521//===-- 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 Concatto8670e492020-02-28 15:11:0322#include "llvm/Support/raw_ostream.h"
CarolineConcatto64ab3302020-02-25 15:11:5223#include <optional>
24#include <set>
25#include <string>
26#include <type_traits>
27
28// Semantic checks for pointer assignment.
29
30namespace Fortran::semantics {
31
32using namespace parser::literals;
33using evaluate::characteristics::DummyDataObject;
34using evaluate::characteristics::FunctionResult;
35using evaluate::characteristics::Procedure;
36using evaluate::characteristics::TypeAndShape;
37using parser::MessageFixedText;
38using parser::MessageFormattedText;
39
40class PointerAssignmentChecker {
41public:
42 PointerAssignmentChecker(evaluate::FoldingContext &context,
43 parser::CharBlock source, const std::string &description)
Tim Keith1f879002020-03-29 04:00:1644 : context_{context}, source_{source}, description_{description} {}
CarolineConcatto64ab3302020-02-25 15:11:5245 PointerAssignmentChecker(evaluate::FoldingContext &context, const Symbol &lhs)
Tim Keith1f879002020-03-29 04:00:1646 : context_{context}, source_{lhs.name()},
47 description_{"pointer '"s + lhs.name().ToString() + '\''}, lhs_{&lhs},
peter klausler641ede92020-12-07 20:08:5848 procedure_{Procedure::Characterize(lhs, context)} {
CarolineConcatto64ab3302020-02-25 15:11:5249 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 klausler4171f802020-06-19 00:17:0457 bool Check(const SomeExpr &);
CarolineConcatto64ab3302020-02-25 15:11:5258
59private:
peter klausler4171f802020-06-19 00:17:0460 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 &);
CarolineConcatto64ab3302020-02-25 15:11:5267 // Target is a procedure
peter klausler4171f802020-06-19 00:17:0468 bool Check(
CarolineConcatto64ab3302020-02-25 15:11:5269 parser::CharBlock rhsName, bool isCall, const Procedure * = nullptr);
70 bool LhsOkForUnlimitedPoly() const;
Tim Keith1f879002020-03-29 04:00:1671 template <typename... A> parser::Message *Say(A &&...);
CarolineConcatto64ab3302020-02-25 15:11:5272
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
84PointerAssignmentChecker &PointerAssignmentChecker::set_lhsType(
85 std::optional<TypeAndShape> &&lhsType) {
86 lhsType_ = std::move(lhsType);
87 return *this;
88}
89
90PointerAssignmentChecker &PointerAssignmentChecker::set_isContiguous(
91 bool isContiguous) {
92 isContiguous_ = isContiguous;
93 return *this;
94}
95
96PointerAssignmentChecker &PointerAssignmentChecker::set_isVolatile(
97 bool isVolatile) {
98 isVolatile_ = isVolatile;
99 return *this;
100}
101
102PointerAssignmentChecker &PointerAssignmentChecker::set_isBoundsRemapping(
103 bool isBoundsRemapping) {
104 isBoundsRemapping_ = isBoundsRemapping;
105 return *this;
106}
107
peter klausler4171f802020-06-19 00:17:04108template <typename T> bool PointerAssignmentChecker::Check(const T &) {
CarolineConcatto64ab3302020-02-25 15:11:52109 // 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 klausler4171f802020-06-19 00:17:04113 return false;
CarolineConcatto64ab3302020-02-25 15:11:52114}
115
Tim Keith1f879002020-03-29 04:00:16116template <typename T>
peter klausler4171f802020-06-19 00:17:04117bool PointerAssignmentChecker::Check(const evaluate::Expr<T> &x) {
118 return std::visit([&](const auto &x) { return Check(x); }, x.u);
CarolineConcatto64ab3302020-02-25 15:11:52119}
120
peter klausler4171f802020-06-19 00:17:04121bool PointerAssignmentChecker::Check(const SomeExpr &rhs) {
Tim Keith1f879002020-03-29 04:00:16122 if (HasVectorSubscript(rhs)) { // C1025
CarolineConcatto64ab3302020-02-25 15:11:52123 Say("An array section with a vector subscript may not be a pointer target"_err_en_US);
peter klausler4171f802020-06-19 00:17:04124 return false;
Tim Keith1f879002020-03-29 04:00:16125 } else if (ExtractCoarrayRef(rhs)) { // C1026
CarolineConcatto64ab3302020-02-25 15:11:52126 Say("A coindexed object may not be a pointer target"_err_en_US);
peter klausler4171f802020-06-19 00:17:04127 return false;
CarolineConcatto64ab3302020-02-25 15:11:52128 } else {
peter klausler4171f802020-06-19 00:17:04129 return std::visit([&](const auto &x) { return Check(x); }, rhs.u);
CarolineConcatto64ab3302020-02-25 15:11:52130 }
131}
132
peter klausler4171f802020-06-19 00:17:04133bool PointerAssignmentChecker::Check(const evaluate::NullPointer &) {
134 return true; // P => NULL() without MOLD=; always OK
CarolineConcatto64ab3302020-02-25 15:11:52135}
136
Tim Keith1f879002020-03-29 04:00:16137template <typename T>
peter klausler4171f802020-06-19 00:17:04138bool PointerAssignmentChecker::Check(const evaluate::FunctionRef<T> &f) {
CarolineConcatto64ab3302020-02-25 15:11:52139 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 }
peter klausler641ede92020-12-07 20:08:58146 auto proc{Procedure::Characterize(f.proc(), context_)};
CarolineConcatto64ab3302020-02-25 15:11:52147 if (!proc) {
peter klausler4171f802020-06-19 00:17:04148 return false;
CarolineConcatto64ab3302020-02-25 15:11:52149 }
150 std::optional<MessageFixedText> msg;
Tim Keith1f879002020-03-29 04:00:16151 const auto &funcResult{proc->functionResult}; // C1025
CarolineConcatto64ab3302020-02-25 15:11:52152 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);
peter klauslerd6a74ec2020-12-15 18:54:36172 if (!lhsType_->IsCompatibleWith(context_.messages(), *frTypeAndShape,
173 "pointer", "function result", false /*elemental*/,
174 true /*left: deferred shape*/, true /*right: deferred shape*/)) {
CarolineConcatto64ab3302020-02-25 15:11:52175 msg = "%s is associated with the result of a reference to function '%s'"
176 " whose pointer result has an incompatible type or shape"_err_en_US;
177 }
178 }
179 if (msg) {
180 auto restorer{common::ScopedSet(lhs_, symbol)};
181 Say(*msg, description_, funcName);
peter klausler4171f802020-06-19 00:17:04182 return false;
CarolineConcatto64ab3302020-02-25 15:11:52183 }
peter klausler4171f802020-06-19 00:17:04184 return true;
CarolineConcatto64ab3302020-02-25 15:11:52185}
186
Tim Keith1f879002020-03-29 04:00:16187template <typename T>
peter klausler4171f802020-06-19 00:17:04188bool PointerAssignmentChecker::Check(const evaluate::Designator<T> &d) {
CarolineConcatto64ab3302020-02-25 15:11:52189 const Symbol *last{d.GetLastSymbol()};
190 const Symbol *base{d.GetBaseObject().symbol()};
191 if (!last || !base) {
192 // P => "character literal"(1:3)
193 context_.messages().Say("Pointer target is not a named entity"_err_en_US);
peter klausler4171f802020-06-19 00:17:04194 return false;
CarolineConcatto64ab3302020-02-25 15:11:52195 }
196 std::optional<std::variant<MessageFixedText, MessageFormattedText>> msg;
197 if (procedure_) {
198 // Shouldn't be here in this function unless lhs is an object pointer.
199 msg = "In assignment to procedure %s, the target is not a procedure or"
200 " procedure pointer"_err_en_US;
Tim Keith1f879002020-03-29 04:00:16201 } else if (!evaluate::GetLastTarget(GetSymbolVector(d))) { // C1025
CarolineConcatto64ab3302020-02-25 15:11:52202 msg = "In assignment to object %s, the target '%s' is not an object with"
203 " POINTER or TARGET attributes"_err_en_US;
204 } else if (auto rhsType{TypeAndShape::Characterize(d, context_)}) {
205 if (!lhsType_) {
206 msg = "%s associated with object '%s' with incompatible type or"
207 " shape"_err_en_US;
208 } else if (rhsType->corank() > 0 &&
Tim Keith1f879002020-03-29 04:00:16209 (isVolatile_ != last->attrs().test(Attr::VOLATILE))) { // C1020
CarolineConcatto64ab3302020-02-25 15:11:52210 // TODO: what if A is VOLATILE in A%B%C? need a better test here
211 if (isVolatile_) {
212 msg = "Pointer may not be VOLATILE when target is a"
213 " non-VOLATILE coarray"_err_en_US;
214 } else {
215 msg = "Pointer must be VOLATILE when target is a"
216 " VOLATILE coarray"_err_en_US;
217 }
218 } else if (rhsType->type().IsUnlimitedPolymorphic()) {
219 if (!LhsOkForUnlimitedPoly()) {
220 msg = "Pointer type must be unlimited polymorphic or non-extensible"
221 " derived type when target is unlimited polymorphic"_err_en_US;
222 }
223 } else {
peter klausler37b2e2b2020-09-30 20:34:23224 if (!lhsType_->type().IsTkCompatibleWith(rhsType->type())) {
CarolineConcatto64ab3302020-02-25 15:11:52225 msg = MessageFormattedText{
226 "Target type %s is not compatible with pointer type %s"_err_en_US,
227 rhsType->type().AsFortran(), lhsType_->type().AsFortran()};
228
229 } else if (!isBoundsRemapping_) {
peter klauslerf862d852020-08-31 18:54:48230 int lhsRank{evaluate::GetRank(lhsType_->shape())};
231 int rhsRank{evaluate::GetRank(rhsType->shape())};
CarolineConcatto64ab3302020-02-25 15:11:52232 if (lhsRank != rhsRank) {
233 msg = MessageFormattedText{
234 "Pointer has rank %d but target has rank %d"_err_en_US, lhsRank,
235 rhsRank};
236 }
237 }
238 }
239 }
240 if (msg) {
241 auto restorer{common::ScopedSet(lhs_, last)};
242 if (auto *m{std::get_if<MessageFixedText>(&*msg)}) {
Caroline Concatto8670e492020-02-28 15:11:03243 std::string buf;
244 llvm::raw_string_ostream ss{buf};
CarolineConcatto64ab3302020-02-25 15:11:52245 d.AsFortran(ss);
246 Say(*m, description_, ss.str());
247 } else {
248 Say(std::get<MessageFormattedText>(*msg));
249 }
peter klausler4171f802020-06-19 00:17:04250 return false;
CarolineConcatto64ab3302020-02-25 15:11:52251 }
peter klausler4171f802020-06-19 00:17:04252 return true;
CarolineConcatto64ab3302020-02-25 15:11:52253}
254
CarolineConcatto64ab3302020-02-25 15:11:52255// Common handling for procedure pointer right-hand sides
peter klausler4171f802020-06-19 00:17:04256bool PointerAssignmentChecker::Check(
CarolineConcatto64ab3302020-02-25 15:11:52257 parser::CharBlock rhsName, bool isCall, const Procedure *rhsProcedure) {
Peter Steinfeldc7574182020-09-25 16:03:17258 if (std::optional<MessageFixedText> msg{
259 evaluate::CheckProcCompatibility(isCall, procedure_, rhsProcedure)}) {
CarolineConcatto64ab3302020-02-25 15:11:52260 Say(std::move(*msg), description_, rhsName);
peter klausler4171f802020-06-19 00:17:04261 return false;
CarolineConcatto64ab3302020-02-25 15:11:52262 }
peter klausler4171f802020-06-19 00:17:04263 return true;
CarolineConcatto64ab3302020-02-25 15:11:52264}
265
peter klausler4171f802020-06-19 00:17:04266bool PointerAssignmentChecker::Check(const evaluate::ProcedureDesignator &d) {
peter klausler641ede92020-12-07 20:08:58267 if (auto chars{Procedure::Characterize(d, context_)}) {
peter klausler4171f802020-06-19 00:17:04268 return Check(d.GetName(), false, &*chars);
CarolineConcatto64ab3302020-02-25 15:11:52269 } else {
peter klausler4171f802020-06-19 00:17:04270 return Check(d.GetName(), false);
CarolineConcatto64ab3302020-02-25 15:11:52271 }
272}
273
peter klausler4171f802020-06-19 00:17:04274bool PointerAssignmentChecker::Check(const evaluate::ProcedureRef &ref) {
CarolineConcatto64ab3302020-02-25 15:11:52275 const Procedure *procedure{nullptr};
peter klausler641ede92020-12-07 20:08:58276 auto chars{Procedure::Characterize(ref, context_)};
CarolineConcatto64ab3302020-02-25 15:11:52277 if (chars) {
278 procedure = &*chars;
279 if (chars->functionResult) {
280 if (const auto *proc{chars->functionResult->IsProcedurePointer()}) {
281 procedure = proc;
282 }
283 }
284 }
peter klausler4171f802020-06-19 00:17:04285 return Check(ref.proc().GetName(), true, procedure);
CarolineConcatto64ab3302020-02-25 15:11:52286}
287
288// The target can be unlimited polymorphic if the pointer is, or if it is
289// a non-extensible derived type.
290bool PointerAssignmentChecker::LhsOkForUnlimitedPoly() const {
291 const auto &type{lhsType_->type()};
292 if (type.category() != TypeCategory::Derived || type.IsAssumedType()) {
293 return false;
294 } else if (type.IsUnlimitedPolymorphic()) {
295 return true;
296 } else {
297 return !IsExtensibleType(&type.GetDerivedTypeSpec());
298 }
299}
300
Tim Keith1f879002020-03-29 04:00:16301template <typename... A>
peter klausler0e9e06a2020-08-06 23:56:14302parser::Message *PointerAssignmentChecker::Say(A &&...x) {
CarolineConcatto64ab3302020-02-25 15:11:52303 auto *msg{context_.messages().Say(std::forward<A>(x)...)};
peter klausler641ede92020-12-07 20:08:58304 if (msg) {
305 if (lhs_) {
306 return evaluate::AttachDeclaration(msg, *lhs_);
307 }
308 if (!source_.empty()) {
309 msg->Attach(source_, "Declaration of %s"_en_US, description_);
310 }
CarolineConcatto64ab3302020-02-25 15:11:52311 }
312 return msg;
313}
314
315// Verify that any bounds on the LHS of a pointer assignment are valid.
316// Return true if it is a bound-remapping so we can perform further checks.
317static bool CheckPointerBounds(
318 evaluate::FoldingContext &context, const evaluate::Assignment &assignment) {
319 auto &messages{context.messages()};
320 const SomeExpr &lhs{assignment.lhs};
321 const SomeExpr &rhs{assignment.rhs};
322 bool isBoundsRemapping{false};
323 std::size_t numBounds{std::visit(
324 common::visitors{
325 [&](const evaluate::Assignment::BoundsSpec &bounds) {
326 return bounds.size();
327 },
328 [&](const evaluate::Assignment::BoundsRemapping &bounds) {
329 isBoundsRemapping = true;
330 evaluate::ExtentExpr lhsSizeExpr{1};
331 for (const auto &bound : bounds) {
332 lhsSizeExpr = std::move(lhsSizeExpr) *
333 (common::Clone(bound.second) - common::Clone(bound.first) +
334 evaluate::ExtentExpr{1});
335 }
336 if (std::optional<std::int64_t> lhsSize{evaluate::ToInt64(
337 evaluate::Fold(context, std::move(lhsSizeExpr)))}) {
338 if (auto shape{evaluate::GetShape(context, rhs)}) {
339 if (std::optional<std::int64_t> rhsSize{
340 evaluate::ToInt64(evaluate::Fold(
341 context, evaluate::GetSize(std::move(*shape))))}) {
342 if (*lhsSize > *rhsSize) {
343 messages.Say(
344 "Pointer bounds require %d elements but target has"
345 " only %d"_err_en_US,
Tim Keith1f879002020-03-29 04:00:16346 *lhsSize, *rhsSize); // 10.2.2.3(9)
CarolineConcatto64ab3302020-02-25 15:11:52347 }
348 }
349 }
350 }
351 return bounds.size();
352 },
353 [](const auto &) -> std::size_t {
354 DIE("not valid for pointer assignment");
355 },
356 },
357 assignment.u)};
358 if (numBounds > 0) {
359 if (lhs.Rank() != static_cast<int>(numBounds)) {
360 messages.Say("Pointer '%s' has rank %d but the number of bounds specified"
361 " is %d"_err_en_US,
Tim Keith1f879002020-03-29 04:00:16362 lhs.AsFortran(), lhs.Rank(), numBounds); // C1018
CarolineConcatto64ab3302020-02-25 15:11:52363 }
364 }
365 if (isBoundsRemapping && rhs.Rank() != 1 &&
peter klausler641ede92020-12-07 20:08:58366 !evaluate::IsSimplyContiguous(rhs, context)) {
CarolineConcatto64ab3302020-02-25 15:11:52367 messages.Say("Pointer bounds remapping target must have rank 1 or be"
Tim Keith1f879002020-03-29 04:00:16368 " simply contiguous"_err_en_US); // 10.2.2.3(9)
CarolineConcatto64ab3302020-02-25 15:11:52369 }
370 return isBoundsRemapping;
371}
372
peter klausler4171f802020-06-19 00:17:04373bool CheckPointerAssignment(
CarolineConcatto64ab3302020-02-25 15:11:52374 evaluate::FoldingContext &context, const evaluate::Assignment &assignment) {
peter klausler4171f802020-06-19 00:17:04375 return CheckPointerAssignment(context, assignment.lhs, assignment.rhs,
376 CheckPointerBounds(context, assignment));
377}
378
379bool CheckPointerAssignment(evaluate::FoldingContext &context,
380 const SomeExpr &lhs, const SomeExpr &rhs, bool isBoundsRemapping) {
CarolineConcatto64ab3302020-02-25 15:11:52381 const Symbol *pointer{GetLastSymbol(lhs)};
382 if (!pointer) {
peter klausler4171f802020-06-19 00:17:04383 return false; // error was reported
CarolineConcatto64ab3302020-02-25 15:11:52384 }
385 if (!IsPointer(*pointer)) {
386 evaluate::SayWithDeclaration(context.messages(), *pointer,
387 "'%s' is not a pointer"_err_en_US, pointer->name());
peter klausler4171f802020-06-19 00:17:04388 return false;
CarolineConcatto64ab3302020-02-25 15:11:52389 }
390 if (pointer->has<ProcEntityDetails>() && evaluate::ExtractCoarrayRef(lhs)) {
Tim Keith1f879002020-03-29 04:00:16391 context.messages().Say( // C1027
CarolineConcatto64ab3302020-02-25 15:11:52392 "Procedure pointer may not be a coindexed object"_err_en_US);
peter klausler4171f802020-06-19 00:17:04393 return false;
CarolineConcatto64ab3302020-02-25 15:11:52394 }
peter klausler4171f802020-06-19 00:17:04395 return PointerAssignmentChecker{context, *pointer}
CarolineConcatto64ab3302020-02-25 15:11:52396 .set_isBoundsRemapping(isBoundsRemapping)
397 .Check(rhs);
398}
399
peter klausler4171f802020-06-19 00:17:04400bool CheckPointerAssignment(
CarolineConcatto64ab3302020-02-25 15:11:52401 evaluate::FoldingContext &context, const Symbol &lhs, const SomeExpr &rhs) {
402 CHECK(IsPointer(lhs));
peter klausler4171f802020-06-19 00:17:04403 return PointerAssignmentChecker{context, lhs}.Check(rhs);
CarolineConcatto64ab3302020-02-25 15:11:52404}
405
peter klausler4171f802020-06-19 00:17:04406bool CheckPointerAssignment(evaluate::FoldingContext &context,
CarolineConcatto64ab3302020-02-25 15:11:52407 parser::CharBlock source, const std::string &description,
408 const DummyDataObject &lhs, const SomeExpr &rhs) {
peter klausler4171f802020-06-19 00:17:04409 return PointerAssignmentChecker{context, source, description}
CarolineConcatto64ab3302020-02-25 15:11:52410 .set_lhsType(common::Clone(lhs.type))
411 .set_isContiguous(lhs.attrs.test(DummyDataObject::Attr::Contiguous))
412 .set_isVolatile(lhs.attrs.test(DummyDataObject::Attr::Volatile))
413 .Check(rhs);
414}
415
peter klausler4171f802020-06-19 00:17:04416bool CheckInitialTarget(evaluate::FoldingContext &context,
417 const SomeExpr &pointer, const SomeExpr &init) {
418 return evaluate::IsInitialDataTarget(init, &context.messages()) &&
419 CheckPointerAssignment(context, pointer, init);
420}
421
Tim Keith1f879002020-03-29 04:00:16422} // namespace Fortran::semantics