1 // Copyright (c) 2019, NVIDIA CORPORATION.  All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "tools.h"
16 
17 namespace Fortran::parser {
18 
GetLastName(const Name & x)19 const Name &GetLastName(const Name &x) { return x; }
20 
GetLastName(const StructureComponent & x)21 const Name &GetLastName(const StructureComponent &x) {
22   return GetLastName(x.component);
23 }
24 
GetLastName(const DataRef & x)25 const Name &GetLastName(const DataRef &x) {
26   return std::visit(
27       common::visitors{
28           [](const Name &name) -> const Name & { return name; },
29           [](const common::Indirection<StructureComponent> &sc)
30               -> const Name & { return GetLastName(sc.value()); },
31           [](const common::Indirection<ArrayElement> &sc) -> const Name & {
32             return GetLastName(sc.value().base);
33           },
34           [](const common::Indirection<CoindexedNamedObject> &ci)
35               -> const Name & { return GetLastName(ci.value().base); },
36       },
37       x.u);
38 }
39 
GetLastName(const Substring & x)40 const Name &GetLastName(const Substring &x) {
41   return GetLastName(std::get<DataRef>(x.t));
42 }
43 
GetLastName(const Designator & x)44 const Name &GetLastName(const Designator &x) {
45   return std::visit(
46       [](const auto &y) -> const Name & { return GetLastName(y); }, x.u);
47 }
48 
GetLastName(const ProcComponentRef & x)49 const Name &GetLastName(const ProcComponentRef &x) {
50   return GetLastName(x.v.thing);
51 }
52 
GetLastName(const ProcedureDesignator & x)53 const Name &GetLastName(const ProcedureDesignator &x) {
54   return std::visit(
55       [](const auto &y) -> const Name & { return GetLastName(y); }, x.u);
56 }
57 
GetLastName(const Call & x)58 const Name &GetLastName(const Call &x) {
59   return GetLastName(std::get<ProcedureDesignator>(x.t));
60 }
61 
GetLastName(const FunctionReference & x)62 const Name &GetLastName(const FunctionReference &x) { return GetLastName(x.v); }
63 
GetLastName(const Variable & x)64 const Name &GetLastName(const Variable &x) {
65   return std::visit(
66       [](const auto &indirection) -> const Name & {
67         return GetLastName(indirection.value());
68       },
69       x.u);
70 }
71 
GetLastName(const AllocateObject & x)72 const Name &GetLastName(const AllocateObject &x) {
73   return std::visit(
74       [](const auto &y) -> const Name & { return GetLastName(y); }, x.u);
75 }
76 
GetCoindexedNamedObject(const DataRef & base)77 const CoindexedNamedObject *GetCoindexedNamedObject(const DataRef &base) {
78   return std::visit(
79       common::visitors{
80           [](const Name &) -> const CoindexedNamedObject * { return nullptr; },
81           [](const common::Indirection<CoindexedNamedObject> &x)
82               -> const CoindexedNamedObject * { return &x.value(); },
83           [](const auto &x) -> const CoindexedNamedObject * {
84             return GetCoindexedNamedObject(x.value().base);
85           },
86       },
87       base.u);
88 }
GetCoindexedNamedObject(const AllocateObject & allocateObject)89 const CoindexedNamedObject *GetCoindexedNamedObject(
90     const AllocateObject &allocateObject) {
91   return std::visit(
92       common::visitors{
93           [](const StructureComponent &x) -> const CoindexedNamedObject * {
94             return GetCoindexedNamedObject(x.base);
95           },
96           [](const auto &) -> const CoindexedNamedObject * { return nullptr; },
97       },
98       allocateObject.u);
99 }
100 }
101