1 //===-- lib/Parser/tools.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/Parser/tools.h" 10 11 namespace Fortran::parser { 12 13 const Name &GetLastName(const Name &x) { return x; } 14 15 const Name &GetLastName(const StructureComponent &x) { 16 return GetLastName(x.component); 17 } 18 19 const Name &GetLastName(const DataRef &x) { 20 return std::visit( 21 common::visitors{ 22 [](const Name &name) -> const Name & { return name; }, 23 [](const common::Indirection<StructureComponent> &sc) 24 -> const Name & { return GetLastName(sc.value()); }, 25 [](const common::Indirection<ArrayElement> &sc) -> const Name & { 26 return GetLastName(sc.value().base); 27 }, 28 [](const common::Indirection<CoindexedNamedObject> &ci) 29 -> const Name & { return GetLastName(ci.value().base); }, 30 }, 31 x.u); 32 } 33 34 const Name &GetLastName(const Substring &x) { 35 return GetLastName(std::get<DataRef>(x.t)); 36 } 37 38 const Name &GetLastName(const Designator &x) { 39 return std::visit( 40 [](const auto &y) -> const Name & { return GetLastName(y); }, x.u); 41 } 42 43 const Name &GetLastName(const ProcComponentRef &x) { 44 return GetLastName(x.v.thing); 45 } 46 47 const Name &GetLastName(const ProcedureDesignator &x) { 48 return std::visit( 49 [](const auto &y) -> const Name & { return GetLastName(y); }, x.u); 50 } 51 52 const Name &GetLastName(const Call &x) { 53 return GetLastName(std::get<ProcedureDesignator>(x.t)); 54 } 55 56 const Name &GetLastName(const FunctionReference &x) { return GetLastName(x.v); } 57 58 const Name &GetLastName(const Variable &x) { 59 return std::visit( 60 [](const auto &indirection) -> const Name & { 61 return GetLastName(indirection.value()); 62 }, 63 x.u); 64 } 65 66 const Name &GetLastName(const AllocateObject &x) { 67 return std::visit( 68 [](const auto &y) -> const Name & { return GetLastName(y); }, x.u); 69 } 70 71 const Name &GetFirstName(const Name &x) { return x; } 72 73 const Name &GetFirstName(const StructureComponent &x) { 74 return GetFirstName(x.base); 75 } 76 77 const Name &GetFirstName(const DataRef &x) { 78 return std::visit( 79 common::visitors{ 80 [](const Name &name) -> const Name & { return name; }, 81 [](const common::Indirection<StructureComponent> &sc) 82 -> const Name & { return GetFirstName(sc.value()); }, 83 [](const common::Indirection<ArrayElement> &sc) -> const Name & { 84 return GetFirstName(sc.value().base); 85 }, 86 [](const common::Indirection<CoindexedNamedObject> &ci) 87 -> const Name & { return GetFirstName(ci.value().base); }, 88 }, 89 x.u); 90 } 91 92 const Name &GetFirstName(const Substring &x) { 93 return GetFirstName(std::get<DataRef>(x.t)); 94 } 95 96 const Name &GetFirstName(const Designator &x) { 97 return std::visit( 98 [](const auto &y) -> const Name & { return GetFirstName(y); }, x.u); 99 } 100 101 const Name &GetFirstName(const ProcComponentRef &x) { 102 return GetFirstName(x.v.thing); 103 } 104 105 const Name &GetFirstName(const ProcedureDesignator &x) { 106 return std::visit( 107 [](const auto &y) -> const Name & { return GetFirstName(y); }, x.u); 108 } 109 110 const Name &GetFirstName(const Call &x) { 111 return GetFirstName(std::get<ProcedureDesignator>(x.t)); 112 } 113 114 const Name &GetFirstName(const FunctionReference &x) { 115 return GetFirstName(x.v); 116 } 117 118 const Name &GetFirstName(const Variable &x) { 119 return std::visit( 120 [](const auto &indirect) -> const Name & { 121 return GetFirstName(indirect.value()); 122 }, 123 x.u); 124 } 125 126 const CoindexedNamedObject *GetCoindexedNamedObject(const DataRef &base) { 127 return std::visit( 128 common::visitors{ 129 [](const Name &) -> const CoindexedNamedObject * { return nullptr; }, 130 [](const common::Indirection<CoindexedNamedObject> &x) 131 -> const CoindexedNamedObject * { return &x.value(); }, 132 [](const auto &x) -> const CoindexedNamedObject * { 133 return GetCoindexedNamedObject(x.value().base); 134 }, 135 }, 136 base.u); 137 } 138 const CoindexedNamedObject *GetCoindexedNamedObject( 139 const Designator &designator) { 140 return std::visit(common::visitors{ 141 [](const DataRef &x) -> const CoindexedNamedObject * { 142 return GetCoindexedNamedObject(x); 143 }, 144 [](const Substring &x) -> const CoindexedNamedObject * { 145 return GetCoindexedNamedObject( 146 std::get<DataRef>(x.t)); 147 }, 148 }, 149 designator.u); 150 } 151 const CoindexedNamedObject *GetCoindexedNamedObject(const Variable &variable) { 152 return std::visit( 153 common::visitors{ 154 [](const common::Indirection<Designator> &designator) 155 -> const CoindexedNamedObject * { 156 return GetCoindexedNamedObject(designator.value()); 157 }, 158 [](const auto &) -> const CoindexedNamedObject * { return nullptr; }, 159 }, 160 variable.u); 161 } 162 const CoindexedNamedObject *GetCoindexedNamedObject( 163 const AllocateObject &allocateObject) { 164 return std::visit( 165 common::visitors{ 166 [](const StructureComponent &x) -> const CoindexedNamedObject * { 167 return GetCoindexedNamedObject(x.base); 168 }, 169 [](const auto &) -> const CoindexedNamedObject * { return nullptr; }, 170 }, 171 allocateObject.u); 172 } 173 } // namespace Fortran::parser 174