forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathexpression.cpp
376 lines (327 loc) · 11.9 KB
/
expression.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//===-- lib/Evaluate/expression.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 "flang/Evaluate/expression.h"
#include "int-power.h"
#include "flang/Common/idioms.h"
#include "flang/Evaluate/common.h"
#include "flang/Evaluate/tools.h"
#include "flang/Evaluate/variable.h"
#include "flang/Parser/char-block.h"
#include "flang/Parser/message.h"
#include "flang/Semantics/scope.h"
#include "flang/Semantics/symbol.h"
#include "flang/Semantics/tools.h"
#include "flang/Semantics/type.h"
#include "llvm/Support/raw_ostream.h"
#include <string>
#include <type_traits>
using namespace Fortran::parser::literals;
namespace Fortran::evaluate {
template <int KIND>
std::optional<Expr<SubscriptInteger>>
Expr<Type<TypeCategory::Character, KIND>>::LEN() const {
using T = std::optional<Expr<SubscriptInteger>>;
return common::visit(
common::visitors{
[](const Constant<Result> &c) -> T {
return AsExpr(Constant<SubscriptInteger>{c.LEN()});
},
[](const ArrayConstructor<Result> &a) -> T {
if (const auto *len{a.LEN()}) {
return T{*len};
} else {
return std::nullopt;
}
},
[](const Parentheses<Result> &x) { return x.left().LEN(); },
[](const Convert<Result> &x) {
return common::visit(
[&](const auto &kx) { return kx.LEN(); }, x.left().u);
},
[](const Concat<KIND> &c) -> T {
if (auto llen{c.left().LEN()}) {
if (auto rlen{c.right().LEN()}) {
return *std::move(llen) + *std::move(rlen);
}
}
return std::nullopt;
},
[](const Extremum<Result> &c) -> T {
if (auto llen{c.left().LEN()}) {
if (auto rlen{c.right().LEN()}) {
return Expr<SubscriptInteger>{Extremum<SubscriptInteger>{
Ordering::Greater, *std::move(llen), *std::move(rlen)}};
}
}
return std::nullopt;
},
[](const Designator<Result> &dr) { return dr.LEN(); },
[](const FunctionRef<Result> &fr) { return fr.LEN(); },
[](const SetLength<KIND> &x) -> T { return x.right(); },
},
u);
}
Expr<SomeType>::~Expr() = default;
#if defined(__APPLE__) && defined(__GNUC__)
template <typename A>
typename ExpressionBase<A>::Derived &ExpressionBase<A>::derived() {
return *static_cast<Derived *>(this);
}
template <typename A>
const typename ExpressionBase<A>::Derived &ExpressionBase<A>::derived() const {
return *static_cast<const Derived *>(this);
}
#endif
template <typename A>
std::optional<DynamicType> ExpressionBase<A>::GetType() const {
if constexpr (IsLengthlessIntrinsicType<Result>) {
return Result::GetType();
} else {
return common::visit(
[&](const auto &x) -> std::optional<DynamicType> {
if constexpr (!common::HasMember<decltype(x), TypelessExpression>) {
return x.GetType();
}
return std::nullopt; // w/o "else" to dodge bogus g++ 8.1 warning
},
derived().u);
}
}
template <typename A> int ExpressionBase<A>::Rank() const {
return common::visit(
[](const auto &x) {
if constexpr (common::HasMember<decltype(x), TypelessExpression>) {
return 0;
} else {
return x.Rank();
}
},
derived().u);
}
template <typename A> int ExpressionBase<A>::Corank() const {
return common::visit(
[](const auto &x) {
if constexpr (common::HasMember<decltype(x), TypelessExpression>) {
return 0;
} else {
return x.Corank();
}
},
derived().u);
}
DynamicType Parentheses<SomeDerived>::GetType() const {
return left().GetType().value();
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
template <typename A> LLVM_DUMP_METHOD void ExpressionBase<A>::dump() const {
llvm::errs() << "Expr is <{" << AsFortran() << "}>\n";
}
#endif
// Equality testing
template <typename A> bool Extremum<A>::operator==(const Extremum &that) const {
return ordering == that.ordering && Base::operator==(that);
}
template <int KIND>
bool LogicalOperation<KIND>::operator==(const LogicalOperation &that) const {
return logicalOperator == that.logicalOperator && Base::operator==(that);
}
template <typename A>
bool Relational<A>::operator==(const Relational &that) const {
return opr == that.opr && Base::operator==(that);
}
bool Relational<SomeType>::operator==(const Relational &that) const {
return u == that.u;
}
bool ImpliedDoIndex::operator==(const ImpliedDoIndex &that) const {
return name == that.name;
}
template <typename T>
bool ImpliedDo<T>::operator==(const ImpliedDo<T> &that) const {
return name_ == that.name_ && lower_ == that.lower_ &&
upper_ == that.upper_ && stride_ == that.stride_ &&
values_ == that.values_;
}
template <typename T>
bool ArrayConstructorValue<T>::operator==(
const ArrayConstructorValue<T> &that) const {
return u == that.u;
}
template <typename R>
bool ArrayConstructorValues<R>::operator==(
const ArrayConstructorValues<R> &that) const {
return values_ == that.values_;
}
template <int KIND>
auto ArrayConstructor<Type<TypeCategory::Character, KIND>>::set_LEN(
Expr<SubscriptInteger> &&len) -> ArrayConstructor & {
length_.emplace(std::move(len));
return *this;
}
template <int KIND>
bool ArrayConstructor<Type<TypeCategory::Character, KIND>>::operator==(
const ArrayConstructor &that) const {
return length_ == that.length_ &&
static_cast<const Base &>(*this) == static_cast<const Base &>(that);
}
bool ArrayConstructor<SomeDerived>::operator==(
const ArrayConstructor &that) const {
return result_ == that.result_ &&
static_cast<const Base &>(*this) == static_cast<const Base &>(that);
;
}
StructureConstructor::StructureConstructor(
const semantics::DerivedTypeSpec &spec,
const StructureConstructorValues &values)
: result_{spec}, values_{values} {}
StructureConstructor::StructureConstructor(
const semantics::DerivedTypeSpec &spec, StructureConstructorValues &&values)
: result_{spec}, values_{std::move(values)} {}
bool StructureConstructor::operator==(const StructureConstructor &that) const {
return result_ == that.result_ && values_ == that.values_;
}
template <int KIND>
bool Expr<Type<TypeCategory::Integer, KIND>>::operator==(
const Expr<Type<TypeCategory::Integer, KIND>> &that) const {
return u == that.u;
}
template <int KIND>
bool Expr<Type<TypeCategory::Real, KIND>>::operator==(
const Expr<Type<TypeCategory::Real, KIND>> &that) const {
return u == that.u;
}
template <int KIND>
bool Expr<Type<TypeCategory::Complex, KIND>>::operator==(
const Expr<Type<TypeCategory::Complex, KIND>> &that) const {
return u == that.u;
}
template <int KIND>
bool Expr<Type<TypeCategory::Logical, KIND>>::operator==(
const Expr<Type<TypeCategory::Logical, KIND>> &that) const {
return u == that.u;
}
template <int KIND>
bool Expr<Type<TypeCategory::Character, KIND>>::operator==(
const Expr<Type<TypeCategory::Character, KIND>> &that) const {
return u == that.u;
}
template <int KIND>
bool Expr<Type<TypeCategory::Unsigned, KIND>>::operator==(
const Expr<Type<TypeCategory::Unsigned, KIND>> &that) const {
return u == that.u;
}
template <TypeCategory CAT>
bool Expr<SomeKind<CAT>>::operator==(const Expr<SomeKind<CAT>> &that) const {
return u == that.u;
}
bool Expr<SomeDerived>::operator==(const Expr<SomeDerived> &that) const {
return u == that.u;
}
bool Expr<SomeCharacter>::operator==(const Expr<SomeCharacter> &that) const {
return u == that.u;
}
bool Expr<SomeType>::operator==(const Expr<SomeType> &that) const {
return u == that.u;
}
DynamicType StructureConstructor::GetType() const { return result_.GetType(); }
std::optional<Expr<SomeType>> StructureConstructor::CreateParentComponent(
const Symbol &component) const {
if (const semantics::DerivedTypeSpec *
parentSpec{GetParentTypeSpec(derivedTypeSpec())}) {
StructureConstructor structureConstructor{*parentSpec};
if (const auto *parentDetails{
component.detailsIf<semantics::DerivedTypeDetails>()}) {
auto parentIter{parentDetails->componentNames().begin()};
for (const auto &childIter : values_) {
if (parentIter == parentDetails->componentNames().end()) {
break; // There are more components in the child
}
SymbolRef componentSymbol{childIter.first};
structureConstructor.Add(
*componentSymbol, common::Clone(childIter.second.value()));
++parentIter;
}
Constant<SomeDerived> constResult{std::move(structureConstructor)};
Expr<SomeDerived> result{std::move(constResult)};
return std::optional<Expr<SomeType>>{result};
}
}
return std::nullopt;
}
static const Symbol *GetParentComponentSymbol(const Symbol &symbol) {
if (symbol.test(Symbol::Flag::ParentComp)) {
// we have a created parent component
const auto &compObject{symbol.get<semantics::ObjectEntityDetails>()};
if (const semantics::DeclTypeSpec * compType{compObject.type()}) {
const semantics::DerivedTypeSpec &dtSpec{compType->derivedTypeSpec()};
const semantics::Symbol &compTypeSymbol{dtSpec.typeSymbol()};
return &compTypeSymbol;
}
}
if (symbol.detailsIf<semantics::DerivedTypeDetails>()) {
// we have an implicit parent type component
return &symbol;
}
return nullptr;
}
std::optional<Expr<SomeType>> StructureConstructor::Find(
const Symbol &component) const {
if (auto iter{values_.find(component)}; iter != values_.end()) {
return iter->second.value();
}
// The component wasn't there directly, see if we're looking for the parent
// component of an extended type
if (const Symbol * typeSymbol{GetParentComponentSymbol(component)}) {
return CreateParentComponent(*typeSymbol);
}
// Look for the component in the parent type component. The parent type
// component is always the first one
if (!values_.empty()) {
const Expr<SomeType> *parentExpr{&values_.begin()->second.value()};
if (const Expr<SomeDerived> *derivedExpr{
std::get_if<Expr<SomeDerived>>(&parentExpr->u)}) {
if (const Constant<SomeDerived> *constExpr{
std::get_if<Constant<SomeDerived>>(&derivedExpr->u)}) {
if (std::optional<StructureConstructor> parentComponentValue{
constExpr->GetScalarValue()}) {
// Try to find the component in the parent structure constructor
return parentComponentValue->Find(component);
}
}
}
}
return std::nullopt;
}
StructureConstructor &StructureConstructor::Add(
const Symbol &symbol, Expr<SomeType> &&expr) {
values_.emplace(symbol, std::move(expr));
return *this;
}
GenericExprWrapper::~GenericExprWrapper() {}
void GenericExprWrapper::Deleter(GenericExprWrapper *p) { delete p; }
GenericAssignmentWrapper::~GenericAssignmentWrapper() {}
void GenericAssignmentWrapper::Deleter(GenericAssignmentWrapper *p) {
delete p;
}
template <TypeCategory CAT> int Expr<SomeKind<CAT>>::GetKind() const {
return common::visit(
[](const auto &kx) { return std::decay_t<decltype(kx)>::Result::kind; },
u);
}
int Expr<SomeCharacter>::GetKind() const {
return common::visit(
[](const auto &kx) { return std::decay_t<decltype(kx)>::Result::kind; },
u);
}
std::optional<Expr<SubscriptInteger>> Expr<SomeCharacter>::LEN() const {
return common::visit([](const auto &kx) { return kx.LEN(); }, u);
}
#ifdef _MSC_VER // disable bogus warning about missing definitions
#pragma warning(disable : 4661)
#endif
INSTANTIATE_EXPRESSION_TEMPLATES
} // namespace Fortran::evaluate