1 //===-- lib/Parser/tools.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "flang/Parser/tools.h"
11 namespace Fortran::parser
{
13 const Name
&GetLastName(const Name
&x
) { return x
; }
15 const Name
&GetLastName(const StructureComponent
&x
) {
16 return GetLastName(x
.component
);
19 const Name
&GetLastName(const DataRef
&x
) {
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
);
28 [](const common::Indirection
<CoindexedNamedObject
> &ci
)
29 -> const Name
& { return GetLastName(ci
.value().base
); },
34 const Name
&GetLastName(const Substring
&x
) {
35 return GetLastName(std::get
<DataRef
>(x
.t
));
38 const Name
&GetLastName(const Designator
&x
) {
40 [](const auto &y
) -> const Name
& { return GetLastName(y
); }, x
.u
);
43 const Name
&GetLastName(const ProcComponentRef
&x
) {
44 return GetLastName(x
.v
.thing
);
47 const Name
&GetLastName(const ProcedureDesignator
&x
) {
49 [](const auto &y
) -> const Name
& { return GetLastName(y
); }, x
.u
);
52 const Name
&GetLastName(const Call
&x
) {
53 return GetLastName(std::get
<ProcedureDesignator
>(x
.t
));
56 const Name
&GetLastName(const FunctionReference
&x
) { return GetLastName(x
.v
); }
58 const Name
&GetLastName(const Variable
&x
) {
60 [](const auto &indirection
) -> const Name
& {
61 return GetLastName(indirection
.value());
66 const Name
&GetLastName(const AllocateObject
&x
) {
68 [](const auto &y
) -> const Name
& { return GetLastName(y
); }, x
.u
);
71 const Name
&GetFirstName(const Name
&x
) { return x
; }
73 const Name
&GetFirstName(const StructureComponent
&x
) {
74 return GetFirstName(x
.base
);
77 const Name
&GetFirstName(const DataRef
&x
) {
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
);
86 [](const common::Indirection
<CoindexedNamedObject
> &ci
)
87 -> const Name
& { return GetFirstName(ci
.value().base
); },
92 const Name
&GetFirstName(const Substring
&x
) {
93 return GetFirstName(std::get
<DataRef
>(x
.t
));
96 const Name
&GetFirstName(const Designator
&x
) {
98 [](const auto &y
) -> const Name
& { return GetFirstName(y
); }, x
.u
);
101 const Name
&GetFirstName(const ProcComponentRef
&x
) {
102 return GetFirstName(x
.v
.thing
);
105 const Name
&GetFirstName(const ProcedureDesignator
&x
) {
106 return common::visit(
107 [](const auto &y
) -> const Name
& { return GetFirstName(y
); }, x
.u
);
110 const Name
&GetFirstName(const Call
&x
) {
111 return GetFirstName(std::get
<ProcedureDesignator
>(x
.t
));
114 const Name
&GetFirstName(const FunctionReference
&x
) {
115 return GetFirstName(x
.v
);
118 const Name
&GetFirstName(const Variable
&x
) {
119 return common::visit(
120 [](const auto &indirect
) -> const Name
& {
121 return GetFirstName(indirect
.value());
126 const Name
&GetFirstName(const EntityDecl
&x
) {
127 return std::get
<ObjectName
>(x
.t
);
130 const CoindexedNamedObject
*GetCoindexedNamedObject(const DataRef
&base
) {
131 return common::visit(
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
);
142 const CoindexedNamedObject
*GetCoindexedNamedObject(
143 const Designator
&designator
) {
144 return common::visit(
146 [](const DataRef
&x
) -> const CoindexedNamedObject
* {
147 return GetCoindexedNamedObject(x
);
149 [](const Substring
&x
) -> const CoindexedNamedObject
* {
150 return GetCoindexedNamedObject(std::get
<DataRef
>(x
.t
));
155 const CoindexedNamedObject
*GetCoindexedNamedObject(const Variable
&variable
) {
156 return common::visit(
158 [](const common::Indirection
<Designator
> &designator
)
159 -> const CoindexedNamedObject
* {
160 return GetCoindexedNamedObject(designator
.value());
162 [](const auto &) -> const CoindexedNamedObject
* { return nullptr; },
166 const CoindexedNamedObject
*GetCoindexedNamedObject(
167 const AllocateObject
&allocateObject
) {
168 return common::visit(
170 [](const StructureComponent
&x
) -> const CoindexedNamedObject
* {
171 return GetCoindexedNamedObject(x
.base
);
173 [](const auto &) -> const CoindexedNamedObject
* { return nullptr; },
177 } // namespace Fortran::parser