// Copyright (c) 2018-2019, 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_CALL_H_ #define FORTRAN_EVALUATE_CALL_H_ #include "common.h" #include "constant.h" #include "formatting.h" #include "type.h" #include "../common/indirection.h" #include "../parser/char-block.h" #include "../semantics/attr.h" #include #include #include namespace Fortran::semantics { class Symbol; } // Mutually referential data structures are represented here with forward // declarations of hitherto undefined class types and a level of indirection. namespace Fortran::evaluate { class Component; class IntrinsicProcTable; } namespace Fortran::evaluate::characteristics { struct Procedure; } extern template class Fortran::common::Indirection; extern template class Fortran::common::Indirection< Fortran::evaluate::characteristics::Procedure, true>; namespace Fortran::evaluate { class ActualArgument { public: // Dummy arguments that are TYPE(*) can be forwarded as actual arguments. // Since that's the only thing one may do with them in Fortran, they're // represented in expressions as a special case of an actual argument. class AssumedType { public: explicit AssumedType(const semantics::Symbol &); DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(AssumedType) const semantics::Symbol &symbol() const { return *symbol_; } int Rank() const; bool operator==(const AssumedType &that) const { return symbol_ == that.symbol_; } std::ostream &AsFortran(std::ostream &) const; private: const semantics::Symbol *symbol_; }; explicit ActualArgument(Expr &&); explicit ActualArgument(common::CopyableIndirection> &&); explicit ActualArgument(AssumedType); ~ActualArgument(); ActualArgument &operator=(Expr &&); Expr *UnwrapExpr() { if (auto *p{ std::get_if>>(&u_)}) { return &p->value(); } else { return nullptr; } } const Expr *UnwrapExpr() const { if (const auto *p{ std::get_if>>(&u_)}) { return &p->value(); } else { return nullptr; } } const semantics::Symbol *GetAssumedTypeDummy() const { if (const AssumedType * aType{std::get_if(&u_)}) { return &aType->symbol(); } else { return nullptr; } } std::optional GetType() const; int Rank() const; bool operator==(const ActualArgument &) const; std::ostream &AsFortran(std::ostream &) const; std::optional keyword; bool isAlternateReturn{false}; // when true, "value" is a label number // TODO: Mark legacy %VAL and %REF arguments private: // 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. std::variant>, AssumedType> u_; }; using ActualArguments = std::vector>; // Intrinsics are identified by their names and the characteristics // of their arguments, at least for now. using IntrinsicProcedure = std::string; struct SpecificIntrinsic { SpecificIntrinsic(IntrinsicProcedure, characteristics::Procedure &&); DECLARE_CONSTRUCTORS_AND_ASSIGNMENTS(SpecificIntrinsic) ~SpecificIntrinsic(); bool operator==(const SpecificIntrinsic &) const; std::ostream &AsFortran(std::ostream &) const; IntrinsicProcedure name; bool isRestrictedSpecific{false}; // if true, can only call it, not pass it common::CopyableIndirection characteristics; }; struct ProcedureDesignator { EVALUATE_UNION_CLASS_BOILERPLATE(ProcedureDesignator) explicit ProcedureDesignator(SpecificIntrinsic &&i) : u{std::move(i)} {} explicit ProcedureDesignator(const semantics::Symbol &n) : u{&n} {} explicit ProcedureDesignator(Component &&); // Exactly one of these will return a non-null pointer. const SpecificIntrinsic *GetSpecificIntrinsic() const; const semantics::Symbol *GetSymbol() const; // symbol or component symbol // Always null if the procedure is intrinsic. const Component *GetComponent() const; std::string GetName() const; std::optional GetType() const; int Rank() const; bool IsElemental() const; Expr LEN() const; std::ostream &AsFortran(std::ostream &) const; // TODO: When calling X%F, pass X as PASS argument unless NOPASS std::variant> u; }; 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 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 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 GetType() const { return proc_.GetType(); } std::optional> Fold(FoldingContext &); // for intrinsics }; FOR_EACH_SPECIFIC_TYPE(extern template class FunctionRef, ) } #endif // FORTRAN_EVALUATE_CALL_H_