2018-12-04 19:55:32 +01:00
|
|
|
// Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
|
2018-10-09 00:35:19 +02:00
|
|
|
//
|
|
|
|
// 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_CALL_H_
|
|
|
|
#define FORTRAN_EVALUATE_CALL_H_
|
|
|
|
|
|
|
|
#include "common.h"
|
2019-02-01 01:04:17 +01:00
|
|
|
#include "constant.h"
|
2018-10-09 00:35:19 +02:00
|
|
|
#include "type.h"
|
|
|
|
#include "../common/indirection.h"
|
|
|
|
#include "../parser/char-block.h"
|
2018-10-18 22:03:51 +02:00
|
|
|
#include "../semantics/attr.h"
|
2018-10-09 00:35:19 +02:00
|
|
|
#include <optional>
|
|
|
|
#include <ostream>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-10-18 22:03:51 +02:00
|
|
|
namespace Fortran::semantics {
|
|
|
|
class Symbol;
|
|
|
|
}
|
|
|
|
|
2018-10-09 00:35:19 +02:00
|
|
|
namespace Fortran::evaluate {
|
|
|
|
|
|
|
|
struct ActualArgument {
|
2018-10-12 02:01:31 +02:00
|
|
|
explicit ActualArgument(Expr<SomeType> &&x) : value{std::move(x)} {}
|
2018-10-09 00:35:19 +02:00
|
|
|
explicit ActualArgument(CopyableIndirection<Expr<SomeType>> &&v)
|
|
|
|
: value{std::move(v)} {}
|
2018-10-18 19:50:55 +02:00
|
|
|
|
2018-10-09 00:35:19 +02:00
|
|
|
std::optional<DynamicType> GetType() const;
|
|
|
|
int Rank() const;
|
2019-01-04 23:05:53 +01:00
|
|
|
bool operator==(const ActualArgument &) const;
|
2018-11-06 00:02:37 +01:00
|
|
|
std::ostream &AsFortran(std::ostream &) const;
|
2018-10-11 23:51:14 +02:00
|
|
|
std::optional<int> VectorSize() const;
|
2018-10-09 00:35:19 +02:00
|
|
|
|
|
|
|
std::optional<parser::CharBlock> keyword;
|
2018-10-18 22:03:51 +02:00
|
|
|
bool isAlternateReturn{false}; // when true, "value" is a label number
|
|
|
|
|
|
|
|
// TODO: Mark legacy %VAL and %REF arguments
|
2018-10-11 23:51:14 +02:00
|
|
|
|
|
|
|
// Subtlety: There is a distinction that must be maintained here between an
|
|
|
|
// actual argument expression that is a variable and one that is not,
|
|
|
|
// e.g. between X and (X). The parser attempts to parse each argument
|
|
|
|
// first as a variable, then as an expression, and the distinction appears
|
|
|
|
// in the parse tree.
|
2018-10-09 00:35:19 +02:00
|
|
|
CopyableIndirection<Expr<SomeType>> value;
|
|
|
|
};
|
|
|
|
|
2018-10-18 00:09:48 +02:00
|
|
|
using ActualArguments = std::vector<std::optional<ActualArgument>>;
|
2018-10-18 22:03:51 +02:00
|
|
|
|
|
|
|
// Intrinsics are identified by their names and the characteristics
|
|
|
|
// of their arguments, at least for now.
|
2018-12-04 19:55:32 +01:00
|
|
|
using IntrinsicProcedure = std::string;
|
2018-10-18 22:03:51 +02:00
|
|
|
|
|
|
|
struct SpecificIntrinsic {
|
|
|
|
explicit SpecificIntrinsic(IntrinsicProcedure n) : name{n} {}
|
|
|
|
SpecificIntrinsic(IntrinsicProcedure n, std::optional<DynamicType> &&dt,
|
|
|
|
int r, semantics::Attrs a)
|
|
|
|
: name{n}, type{std::move(dt)}, rank{r}, attrs{a} {}
|
2018-12-04 19:55:32 +01:00
|
|
|
SpecificIntrinsic(const SpecificIntrinsic &) = default;
|
|
|
|
SpecificIntrinsic(SpecificIntrinsic &&) = default;
|
|
|
|
SpecificIntrinsic &operator=(const SpecificIntrinsic &) = default;
|
|
|
|
SpecificIntrinsic &operator=(SpecificIntrinsic &&) = default;
|
2019-01-04 23:05:53 +01:00
|
|
|
bool operator==(const SpecificIntrinsic &) const;
|
2018-11-06 00:02:37 +01:00
|
|
|
std::ostream &AsFortran(std::ostream &) const;
|
2018-10-18 22:03:51 +02:00
|
|
|
|
|
|
|
IntrinsicProcedure name;
|
|
|
|
bool isRestrictedSpecific{false}; // if true, can only call it
|
|
|
|
std::optional<DynamicType> type; // absent if and only if subroutine call
|
|
|
|
int rank{0};
|
|
|
|
semantics::Attrs attrs; // ELEMENTAL, POINTER
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ProcedureDesignator {
|
|
|
|
EVALUATE_UNION_CLASS_BOILERPLATE(ProcedureDesignator)
|
|
|
|
explicit ProcedureDesignator(SpecificIntrinsic &&i) : u{std::move(i)} {}
|
|
|
|
explicit ProcedureDesignator(const semantics::Symbol &n) : u{&n} {}
|
|
|
|
std::optional<DynamicType> GetType() const;
|
|
|
|
int Rank() const;
|
|
|
|
bool IsElemental() const;
|
|
|
|
Expr<SubscriptInteger> LEN() const;
|
|
|
|
const semantics::Symbol *GetSymbol() const;
|
2018-11-06 00:02:37 +01:00
|
|
|
std::ostream &AsFortran(std::ostream &) const;
|
2018-10-18 22:03:51 +02:00
|
|
|
|
|
|
|
// TODO: When calling X%F, pass X as PASS argument unless NOPASS
|
|
|
|
std::variant<SpecificIntrinsic, const semantics::Symbol *> u;
|
|
|
|
};
|
2019-02-01 01:04:17 +01:00
|
|
|
|
|
|
|
class ProcedureRef {
|
|
|
|
public:
|
|
|
|
CLASS_BOILERPLATE(ProcedureRef)
|
|
|
|
ProcedureRef(ProcedureDesignator &&p, ActualArguments &&a)
|
|
|
|
: proc_{std::move(p)}, arguments_(std::move(a)) {}
|
|
|
|
|
|
|
|
ProcedureDesignator &proc() { return proc_; }
|
|
|
|
const ProcedureDesignator &proc() const { return proc_; }
|
|
|
|
ActualArguments &arguments() { return arguments_; }
|
|
|
|
const ActualArguments &arguments() const { return arguments_; }
|
|
|
|
|
|
|
|
Expr<SubscriptInteger> LEN() const;
|
|
|
|
int Rank() const { return proc_.Rank(); }
|
|
|
|
bool IsElemental() const { return proc_.IsElemental(); }
|
|
|
|
bool operator==(const ProcedureRef &) const;
|
|
|
|
std::ostream &AsFortran(std::ostream &) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
ProcedureDesignator proc_;
|
|
|
|
ActualArguments arguments_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename A> class FunctionRef : public ProcedureRef {
|
|
|
|
public:
|
|
|
|
using Result = A;
|
|
|
|
CLASS_BOILERPLATE(FunctionRef)
|
|
|
|
FunctionRef(ProcedureRef &&pr) : ProcedureRef{std::move(pr)} {}
|
|
|
|
FunctionRef(ProcedureDesignator &&p, ActualArguments &&a)
|
|
|
|
: ProcedureRef{std::move(p), std::move(a)} {}
|
|
|
|
|
|
|
|
std::optional<DynamicType> GetType() const { return proc_.GetType(); }
|
|
|
|
std::optional<Constant<Result>> Fold(FoldingContext &); // for intrinsics
|
|
|
|
};
|
|
|
|
|
|
|
|
FOR_EACH_SPECIFIC_TYPE(extern template class FunctionRef)
|
2018-10-25 14:55:23 +02:00
|
|
|
}
|
2018-10-09 00:35:19 +02:00
|
|
|
#endif // FORTRAN_EVALUATE_CALL_H_
|