2018-06-27 00:18:53 +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.
|
|
|
|
|
|
|
|
#ifndef FORTRAN_EVALUATE_VARIABLE_H_
|
|
|
|
#define FORTRAN_EVALUATE_VARIABLE_H_
|
|
|
|
|
2018-07-09 21:46:51 +02:00
|
|
|
// Defines data structures to represent data access and function calls
|
|
|
|
// for use in expressions and assignment statements. Both copy and move
|
|
|
|
// semantics are supported. The representation adheres closely to the
|
|
|
|
// Fortran 2018 language standard (q.v.) and uses strong typing to ensure
|
|
|
|
// that only admissable combinations can be constructed.
|
|
|
|
|
2018-07-07 00:12:33 +02:00
|
|
|
#include "common.h"
|
|
|
|
#include "expression-forward.h"
|
2018-07-11 23:50:08 +02:00
|
|
|
#include "intrinsics.h"
|
2018-07-07 00:12:33 +02:00
|
|
|
#include "../common/idioms.h"
|
|
|
|
#include "../semantics/symbol.h"
|
2018-06-27 00:18:53 +02:00
|
|
|
#include <optional>
|
2018-07-07 00:12:33 +02:00
|
|
|
#include <ostream>
|
2018-06-27 00:18:53 +02:00
|
|
|
#include <variant>
|
2018-07-04 00:14:48 +02:00
|
|
|
#include <vector>
|
2018-06-27 00:18:53 +02:00
|
|
|
|
|
|
|
namespace Fortran::evaluate {
|
|
|
|
|
2018-07-04 00:14:48 +02:00
|
|
|
using semantics::Symbol;
|
2018-06-27 00:18:53 +02:00
|
|
|
|
2018-07-09 21:46:51 +02:00
|
|
|
// Forward declarations
|
2018-07-13 01:58:21 +02:00
|
|
|
class DataRef;
|
|
|
|
class Variable;
|
|
|
|
class ActualFunctionArg;
|
2018-07-09 21:46:51 +02:00
|
|
|
|
|
|
|
// Subscript and cosubscript expressions are of a kind that matches the
|
|
|
|
// address size, at least at the top level.
|
2018-07-11 23:50:08 +02:00
|
|
|
using SubscriptIntegerExpr = IntegerExpr<SubscriptInteger::kind>;
|
|
|
|
using IndirectSubscriptIntegerExpr = CopyableIndirection<SubscriptIntegerExpr>;
|
2018-07-09 21:46:51 +02:00
|
|
|
|
|
|
|
// R913 structure-component & C920: Defined to be a multi-part
|
|
|
|
// data-ref whose last part has no subscripts (or image-selector, although
|
|
|
|
// that isn't explicit in the document). Pointer and allocatable components
|
|
|
|
// are not explicitly indirected in this representation.
|
|
|
|
// Complex components (%RE, %IM) are isolated below in ComplexPart.
|
2018-07-12 18:40:18 +02:00
|
|
|
class Component {
|
|
|
|
public:
|
2018-07-07 00:12:33 +02:00
|
|
|
CLASS_BOILERPLATE(Component)
|
2018-07-12 18:40:18 +02:00
|
|
|
Component(const DataRef &b, const Symbol &c) : base_{b}, symbol_{&c} {}
|
|
|
|
Component(DataRef &&b, const Symbol &c) : base_{std::move(b)}, symbol_{&c} {}
|
2018-07-10 01:23:12 +02:00
|
|
|
Component(CopyableIndirection<DataRef> &&b, const Symbol &c)
|
2018-07-12 18:40:18 +02:00
|
|
|
: base_{std::move(b)}, symbol_{&c} {}
|
|
|
|
const DataRef &base() const { return *base_; }
|
|
|
|
DataRef &base() { return *base_; }
|
|
|
|
const Symbol &symbol() const { return *symbol_; }
|
2018-07-11 23:50:08 +02:00
|
|
|
SubscriptIntegerExpr LEN() const;
|
2018-07-13 01:58:21 +02:00
|
|
|
|
2018-07-12 18:40:18 +02:00
|
|
|
private:
|
|
|
|
CopyableIndirection<DataRef> base_;
|
|
|
|
const Symbol *symbol_;
|
2018-06-27 00:18:53 +02:00
|
|
|
};
|
|
|
|
|
2018-07-09 21:46:51 +02:00
|
|
|
// R921 subscript-triplet
|
2018-07-13 01:07:43 +02:00
|
|
|
class Triplet {
|
|
|
|
public:
|
2018-07-07 00:12:33 +02:00
|
|
|
CLASS_BOILERPLATE(Triplet)
|
2018-07-09 21:46:51 +02:00
|
|
|
Triplet(std::optional<SubscriptIntegerExpr> &&,
|
|
|
|
std::optional<SubscriptIntegerExpr> &&,
|
|
|
|
std::optional<SubscriptIntegerExpr> &&);
|
2018-07-13 01:07:43 +02:00
|
|
|
std::optional<SubscriptIntegerExpr> lower() const;
|
|
|
|
std::optional<SubscriptIntegerExpr> upper() const;
|
|
|
|
std::optional<SubscriptIntegerExpr> stride() const;
|
2018-07-13 01:58:21 +02:00
|
|
|
|
2018-07-13 01:07:43 +02:00
|
|
|
private:
|
|
|
|
std::optional<IndirectSubscriptIntegerExpr> lower_, upper_, stride_;
|
2018-06-27 00:18:53 +02:00
|
|
|
};
|
|
|
|
|
2018-07-09 21:46:51 +02:00
|
|
|
// R919 subscript when rank 0, R923 vector-subscript when rank 1
|
2018-07-13 01:07:43 +02:00
|
|
|
class Subscript {
|
|
|
|
public:
|
2018-07-07 00:12:33 +02:00
|
|
|
CLASS_BOILERPLATE(Subscript)
|
2018-07-11 23:50:08 +02:00
|
|
|
explicit Subscript(const SubscriptIntegerExpr &s)
|
2018-07-13 01:07:43 +02:00
|
|
|
: u_{IndirectSubscriptIntegerExpr::Make(s)} {}
|
2018-07-11 23:50:08 +02:00
|
|
|
explicit Subscript(SubscriptIntegerExpr &&s)
|
2018-07-13 01:07:43 +02:00
|
|
|
: u_{IndirectSubscriptIntegerExpr::Make(std::move(s))} {}
|
|
|
|
explicit Subscript(const Triplet &t) : u_{t} {}
|
|
|
|
explicit Subscript(Triplet &&t) : u_{std::move(t)} {}
|
2018-07-13 01:58:21 +02:00
|
|
|
|
2018-07-13 01:07:43 +02:00
|
|
|
private:
|
|
|
|
std::variant<IndirectSubscriptIntegerExpr, Triplet> u_;
|
2018-06-27 00:18:53 +02:00
|
|
|
};
|
|
|
|
|
2018-07-09 21:46:51 +02:00
|
|
|
// R917 array-element, R918 array-section; however, the case of an
|
|
|
|
// array-section that is a complex-part-designator is represented here
|
|
|
|
// as a ComplexPart instead. C919 & C925 require that at most one set of
|
|
|
|
// subscripts have rank greater than 0, but that is not explicit in
|
|
|
|
// these types.
|
2018-07-13 01:07:43 +02:00
|
|
|
class ArrayRef {
|
|
|
|
public:
|
2018-07-07 00:12:33 +02:00
|
|
|
CLASS_BOILERPLATE(ArrayRef)
|
|
|
|
ArrayRef(const Symbol &n, std::vector<Subscript> &&ss)
|
2018-07-13 01:07:43 +02:00
|
|
|
: u_{&n}, subscript_(std::move(ss)) {}
|
2018-07-07 00:12:33 +02:00
|
|
|
ArrayRef(Component &&c, std::vector<Subscript> &&ss)
|
2018-07-13 01:07:43 +02:00
|
|
|
: u_{std::move(c)}, subscript_(std::move(ss)) {}
|
2018-07-11 23:50:08 +02:00
|
|
|
SubscriptIntegerExpr LEN() const;
|
2018-07-13 01:58:21 +02:00
|
|
|
|
2018-07-13 01:07:43 +02:00
|
|
|
private:
|
|
|
|
std::variant<const Symbol *, Component> u_;
|
|
|
|
std::vector<Subscript> subscript_;
|
2018-06-27 00:18:53 +02:00
|
|
|
};
|
|
|
|
|
2018-07-09 21:46:51 +02:00
|
|
|
// R914 coindexed-named-object
|
|
|
|
// R924 image-selector, R926 image-selector-spec.
|
|
|
|
// C824 severely limits the usage of derived types with coarray ultimate
|
|
|
|
// components: they can't be pointers, allocatables, arrays, coarrays, or
|
|
|
|
// function results. They can be components of other derived types.
|
|
|
|
// C930 precludes having both TEAM= and TEAM_NUMBER=.
|
|
|
|
// TODO C931 prohibits the use of a coindexed object as a stat-variable.
|
2018-07-13 01:07:43 +02:00
|
|
|
class CoarrayRef {
|
|
|
|
public:
|
2018-07-07 00:12:33 +02:00
|
|
|
CLASS_BOILERPLATE(CoarrayRef)
|
2018-07-11 23:50:08 +02:00
|
|
|
CoarrayRef(std::vector<const Symbol *> &&,
|
|
|
|
std::vector<SubscriptIntegerExpr> &&,
|
|
|
|
std::vector<SubscriptIntegerExpr> &&); // TODO: stat & team?
|
2018-07-13 01:07:43 +02:00
|
|
|
CoarrayRef &setStat(Variable &&);
|
|
|
|
CoarrayRef &setTeam(Variable &&, bool isTeamNumber = false);
|
2018-07-11 23:50:08 +02:00
|
|
|
SubscriptIntegerExpr LEN() const;
|
2018-07-13 01:58:21 +02:00
|
|
|
|
2018-07-13 01:07:43 +02:00
|
|
|
private:
|
|
|
|
std::vector<const Symbol *> base_;
|
|
|
|
std::vector<SubscriptIntegerExpr> subscript_, cosubscript_;
|
|
|
|
std::optional<CopyableIndirection<Variable>> stat_, team_;
|
|
|
|
bool teamIsTeamNumber_{false}; // false: TEAM=, true: TEAM_NUMBER=
|
2018-07-04 00:14:48 +02:00
|
|
|
};
|
|
|
|
|
2018-07-09 21:46:51 +02:00
|
|
|
// R911 data-ref is defined syntactically as a series of part-refs, which
|
2018-07-11 23:50:08 +02:00
|
|
|
// would be far too expressive if the constraints were ignored. Here, the
|
|
|
|
// possible outcomes are spelled out. Note that a data-ref cannot include
|
|
|
|
// a terminal substring range or complex component designator; use
|
|
|
|
// R901 designator for that.
|
2018-07-13 01:58:21 +02:00
|
|
|
class DataRef {
|
|
|
|
public:
|
2018-07-07 00:12:33 +02:00
|
|
|
CLASS_BOILERPLATE(DataRef)
|
2018-07-13 01:58:21 +02:00
|
|
|
explicit DataRef(const Symbol &n) : u_{&n} {}
|
|
|
|
explicit DataRef(Component &&c) : u_{std::move(c)} {}
|
|
|
|
explicit DataRef(ArrayRef &&a) : u_{std::move(a)} {}
|
|
|
|
explicit DataRef(CoarrayRef &&a) : u_{std::move(a)} {}
|
2018-07-11 23:50:08 +02:00
|
|
|
SubscriptIntegerExpr LEN() const;
|
2018-07-13 01:58:21 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::variant<const Symbol *, Component, ArrayRef, CoarrayRef> u_;
|
2018-07-04 00:14:48 +02:00
|
|
|
};
|
|
|
|
|
2018-07-09 21:46:51 +02:00
|
|
|
// R908 substring, R909 parent-string, R910 substring-range.
|
|
|
|
// The base object of a substring can be a literal.
|
|
|
|
// In the F2018 standard, substrings of array sections are parsed as
|
|
|
|
// variants of sections instead.
|
2018-07-13 01:58:21 +02:00
|
|
|
class Substring {
|
|
|
|
public:
|
2018-08-08 20:29:05 +02:00
|
|
|
using FoldableTrait = std::true_type;
|
2018-07-07 00:12:33 +02:00
|
|
|
CLASS_BOILERPLATE(Substring)
|
2018-07-11 23:50:08 +02:00
|
|
|
Substring(DataRef &&, std::optional<SubscriptIntegerExpr> &&,
|
|
|
|
std::optional<SubscriptIntegerExpr> &&);
|
|
|
|
Substring(std::string &&, std::optional<SubscriptIntegerExpr> &&,
|
|
|
|
std::optional<SubscriptIntegerExpr> &&);
|
|
|
|
|
2018-07-13 01:58:21 +02:00
|
|
|
SubscriptIntegerExpr first() const;
|
|
|
|
SubscriptIntegerExpr last() const;
|
2018-07-11 23:50:08 +02:00
|
|
|
SubscriptIntegerExpr LEN() const;
|
2018-08-08 20:29:05 +02:00
|
|
|
std::optional<std::string> Fold(FoldingContext &);
|
2018-07-11 23:50:08 +02:00
|
|
|
|
2018-07-13 01:58:21 +02:00
|
|
|
private:
|
|
|
|
std::variant<DataRef, std::string> u_;
|
|
|
|
std::optional<IndirectSubscriptIntegerExpr> first_, last_;
|
2018-07-04 00:14:48 +02:00
|
|
|
};
|
|
|
|
|
2018-07-09 21:46:51 +02:00
|
|
|
// R915 complex-part-designator
|
|
|
|
// In the F2018 standard, complex parts of array sections are parsed as
|
|
|
|
// variants of sections instead.
|
2018-07-13 01:58:21 +02:00
|
|
|
class ComplexPart {
|
|
|
|
public:
|
2018-07-07 00:12:33 +02:00
|
|
|
ENUM_CLASS(Part, RE, IM)
|
|
|
|
CLASS_BOILERPLATE(ComplexPart)
|
2018-07-13 01:58:21 +02:00
|
|
|
ComplexPart(DataRef &&z, Part p) : complex_{std::move(z)}, part_{p} {}
|
|
|
|
const DataRef &complex() const { return complex_; }
|
|
|
|
Part part() const { return part_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
DataRef complex_;
|
|
|
|
Part part_;
|
2018-06-27 00:18:53 +02:00
|
|
|
};
|
|
|
|
|
2018-07-09 21:46:51 +02:00
|
|
|
// R901 designator is the most general data reference object, apart from
|
|
|
|
// calls to pointer-valued functions.
|
2018-07-13 01:58:21 +02:00
|
|
|
class Designator {
|
|
|
|
public:
|
2018-07-07 00:12:33 +02:00
|
|
|
CLASS_BOILERPLATE(Designator)
|
2018-07-13 01:58:21 +02:00
|
|
|
explicit Designator(DataRef &&d) : u_{std::move(d)} {}
|
|
|
|
explicit Designator(Substring &&s) : u_{std::move(s)} {}
|
|
|
|
explicit Designator(ComplexPart &&c) : u_{std::move(c)} {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::variant<DataRef, Substring, ComplexPart> u_;
|
2018-06-27 00:18:53 +02:00
|
|
|
};
|
|
|
|
|
2018-07-13 01:58:21 +02:00
|
|
|
class ProcedureDesignator {
|
|
|
|
public:
|
2018-07-07 00:12:33 +02:00
|
|
|
CLASS_BOILERPLATE(ProcedureDesignator)
|
2018-07-13 01:58:21 +02:00
|
|
|
explicit ProcedureDesignator(IntrinsicProcedure p) : u_{p} {}
|
|
|
|
explicit ProcedureDesignator(const Symbol &n) : u_{&n} {}
|
|
|
|
explicit ProcedureDesignator(const Component &c) : u_{c} {}
|
|
|
|
explicit ProcedureDesignator(Component &&c) : u_{std::move(c)} {}
|
2018-07-11 23:50:08 +02:00
|
|
|
SubscriptIntegerExpr LEN() const;
|
2018-07-13 01:58:21 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::variant<IntrinsicProcedure, const Symbol *, Component> u_;
|
2018-07-04 00:14:48 +02:00
|
|
|
};
|
|
|
|
|
2018-07-13 01:58:21 +02:00
|
|
|
template<typename ARG> class ProcedureRef {
|
|
|
|
public:
|
2018-07-10 01:23:12 +02:00
|
|
|
using ArgumentType = CopyableIndirection<ARG>;
|
2018-07-07 00:12:33 +02:00
|
|
|
CLASS_BOILERPLATE(ProcedureRef)
|
2018-07-07 01:59:05 +02:00
|
|
|
ProcedureRef(ProcedureDesignator &&p, std::vector<ArgumentType> &&a)
|
2018-07-13 01:58:21 +02:00
|
|
|
: proc_{std::move(p)}, argument_(std::move(a)) {}
|
|
|
|
const ProcedureDesignator &proc() const { return proc_; }
|
|
|
|
const std::vector<ArgumentType> &argument() const { return argument_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ProcedureDesignator proc_;
|
|
|
|
std::vector<ArgumentType> argument_;
|
2018-06-27 00:18:53 +02:00
|
|
|
};
|
|
|
|
|
2018-07-07 01:59:05 +02:00
|
|
|
using FunctionRef = ProcedureRef<ActualFunctionArg>;
|
|
|
|
|
2018-07-13 01:58:21 +02:00
|
|
|
class Variable {
|
|
|
|
public:
|
2018-07-07 00:12:33 +02:00
|
|
|
CLASS_BOILERPLATE(Variable)
|
2018-07-13 01:58:21 +02:00
|
|
|
explicit Variable(Designator &&d) : u_{std::move(d)} {}
|
|
|
|
explicit Variable(FunctionRef &&p) : u_{std::move(p)} {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::variant<Designator, FunctionRef> u_;
|
2018-07-07 01:59:05 +02:00
|
|
|
};
|
|
|
|
|
2018-07-13 01:58:21 +02:00
|
|
|
class ActualFunctionArg {
|
|
|
|
public:
|
2018-07-07 01:59:05 +02:00
|
|
|
CLASS_BOILERPLATE(ActualFunctionArg)
|
2018-07-13 01:58:21 +02:00
|
|
|
explicit ActualFunctionArg(GenericExpr &&x) : u_{std::move(x)} {}
|
|
|
|
explicit ActualFunctionArg(Variable &&x) : u_{std::move(x)} {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::variant<CopyableIndirection<GenericExpr>, Variable> u_;
|
2018-07-04 00:14:48 +02:00
|
|
|
};
|
|
|
|
|
2018-07-07 00:12:33 +02:00
|
|
|
struct Label { // TODO: this is a placeholder
|
|
|
|
CLASS_BOILERPLATE(Label)
|
|
|
|
explicit Label(int lab) : label{lab} {}
|
|
|
|
int label;
|
|
|
|
};
|
|
|
|
|
2018-07-13 01:58:21 +02:00
|
|
|
class ActualSubroutineArg {
|
|
|
|
public:
|
2018-07-07 01:59:05 +02:00
|
|
|
CLASS_BOILERPLATE(ActualSubroutineArg)
|
2018-07-13 01:58:21 +02:00
|
|
|
explicit ActualSubroutineArg(GenericExpr &&x) : u_{std::move(x)} {}
|
|
|
|
explicit ActualSubroutineArg(Variable &&x) : u_{std::move(x)} {}
|
|
|
|
explicit ActualSubroutineArg(const Label &l) : u_{&l} {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::variant<CopyableIndirection<GenericExpr>, Variable, const Label *> u_;
|
2018-07-04 00:14:48 +02:00
|
|
|
};
|
2018-07-07 01:59:05 +02:00
|
|
|
|
|
|
|
using SubroutineRef = ProcedureRef<ActualSubroutineArg>;
|
|
|
|
|
2018-06-27 00:18:53 +02:00
|
|
|
} // namespace Fortran::evaluate
|
2018-07-07 00:12:33 +02:00
|
|
|
|
|
|
|
// This inclusion must follow the definitions in this header due to
|
|
|
|
// mutual references.
|
|
|
|
#include "expression.h"
|
|
|
|
|
2018-06-27 00:18:53 +02:00
|
|
|
#endif // FORTRAN_EVALUATE_VARIABLE_H_
|