1 //===-- lib/Semantics/check-nullify.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-nullify.h"
10 #include "assignment.h"
11 #include "flang/Evaluate/expression.h"
12 #include "flang/Parser/message.h"
13 #include "flang/Parser/parse-tree.h"
14 #include "flang/Semantics/expression.h"
15 #include "flang/Semantics/tools.h"
16 
17 namespace Fortran::semantics {
18 
19 void NullifyChecker::Leave(const parser::NullifyStmt &nullifyStmt) {
20   CHECK(context_.location());
21   const Scope &scope{context_.FindScope(*context_.location())};
22   const Scope *pure{FindPureProcedureContaining(scope)};
23   parser::ContextualMessages messages{
24       *context_.location(), &context_.messages()};
25   for (const parser::PointerObject &pointerObject : nullifyStmt.v) {
26     std::visit(
27         common::visitors{
28             [&](const parser::Name &name) {
29               const Symbol *symbol{name.symbol};
30               if (context_.HasError(symbol)) {
31                 // already reported an error
32               } else if (!IsVariableName(*symbol) && !IsProcName(*symbol)) {
33                 messages.Say(name.source,
34                     "name in NULLIFY statement must be a variable or procedure pointer name"_err_en_US);
35               } else if (!IsPointer(*symbol)) { // C951
36                 messages.Say(name.source,
37                     "name in NULLIFY statement must have the POINTER attribute"_err_en_US);
38               } else if (pure) {
39                 CheckDefinabilityInPureScope(messages, *symbol, scope, *pure);
40               }
41             },
42             [&](const parser::StructureComponent &structureComponent) {
43               if (const auto *checkedExpr{GetExpr(pointerObject)}) {
44                 if (!IsPointer(*structureComponent.component.symbol)) { // C951
45                   messages.Say(structureComponent.component.source,
46                       "component in NULLIFY statement must have the POINTER attribute"_err_en_US);
47                 } else if (pure) {
48                   if (const Symbol * symbol{GetFirstSymbol(*checkedExpr)}) {
49                     CheckDefinabilityInPureScope(
50                         messages, *symbol, scope, *pure);
51                   }
52                 }
53               }
54             },
55         },
56         pointerObject.u);
57   }
58   // From 9.7.3.1(1)
59   //   A pointer-object shall not depend on the value,
60   //   bounds, or association status of another pointer-
61   //   object in the same NULLIFY statement.
62   // This restriction is the programmer's responsibilty.
63   // Some dependencies can be found compile time or at
64   // runtime, but for now we choose to skip such checks.
65 }
66 } // namespace Fortran::semantics
67