/
check-data.cpp
137 lines (128 loc) · 5.22 KB
/
check-data.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//===-- lib/Semantics/check-data.cpp --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "check-data.h"
namespace Fortran::semantics {
template <typename T> void DataChecker::CheckIfConstantSubscript(const T &x) {
evaluate::ExpressionAnalyzer exprAnalyzer{context_};
if (MaybeExpr checked{exprAnalyzer.Analyze(x)}) {
if (!evaluate::IsConstantExpr(*checked)) { // C875,C881
context_.Say(parser::FindSourceLocation(x),
"Data object must have constant bounds"_err_en_US);
}
}
}
void DataChecker::CheckSubscript(const parser::SectionSubscript &subscript) {
std::visit(common::visitors{
[&](const parser::SubscriptTriplet &triplet) {
CheckIfConstantSubscript(std::get<0>(triplet.t));
CheckIfConstantSubscript(std::get<1>(triplet.t));
CheckIfConstantSubscript(std::get<2>(triplet.t));
},
[&](const parser::IntExpr &intExpr) {
CheckIfConstantSubscript(intExpr);
},
},
subscript.u);
}
// Returns false if DataRef has no subscript
bool DataChecker::CheckAllSubscriptsInDataRef(
const parser::DataRef &dataRef, parser::CharBlock source) {
return std::visit(
common::visitors{
[&](const parser::Name &) { return false; },
[&](const common::Indirection<parser::StructureComponent>
&structureComp) {
return CheckAllSubscriptsInDataRef(
structureComp.value().base, source);
},
[&](const common::Indirection<parser::ArrayElement> &arrayElem) {
for (auto &subscript : arrayElem.value().subscripts) {
CheckSubscript(subscript);
}
CheckAllSubscriptsInDataRef(arrayElem.value().base, source);
return true;
},
[&](const common::Indirection<parser::CoindexedNamedObject>
&coindexedObj) { // C874
context_.Say(source,
"Data object must not be a coindexed variable"_err_en_US);
CheckAllSubscriptsInDataRef(coindexedObj.value().base, source);
return true;
},
},
dataRef.u);
}
void DataChecker::Leave(const parser::DataStmtConstant &dataConst) {
if (auto *structure{
std::get_if<parser::StructureConstructor>(&dataConst.u)}) {
for (const auto &component :
std::get<std::list<parser::ComponentSpec>>(structure->t)) {
const parser::Expr &parsedExpr{
std::get<parser::ComponentDataSource>(component.t).v.value()};
if (const auto *expr{GetExpr(parsedExpr)}) {
if (!evaluate::IsConstantExpr(*expr)) { // C884
context_.Say(parsedExpr.source,
"Structure constructor in data value must be a constant expression"_err_en_US);
}
}
}
}
}
// TODO: C876, C877, C879
void DataChecker::Leave(const parser::DataImpliedDo &dataImpliedDo) {
for (const auto &object :
std::get<std::list<parser::DataIDoObject>>(dataImpliedDo.t)) {
if (const auto *designator{parser::Unwrap<parser::Designator>(object)}) {
if (auto *dataRef{std::get_if<parser::DataRef>(&designator->u)}) {
evaluate::ExpressionAnalyzer exprAnalyzer{context_};
if (MaybeExpr checked{exprAnalyzer.Analyze(*dataRef)}) {
if (evaluate::IsConstantExpr(*checked)) { // C878
context_.Say(designator->source,
"Data implied do object must be a variable"_err_en_US);
}
}
if (!CheckAllSubscriptsInDataRef(*dataRef,
designator->source)) { // C880
context_.Say(designator->source,
"Data implied do object must be subscripted"_err_en_US);
}
}
}
}
}
void DataChecker::Leave(const parser::DataStmtObject &dataObject) {
if (std::get_if<common::Indirection<parser::Variable>>(&dataObject.u)) {
if (const auto *designator{
parser::Unwrap<parser::Designator>(dataObject)}) {
if (auto *dataRef{std::get_if<parser::DataRef>(&designator->u)}) {
CheckAllSubscriptsInDataRef(*dataRef, designator->source);
}
} else { // C875
context_.Say(parser::FindSourceLocation(dataObject),
"Data object variable must not be a function reference"_err_en_US);
}
}
}
void DataChecker::Leave(const parser::DataStmtRepeat &dataRepeat) {
if (const auto *designator{parser::Unwrap<parser::Designator>(dataRepeat)}) {
if (auto *dataRef{std::get_if<parser::DataRef>(&designator->u)}) {
evaluate::ExpressionAnalyzer exprAnalyzer{context_};
if (MaybeExpr checked{exprAnalyzer.Analyze(*dataRef)}) {
auto expr{
evaluate::Fold(context_.foldingContext(), std::move(checked))};
if (auto i64{ToInt64(expr)}) {
if (*i64 < 0) { // C882
context_.Say(designator->source,
"Repeat count for data value must not be negative"_err_en_US);
}
}
}
}
}
}
} // namespace Fortran::semantics