|
| 1 | +//===-- lib/Semantics/symbol-set-closure.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-set-closure.h" |
| 10 | +#include "flang/Common/idioms.h" |
| 11 | +#include "flang/Common/visit.h" |
| 12 | + |
| 13 | +namespace Fortran::semantics { |
| 14 | + |
| 15 | +class Collector { |
| 16 | +public: |
| 17 | + explicit Collector(int flags) : flags_{flags} {} |
| 18 | + |
| 19 | + UnorderedSymbolSet Collected() { return std::move(set_); } |
| 20 | + |
| 21 | + void operator()(const Symbol &x) { set_.insert(x); } |
| 22 | + void operator()(SymbolRef x) { (*this)(*x); } |
| 23 | + template <typename A> void operator()(const std::optional<A> &x) { |
| 24 | + if (x) { |
| 25 | + (*this)(*x); |
| 26 | + } |
| 27 | + } |
| 28 | + template <typename A> void operator()(const A *x) { |
| 29 | + if (x) { |
| 30 | + (*this)(*x); |
| 31 | + } |
| 32 | + } |
| 33 | + void operator()(const UnorderedSymbolSet &x) { |
| 34 | + for (const Symbol &symbol : x) { |
| 35 | + (*this)(symbol); |
| 36 | + } |
| 37 | + } |
| 38 | + void operator()(const SourceOrderedSymbolSet &x) { |
| 39 | + for (const Symbol &symbol : x) { |
| 40 | + (*this)(symbol); |
| 41 | + } |
| 42 | + } |
| 43 | + void operator()(const Scope &x) { |
| 44 | + for (const auto &[_, ref] : x) { |
| 45 | + (*this)(*ref); |
| 46 | + } |
| 47 | + } |
| 48 | + template <typename T> void operator()(const evaluate::Expr<T> &x) { |
| 49 | + UnorderedSymbolSet exprSyms{evaluate::CollectSymbols(x)}; |
| 50 | + for (const Symbol &sym : exprSyms) { |
| 51 | + if (!sym.owner().IsDerivedType() || sym.has<DerivedTypeDetails>() || |
| 52 | + (flags_ & IncludeComponentsInExprs)) { |
| 53 | + (*this)(sym); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + void operator()(const DeclTypeSpec &type) { |
| 58 | + if (type.category() == DeclTypeSpec::Category::Character) { |
| 59 | + (*this)(type.characterTypeSpec().length()); |
| 60 | + } else { |
| 61 | + (*this)(type.AsDerived()); |
| 62 | + } |
| 63 | + } |
| 64 | + void operator()(const DerivedTypeSpec &type) { |
| 65 | + (*this)(type.originalTypeSymbol()); |
| 66 | + for (const auto &[_, value] : type.parameters()) { |
| 67 | + (*this)(value); |
| 68 | + } |
| 69 | + } |
| 70 | + void operator()(const ParamValue &x) { (*this)(x.GetExplicit()); } |
| 71 | + void operator()(const Bound &x) { (*this)(x.GetExplicit()); } |
| 72 | + void operator()(const ShapeSpec &x) { |
| 73 | + (*this)(x.lbound()); |
| 74 | + (*this)(x.ubound()); |
| 75 | + } |
| 76 | + void operator()(const ArraySpec &x) { |
| 77 | + for (const ShapeSpec &shapeSpec : x) { |
| 78 | + (*this)(shapeSpec); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | +private: |
| 83 | + UnorderedSymbolSet set_; |
| 84 | + int flags_{NoDependenceCollectionFlags}; |
| 85 | +}; |
| 86 | + |
| 87 | +UnorderedSymbolSet CollectAllDependences(const Scope &scope, int flags) { |
| 88 | + UnorderedSymbolSet basis; |
| 89 | + for (const auto &[_, symbol] : scope) { |
| 90 | + basis.insert(*symbol); |
| 91 | + } |
| 92 | + return CollectAllDependences(basis, flags); |
| 93 | +} |
| 94 | + |
| 95 | +UnorderedSymbolSet CollectAllDependences( |
| 96 | + const UnorderedSymbolSet &original, int flags) { |
| 97 | + UnorderedSymbolSet result; |
| 98 | + if (flags & IncludeOriginalSymbols) { |
| 99 | + result = original; |
| 100 | + } |
| 101 | + UnorderedSymbolSet work{original}; |
| 102 | + while (!work.empty()) { |
| 103 | + Collector collect{flags}; |
| 104 | + for (const Symbol &symbol : work) { |
| 105 | + if (symbol.test(Symbol::Flag::CompilerCreated)) { |
| 106 | + continue; |
| 107 | + } |
| 108 | + collect(symbol.GetType()); |
| 109 | + common::visit( |
| 110 | + common::visitors{ |
| 111 | + [&collect, &symbol](const ObjectEntityDetails &x) { |
| 112 | + collect(x.shape()); |
| 113 | + collect(x.coshape()); |
| 114 | + if (IsNamedConstant(symbol) || symbol.owner().IsDerivedType()) { |
| 115 | + collect(x.init()); |
| 116 | + } |
| 117 | + collect(x.commonBlock()); |
| 118 | + if (const auto *set{FindEquivalenceSet(symbol)}) { |
| 119 | + for (const EquivalenceObject &equivObject : *set) { |
| 120 | + collect(equivObject.symbol); |
| 121 | + } |
| 122 | + } |
| 123 | + }, |
| 124 | + [&collect, &symbol](const ProcEntityDetails &x) { |
| 125 | + collect(x.rawProcInterface()); |
| 126 | + if (symbol.owner().IsDerivedType()) { |
| 127 | + collect(x.init()); |
| 128 | + } |
| 129 | + // TODO: worry about procedure pointers in common blocks? |
| 130 | + }, |
| 131 | + [&collect](const ProcBindingDetails &x) { collect(x.symbol()); }, |
| 132 | + [&collect](const SubprogramDetails &x) { |
| 133 | + for (const Symbol *dummy : x.dummyArgs()) { |
| 134 | + collect(dummy); |
| 135 | + } |
| 136 | + if (x.isFunction()) { |
| 137 | + collect(x.result()); |
| 138 | + } |
| 139 | + }, |
| 140 | + [&collect, &symbol]( |
| 141 | + const DerivedTypeDetails &) { collect(symbol.scope()); }, |
| 142 | + [&collect, flags](const GenericDetails &x) { |
| 143 | + collect(x.derivedType()); |
| 144 | + collect(x.specific()); |
| 145 | + for (const Symbol &use : x.uses()) { |
| 146 | + collect(use); |
| 147 | + } |
| 148 | + if (flags & IncludeSpecificsOfGenerics) { |
| 149 | + for (const Symbol &specific : x.specificProcs()) { |
| 150 | + collect(specific); |
| 151 | + } |
| 152 | + } |
| 153 | + }, |
| 154 | + [&collect](const NamelistDetails &x) { |
| 155 | + for (const Symbol &symbol : x.objects()) { |
| 156 | + collect(symbol); |
| 157 | + } |
| 158 | + }, |
| 159 | + [&collect](const CommonBlockDetails &x) { |
| 160 | + for (auto ref : x.objects()) { |
| 161 | + collect(*ref); |
| 162 | + } |
| 163 | + }, |
| 164 | + [&collect, &symbol, flags](const UseDetails &x) { |
| 165 | + if (flags & FollowUseAssociations) { |
| 166 | + collect(x.symbol()); |
| 167 | + } |
| 168 | + }, |
| 169 | + [&collect](const HostAssocDetails &x) { collect(x.symbol()); }, |
| 170 | + [](const auto &) {}, |
| 171 | + }, |
| 172 | + symbol.details()); |
| 173 | + } |
| 174 | + work.clear(); |
| 175 | + for (const Symbol &symbol : collect.Collected()) { |
| 176 | + if (result.find(symbol) == result.end() && |
| 177 | + ((flags & IncludeOriginalSymbols) || |
| 178 | + original.find(symbol) == original.end())) { |
| 179 | + result.insert(symbol); |
| 180 | + work.insert(symbol); |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + return result; |
| 185 | +} |
| 186 | + |
| 187 | +} // namespace Fortran::semantics |
0 commit comments