xref: /llvm-project/flang/lib/Evaluate/call.cpp (revision 64ab3302d5a130c00b66a6957b2e7f0c9b9c537d)
1 //===-- lib/Evaluate/call.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "flang/Evaluate/call.h"
10 #include "flang/Common/idioms.h"
11 #include "flang/Evaluate/characteristics.h"
12 #include "flang/Evaluate/expression.h"
13 #include "flang/Evaluate/tools.h"
14 #include "flang/Semantics/symbol.h"
15 
16 namespace Fortran::evaluate {
17 
18 DEFINE_DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(ActualArgument)
19 ActualArgument::ActualArgument(Expr<SomeType> &&x) : u_{std::move(x)} {}
20 ActualArgument::ActualArgument(common::CopyableIndirection<Expr<SomeType>> &&v)
21   : u_{std::move(v)} {}
22 ActualArgument::ActualArgument(AssumedType x) : u_{x} {}
23 ActualArgument::~ActualArgument() {}
24 
25 ActualArgument::AssumedType::AssumedType(const Symbol &symbol)
26   : symbol_{symbol} {
27   const semantics::DeclTypeSpec *type{symbol.GetType()};
28   CHECK(type && type->category() == semantics::DeclTypeSpec::TypeStar);
29 }
30 
31 int ActualArgument::AssumedType::Rank() const { return symbol_->Rank(); }
32 
33 ActualArgument &ActualArgument::operator=(Expr<SomeType> &&expr) {
34   u_ = std::move(expr);
35   return *this;
36 }
37 
38 std::optional<DynamicType> ActualArgument::GetType() const {
39   if (const Expr<SomeType> *expr{UnwrapExpr()}) {
40     return expr->GetType();
41   } else if (std::holds_alternative<AssumedType>(u_)) {
42     return DynamicType::AssumedType();
43   } else {
44     return std::nullopt;
45   }
46 }
47 
48 int ActualArgument::Rank() const {
49   if (const Expr<SomeType> *expr{UnwrapExpr()}) {
50     return expr->Rank();
51   } else {
52     return std::get<AssumedType>(u_).Rank();
53   }
54 }
55 
56 bool ActualArgument::operator==(const ActualArgument &that) const {
57   return keyword_ == that.keyword_ &&
58       isAlternateReturn_ == that.isAlternateReturn_ &&
59       isPassedObject_ == that.isPassedObject_ && u_ == that.u_;
60 }
61 
62 void ActualArgument::Parenthesize() {
63   u_ = evaluate::Parenthesize(std::move(DEREF(UnwrapExpr())));
64 }
65 
66 SpecificIntrinsic::SpecificIntrinsic(
67     IntrinsicProcedure n, characteristics::Procedure &&chars)
68   : name{n}, characteristics{new characteristics::Procedure{std::move(chars)}} {
69 }
70 
71 DEFINE_DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(SpecificIntrinsic)
72 
73 SpecificIntrinsic::~SpecificIntrinsic() {}
74 
75 bool SpecificIntrinsic::operator==(const SpecificIntrinsic &that) const {
76   return name == that.name && characteristics == that.characteristics;
77 }
78 
79 ProcedureDesignator::ProcedureDesignator(Component &&c)
80   : u{common::CopyableIndirection<Component>::Make(std::move(c))} {}
81 
82 std::optional<DynamicType> ProcedureDesignator::GetType() const {
83   if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&u)}) {
84     if (const auto &result{intrinsic->characteristics.value().functionResult}) {
85       if (const auto *typeAndShape{result->GetTypeAndShape()}) {
86         return typeAndShape->type();
87       }
88     }
89   } else {
90     return DynamicType::From(GetSymbol());
91   }
92   return std::nullopt;
93 }
94 
95 int ProcedureDesignator::Rank() const {
96   if (const Symbol * symbol{GetSymbol()}) {
97     return symbol->Rank();
98   }
99   if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&u)}) {
100     if (const auto &result{intrinsic->characteristics.value().functionResult}) {
101       if (const auto *typeAndShape{result->GetTypeAndShape()}) {
102         CHECK(!typeAndShape->attrs().test(
103             characteristics::TypeAndShape::Attr::AssumedRank));
104         return typeAndShape->Rank();
105       }
106     }
107   }
108   DIE("ProcedureDesignator::Rank(): no case");
109   return 0;
110 }
111 
112 const Symbol *ProcedureDesignator::GetInterfaceSymbol() const {
113   if (const Symbol * symbol{GetSymbol()}) {
114     if (const auto *details{
115             symbol->detailsIf<semantics::ProcEntityDetails>()}) {
116       return details->interface().symbol();
117     }
118   }
119   return nullptr;
120 }
121 
122 bool ProcedureDesignator::IsElemental() const {
123   if (const Symbol * interface{GetInterfaceSymbol()}) {
124     return interface->attrs().test(semantics::Attr::ELEMENTAL);
125   } else if (const Symbol * symbol{GetSymbol()}) {
126     return symbol->attrs().test(semantics::Attr::ELEMENTAL);
127   } else if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&u)}) {
128     return intrinsic->characteristics.value().attrs.test(
129         characteristics::Procedure::Attr::Elemental);
130   } else {
131     DIE("ProcedureDesignator::IsElemental(): no case");
132   }
133   return false;
134 }
135 
136 const SpecificIntrinsic *ProcedureDesignator::GetSpecificIntrinsic() const {
137   return std::get_if<SpecificIntrinsic>(&u);
138 }
139 
140 const Component *ProcedureDesignator::GetComponent() const {
141   if (auto *c{std::get_if<common::CopyableIndirection<Component>>(&u)}) {
142     return &c->value();
143   } else {
144     return nullptr;
145   }
146 }
147 
148 const Symbol *ProcedureDesignator::GetSymbol() const {
149   return std::visit(
150       common::visitors{
151           [](SymbolRef symbol) { return &*symbol; },
152           [](const common::CopyableIndirection<Component> &c) {
153             return &c.value().GetLastSymbol();
154           },
155           [](const auto &) -> const Symbol * { return nullptr; },
156       },
157       u);
158 }
159 
160 std::string ProcedureDesignator::GetName() const {
161   return std::visit(
162       common::visitors{
163           [](const SpecificIntrinsic &i) { return i.name; },
164           [](const Symbol &symbol) { return symbol.name().ToString(); },
165           [](const common::CopyableIndirection<Component> &c) {
166             return c.value().GetLastSymbol().name().ToString();
167           },
168       },
169       u);
170 }
171 
172 std::optional<Expr<SubscriptInteger>> ProcedureRef::LEN() const {
173   if (const auto *intrinsic{std::get_if<SpecificIntrinsic>(&proc_.u)}) {
174     if (intrinsic->name == "repeat") {
175       // LEN(REPEAT(ch,n)) == LEN(ch) * n
176       CHECK(arguments_.size() == 2);
177       const auto *stringArg{
178           UnwrapExpr<Expr<SomeCharacter>>(arguments_[0].value())};
179       const auto *nCopiesArg{
180           UnwrapExpr<Expr<SomeInteger>>(arguments_[1].value())};
181       CHECK(stringArg && nCopiesArg);
182       if (auto stringLen{stringArg->LEN()}) {
183         auto converted{ConvertTo(*stringLen, common::Clone(*nCopiesArg))};
184         return *std::move(stringLen) * std::move(converted);
185       }
186     }
187     // Some other cases (e.g., LEN(CHAR(...))) are handled in
188     // ProcedureDesignator::LEN() because they're independent of the
189     // lengths of the actual arguments.
190   }
191   return proc_.LEN();
192 }
193 
194 int ProcedureRef::Rank() const {
195   if (IsElemental()) {
196     for (const auto &arg : arguments_) {
197       if (arg) {
198         if (int rank{arg->Rank()}; rank > 0) {
199           return rank;
200         }
201       }
202     }
203     return 0;
204   } else {
205     return proc_.Rank();
206   }
207 }
208 
209 ProcedureRef::~ProcedureRef() {}
210 
211 FOR_EACH_SPECIFIC_TYPE(template class FunctionRef, )
212 }
213 DEFINE_DELETER(Fortran::evaluate::ProcedureRef)
214