1 //===-- lib/Semantics/symbol.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/Semantics/symbol.h"
10 #include "flang/Common/idioms.h"
11 #include "flang/Evaluate/expression.h"
12 #include "flang/Semantics/scope.h"
13 #include "flang/Semantics/semantics.h"
14 #include "flang/Semantics/tools.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include <string>
17 
18 namespace Fortran::semantics {
19 
20 template <typename T>
21 static void DumpOptional(llvm::raw_ostream &os, const char *label, const T &x) {
22   if (x) {
23     os << ' ' << label << ':' << *x;
24   }
25 }
26 template <typename T>
27 static void DumpExpr(llvm::raw_ostream &os, const char *label,
28     const std::optional<evaluate::Expr<T>> &x) {
29   if (x) {
30     x->AsFortran(os << ' ' << label << ':');
31   }
32 }
33 
34 static void DumpBool(llvm::raw_ostream &os, const char *label, bool x) {
35   if (x) {
36     os << ' ' << label;
37   }
38 }
39 
40 static void DumpSymbolVector(llvm::raw_ostream &os, const SymbolVector &list) {
41   char sep{' '};
42   for (const Symbol &elem : list) {
43     os << sep << elem.name();
44     sep = ',';
45   }
46 }
47 
48 static void DumpType(llvm::raw_ostream &os, const Symbol &symbol) {
49   if (const auto *type{symbol.GetType()}) {
50     os << *type << ' ';
51   }
52 }
53 static void DumpType(llvm::raw_ostream &os, const DeclTypeSpec *type) {
54   if (type) {
55     os << ' ' << *type;
56   }
57 }
58 
59 template <typename T>
60 static void DumpList(llvm::raw_ostream &os, const char *label, const T &list) {
61   if (!list.empty()) {
62     os << ' ' << label << ':';
63     char sep{' '};
64     for (const auto &elem : list) {
65       os << sep << elem;
66       sep = ',';
67     }
68   }
69 }
70 
71 const Scope *ModuleDetails::parent() const {
72   return isSubmodule_ && scope_ ? &scope_->parent() : nullptr;
73 }
74 const Scope *ModuleDetails::ancestor() const {
75   return isSubmodule_ && scope_ ? FindModuleContaining(*scope_) : nullptr;
76 }
77 void ModuleDetails::set_scope(const Scope *scope) {
78   CHECK(!scope_);
79   bool scopeIsSubmodule{scope->parent().kind() == Scope::Kind::Module};
80   CHECK(isSubmodule_ == scopeIsSubmodule);
81   scope_ = scope;
82 }
83 
84 llvm::raw_ostream &operator<<(
85     llvm::raw_ostream &os, const SubprogramDetails &x) {
86   DumpBool(os, "isInterface", x.isInterface_);
87   DumpExpr(os, "bindName", x.bindName_);
88   if (x.result_) {
89     DumpType(os << " result:", x.result());
90     os << x.result_->name();
91     if (!x.result_->attrs().empty()) {
92       os << ", " << x.result_->attrs();
93     }
94   }
95   if (x.entryScope_) {
96     os << " entry";
97     if (x.entryScope_->symbol()) {
98       os << " in " << x.entryScope_->symbol()->name();
99     }
100   }
101   char sep{'('};
102   os << ' ';
103   for (const Symbol *arg : x.dummyArgs_) {
104     os << sep;
105     sep = ',';
106     if (arg) {
107       DumpType(os, *arg);
108       os << arg->name();
109     } else {
110       os << '*';
111     }
112   }
113   os << (sep == '(' ? "()" : ")");
114   if (x.stmtFunction_) {
115     os << " -> " << x.stmtFunction_->AsFortran();
116   }
117   return os;
118 }
119 
120 void EntityDetails::set_type(const DeclTypeSpec &type) {
121   CHECK(!type_);
122   type_ = &type;
123 }
124 
125 void AssocEntityDetails::set_rank(int rank) { rank_ = rank; }
126 void EntityDetails::ReplaceType(const DeclTypeSpec &type) { type_ = &type; }
127 
128 void ObjectEntityDetails::set_shape(const ArraySpec &shape) {
129   CHECK(shape_.empty());
130   for (const auto &shapeSpec : shape) {
131     shape_.push_back(shapeSpec);
132   }
133 }
134 void ObjectEntityDetails::set_coshape(const ArraySpec &coshape) {
135   CHECK(coshape_.empty());
136   for (const auto &shapeSpec : coshape) {
137     coshape_.push_back(shapeSpec);
138   }
139 }
140 
141 ProcEntityDetails::ProcEntityDetails(EntityDetails &&d) : EntityDetails(d) {
142   if (type()) {
143     interface_.set_type(*type());
144   }
145 }
146 
147 UseErrorDetails::UseErrorDetails(const UseDetails &useDetails) {
148   add_occurrence(useDetails.location(), *GetUsedModule(useDetails).scope());
149 }
150 UseErrorDetails &UseErrorDetails::add_occurrence(
151     const SourceName &location, const Scope &module) {
152   occurrences_.push_back(std::make_pair(location, &module));
153   return *this;
154 }
155 
156 void GenericDetails::AddSpecificProc(
157     const Symbol &proc, SourceName bindingName) {
158   specificProcs_.push_back(proc);
159   bindingNames_.push_back(bindingName);
160 }
161 void GenericDetails::set_specific(Symbol &specific) {
162   CHECK(!specific_);
163   CHECK(!derivedType_);
164   specific_ = &specific;
165 }
166 void GenericDetails::set_derivedType(Symbol &derivedType) {
167   CHECK(!specific_);
168   CHECK(!derivedType_);
169   derivedType_ = &derivedType;
170 }
171 
172 const Symbol *GenericDetails::CheckSpecific() const {
173   return const_cast<GenericDetails *>(this)->CheckSpecific();
174 }
175 Symbol *GenericDetails::CheckSpecific() {
176   if (specific_) {
177     for (const Symbol &proc : specificProcs_) {
178       if (&proc == specific_) {
179         return nullptr;
180       }
181     }
182     return specific_;
183   } else {
184     return nullptr;
185   }
186 }
187 
188 void GenericDetails::CopyFrom(const GenericDetails &from) {
189   CHECK(specificProcs_.size() == bindingNames_.size());
190   CHECK(from.specificProcs_.size() == from.bindingNames_.size());
191   if (from.specific_) {
192     CHECK(!specific_ || specific_ == from.specific_);
193     specific_ = from.specific_;
194   }
195   if (from.derivedType_) {
196     CHECK(!derivedType_ || derivedType_ == from.derivedType_);
197     derivedType_ = from.derivedType_;
198   }
199   for (std::size_t i{0}; i < from.specificProcs_.size(); ++i) {
200     if (std::find_if(specificProcs_.begin(), specificProcs_.end(),
201             [&](const Symbol &mySymbol) {
202               return &mySymbol == &*from.specificProcs_[i];
203             }) == specificProcs_.end()) {
204       specificProcs_.push_back(from.specificProcs_[i]);
205       bindingNames_.push_back(from.bindingNames_[i]);
206     }
207   }
208 }
209 
210 // The name of the kind of details for this symbol.
211 // This is primarily for debugging.
212 std::string DetailsToString(const Details &details) {
213   return std::visit(
214       common::visitors{
215           [](const UnknownDetails &) { return "Unknown"; },
216           [](const MainProgramDetails &) { return "MainProgram"; },
217           [](const ModuleDetails &) { return "Module"; },
218           [](const SubprogramDetails &) { return "Subprogram"; },
219           [](const SubprogramNameDetails &) { return "SubprogramName"; },
220           [](const EntityDetails &) { return "Entity"; },
221           [](const ObjectEntityDetails &) { return "ObjectEntity"; },
222           [](const ProcEntityDetails &) { return "ProcEntity"; },
223           [](const DerivedTypeDetails &) { return "DerivedType"; },
224           [](const UseDetails &) { return "Use"; },
225           [](const UseErrorDetails &) { return "UseError"; },
226           [](const HostAssocDetails &) { return "HostAssoc"; },
227           [](const GenericDetails &) { return "Generic"; },
228           [](const ProcBindingDetails &) { return "ProcBinding"; },
229           [](const NamelistDetails &) { return "Namelist"; },
230           [](const CommonBlockDetails &) { return "CommonBlockDetails"; },
231           [](const FinalProcDetails &) { return "FinalProc"; },
232           [](const TypeParamDetails &) { return "TypeParam"; },
233           [](const MiscDetails &) { return "Misc"; },
234           [](const AssocEntityDetails &) { return "AssocEntity"; },
235       },
236       details);
237 }
238 
239 const std::string Symbol::GetDetailsName() const {
240   return DetailsToString(details_);
241 }
242 
243 void Symbol::set_details(Details &&details) {
244   CHECK(CanReplaceDetails(details));
245   details_ = std::move(details);
246 }
247 
248 bool Symbol::CanReplaceDetails(const Details &details) const {
249   if (has<UnknownDetails>()) {
250     return true; // can always replace UnknownDetails
251   } else {
252     return std::visit(
253         common::visitors{
254             [](const UseErrorDetails &) { return true; },
255             [&](const ObjectEntityDetails &) { return has<EntityDetails>(); },
256             [&](const ProcEntityDetails &) { return has<EntityDetails>(); },
257             [&](const SubprogramDetails &) {
258               return has<SubprogramNameDetails>() || has<EntityDetails>();
259             },
260             [&](const DerivedTypeDetails &) {
261               auto *derived{detailsIf<DerivedTypeDetails>()};
262               return derived && derived->isForwardReferenced();
263             },
264             [](const auto &) { return false; },
265         },
266         details);
267   }
268 }
269 
270 // Usually a symbol's name is the first occurrence in the source, but sometimes
271 // we want to replace it with one at a different location (but same characters).
272 void Symbol::ReplaceName(const SourceName &name) {
273   CHECK(name == name_);
274   name_ = name;
275 }
276 
277 void Symbol::SetType(const DeclTypeSpec &type) {
278   std::visit(common::visitors{
279                  [&](EntityDetails &x) { x.set_type(type); },
280                  [&](ObjectEntityDetails &x) { x.set_type(type); },
281                  [&](AssocEntityDetails &x) { x.set_type(type); },
282                  [&](ProcEntityDetails &x) { x.interface().set_type(type); },
283                  [&](TypeParamDetails &x) { x.set_type(type); },
284                  [](auto &) {},
285              },
286       details_);
287 }
288 
289 bool Symbol::IsFuncResult() const {
290   return std::visit(
291       common::visitors{[](const EntityDetails &x) { return x.isFuncResult(); },
292           [](const ObjectEntityDetails &x) { return x.isFuncResult(); },
293           [](const ProcEntityDetails &x) { return x.isFuncResult(); },
294           [](const HostAssocDetails &x) { return x.symbol().IsFuncResult(); },
295           [](const auto &) { return false; }},
296       details_);
297 }
298 
299 bool Symbol::IsObjectArray() const {
300   const auto *details{std::get_if<ObjectEntityDetails>(&details_)};
301   return details && details->IsArray();
302 }
303 
304 bool Symbol::IsSubprogram() const {
305   return std::visit(
306       common::visitors{
307           [](const SubprogramDetails &) { return true; },
308           [](const SubprogramNameDetails &) { return true; },
309           [](const GenericDetails &) { return true; },
310           [](const UseDetails &x) { return x.symbol().IsSubprogram(); },
311           [](const auto &) { return false; },
312       },
313       details_);
314 }
315 
316 bool Symbol::IsFromModFile() const {
317   return test(Flag::ModFile) ||
318       (!owner_->IsGlobal() && owner_->symbol()->IsFromModFile());
319 }
320 
321 ObjectEntityDetails::ObjectEntityDetails(EntityDetails &&d)
322     : EntityDetails(d) {}
323 
324 llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const EntityDetails &x) {
325   DumpBool(os, "dummy", x.isDummy());
326   DumpBool(os, "funcResult", x.isFuncResult());
327   if (x.type()) {
328     os << " type: " << *x.type();
329   }
330   DumpExpr(os, "bindName", x.bindName_);
331   return os;
332 }
333 
334 llvm::raw_ostream &operator<<(
335     llvm::raw_ostream &os, const ObjectEntityDetails &x) {
336   os << *static_cast<const EntityDetails *>(&x);
337   DumpList(os, "shape", x.shape());
338   DumpList(os, "coshape", x.coshape());
339   DumpExpr(os, "init", x.init_);
340   return os;
341 }
342 
343 llvm::raw_ostream &operator<<(
344     llvm::raw_ostream &os, const AssocEntityDetails &x) {
345   os << *static_cast<const EntityDetails *>(&x);
346   if (auto assocRank{x.rank()}) {
347     os << " rank: " << *assocRank;
348   }
349   DumpExpr(os, "expr", x.expr());
350   return os;
351 }
352 
353 llvm::raw_ostream &operator<<(
354     llvm::raw_ostream &os, const ProcEntityDetails &x) {
355   if (auto *symbol{x.interface_.symbol()}) {
356     os << ' ' << symbol->name();
357   } else {
358     DumpType(os, x.interface_.type());
359   }
360   DumpExpr(os, "bindName", x.bindName());
361   DumpOptional(os, "passName", x.passName());
362   if (x.init()) {
363     if (const Symbol * target{*x.init()}) {
364       os << " => " << target->name();
365     } else {
366       os << " => NULL()";
367     }
368   }
369   return os;
370 }
371 
372 llvm::raw_ostream &operator<<(
373     llvm::raw_ostream &os, const DerivedTypeDetails &x) {
374   DumpBool(os, "sequence", x.sequence_);
375   DumpList(os, "components", x.componentNames_);
376   return os;
377 }
378 
379 llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Details &details) {
380   os << DetailsToString(details);
381   std::visit( //
382       common::visitors{
383           [&](const UnknownDetails &) {},
384           [&](const MainProgramDetails &) {},
385           [&](const ModuleDetails &x) {
386             if (x.isSubmodule()) {
387               os << " (";
388               if (x.ancestor()) {
389                 auto ancestor{x.ancestor()->GetName().value()};
390                 os << ancestor;
391                 if (x.parent()) {
392                   auto parent{x.parent()->GetName().value()};
393                   if (ancestor != parent) {
394                     os << ':' << parent;
395                   }
396                 }
397               }
398               os << ")";
399             }
400           },
401           [&](const SubprogramNameDetails &x) {
402             os << ' ' << EnumToString(x.kind());
403           },
404           [&](const UseDetails &x) {
405             os << " from " << x.symbol().name() << " in "
406                << GetUsedModule(x).name();
407           },
408           [&](const UseErrorDetails &x) {
409             os << " uses:";
410             for (const auto &[location, module] : x.occurrences()) {
411               os << " from " << module->GetName().value() << " at " << location;
412             }
413           },
414           [](const HostAssocDetails &) {},
415           [&](const GenericDetails &x) {
416             os << ' ' << x.kind().ToString();
417             DumpBool(os, "(specific)", x.specific() != nullptr);
418             DumpBool(os, "(derivedType)", x.derivedType() != nullptr);
419             os << " procs:";
420             DumpSymbolVector(os, x.specificProcs());
421           },
422           [&](const ProcBindingDetails &x) {
423             os << " => " << x.symbol().name();
424             DumpOptional(os, "passName", x.passName());
425           },
426           [&](const NamelistDetails &x) {
427             os << ':';
428             DumpSymbolVector(os, x.objects());
429           },
430           [&](const CommonBlockDetails &x) {
431             if (x.alignment()) {
432               os << " alignment=" << x.alignment();
433             }
434             os << ':';
435             for (const auto &object : x.objects()) {
436               os << ' ' << object->name();
437             }
438           },
439           [&](const FinalProcDetails &) {},
440           [&](const TypeParamDetails &x) {
441             DumpOptional(os, "type", x.type());
442             os << ' ' << common::EnumToString(x.attr());
443             DumpExpr(os, "init", x.init());
444           },
445           [&](const MiscDetails &x) {
446             os << ' ' << MiscDetails::EnumToString(x.kind());
447           },
448           [&](const auto &x) { os << x; },
449       },
450       details);
451   return os;
452 }
453 
454 llvm::raw_ostream &operator<<(llvm::raw_ostream &o, Symbol::Flag flag) {
455   return o << Symbol::EnumToString(flag);
456 }
457 
458 llvm::raw_ostream &operator<<(
459     llvm::raw_ostream &o, const Symbol::Flags &flags) {
460   std::size_t n{flags.count()};
461   std::size_t seen{0};
462   for (std::size_t j{0}; seen < n; ++j) {
463     Symbol::Flag flag{static_cast<Symbol::Flag>(j)};
464     if (flags.test(flag)) {
465       if (seen++ > 0) {
466         o << ", ";
467       }
468       o << flag;
469     }
470   }
471   return o;
472 }
473 
474 llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Symbol &symbol) {
475   os << symbol.name();
476   if (!symbol.attrs().empty()) {
477     os << ", " << symbol.attrs();
478   }
479   if (!symbol.flags().empty()) {
480     os << " (" << symbol.flags() << ')';
481   }
482   if (symbol.size_) {
483     os << " size=" << symbol.size_ << " offset=" << symbol.offset_;
484   }
485   os << ": " << symbol.details_;
486   return os;
487 }
488 
489 // Output a unique name for a scope by qualifying it with the names of
490 // parent scopes. For scopes without corresponding symbols, use the kind
491 // with an index (e.g. Block1, Block2, etc.).
492 static void DumpUniqueName(llvm::raw_ostream &os, const Scope &scope) {
493   if (!scope.IsGlobal()) {
494     DumpUniqueName(os, scope.parent());
495     os << '/';
496     if (auto *scopeSymbol{scope.symbol()};
497         scopeSymbol && !scopeSymbol->name().empty()) {
498       os << scopeSymbol->name();
499     } else {
500       int index{1};
501       for (auto &child : scope.parent().children()) {
502         if (child == scope) {
503           break;
504         }
505         if (child.kind() == scope.kind()) {
506           ++index;
507         }
508       }
509       os << Scope::EnumToString(scope.kind()) << index;
510     }
511   }
512 }
513 
514 // Dump a symbol for UnparseWithSymbols. This will be used for tests so the
515 // format should be reasonably stable.
516 llvm::raw_ostream &DumpForUnparse(
517     llvm::raw_ostream &os, const Symbol &symbol, bool isDef) {
518   DumpUniqueName(os, symbol.owner());
519   os << '/' << symbol.name();
520   if (isDef) {
521     if (!symbol.attrs().empty()) {
522       os << ' ' << symbol.attrs();
523     }
524     if (!symbol.flags().empty()) {
525       os << " (" << symbol.flags() << ')';
526     }
527     os << ' ' << symbol.GetDetailsName();
528     DumpType(os, symbol.GetType());
529   }
530   return os;
531 }
532 
533 const DerivedTypeSpec *Symbol::GetParentTypeSpec(const Scope *scope) const {
534   if (const Symbol * parentComponent{GetParentComponent(scope)}) {
535     const auto &object{parentComponent->get<ObjectEntityDetails>()};
536     return &object.type()->derivedTypeSpec();
537   } else {
538     return nullptr;
539   }
540 }
541 
542 const Symbol *Symbol::GetParentComponent(const Scope *scope) const {
543   if (const auto *dtDetails{detailsIf<DerivedTypeDetails>()}) {
544     if (!scope) {
545       scope = scope_;
546     }
547     return dtDetails->GetParentComponent(DEREF(scope));
548   } else {
549     return nullptr;
550   }
551 }
552 
553 void DerivedTypeDetails::add_component(const Symbol &symbol) {
554   if (symbol.test(Symbol::Flag::ParentComp)) {
555     CHECK(componentNames_.empty());
556   }
557   componentNames_.push_back(symbol.name());
558 }
559 
560 const Symbol *DerivedTypeDetails::GetParentComponent(const Scope &scope) const {
561   if (auto extends{GetParentComponentName()}) {
562     if (auto iter{scope.find(*extends)}; iter != scope.cend()) {
563       if (const Symbol & symbol{*iter->second};
564           symbol.test(Symbol::Flag::ParentComp)) {
565         return &symbol;
566       }
567     }
568   }
569   return nullptr;
570 }
571 
572 void TypeParamDetails::set_type(const DeclTypeSpec &type) {
573   CHECK(!type_);
574   type_ = &type;
575 }
576 
577 bool GenericKind::IsIntrinsicOperator() const {
578   return Is(OtherKind::Concat) || Has<common::LogicalOperator>() ||
579       Has<common::NumericOperator>() || Has<common::RelationalOperator>();
580 }
581 
582 bool GenericKind::IsOperator() const {
583   return IsDefinedOperator() || IsIntrinsicOperator();
584 }
585 
586 std::string GenericKind::ToString() const {
587   return std::visit(
588       common::visitors {
589         [](const OtherKind &x) { return EnumToString(x); },
590             [](const DefinedIo &x) { return EnumToString(x); },
591 #if !__clang__ && __GNUC__ == 7 && __GNUC_MINOR__ == 2
592             [](const common::NumericOperator &x) {
593               return common::EnumToString(x);
594             },
595             [](const common::LogicalOperator &x) {
596               return common::EnumToString(x);
597             },
598             [](const common::RelationalOperator &x) {
599               return common::EnumToString(x);
600             },
601 #else
602             [](const auto &x) { return common::EnumToString(x); },
603 #endif
604       },
605       u);
606 }
607 
608 bool GenericKind::Is(GenericKind::OtherKind x) const {
609   const OtherKind *y{std::get_if<OtherKind>(&u)};
610   return y && *y == x;
611 }
612 
613 } // namespace Fortran::semantics
614