1 //===-- lib/Semantics/check-data.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 // DATA statement semantic analysis. 10 // - Applies static semantic checks to the variables in each data-stmt-set with 11 // class DataVarChecker; 12 // - Invokes conversion of DATA statement values to static initializers 13 14 #include "check-data.h" 15 #include "data-to-inits.h" 16 #include "flang/Evaluate/traverse.h" 17 #include "flang/Parser/parse-tree.h" 18 #include "flang/Parser/tools.h" 19 #include "flang/Semantics/tools.h" 20 #include <algorithm> 21 #include <vector> 22 23 namespace Fortran::semantics { 24 25 // Ensures that references to an implied DO loop control variable are 26 // represented as such in the "body" of the implied DO loop. 27 void DataChecker::Enter(const parser::DataImpliedDo &x) { 28 auto name{std::get<parser::DataImpliedDo::Bounds>(x.t).name.thing.thing}; 29 int kind{evaluate::ResultType<evaluate::ImpliedDoIndex>::kind}; 30 if (const auto dynamicType{evaluate::DynamicType::From(*name.symbol)}) { 31 if (dynamicType->category() == TypeCategory::Integer) { 32 kind = dynamicType->kind(); 33 } 34 } 35 exprAnalyzer_.AddImpliedDo(name.source, kind); 36 } 37 38 void DataChecker::Leave(const parser::DataImpliedDo &x) { 39 auto name{std::get<parser::DataImpliedDo::Bounds>(x.t).name.thing.thing}; 40 exprAnalyzer_.RemoveImpliedDo(name.source); 41 } 42 43 // DataVarChecker applies static checks once to each variable that appears 44 // in a data-stmt-set. These checks are independent of the values that 45 // correspond to the variables. 46 class DataVarChecker : public evaluate::AllTraverse<DataVarChecker, true> { 47 public: 48 using Base = evaluate::AllTraverse<DataVarChecker, true>; 49 DataVarChecker(SemanticsContext &c, parser::CharBlock src) 50 : Base{*this}, context_{c}, source_{src} {} 51 using Base::operator(); 52 bool HasComponentWithoutSubscripts() const { 53 return hasComponent_ && !hasSubscript_; 54 } 55 bool operator()(const Symbol &symbol) { // C876 56 // 8.6.7p(2) - precludes non-pointers of derived types with 57 // default component values 58 const Scope &scope{context_.FindScope(source_)}; 59 bool isFirstSymbol{isFirstSymbol_}; 60 isFirstSymbol_ = false; 61 if (const char *whyNot{IsAutomatic(symbol) ? "Automatic variable" 62 : IsDummy(symbol) ? "Dummy argument" 63 : IsFunctionResult(symbol) ? "Function result" 64 : IsAllocatable(symbol) ? "Allocatable" 65 : IsInitialized(symbol, true) ? "Default-initialized" 66 : IsInBlankCommon(symbol) ? "Blank COMMON object" 67 : IsProcedure(symbol) && !IsPointer(symbol) ? "Procedure" 68 // remaining checks don't apply to components 69 : !isFirstSymbol ? nullptr 70 : IsHostAssociated(symbol, scope) ? "Host-associated object" 71 : IsUseAssociated(symbol, scope) ? "USE-associated object" 72 : symbol.has<AssocEntityDetails>() ? "Construct association" 73 : IsPointer(symbol) && (hasComponent_ || hasSubscript_) 74 ? "Target of pointer" 75 : nullptr}) { 76 context_.Say(source_, 77 "%s '%s' must not be initialized in a DATA statement"_err_en_US, 78 whyNot, symbol.name()); 79 return false; 80 } else if (IsProcedurePointer(symbol)) { 81 context_.Say(source_, 82 "Procedure pointer '%s' in a DATA statement is not standard"_en_US, 83 symbol.name()); 84 } 85 return true; 86 } 87 bool operator()(const evaluate::Component &component) { 88 hasComponent_ = true; 89 const Symbol &lastSymbol{component.GetLastSymbol()}; 90 if (isPointerAllowed_) { 91 if (IsPointer(lastSymbol) && hasSubscript_) { // C877 92 context_.Say(source_, 93 "Rightmost data object pointer '%s' must not be subscripted"_err_en_US, 94 lastSymbol.name().ToString()); 95 return false; 96 } 97 RestrictPointer(); 98 } else { 99 if (IsPointer(lastSymbol)) { // C877 100 context_.Say(source_, 101 "Data object must not contain pointer '%s' as a non-rightmost part"_err_en_US, 102 lastSymbol.name().ToString()); 103 return false; 104 } 105 } 106 return (*this)(component.base()) && (*this)(lastSymbol); 107 } 108 bool operator()(const evaluate::ArrayRef &arrayRef) { 109 hasSubscript_ = true; 110 return (*this)(arrayRef.base()) && (*this)(arrayRef.subscript()); 111 } 112 bool operator()(const evaluate::Substring &substring) { 113 hasSubscript_ = true; 114 return (*this)(substring.parent()) && (*this)(substring.lower()) && 115 (*this)(substring.upper()); 116 } 117 bool operator()(const evaluate::CoarrayRef &) { // C874 118 context_.Say( 119 source_, "Data object must not be a coindexed variable"_err_en_US); 120 return false; 121 } 122 bool operator()(const evaluate::Subscript &subs) { 123 DataVarChecker subscriptChecker{context_, source_}; 124 subscriptChecker.RestrictPointer(); 125 return std::visit( 126 common::visitors{ 127 [&](const evaluate::IndirectSubscriptIntegerExpr &expr) { 128 return CheckSubscriptExpr(expr); 129 }, 130 [&](const evaluate::Triplet &triplet) { 131 return CheckSubscriptExpr(triplet.lower()) && 132 CheckSubscriptExpr(triplet.upper()) && 133 CheckSubscriptExpr(triplet.stride()); 134 }, 135 }, 136 subs.u) && 137 subscriptChecker(subs.u); 138 } 139 template <typename T> 140 bool operator()(const evaluate::FunctionRef<T> &) const { // C875 141 context_.Say(source_, 142 "Data object variable must not be a function reference"_err_en_US); 143 return false; 144 } 145 void RestrictPointer() { isPointerAllowed_ = false; } 146 147 private: 148 bool CheckSubscriptExpr( 149 const std::optional<evaluate::IndirectSubscriptIntegerExpr> &x) const { 150 return !x || CheckSubscriptExpr(*x); 151 } 152 bool CheckSubscriptExpr( 153 const evaluate::IndirectSubscriptIntegerExpr &expr) const { 154 return CheckSubscriptExpr(expr.value()); 155 } 156 bool CheckSubscriptExpr( 157 const evaluate::Expr<evaluate::SubscriptInteger> &expr) const { 158 if (!evaluate::IsConstantExpr(expr)) { // C875,C881 159 context_.Say( 160 source_, "Data object must have constant subscripts"_err_en_US); 161 return false; 162 } else { 163 return true; 164 } 165 } 166 167 SemanticsContext &context_; 168 parser::CharBlock source_; 169 bool hasComponent_{false}; 170 bool hasSubscript_{false}; 171 bool isPointerAllowed_{true}; 172 bool isFirstSymbol_{true}; 173 }; 174 175 void DataChecker::Leave(const parser::DataIDoObject &object) { 176 if (const auto *designator{ 177 std::get_if<parser::Scalar<common::Indirection<parser::Designator>>>( 178 &object.u)}) { 179 if (MaybeExpr expr{exprAnalyzer_.Analyze(*designator)}) { 180 auto source{designator->thing.value().source}; 181 if (evaluate::IsConstantExpr(*expr)) { // C878,C879 182 exprAnalyzer_.context().Say( 183 source, "Data implied do object must be a variable"_err_en_US); 184 } else { 185 DataVarChecker checker{exprAnalyzer_.context(), source}; 186 if (checker(*expr)) { 187 if (checker.HasComponentWithoutSubscripts()) { // C880 188 exprAnalyzer_.context().Say(source, 189 "Data implied do structure component must be subscripted"_err_en_US); 190 } else { 191 return; 192 } 193 } 194 } 195 } 196 currentSetHasFatalErrors_ = true; 197 } 198 } 199 200 void DataChecker::Leave(const parser::DataStmtObject &dataObject) { 201 std::visit(common::visitors{ 202 [](const parser::DataImpliedDo &) { // has own Enter()/Leave() 203 }, 204 [&](const auto &var) { 205 auto expr{exprAnalyzer_.Analyze(var)}; 206 if (!expr || 207 !DataVarChecker{exprAnalyzer_.context(), 208 parser::FindSourceLocation(dataObject)}(*expr)) { 209 currentSetHasFatalErrors_ = true; 210 } 211 }, 212 }, 213 dataObject.u); 214 } 215 216 void DataChecker::Leave(const parser::DataStmtSet &set) { 217 if (!currentSetHasFatalErrors_) { 218 AccumulateDataInitializations(inits_, exprAnalyzer_, set); 219 } 220 currentSetHasFatalErrors_ = false; 221 } 222 223 void DataChecker::CompileDataInitializationsIntoInitializers() { 224 ConvertToInitializers(inits_, exprAnalyzer_); 225 } 226 227 } // namespace Fortran::semantics 228