1 //===-- lib/Evaluate/characteristics.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/characteristics.h"
10 #include "flang/Common/indirection.h"
11 #include "flang/Evaluate/check-expression.h"
12 #include "flang/Evaluate/fold.h"
13 #include "flang/Evaluate/intrinsics.h"
14 #include "flang/Evaluate/tools.h"
15 #include "flang/Evaluate/type.h"
16 #include "flang/Parser/message.h"
17 #include "flang/Semantics/scope.h"
18 #include "flang/Semantics/symbol.h"
19 #include "flang/Semantics/tools.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <initializer_list>
22
23 using namespace Fortran::parser::literals;
24
25 namespace Fortran::evaluate::characteristics {
26
27 // Copy attributes from a symbol to dst based on the mapping in pairs.
28 template <typename A, typename B>
CopyAttrs(const semantics::Symbol & src,A & dst,const std::initializer_list<std::pair<semantics::Attr,B>> & pairs)29 static void CopyAttrs(const semantics::Symbol &src, A &dst,
30 const std::initializer_list<std::pair<semantics::Attr, B>> &pairs) {
31 for (const auto &pair : pairs) {
32 if (src.attrs().test(pair.first)) {
33 dst.attrs.set(pair.second);
34 }
35 }
36 }
37
38 // Shapes of function results and dummy arguments have to have
39 // the same rank, the same deferred dimensions, and the same
40 // values for explicit dimensions when constant.
ShapesAreCompatible(const Shape & x,const Shape & y)41 bool ShapesAreCompatible(const Shape &x, const Shape &y) {
42 if (x.size() != y.size()) {
43 return false;
44 }
45 auto yIter{y.begin()};
46 for (const auto &xDim : x) {
47 const auto &yDim{*yIter++};
48 if (xDim) {
49 if (!yDim || ToInt64(*xDim) != ToInt64(*yDim)) {
50 return false;
51 }
52 } else if (yDim) {
53 return false;
54 }
55 }
56 return true;
57 }
58
operator ==(const TypeAndShape & that) const59 bool TypeAndShape::operator==(const TypeAndShape &that) const {
60 return type_ == that.type_ && ShapesAreCompatible(shape_, that.shape_) &&
61 attrs_ == that.attrs_ && corank_ == that.corank_;
62 }
63
Rewrite(FoldingContext & context)64 TypeAndShape &TypeAndShape::Rewrite(FoldingContext &context) {
65 LEN_ = Fold(context, std::move(LEN_));
66 shape_ = Fold(context, std::move(shape_));
67 return *this;
68 }
69
Characterize(const semantics::Symbol & symbol,FoldingContext & context)70 std::optional<TypeAndShape> TypeAndShape::Characterize(
71 const semantics::Symbol &symbol, FoldingContext &context) {
72 const auto &ultimate{symbol.GetUltimate()};
73 return common::visit(
74 common::visitors{
75 [&](const semantics::ProcEntityDetails &proc) {
76 const semantics::ProcInterface &interface { proc.interface() };
77 if (interface.type()) {
78 return Characterize(*interface.type(), context);
79 } else if (interface.symbol()) {
80 return Characterize(*interface.symbol(), context);
81 } else {
82 return std::optional<TypeAndShape>{};
83 }
84 },
85 [&](const semantics::AssocEntityDetails &assoc) {
86 return Characterize(assoc, context);
87 },
88 [&](const semantics::ProcBindingDetails &binding) {
89 return Characterize(binding.symbol(), context);
90 },
91 [&](const auto &x) -> std::optional<TypeAndShape> {
92 using Ty = std::decay_t<decltype(x)>;
93 if constexpr (std::is_same_v<Ty, semantics::EntityDetails> ||
94 std::is_same_v<Ty, semantics::ObjectEntityDetails> ||
95 std::is_same_v<Ty, semantics::TypeParamDetails>) {
96 if (const semantics::DeclTypeSpec * type{ultimate.GetType()}) {
97 if (auto dyType{DynamicType::From(*type)}) {
98 TypeAndShape result{
99 std::move(*dyType), GetShape(context, ultimate)};
100 result.AcquireAttrs(ultimate);
101 result.AcquireLEN(ultimate);
102 return std::move(result.Rewrite(context));
103 }
104 }
105 }
106 return std::nullopt;
107 },
108 },
109 // GetUltimate() used here, not ResolveAssociations(), because
110 // we need the type/rank of an associate entity from TYPE IS,
111 // CLASS IS, or RANK statement.
112 ultimate.details());
113 }
114
Characterize(const semantics::AssocEntityDetails & assoc,FoldingContext & context)115 std::optional<TypeAndShape> TypeAndShape::Characterize(
116 const semantics::AssocEntityDetails &assoc, FoldingContext &context) {
117 std::optional<TypeAndShape> result;
118 if (auto type{DynamicType::From(assoc.type())}) {
119 if (auto rank{assoc.rank()}) {
120 if (*rank >= 0 && *rank <= common::maxRank) {
121 result = TypeAndShape{std::move(*type), Shape(*rank)};
122 }
123 } else if (auto shape{GetShape(context, assoc.expr())}) {
124 result = TypeAndShape{std::move(*type), std::move(*shape)};
125 }
126 if (result && type->category() == TypeCategory::Character) {
127 if (const auto *chExpr{UnwrapExpr<Expr<SomeCharacter>>(assoc.expr())}) {
128 if (auto len{chExpr->LEN()}) {
129 result->set_LEN(std::move(*len));
130 }
131 }
132 }
133 }
134 return Fold(context, std::move(result));
135 }
136
Characterize(const semantics::DeclTypeSpec & spec,FoldingContext & context)137 std::optional<TypeAndShape> TypeAndShape::Characterize(
138 const semantics::DeclTypeSpec &spec, FoldingContext &context) {
139 if (auto type{DynamicType::From(spec)}) {
140 return Fold(context, TypeAndShape{std::move(*type)});
141 } else {
142 return std::nullopt;
143 }
144 }
145
Characterize(const ActualArgument & arg,FoldingContext & context)146 std::optional<TypeAndShape> TypeAndShape::Characterize(
147 const ActualArgument &arg, FoldingContext &context) {
148 return Characterize(arg.UnwrapExpr(), context);
149 }
150
IsCompatibleWith(parser::ContextualMessages & messages,const TypeAndShape & that,const char * thisIs,const char * thatIs,bool omitShapeConformanceCheck,enum CheckConformanceFlags::Flags flags) const151 bool TypeAndShape::IsCompatibleWith(parser::ContextualMessages &messages,
152 const TypeAndShape &that, const char *thisIs, const char *thatIs,
153 bool omitShapeConformanceCheck,
154 enum CheckConformanceFlags::Flags flags) const {
155 if (!type_.IsTkCompatibleWith(that.type_)) {
156 messages.Say(
157 "%1$s type '%2$s' is not compatible with %3$s type '%4$s'"_err_en_US,
158 thatIs, that.AsFortran(), thisIs, AsFortran());
159 return false;
160 }
161 return omitShapeConformanceCheck ||
162 CheckConformance(messages, shape_, that.shape_, flags, thisIs, thatIs)
163 .value_or(true /*fail only when nonconformance is known now*/);
164 }
165
MeasureElementSizeInBytes(FoldingContext & foldingContext,bool align) const166 std::optional<Expr<SubscriptInteger>> TypeAndShape::MeasureElementSizeInBytes(
167 FoldingContext &foldingContext, bool align) const {
168 if (LEN_) {
169 CHECK(type_.category() == TypeCategory::Character);
170 return Fold(foldingContext,
171 Expr<SubscriptInteger>{
172 foldingContext.targetCharacteristics().GetByteSize(
173 type_.category(), type_.kind())} *
174 Expr<SubscriptInteger>{*LEN_});
175 }
176 if (auto elementBytes{type_.MeasureSizeInBytes(foldingContext, align)}) {
177 return Fold(foldingContext, std::move(*elementBytes));
178 }
179 return std::nullopt;
180 }
181
MeasureSizeInBytes(FoldingContext & foldingContext) const182 std::optional<Expr<SubscriptInteger>> TypeAndShape::MeasureSizeInBytes(
183 FoldingContext &foldingContext) const {
184 if (auto elements{GetSize(Shape{shape_})}) {
185 // Sizes of arrays (even with single elements) are multiples of
186 // their alignments.
187 if (auto elementBytes{
188 MeasureElementSizeInBytes(foldingContext, GetRank(shape_) > 0)}) {
189 return Fold(
190 foldingContext, std::move(*elements) * std::move(*elementBytes));
191 }
192 }
193 return std::nullopt;
194 }
195
AcquireAttrs(const semantics::Symbol & symbol)196 void TypeAndShape::AcquireAttrs(const semantics::Symbol &symbol) {
197 if (IsAssumedShape(symbol)) {
198 attrs_.set(Attr::AssumedShape);
199 }
200 if (IsDeferredShape(symbol)) {
201 attrs_.set(Attr::DeferredShape);
202 }
203 if (const auto *object{
204 symbol.GetUltimate().detailsIf<semantics::ObjectEntityDetails>()}) {
205 corank_ = object->coshape().Rank();
206 if (object->IsAssumedRank()) {
207 attrs_.set(Attr::AssumedRank);
208 }
209 if (object->IsAssumedSize()) {
210 attrs_.set(Attr::AssumedSize);
211 }
212 if (object->IsCoarray()) {
213 attrs_.set(Attr::Coarray);
214 }
215 }
216 }
217
AcquireLEN()218 void TypeAndShape::AcquireLEN() {
219 if (auto len{type_.GetCharLength()}) {
220 LEN_ = std::move(len);
221 }
222 }
223
AcquireLEN(const semantics::Symbol & symbol)224 void TypeAndShape::AcquireLEN(const semantics::Symbol &symbol) {
225 if (type_.category() == TypeCategory::Character) {
226 if (auto len{DataRef{symbol}.LEN()}) {
227 LEN_ = std::move(*len);
228 }
229 }
230 }
231
AsFortran() const232 std::string TypeAndShape::AsFortran() const {
233 return type_.AsFortran(LEN_ ? LEN_->AsFortran() : "");
234 }
235
Dump(llvm::raw_ostream & o) const236 llvm::raw_ostream &TypeAndShape::Dump(llvm::raw_ostream &o) const {
237 o << type_.AsFortran(LEN_ ? LEN_->AsFortran() : "");
238 attrs_.Dump(o, EnumToString);
239 if (!shape_.empty()) {
240 o << " dimension";
241 char sep{'('};
242 for (const auto &expr : shape_) {
243 o << sep;
244 sep = ',';
245 if (expr) {
246 expr->AsFortran(o);
247 } else {
248 o << ':';
249 }
250 }
251 o << ')';
252 }
253 return o;
254 }
255
operator ==(const DummyDataObject & that) const256 bool DummyDataObject::operator==(const DummyDataObject &that) const {
257 return type == that.type && attrs == that.attrs && intent == that.intent &&
258 coshape == that.coshape;
259 }
260
AreCompatibleDummyDataObjectShapes(const Shape & x,const Shape & y)261 static bool AreCompatibleDummyDataObjectShapes(const Shape &x, const Shape &y) {
262 // TODO: Validate more than just compatible ranks
263 return GetRank(x) == GetRank(y);
264 }
265
IsCompatibleWith(const DummyDataObject & actual,std::string * whyNot) const266 bool DummyDataObject::IsCompatibleWith(
267 const DummyDataObject &actual, std::string *whyNot) const {
268 if (!AreCompatibleDummyDataObjectShapes(type.shape(), actual.type.shape())) {
269 if (whyNot) {
270 *whyNot = "incompatible dummy data object shapes";
271 }
272 return false;
273 }
274 if (!type.type().IsTkCompatibleWith(actual.type.type())) {
275 if (whyNot) {
276 *whyNot = "incompatible dummy data object types: "s +
277 type.type().AsFortran() + " vs " + actual.type.type().AsFortran();
278 }
279 return false;
280 }
281 if (attrs != actual.attrs) {
282 if (whyNot) {
283 *whyNot = "incompatible dummy data object attributes";
284 }
285 return false;
286 }
287 if (intent != actual.intent) {
288 if (whyNot) {
289 *whyNot = "incompatible dummy data object intents";
290 }
291 return false;
292 }
293 if (coshape != actual.coshape) {
294 if (whyNot) {
295 *whyNot = "incompatible dummy data object coshapes";
296 }
297 return false;
298 }
299 return true;
300 }
301
GetIntent(const semantics::Attrs & attrs)302 static common::Intent GetIntent(const semantics::Attrs &attrs) {
303 if (attrs.test(semantics::Attr::INTENT_IN)) {
304 return common::Intent::In;
305 } else if (attrs.test(semantics::Attr::INTENT_OUT)) {
306 return common::Intent::Out;
307 } else if (attrs.test(semantics::Attr::INTENT_INOUT)) {
308 return common::Intent::InOut;
309 } else {
310 return common::Intent::Default;
311 }
312 }
313
Characterize(const semantics::Symbol & symbol,FoldingContext & context)314 std::optional<DummyDataObject> DummyDataObject::Characterize(
315 const semantics::Symbol &symbol, FoldingContext &context) {
316 if (symbol.has<semantics::ObjectEntityDetails>() ||
317 symbol.has<semantics::EntityDetails>()) {
318 if (auto type{TypeAndShape::Characterize(symbol, context)}) {
319 std::optional<DummyDataObject> result{std::move(*type)};
320 using semantics::Attr;
321 CopyAttrs<DummyDataObject, DummyDataObject::Attr>(symbol, *result,
322 {
323 {Attr::OPTIONAL, DummyDataObject::Attr::Optional},
324 {Attr::ALLOCATABLE, DummyDataObject::Attr::Allocatable},
325 {Attr::ASYNCHRONOUS, DummyDataObject::Attr::Asynchronous},
326 {Attr::CONTIGUOUS, DummyDataObject::Attr::Contiguous},
327 {Attr::VALUE, DummyDataObject::Attr::Value},
328 {Attr::VOLATILE, DummyDataObject::Attr::Volatile},
329 {Attr::POINTER, DummyDataObject::Attr::Pointer},
330 {Attr::TARGET, DummyDataObject::Attr::Target},
331 });
332 result->intent = GetIntent(symbol.attrs());
333 return result;
334 }
335 }
336 return std::nullopt;
337 }
338
CanBePassedViaImplicitInterface() const339 bool DummyDataObject::CanBePassedViaImplicitInterface() const {
340 if ((attrs &
341 Attrs{Attr::Allocatable, Attr::Asynchronous, Attr::Optional,
342 Attr::Pointer, Attr::Target, Attr::Value, Attr::Volatile})
343 .any()) {
344 return false; // 15.4.2.2(3)(a)
345 } else if ((type.attrs() &
346 TypeAndShape::Attrs{TypeAndShape::Attr::AssumedShape,
347 TypeAndShape::Attr::AssumedRank,
348 TypeAndShape::Attr::Coarray})
349 .any()) {
350 return false; // 15.4.2.2(3)(b-d)
351 } else if (type.type().IsPolymorphic()) {
352 return false; // 15.4.2.2(3)(f)
353 } else if (const auto *derived{GetDerivedTypeSpec(type.type())}) {
354 return derived->parameters().empty(); // 15.4.2.2(3)(e)
355 } else {
356 return true;
357 }
358 }
359
Dump(llvm::raw_ostream & o) const360 llvm::raw_ostream &DummyDataObject::Dump(llvm::raw_ostream &o) const {
361 attrs.Dump(o, EnumToString);
362 if (intent != common::Intent::Default) {
363 o << "INTENT(" << common::EnumToString(intent) << ')';
364 }
365 type.Dump(o);
366 if (!coshape.empty()) {
367 char sep{'['};
368 for (const auto &expr : coshape) {
369 expr.AsFortran(o << sep);
370 sep = ',';
371 }
372 }
373 return o;
374 }
375
DummyProcedure(Procedure && p)376 DummyProcedure::DummyProcedure(Procedure &&p)
377 : procedure{new Procedure{std::move(p)}} {}
378
operator ==(const DummyProcedure & that) const379 bool DummyProcedure::operator==(const DummyProcedure &that) const {
380 return attrs == that.attrs && intent == that.intent &&
381 procedure.value() == that.procedure.value();
382 }
383
IsCompatibleWith(const DummyProcedure & actual,std::string * whyNot) const384 bool DummyProcedure::IsCompatibleWith(
385 const DummyProcedure &actual, std::string *whyNot) const {
386 if (attrs != actual.attrs) {
387 if (whyNot) {
388 *whyNot = "incompatible dummy procedure attributes";
389 }
390 return false;
391 }
392 if (intent != actual.intent) {
393 if (whyNot) {
394 *whyNot = "incompatible dummy procedure intents";
395 }
396 return false;
397 }
398 if (!procedure.value().IsCompatibleWith(actual.procedure.value(), whyNot)) {
399 if (whyNot) {
400 *whyNot = "incompatible dummy procedure interfaces: "s + *whyNot;
401 }
402 return false;
403 }
404 return true;
405 }
406
GetSeenProcs(const semantics::UnorderedSymbolSet & seenProcs)407 static std::string GetSeenProcs(
408 const semantics::UnorderedSymbolSet &seenProcs) {
409 // Sort the symbols so that they appear in the same order on all platforms
410 auto ordered{semantics::OrderBySourcePosition(seenProcs)};
411 std::string result;
412 llvm::interleave(
413 ordered,
414 [&](const SymbolRef p) { result += '\'' + p->name().ToString() + '\''; },
415 [&]() { result += ", "; });
416 return result;
417 }
418
419 // These functions with arguments of type UnorderedSymbolSet are used with
420 // mutually recursive calls when characterizing a Procedure, a DummyArgument,
421 // or a DummyProcedure to detect circularly defined procedures as required by
422 // 15.4.3.6, paragraph 2.
423 static std::optional<DummyArgument> CharacterizeDummyArgument(
424 const semantics::Symbol &symbol, FoldingContext &context,
425 semantics::UnorderedSymbolSet seenProcs);
426 static std::optional<FunctionResult> CharacterizeFunctionResult(
427 const semantics::Symbol &symbol, FoldingContext &context,
428 semantics::UnorderedSymbolSet seenProcs);
429
CharacterizeProcedure(const semantics::Symbol & original,FoldingContext & context,semantics::UnorderedSymbolSet seenProcs)430 static std::optional<Procedure> CharacterizeProcedure(
431 const semantics::Symbol &original, FoldingContext &context,
432 semantics::UnorderedSymbolSet seenProcs) {
433 Procedure result;
434 const auto &symbol{ResolveAssociations(original)};
435 if (seenProcs.find(symbol) != seenProcs.end()) {
436 std::string procsList{GetSeenProcs(seenProcs)};
437 context.messages().Say(symbol.name(),
438 "Procedure '%s' is recursively defined. Procedures in the cycle:"
439 " %s"_err_en_US,
440 symbol.name(), procsList);
441 return std::nullopt;
442 }
443 seenProcs.insert(symbol);
444 if (IsElementalProcedure(symbol)) {
445 result.attrs.set(Procedure::Attr::Elemental);
446 }
447 CopyAttrs<Procedure, Procedure::Attr>(symbol, result,
448 {
449 {semantics::Attr::BIND_C, Procedure::Attr::BindC},
450 });
451 if (IsPureProcedure(symbol) || // works for ENTRY too
452 (!symbol.attrs().test(semantics::Attr::IMPURE) &&
453 result.attrs.test(Procedure::Attr::Elemental))) {
454 result.attrs.set(Procedure::Attr::Pure);
455 }
456 return common::visit(
457 common::visitors{
458 [&](const semantics::SubprogramDetails &subp)
459 -> std::optional<Procedure> {
460 if (subp.isFunction()) {
461 if (auto fr{CharacterizeFunctionResult(
462 subp.result(), context, seenProcs)}) {
463 result.functionResult = std::move(fr);
464 } else {
465 return std::nullopt;
466 }
467 } else {
468 result.attrs.set(Procedure::Attr::Subroutine);
469 }
470 for (const semantics::Symbol *arg : subp.dummyArgs()) {
471 if (!arg) {
472 if (subp.isFunction()) {
473 return std::nullopt;
474 } else {
475 result.dummyArguments.emplace_back(AlternateReturn{});
476 }
477 } else if (auto argCharacteristics{CharacterizeDummyArgument(
478 *arg, context, seenProcs)}) {
479 result.dummyArguments.emplace_back(
480 std::move(argCharacteristics.value()));
481 } else {
482 return std::nullopt;
483 }
484 }
485 return result;
486 },
487 [&](const semantics::ProcEntityDetails &proc)
488 -> std::optional<Procedure> {
489 if (symbol.attrs().test(semantics::Attr::INTRINSIC)) {
490 // Fails when the intrinsic is not a specific intrinsic function
491 // from F'2018 table 16.2. In order to handle forward references,
492 // attempts to use impermissible intrinsic procedures as the
493 // interfaces of procedure pointers are caught and flagged in
494 // declaration checking in Semantics.
495 auto intrinsic{context.intrinsics().IsSpecificIntrinsicFunction(
496 symbol.name().ToString())};
497 if (intrinsic && intrinsic->isRestrictedSpecific) {
498 intrinsic.reset(); // Exclude intrinsics from table 16.3.
499 }
500 return intrinsic;
501 }
502 const semantics::ProcInterface &interface { proc.interface() };
503 if (const semantics::Symbol * interfaceSymbol{interface.symbol()}) {
504 auto interface {
505 CharacterizeProcedure(*interfaceSymbol, context, seenProcs)
506 };
507 if (interface && IsPointer(symbol)) {
508 interface->attrs.reset(Procedure::Attr::Elemental);
509 }
510 return interface;
511 } else {
512 result.attrs.set(Procedure::Attr::ImplicitInterface);
513 const semantics::DeclTypeSpec *type{interface.type()};
514 if (symbol.test(semantics::Symbol::Flag::Subroutine)) {
515 // ignore any implicit typing
516 result.attrs.set(Procedure::Attr::Subroutine);
517 } else if (type) {
518 if (auto resultType{DynamicType::From(*type)}) {
519 result.functionResult = FunctionResult{*resultType};
520 } else {
521 return std::nullopt;
522 }
523 } else if (symbol.test(semantics::Symbol::Flag::Function)) {
524 return std::nullopt;
525 }
526 // The PASS name, if any, is not a characteristic.
527 return result;
528 }
529 },
530 [&](const semantics::ProcBindingDetails &binding) {
531 if (auto result{CharacterizeProcedure(
532 binding.symbol(), context, seenProcs)}) {
533 if (!symbol.attrs().test(semantics::Attr::NOPASS)) {
534 auto passName{binding.passName()};
535 for (auto &dummy : result->dummyArguments) {
536 if (!passName || dummy.name.c_str() == *passName) {
537 dummy.pass = true;
538 return result;
539 }
540 }
541 DIE("PASS argument missing");
542 }
543 return result;
544 } else {
545 return std::optional<Procedure>{};
546 }
547 },
548 [&](const semantics::UseDetails &use) {
549 return CharacterizeProcedure(use.symbol(), context, seenProcs);
550 },
551 [](const semantics::UseErrorDetails &) {
552 // Ambiguous use-association will be handled later during symbol
553 // checks, ignore UseErrorDetails here without actual symbol usage.
554 return std::optional<Procedure>{};
555 },
556 [&](const semantics::HostAssocDetails &assoc) {
557 return CharacterizeProcedure(assoc.symbol(), context, seenProcs);
558 },
559 [&](const semantics::EntityDetails &) {
560 context.messages().Say(
561 "Procedure '%s' is referenced before being sufficiently defined in a context where it must be so"_err_en_US,
562 symbol.name());
563 return std::optional<Procedure>{};
564 },
565 [&](const semantics::SubprogramNameDetails &) {
566 context.messages().Say(
567 "Procedure '%s' is referenced before being sufficiently defined in a context where it must be so"_err_en_US,
568 symbol.name());
569 return std::optional<Procedure>{};
570 },
571 [&](const auto &) {
572 context.messages().Say(
573 "'%s' is not a procedure"_err_en_US, symbol.name());
574 return std::optional<Procedure>{};
575 },
576 },
577 symbol.details());
578 }
579
CharacterizeDummyProcedure(const semantics::Symbol & symbol,FoldingContext & context,semantics::UnorderedSymbolSet seenProcs)580 static std::optional<DummyProcedure> CharacterizeDummyProcedure(
581 const semantics::Symbol &symbol, FoldingContext &context,
582 semantics::UnorderedSymbolSet seenProcs) {
583 if (auto procedure{CharacterizeProcedure(symbol, context, seenProcs)}) {
584 // Dummy procedures may not be elemental. Elemental dummy procedure
585 // interfaces are errors when the interface is not intrinsic, and that
586 // error is caught elsewhere. Elemental intrinsic interfaces are
587 // made non-elemental.
588 procedure->attrs.reset(Procedure::Attr::Elemental);
589 DummyProcedure result{std::move(procedure.value())};
590 CopyAttrs<DummyProcedure, DummyProcedure::Attr>(symbol, result,
591 {
592 {semantics::Attr::OPTIONAL, DummyProcedure::Attr::Optional},
593 {semantics::Attr::POINTER, DummyProcedure::Attr::Pointer},
594 });
595 result.intent = GetIntent(symbol.attrs());
596 return result;
597 } else {
598 return std::nullopt;
599 }
600 }
601
Dump(llvm::raw_ostream & o) const602 llvm::raw_ostream &DummyProcedure::Dump(llvm::raw_ostream &o) const {
603 attrs.Dump(o, EnumToString);
604 if (intent != common::Intent::Default) {
605 o << "INTENT(" << common::EnumToString(intent) << ')';
606 }
607 procedure.value().Dump(o);
608 return o;
609 }
610
Dump(llvm::raw_ostream & o) const611 llvm::raw_ostream &AlternateReturn::Dump(llvm::raw_ostream &o) const {
612 return o << '*';
613 }
614
~DummyArgument()615 DummyArgument::~DummyArgument() {}
616
operator ==(const DummyArgument & that) const617 bool DummyArgument::operator==(const DummyArgument &that) const {
618 return u == that.u; // name and passed-object usage are not characteristics
619 }
620
IsCompatibleWith(const DummyArgument & actual,std::string * whyNot) const621 bool DummyArgument::IsCompatibleWith(
622 const DummyArgument &actual, std::string *whyNot) const {
623 if (const auto *ifaceData{std::get_if<DummyDataObject>(&u)}) {
624 if (const auto *actualData{std::get_if<DummyDataObject>(&actual.u)}) {
625 return ifaceData->IsCompatibleWith(*actualData, whyNot);
626 }
627 if (whyNot) {
628 *whyNot = "one dummy argument is an object, the other is not";
629 }
630 } else if (const auto *ifaceProc{std::get_if<DummyProcedure>(&u)}) {
631 if (const auto *actualProc{std::get_if<DummyProcedure>(&actual.u)}) {
632 return ifaceProc->IsCompatibleWith(*actualProc, whyNot);
633 }
634 if (whyNot) {
635 *whyNot = "one dummy argument is a procedure, the other is not";
636 }
637 } else {
638 CHECK(std::holds_alternative<AlternateReturn>(u));
639 if (std::holds_alternative<AlternateReturn>(actual.u)) {
640 return true;
641 }
642 if (whyNot) {
643 *whyNot = "one dummy argument is an alternate return, the other is not";
644 }
645 }
646 return false;
647 }
648
CharacterizeDummyArgument(const semantics::Symbol & symbol,FoldingContext & context,semantics::UnorderedSymbolSet seenProcs)649 static std::optional<DummyArgument> CharacterizeDummyArgument(
650 const semantics::Symbol &symbol, FoldingContext &context,
651 semantics::UnorderedSymbolSet seenProcs) {
652 auto name{symbol.name().ToString()};
653 if (symbol.has<semantics::ObjectEntityDetails>() ||
654 symbol.has<semantics::EntityDetails>()) {
655 if (auto obj{DummyDataObject::Characterize(symbol, context)}) {
656 return DummyArgument{std::move(name), std::move(obj.value())};
657 }
658 } else if (auto proc{
659 CharacterizeDummyProcedure(symbol, context, seenProcs)}) {
660 return DummyArgument{std::move(name), std::move(proc.value())};
661 }
662 return std::nullopt;
663 }
664
FromActual(std::string && name,const Expr<SomeType> & expr,FoldingContext & context)665 std::optional<DummyArgument> DummyArgument::FromActual(
666 std::string &&name, const Expr<SomeType> &expr, FoldingContext &context) {
667 return common::visit(
668 common::visitors{
669 [&](const BOZLiteralConstant &) {
670 return std::make_optional<DummyArgument>(std::move(name),
671 DummyDataObject{
672 TypeAndShape{DynamicType::TypelessIntrinsicArgument()}});
673 },
674 [&](const NullPointer &) {
675 return std::make_optional<DummyArgument>(std::move(name),
676 DummyDataObject{
677 TypeAndShape{DynamicType::TypelessIntrinsicArgument()}});
678 },
679 [&](const ProcedureDesignator &designator) {
680 if (auto proc{Procedure::Characterize(designator, context)}) {
681 return std::make_optional<DummyArgument>(
682 std::move(name), DummyProcedure{std::move(*proc)});
683 } else {
684 return std::optional<DummyArgument>{};
685 }
686 },
687 [&](const ProcedureRef &call) {
688 if (auto proc{Procedure::Characterize(call, context)}) {
689 return std::make_optional<DummyArgument>(
690 std::move(name), DummyProcedure{std::move(*proc)});
691 } else {
692 return std::optional<DummyArgument>{};
693 }
694 },
695 [&](const auto &) {
696 if (auto type{TypeAndShape::Characterize(expr, context)}) {
697 return std::make_optional<DummyArgument>(
698 std::move(name), DummyDataObject{std::move(*type)});
699 } else {
700 return std::optional<DummyArgument>{};
701 }
702 },
703 },
704 expr.u);
705 }
706
IsOptional() const707 bool DummyArgument::IsOptional() const {
708 return common::visit(
709 common::visitors{
710 [](const DummyDataObject &data) {
711 return data.attrs.test(DummyDataObject::Attr::Optional);
712 },
713 [](const DummyProcedure &proc) {
714 return proc.attrs.test(DummyProcedure::Attr::Optional);
715 },
716 [](const AlternateReturn &) { return false; },
717 },
718 u);
719 }
720
SetOptional(bool value)721 void DummyArgument::SetOptional(bool value) {
722 common::visit(common::visitors{
723 [value](DummyDataObject &data) {
724 data.attrs.set(DummyDataObject::Attr::Optional, value);
725 },
726 [value](DummyProcedure &proc) {
727 proc.attrs.set(DummyProcedure::Attr::Optional, value);
728 },
729 [](AlternateReturn &) { DIE("cannot set optional"); },
730 },
731 u);
732 }
733
SetIntent(common::Intent intent)734 void DummyArgument::SetIntent(common::Intent intent) {
735 common::visit(common::visitors{
736 [intent](DummyDataObject &data) { data.intent = intent; },
737 [intent](DummyProcedure &proc) { proc.intent = intent; },
738 [](AlternateReturn &) { DIE("cannot set intent"); },
739 },
740 u);
741 }
742
GetIntent() const743 common::Intent DummyArgument::GetIntent() const {
744 return common::visit(
745 common::visitors{
746 [](const DummyDataObject &data) { return data.intent; },
747 [](const DummyProcedure &proc) { return proc.intent; },
748 [](const AlternateReturn &) -> common::Intent {
749 DIE("Alternate returns have no intent");
750 },
751 },
752 u);
753 }
754
CanBePassedViaImplicitInterface() const755 bool DummyArgument::CanBePassedViaImplicitInterface() const {
756 if (const auto *object{std::get_if<DummyDataObject>(&u)}) {
757 return object->CanBePassedViaImplicitInterface();
758 } else {
759 return true;
760 }
761 }
762
IsTypelessIntrinsicDummy() const763 bool DummyArgument::IsTypelessIntrinsicDummy() const {
764 const auto *argObj{std::get_if<characteristics::DummyDataObject>(&u)};
765 return argObj && argObj->type.type().IsTypelessIntrinsicArgument();
766 }
767
Dump(llvm::raw_ostream & o) const768 llvm::raw_ostream &DummyArgument::Dump(llvm::raw_ostream &o) const {
769 if (!name.empty()) {
770 o << name << '=';
771 }
772 if (pass) {
773 o << " PASS";
774 }
775 common::visit([&](const auto &x) { x.Dump(o); }, u);
776 return o;
777 }
778
FunctionResult(DynamicType t)779 FunctionResult::FunctionResult(DynamicType t) : u{TypeAndShape{t}} {}
FunctionResult(TypeAndShape && t)780 FunctionResult::FunctionResult(TypeAndShape &&t) : u{std::move(t)} {}
FunctionResult(Procedure && p)781 FunctionResult::FunctionResult(Procedure &&p) : u{std::move(p)} {}
~FunctionResult()782 FunctionResult::~FunctionResult() {}
783
operator ==(const FunctionResult & that) const784 bool FunctionResult::operator==(const FunctionResult &that) const {
785 return attrs == that.attrs && u == that.u;
786 }
787
CharacterizeFunctionResult(const semantics::Symbol & symbol,FoldingContext & context,semantics::UnorderedSymbolSet seenProcs)788 static std::optional<FunctionResult> CharacterizeFunctionResult(
789 const semantics::Symbol &symbol, FoldingContext &context,
790 semantics::UnorderedSymbolSet seenProcs) {
791 if (symbol.has<semantics::ObjectEntityDetails>()) {
792 if (auto type{TypeAndShape::Characterize(symbol, context)}) {
793 FunctionResult result{std::move(*type)};
794 CopyAttrs<FunctionResult, FunctionResult::Attr>(symbol, result,
795 {
796 {semantics::Attr::ALLOCATABLE, FunctionResult::Attr::Allocatable},
797 {semantics::Attr::CONTIGUOUS, FunctionResult::Attr::Contiguous},
798 {semantics::Attr::POINTER, FunctionResult::Attr::Pointer},
799 });
800 return result;
801 }
802 } else if (auto maybeProc{
803 CharacterizeProcedure(symbol, context, seenProcs)}) {
804 FunctionResult result{std::move(*maybeProc)};
805 result.attrs.set(FunctionResult::Attr::Pointer);
806 return result;
807 }
808 return std::nullopt;
809 }
810
Characterize(const Symbol & symbol,FoldingContext & context)811 std::optional<FunctionResult> FunctionResult::Characterize(
812 const Symbol &symbol, FoldingContext &context) {
813 semantics::UnorderedSymbolSet seenProcs;
814 return CharacterizeFunctionResult(symbol, context, seenProcs);
815 }
816
IsAssumedLengthCharacter() const817 bool FunctionResult::IsAssumedLengthCharacter() const {
818 if (const auto *ts{std::get_if<TypeAndShape>(&u)}) {
819 return ts->type().IsAssumedLengthCharacter();
820 } else {
821 return false;
822 }
823 }
824
CanBeReturnedViaImplicitInterface() const825 bool FunctionResult::CanBeReturnedViaImplicitInterface() const {
826 if (attrs.test(Attr::Pointer) || attrs.test(Attr::Allocatable)) {
827 return false; // 15.4.2.2(4)(b)
828 } else if (const auto *typeAndShape{GetTypeAndShape()}) {
829 if (typeAndShape->Rank() > 0) {
830 return false; // 15.4.2.2(4)(a)
831 } else {
832 const DynamicType &type{typeAndShape->type()};
833 switch (type.category()) {
834 case TypeCategory::Character:
835 if (type.knownLength()) {
836 return true;
837 } else if (const auto *param{type.charLengthParamValue()}) {
838 if (const auto &expr{param->GetExplicit()}) {
839 return IsConstantExpr(*expr); // 15.4.2.2(4)(c)
840 } else if (param->isAssumed()) {
841 return true;
842 }
843 }
844 return false;
845 case TypeCategory::Derived:
846 if (!type.IsPolymorphic()) {
847 const auto &spec{type.GetDerivedTypeSpec()};
848 for (const auto &pair : spec.parameters()) {
849 if (const auto &expr{pair.second.GetExplicit()}) {
850 if (!IsConstantExpr(*expr)) {
851 return false; // 15.4.2.2(4)(c)
852 }
853 }
854 }
855 return true;
856 }
857 return false;
858 default:
859 return true;
860 }
861 }
862 } else {
863 return false; // 15.4.2.2(4)(b) - procedure pointer
864 }
865 }
866
IsCompatibleWith(const FunctionResult & actual,std::string * whyNot) const867 bool FunctionResult::IsCompatibleWith(
868 const FunctionResult &actual, std::string *whyNot) const {
869 Attrs actualAttrs{actual.attrs};
870 if (!attrs.test(Attr::Contiguous)) {
871 actualAttrs.reset(Attr::Contiguous);
872 }
873 if (attrs != actualAttrs) {
874 if (whyNot) {
875 *whyNot = "function results have incompatible attributes";
876 }
877 } else if (const auto *ifaceTypeShape{std::get_if<TypeAndShape>(&u)}) {
878 if (const auto *actualTypeShape{std::get_if<TypeAndShape>(&actual.u)}) {
879 if (ifaceTypeShape->Rank() != actualTypeShape->Rank()) {
880 if (whyNot) {
881 *whyNot = "function results have distinct ranks";
882 }
883 } else if (!attrs.test(Attr::Allocatable) && !attrs.test(Attr::Pointer) &&
884 ifaceTypeShape->shape() != actualTypeShape->shape()) {
885 if (whyNot) {
886 *whyNot = "function results have distinct extents";
887 }
888 } else if (!ifaceTypeShape->type().IsTkCompatibleWith(
889 actualTypeShape->type())) {
890 if (whyNot) {
891 *whyNot = "function results have incompatible types: "s +
892 ifaceTypeShape->type().AsFortran() + " vs "s +
893 actualTypeShape->type().AsFortran();
894 }
895 } else {
896 return true;
897 }
898 } else {
899 if (whyNot) {
900 *whyNot = "function result type and shape are not known";
901 }
902 }
903 } else {
904 const auto *ifaceProc{std::get_if<CopyableIndirection<Procedure>>(&u)};
905 CHECK(ifaceProc != nullptr);
906 if (const auto *actualProc{
907 std::get_if<CopyableIndirection<Procedure>>(&actual.u)}) {
908 if (ifaceProc->value().IsCompatibleWith(actualProc->value(), whyNot)) {
909 return true;
910 }
911 if (whyNot) {
912 *whyNot =
913 "function results are incompatible procedure pointers: "s + *whyNot;
914 }
915 } else {
916 if (whyNot) {
917 *whyNot =
918 "one function result is a procedure pointer, the other is not";
919 }
920 }
921 }
922 return false;
923 }
924
Dump(llvm::raw_ostream & o) const925 llvm::raw_ostream &FunctionResult::Dump(llvm::raw_ostream &o) const {
926 attrs.Dump(o, EnumToString);
927 common::visit(common::visitors{
928 [&](const TypeAndShape &ts) { ts.Dump(o); },
929 [&](const CopyableIndirection<Procedure> &p) {
930 p.value().Dump(o << " procedure(") << ')';
931 },
932 },
933 u);
934 return o;
935 }
936
Procedure(FunctionResult && fr,DummyArguments && args,Attrs a)937 Procedure::Procedure(FunctionResult &&fr, DummyArguments &&args, Attrs a)
938 : functionResult{std::move(fr)}, dummyArguments{std::move(args)}, attrs{a} {
939 }
Procedure(DummyArguments && args,Attrs a)940 Procedure::Procedure(DummyArguments &&args, Attrs a)
941 : dummyArguments{std::move(args)}, attrs{a} {}
~Procedure()942 Procedure::~Procedure() {}
943
operator ==(const Procedure & that) const944 bool Procedure::operator==(const Procedure &that) const {
945 return attrs == that.attrs && functionResult == that.functionResult &&
946 dummyArguments == that.dummyArguments;
947 }
948
IsCompatibleWith(const Procedure & actual,std::string * whyNot,const SpecificIntrinsic * specificIntrinsic) const949 bool Procedure::IsCompatibleWith(const Procedure &actual, std::string *whyNot,
950 const SpecificIntrinsic *specificIntrinsic) const {
951 // 15.5.2.9(1): if dummy is not pure, actual need not be.
952 // Ditto with elemental.
953 Attrs actualAttrs{actual.attrs};
954 if (!attrs.test(Attr::Pure)) {
955 actualAttrs.reset(Attr::Pure);
956 }
957 if (!attrs.test(Attr::Elemental) && specificIntrinsic) {
958 actualAttrs.reset(Attr::Elemental);
959 }
960 Attrs differences{attrs ^ actualAttrs};
961 differences.reset(Attr::Subroutine); // dealt with specifically later
962 if (!differences.empty()) {
963 if (whyNot) {
964 auto sep{": "s};
965 *whyNot = "incompatible procedure attributes";
966 differences.IterateOverMembers([&](Attr x) {
967 *whyNot += sep + EnumToString(x);
968 sep = ", ";
969 });
970 }
971 } else if ((IsFunction() && actual.IsSubroutine()) ||
972 (IsSubroutine() && actual.IsFunction())) {
973 if (whyNot) {
974 *whyNot =
975 "incompatible procedures: one is a function, the other a subroutine";
976 }
977 } else if (functionResult && actual.functionResult &&
978 !functionResult->IsCompatibleWith(*actual.functionResult, whyNot)) {
979 } else if (dummyArguments.size() != actual.dummyArguments.size()) {
980 if (whyNot) {
981 *whyNot = "distinct numbers of dummy arguments";
982 }
983 } else {
984 for (std::size_t j{0}; j < dummyArguments.size(); ++j) {
985 if (!dummyArguments[j].IsCompatibleWith(
986 actual.dummyArguments[j], whyNot)) {
987 if (whyNot) {
988 *whyNot = "incompatible dummy argument #"s + std::to_string(j + 1) +
989 ": "s + *whyNot;
990 }
991 return false;
992 }
993 }
994 return true;
995 }
996 return false;
997 }
998
FindPassIndex(std::optional<parser::CharBlock> name) const999 int Procedure::FindPassIndex(std::optional<parser::CharBlock> name) const {
1000 int argCount{static_cast<int>(dummyArguments.size())};
1001 int index{0};
1002 if (name) {
1003 while (index < argCount && *name != dummyArguments[index].name.c_str()) {
1004 ++index;
1005 }
1006 }
1007 CHECK(index < argCount);
1008 return index;
1009 }
1010
CanOverride(const Procedure & that,std::optional<int> passIndex) const1011 bool Procedure::CanOverride(
1012 const Procedure &that, std::optional<int> passIndex) const {
1013 // A pure procedure may override an impure one (7.5.7.3(2))
1014 if ((that.attrs.test(Attr::Pure) && !attrs.test(Attr::Pure)) ||
1015 that.attrs.test(Attr::Elemental) != attrs.test(Attr::Elemental) ||
1016 functionResult != that.functionResult) {
1017 return false;
1018 }
1019 int argCount{static_cast<int>(dummyArguments.size())};
1020 if (argCount != static_cast<int>(that.dummyArguments.size())) {
1021 return false;
1022 }
1023 for (int j{0}; j < argCount; ++j) {
1024 if ((!passIndex || j != *passIndex) &&
1025 dummyArguments[j] != that.dummyArguments[j]) {
1026 return false;
1027 }
1028 }
1029 return true;
1030 }
1031
Characterize(const semantics::Symbol & original,FoldingContext & context)1032 std::optional<Procedure> Procedure::Characterize(
1033 const semantics::Symbol &original, FoldingContext &context) {
1034 semantics::UnorderedSymbolSet seenProcs;
1035 return CharacterizeProcedure(original, context, seenProcs);
1036 }
1037
Characterize(const ProcedureDesignator & proc,FoldingContext & context)1038 std::optional<Procedure> Procedure::Characterize(
1039 const ProcedureDesignator &proc, FoldingContext &context) {
1040 if (const auto *symbol{proc.GetSymbol()}) {
1041 if (auto result{
1042 characteristics::Procedure::Characterize(*symbol, context)}) {
1043 return result;
1044 }
1045 } else if (const auto *intrinsic{proc.GetSpecificIntrinsic()}) {
1046 return intrinsic->characteristics.value();
1047 }
1048 return std::nullopt;
1049 }
1050
Characterize(const ProcedureRef & ref,FoldingContext & context)1051 std::optional<Procedure> Procedure::Characterize(
1052 const ProcedureRef &ref, FoldingContext &context) {
1053 if (auto callee{Characterize(ref.proc(), context)}) {
1054 if (callee->functionResult) {
1055 if (const Procedure *
1056 proc{callee->functionResult->IsProcedurePointer()}) {
1057 return {*proc};
1058 }
1059 }
1060 }
1061 return std::nullopt;
1062 }
1063
CanBeCalledViaImplicitInterface() const1064 bool Procedure::CanBeCalledViaImplicitInterface() const {
1065 // TODO: Pass back information on why we return false
1066 if (attrs.test(Attr::Elemental) || attrs.test(Attr::BindC)) {
1067 return false; // 15.4.2.2(5,6)
1068 } else if (IsFunction() &&
1069 !functionResult->CanBeReturnedViaImplicitInterface()) {
1070 return false;
1071 } else {
1072 for (const DummyArgument &arg : dummyArguments) {
1073 if (!arg.CanBePassedViaImplicitInterface()) {
1074 return false;
1075 }
1076 }
1077 return true;
1078 }
1079 }
1080
Dump(llvm::raw_ostream & o) const1081 llvm::raw_ostream &Procedure::Dump(llvm::raw_ostream &o) const {
1082 attrs.Dump(o, EnumToString);
1083 if (functionResult) {
1084 functionResult->Dump(o << "TYPE(") << ") FUNCTION";
1085 } else if (attrs.test(Attr::Subroutine)) {
1086 o << "SUBROUTINE";
1087 } else {
1088 o << "EXTERNAL";
1089 }
1090 char sep{'('};
1091 for (const auto &dummy : dummyArguments) {
1092 dummy.Dump(o << sep);
1093 sep = ',';
1094 }
1095 return o << (sep == '(' ? "()" : ")");
1096 }
1097
1098 // Utility class to determine if Procedures, etc. are distinguishable
1099 class DistinguishUtils {
1100 public:
DistinguishUtils(const common::LanguageFeatureControl & features)1101 explicit DistinguishUtils(const common::LanguageFeatureControl &features)
1102 : features_{features} {}
1103
1104 // Are these procedures distinguishable for a generic name?
1105 bool Distinguishable(const Procedure &, const Procedure &) const;
1106 // Are these procedures distinguishable for a generic operator or assignment?
1107 bool DistinguishableOpOrAssign(const Procedure &, const Procedure &) const;
1108
1109 private:
1110 struct CountDummyProcedures {
CountDummyProceduresFortran::evaluate::characteristics::DistinguishUtils::CountDummyProcedures1111 CountDummyProcedures(const DummyArguments &args) {
1112 for (const DummyArgument &arg : args) {
1113 if (std::holds_alternative<DummyProcedure>(arg.u)) {
1114 total += 1;
1115 notOptional += !arg.IsOptional();
1116 }
1117 }
1118 }
1119 int total{0};
1120 int notOptional{0};
1121 };
1122
1123 bool Rule3Distinguishable(const Procedure &, const Procedure &) const;
1124 const DummyArgument *Rule1DistinguishingArg(
1125 const DummyArguments &, const DummyArguments &) const;
1126 int FindFirstToDistinguishByPosition(
1127 const DummyArguments &, const DummyArguments &) const;
1128 int FindLastToDistinguishByName(
1129 const DummyArguments &, const DummyArguments &) const;
1130 int CountCompatibleWith(const DummyArgument &, const DummyArguments &) const;
1131 int CountNotDistinguishableFrom(
1132 const DummyArgument &, const DummyArguments &) const;
1133 bool Distinguishable(const DummyArgument &, const DummyArgument &) const;
1134 bool Distinguishable(const DummyDataObject &, const DummyDataObject &) const;
1135 bool Distinguishable(const DummyProcedure &, const DummyProcedure &) const;
1136 bool Distinguishable(const FunctionResult &, const FunctionResult &) const;
1137 bool Distinguishable(const TypeAndShape &, const TypeAndShape &) const;
1138 bool IsTkrCompatible(const DummyArgument &, const DummyArgument &) const;
1139 bool IsTkrCompatible(const TypeAndShape &, const TypeAndShape &) const;
1140 const DummyArgument *GetAtEffectivePosition(
1141 const DummyArguments &, int) const;
1142 const DummyArgument *GetPassArg(const Procedure &) const;
1143
1144 const common::LanguageFeatureControl &features_;
1145 };
1146
1147 // Simpler distinguishability rules for operators and assignment
DistinguishableOpOrAssign(const Procedure & proc1,const Procedure & proc2) const1148 bool DistinguishUtils::DistinguishableOpOrAssign(
1149 const Procedure &proc1, const Procedure &proc2) const {
1150 auto &args1{proc1.dummyArguments};
1151 auto &args2{proc2.dummyArguments};
1152 if (args1.size() != args2.size()) {
1153 return true; // C1511: distinguishable based on number of arguments
1154 }
1155 for (std::size_t i{0}; i < args1.size(); ++i) {
1156 if (Distinguishable(args1[i], args2[i])) {
1157 return true; // C1511, C1512: distinguishable based on this arg
1158 }
1159 }
1160 return false;
1161 }
1162
Distinguishable(const Procedure & proc1,const Procedure & proc2) const1163 bool DistinguishUtils::Distinguishable(
1164 const Procedure &proc1, const Procedure &proc2) const {
1165 auto &args1{proc1.dummyArguments};
1166 auto &args2{proc2.dummyArguments};
1167 auto count1{CountDummyProcedures(args1)};
1168 auto count2{CountDummyProcedures(args2)};
1169 if (count1.notOptional > count2.total || count2.notOptional > count1.total) {
1170 return true; // distinguishable based on C1514 rule 2
1171 }
1172 if (Rule3Distinguishable(proc1, proc2)) {
1173 return true; // distinguishable based on C1514 rule 3
1174 }
1175 if (Rule1DistinguishingArg(args1, args2)) {
1176 return true; // distinguishable based on C1514 rule 1
1177 }
1178 int pos1{FindFirstToDistinguishByPosition(args1, args2)};
1179 int name1{FindLastToDistinguishByName(args1, args2)};
1180 if (pos1 >= 0 && pos1 <= name1) {
1181 return true; // distinguishable based on C1514 rule 4
1182 }
1183 int pos2{FindFirstToDistinguishByPosition(args2, args1)};
1184 int name2{FindLastToDistinguishByName(args2, args1)};
1185 if (pos2 >= 0 && pos2 <= name2) {
1186 return true; // distinguishable based on C1514 rule 4
1187 }
1188 return false;
1189 }
1190
1191 // C1514 rule 3: Procedures are distinguishable if both have a passed-object
1192 // dummy argument and those are distinguishable.
Rule3Distinguishable(const Procedure & proc1,const Procedure & proc2) const1193 bool DistinguishUtils::Rule3Distinguishable(
1194 const Procedure &proc1, const Procedure &proc2) const {
1195 const DummyArgument *pass1{GetPassArg(proc1)};
1196 const DummyArgument *pass2{GetPassArg(proc2)};
1197 return pass1 && pass2 && Distinguishable(*pass1, *pass2);
1198 }
1199
1200 // Find a non-passed-object dummy data object in one of the argument lists
1201 // that satisfies C1514 rule 1. I.e. x such that:
1202 // - m is the number of dummy data objects in one that are nonoptional,
1203 // are not passed-object, that x is TKR compatible with
1204 // - n is the number of non-passed-object dummy data objects, in the other
1205 // that are not distinguishable from x
1206 // - m is greater than n
Rule1DistinguishingArg(const DummyArguments & args1,const DummyArguments & args2) const1207 const DummyArgument *DistinguishUtils::Rule1DistinguishingArg(
1208 const DummyArguments &args1, const DummyArguments &args2) const {
1209 auto size1{args1.size()};
1210 auto size2{args2.size()};
1211 for (std::size_t i{0}; i < size1 + size2; ++i) {
1212 const DummyArgument &x{i < size1 ? args1[i] : args2[i - size1]};
1213 if (!x.pass && std::holds_alternative<DummyDataObject>(x.u)) {
1214 if (CountCompatibleWith(x, args1) >
1215 CountNotDistinguishableFrom(x, args2) ||
1216 CountCompatibleWith(x, args2) >
1217 CountNotDistinguishableFrom(x, args1)) {
1218 return &x;
1219 }
1220 }
1221 }
1222 return nullptr;
1223 }
1224
1225 // Find the index of the first nonoptional non-passed-object dummy argument
1226 // in args1 at an effective position such that either:
1227 // - args2 has no dummy argument at that effective position
1228 // - the dummy argument at that position is distinguishable from it
FindFirstToDistinguishByPosition(const DummyArguments & args1,const DummyArguments & args2) const1229 int DistinguishUtils::FindFirstToDistinguishByPosition(
1230 const DummyArguments &args1, const DummyArguments &args2) const {
1231 int effective{0}; // position of arg1 in list, ignoring passed arg
1232 for (std::size_t i{0}; i < args1.size(); ++i) {
1233 const DummyArgument &arg1{args1.at(i)};
1234 if (!arg1.pass && !arg1.IsOptional()) {
1235 const DummyArgument *arg2{GetAtEffectivePosition(args2, effective)};
1236 if (!arg2 || Distinguishable(arg1, *arg2)) {
1237 return i;
1238 }
1239 }
1240 effective += !arg1.pass;
1241 }
1242 return -1;
1243 }
1244
1245 // Find the index of the last nonoptional non-passed-object dummy argument
1246 // in args1 whose name is such that either:
1247 // - args2 has no dummy argument with that name
1248 // - the dummy argument with that name is distinguishable from it
FindLastToDistinguishByName(const DummyArguments & args1,const DummyArguments & args2) const1249 int DistinguishUtils::FindLastToDistinguishByName(
1250 const DummyArguments &args1, const DummyArguments &args2) const {
1251 std::map<std::string, const DummyArgument *> nameToArg;
1252 for (const auto &arg2 : args2) {
1253 nameToArg.emplace(arg2.name, &arg2);
1254 }
1255 for (int i = args1.size() - 1; i >= 0; --i) {
1256 const DummyArgument &arg1{args1.at(i)};
1257 if (!arg1.pass && !arg1.IsOptional()) {
1258 auto it{nameToArg.find(arg1.name)};
1259 if (it == nameToArg.end() || Distinguishable(arg1, *it->second)) {
1260 return i;
1261 }
1262 }
1263 }
1264 return -1;
1265 }
1266
1267 // Count the dummy data objects in args that are nonoptional, are not
1268 // passed-object, and that x is TKR compatible with
CountCompatibleWith(const DummyArgument & x,const DummyArguments & args) const1269 int DistinguishUtils::CountCompatibleWith(
1270 const DummyArgument &x, const DummyArguments &args) const {
1271 return std::count_if(args.begin(), args.end(), [&](const DummyArgument &y) {
1272 return !y.pass && !y.IsOptional() && IsTkrCompatible(x, y);
1273 });
1274 }
1275
1276 // Return the number of dummy data objects in args that are not
1277 // distinguishable from x and not passed-object.
CountNotDistinguishableFrom(const DummyArgument & x,const DummyArguments & args) const1278 int DistinguishUtils::CountNotDistinguishableFrom(
1279 const DummyArgument &x, const DummyArguments &args) const {
1280 return std::count_if(args.begin(), args.end(), [&](const DummyArgument &y) {
1281 return !y.pass && std::holds_alternative<DummyDataObject>(y.u) &&
1282 !Distinguishable(y, x);
1283 });
1284 }
1285
Distinguishable(const DummyArgument & x,const DummyArgument & y) const1286 bool DistinguishUtils::Distinguishable(
1287 const DummyArgument &x, const DummyArgument &y) const {
1288 if (x.u.index() != y.u.index()) {
1289 return true; // different kind: data/proc/alt-return
1290 }
1291 return common::visit(
1292 common::visitors{
1293 [&](const DummyDataObject &z) {
1294 return Distinguishable(z, std::get<DummyDataObject>(y.u));
1295 },
1296 [&](const DummyProcedure &z) {
1297 return Distinguishable(z, std::get<DummyProcedure>(y.u));
1298 },
1299 [&](const AlternateReturn &) { return false; },
1300 },
1301 x.u);
1302 }
1303
Distinguishable(const DummyDataObject & x,const DummyDataObject & y) const1304 bool DistinguishUtils::Distinguishable(
1305 const DummyDataObject &x, const DummyDataObject &y) const {
1306 using Attr = DummyDataObject::Attr;
1307 if (Distinguishable(x.type, y.type)) {
1308 return true;
1309 } else if (x.attrs.test(Attr::Allocatable) && y.attrs.test(Attr::Pointer) &&
1310 y.intent != common::Intent::In) {
1311 return true;
1312 } else if (y.attrs.test(Attr::Allocatable) && x.attrs.test(Attr::Pointer) &&
1313 x.intent != common::Intent::In) {
1314 return true;
1315 } else if (features_.IsEnabled(
1316 common::LanguageFeature::DistinguishableSpecifics) &&
1317 (x.attrs.test(Attr::Allocatable) || x.attrs.test(Attr::Pointer)) &&
1318 (y.attrs.test(Attr::Allocatable) || y.attrs.test(Attr::Pointer)) &&
1319 (x.type.type().IsUnlimitedPolymorphic() !=
1320 y.type.type().IsUnlimitedPolymorphic() ||
1321 x.type.type().IsPolymorphic() != y.type.type().IsPolymorphic())) {
1322 // Extension: Per 15.5.2.5(2), an allocatable/pointer dummy and its
1323 // corresponding actual argument must both or neither be polymorphic,
1324 // and must both or neither be unlimited polymorphic. So when exactly
1325 // one of two dummy arguments is polymorphic or unlimited polymorphic,
1326 // any actual argument that is admissible to one of them cannot also match
1327 // the other one.
1328 return true;
1329 } else {
1330 return false;
1331 }
1332 }
1333
Distinguishable(const DummyProcedure & x,const DummyProcedure & y) const1334 bool DistinguishUtils::Distinguishable(
1335 const DummyProcedure &x, const DummyProcedure &y) const {
1336 const Procedure &xProc{x.procedure.value()};
1337 const Procedure &yProc{y.procedure.value()};
1338 if (Distinguishable(xProc, yProc)) {
1339 return true;
1340 } else {
1341 const std::optional<FunctionResult> &xResult{xProc.functionResult};
1342 const std::optional<FunctionResult> &yResult{yProc.functionResult};
1343 return xResult ? !yResult || Distinguishable(*xResult, *yResult)
1344 : yResult.has_value();
1345 }
1346 }
1347
Distinguishable(const FunctionResult & x,const FunctionResult & y) const1348 bool DistinguishUtils::Distinguishable(
1349 const FunctionResult &x, const FunctionResult &y) const {
1350 if (x.u.index() != y.u.index()) {
1351 return true; // one is data object, one is procedure
1352 }
1353 return common::visit(
1354 common::visitors{
1355 [&](const TypeAndShape &z) {
1356 return Distinguishable(z, std::get<TypeAndShape>(y.u));
1357 },
1358 [&](const CopyableIndirection<Procedure> &z) {
1359 return Distinguishable(z.value(),
1360 std::get<CopyableIndirection<Procedure>>(y.u).value());
1361 },
1362 },
1363 x.u);
1364 }
1365
Distinguishable(const TypeAndShape & x,const TypeAndShape & y) const1366 bool DistinguishUtils::Distinguishable(
1367 const TypeAndShape &x, const TypeAndShape &y) const {
1368 return !IsTkrCompatible(x, y) && !IsTkrCompatible(y, x);
1369 }
1370
1371 // Compatibility based on type, kind, and rank
IsTkrCompatible(const DummyArgument & x,const DummyArgument & y) const1372 bool DistinguishUtils::IsTkrCompatible(
1373 const DummyArgument &x, const DummyArgument &y) const {
1374 const auto *obj1{std::get_if<DummyDataObject>(&x.u)};
1375 const auto *obj2{std::get_if<DummyDataObject>(&y.u)};
1376 return obj1 && obj2 && IsTkrCompatible(obj1->type, obj2->type);
1377 }
IsTkrCompatible(const TypeAndShape & x,const TypeAndShape & y) const1378 bool DistinguishUtils::IsTkrCompatible(
1379 const TypeAndShape &x, const TypeAndShape &y) const {
1380 return x.type().IsTkCompatibleWith(y.type()) &&
1381 (x.attrs().test(TypeAndShape::Attr::AssumedRank) ||
1382 y.attrs().test(TypeAndShape::Attr::AssumedRank) ||
1383 x.Rank() == y.Rank());
1384 }
1385
1386 // Return the argument at the given index, ignoring the passed arg
GetAtEffectivePosition(const DummyArguments & args,int index) const1387 const DummyArgument *DistinguishUtils::GetAtEffectivePosition(
1388 const DummyArguments &args, int index) const {
1389 for (const DummyArgument &arg : args) {
1390 if (!arg.pass) {
1391 if (index == 0) {
1392 return &arg;
1393 }
1394 --index;
1395 }
1396 }
1397 return nullptr;
1398 }
1399
1400 // Return the passed-object dummy argument of this procedure, if any
GetPassArg(const Procedure & proc) const1401 const DummyArgument *DistinguishUtils::GetPassArg(const Procedure &proc) const {
1402 for (const auto &arg : proc.dummyArguments) {
1403 if (arg.pass) {
1404 return &arg;
1405 }
1406 }
1407 return nullptr;
1408 }
1409
Distinguishable(const common::LanguageFeatureControl & features,const Procedure & x,const Procedure & y)1410 bool Distinguishable(const common::LanguageFeatureControl &features,
1411 const Procedure &x, const Procedure &y) {
1412 return DistinguishUtils{features}.Distinguishable(x, y);
1413 }
1414
DistinguishableOpOrAssign(const common::LanguageFeatureControl & features,const Procedure & x,const Procedure & y)1415 bool DistinguishableOpOrAssign(const common::LanguageFeatureControl &features,
1416 const Procedure &x, const Procedure &y) {
1417 return DistinguishUtils{features}.DistinguishableOpOrAssign(x, y);
1418 }
1419
1420 DEFINE_DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(DummyArgument)
1421 DEFINE_DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(DummyProcedure)
1422 DEFINE_DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(FunctionResult)
1423 DEFINE_DEFAULT_CONSTRUCTORS_AND_ASSIGNMENTS(Procedure)
1424 } // namespace Fortran::evaluate::characteristics
1425
1426 template class Fortran::common::Indirection<
1427 Fortran::evaluate::characteristics::Procedure, true>;
1428