2018-05-01 21:50:34 +02:00
|
|
|
// Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-02-08 01:27:36 +01:00
|
|
|
#include "parse-tree.h"
|
2018-01-30 20:54:04 +01:00
|
|
|
#include "idioms.h"
|
|
|
|
#include "indirection.h"
|
2018-03-31 01:21:12 +02:00
|
|
|
#include "user-state.h"
|
2018-01-30 20:54:04 +01:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-05-02 22:48:12 +02:00
|
|
|
namespace Fortran::parser {
|
2018-01-30 20:54:04 +01:00
|
|
|
|
|
|
|
// R867
|
|
|
|
ImportStmt::ImportStmt(Kind &&k, std::list<Name> &&n)
|
2018-02-05 23:29:26 +01:00
|
|
|
: kind{k}, names(std::move(n)) {
|
2018-01-30 20:54:04 +01:00
|
|
|
CHECK(kind == Kind::Default || kind == Kind::Only || names.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
// R901 designator
|
|
|
|
bool Designator::EndsInBareName() const {
|
2018-02-05 23:29:26 +01:00
|
|
|
return std::visit(
|
|
|
|
visitors{[](const ObjectName &) { return true; },
|
2018-04-13 23:21:13 +02:00
|
|
|
[](const DataRef &dr) {
|
2018-02-05 23:29:26 +01:00
|
|
|
return std::holds_alternative<Name>(dr.u) ||
|
|
|
|
std::holds_alternative<Indirection<StructureComponent>>(dr.u);
|
|
|
|
},
|
|
|
|
[](const Substring &) { return false; }},
|
|
|
|
u);
|
2018-01-30 20:54:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// R911 data-ref -> part-ref [% part-ref]...
|
2018-04-13 23:21:13 +02:00
|
|
|
DataRef::DataRef(std::list<PartRef> &&prl) : u{std::move(prl.front().name)} {
|
2018-01-30 20:54:04 +01:00
|
|
|
for (bool first{true}; !prl.empty(); first = false, prl.pop_front()) {
|
|
|
|
PartRef &pr{prl.front()};
|
|
|
|
if (!first) {
|
2018-02-05 23:29:26 +01:00
|
|
|
u = Indirection<StructureComponent>{std::move(*this), std::move(pr.name)};
|
2018-01-30 20:54:04 +01:00
|
|
|
}
|
|
|
|
if (!pr.subscripts.empty()) {
|
2018-02-05 23:29:26 +01:00
|
|
|
u = Indirection<ArrayElement>{std::move(*this), std::move(pr.subscripts)};
|
2018-01-30 20:54:04 +01:00
|
|
|
}
|
|
|
|
if (pr.imageSelector.has_value()) {
|
|
|
|
u = Indirection<CoindexedNamedObject>{
|
2018-02-05 23:29:26 +01:00
|
|
|
std::move(*this), std::move(*pr.imageSelector)};
|
2018-01-30 20:54:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// R1001 - R1022 expression
|
|
|
|
Expr::Expr(Designator &&x) : u{Indirection<Designator>(std::move(x))} {}
|
|
|
|
Expr::Expr(FunctionReference &&x)
|
|
|
|
: u{Indirection<FunctionReference>(std::move(x))} {}
|
2018-04-19 16:07:47 +02:00
|
|
|
|
2018-04-23 21:33:10 +02:00
|
|
|
static Designator MakeArrayElementRef(Name &name, std::list<Expr> &subscripts) {
|
|
|
|
ArrayElement arrayElement{name, std::list<SectionSubscript>{}};
|
|
|
|
for (Expr &expr : subscripts) {
|
|
|
|
arrayElement.subscripts.push_back(
|
|
|
|
SectionSubscript{Scalar{Integer{Indirection{std::move(expr)}}}});
|
|
|
|
}
|
|
|
|
return Designator{DataRef{Indirection{std::move(arrayElement)}}};
|
|
|
|
}
|
|
|
|
|
|
|
|
Designator FunctionReference::ConvertToArrayElementRef() {
|
|
|
|
auto &name = std::get<parser::Name>(std::get<ProcedureDesignator>(v.t).u);
|
|
|
|
std::list<Expr> args;
|
|
|
|
for (auto &arg : std::get<std::list<ActualArgSpec>>(v.t)) {
|
|
|
|
std::visit(
|
|
|
|
visitors{
|
|
|
|
[&](Indirection<Expr> &y) { args.push_back(std::move(*y)); },
|
|
|
|
[&](Indirection<Variable> &y) {
|
|
|
|
args.push_back(std::visit(
|
|
|
|
visitors{
|
|
|
|
[&](Indirection<Designator> &z) {
|
|
|
|
return Expr{std::move(*z)};
|
|
|
|
},
|
|
|
|
[&](Indirection<FunctionReference> &z) {
|
|
|
|
return Expr{std::move(*z)};
|
|
|
|
},
|
|
|
|
},
|
|
|
|
y->u));
|
|
|
|
},
|
|
|
|
[&](auto &) { CHECK(!"unexpected kind of ActualArg"); },
|
|
|
|
},
|
|
|
|
std::get<ActualArg>(arg.t).u);
|
|
|
|
}
|
|
|
|
return MakeArrayElementRef(name, args);
|
|
|
|
}
|
|
|
|
|
2018-04-19 16:07:47 +02:00
|
|
|
// R1544 stmt-function-stmt
|
|
|
|
// Convert this stmt-function-stmt to an array element assignment statement.
|
|
|
|
Statement<ActionStmt> StmtFunctionStmt::ConvertToAssignment() {
|
|
|
|
auto &funcName = std::get<Name>(t);
|
|
|
|
auto &funcArgs = std::get<std::list<Name>>(t);
|
|
|
|
auto &funcExpr = std::get<Scalar<Expr>>(t).thing;
|
2018-04-23 21:33:10 +02:00
|
|
|
std::list<Expr> subscripts;
|
2018-04-19 16:07:47 +02:00
|
|
|
for (Name &arg : funcArgs) {
|
2018-04-23 21:33:10 +02:00
|
|
|
subscripts.push_back(Expr{Indirection{Designator{arg}}});
|
2018-04-19 16:07:47 +02:00
|
|
|
}
|
2018-04-23 21:33:10 +02:00
|
|
|
auto &&variable =
|
|
|
|
Variable{Indirection{MakeArrayElementRef(funcName, subscripts)}};
|
2018-04-19 16:07:47 +02:00
|
|
|
return Statement{std::nullopt,
|
|
|
|
ActionStmt{Indirection{
|
|
|
|
AssignmentStmt{std::move(variable), std::move(funcExpr)}}}};
|
|
|
|
}
|
|
|
|
|
2018-06-12 00:40:31 +02:00
|
|
|
std::ostream &operator<<(std::ostream &os, const Name &x) {
|
|
|
|
return os << x.ToString();
|
|
|
|
}
|
|
|
|
std::ostream &operator<<(std::ostream &os, const CharBlock &x) {
|
|
|
|
return os << x.ToString();
|
|
|
|
}
|
|
|
|
|
2018-05-02 22:48:12 +02:00
|
|
|
} // namespace Fortran::parser
|