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