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 CoindexedNamedObject *GetCoindexedNamedObject(const DataRef &base) {
72   return std::visit(
73       common::visitors{
74           [](const Name &) -> const CoindexedNamedObject * { return nullptr; },
75           [](const common::Indirection<CoindexedNamedObject> &x)
76               -> const CoindexedNamedObject * { return &x.value(); },
77           [](const auto &x) -> const CoindexedNamedObject * {
78             return GetCoindexedNamedObject(x.value().base);
79           },
80       },
81       base.u);
82 }
83 const CoindexedNamedObject *GetCoindexedNamedObject(
84     const AllocateObject &allocateObject) {
85   return std::visit(
86       common::visitors{
87           [](const StructureComponent &x) -> const CoindexedNamedObject * {
88             return GetCoindexedNamedObject(x.base);
89           },
90           [](const auto &) -> const CoindexedNamedObject * { return nullptr; },
91       },
92       allocateObject.u);
93 }
94 } // namespace Fortran::parser
95