1 //===-- lib/Parser/user-state.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/user-state.h"
10 #include "stmt-parser.h"
11 #include "type-parsers.h"
12 #include "flang/Parser/parse-state.h"
13 #include <optional>
14 
15 namespace Fortran::parser {
16 
Parse(ParseState & state)17 std::optional<Success> StartNewSubprogram::Parse(ParseState &state) {
18   if (auto *ustate{state.userState()}) {
19     ustate->NewSubprogram();
20   }
21   return Success{};
22 }
23 
Parse(ParseState & state)24 std::optional<CapturedLabelDoStmt::resultType> CapturedLabelDoStmt::Parse(
25     ParseState &state) {
26   static constexpr auto parser{statement(indirect(Parser<LabelDoStmt>{}))};
27   auto result{parser.Parse(state)};
28   if (result) {
29     if (auto *ustate{state.userState()}) {
30       ustate->NewDoLabel(std::get<Label>(result->statement.value().t));
31     }
32   }
33   return result;
34 }
35 
36 std::optional<EndDoStmtForCapturedLabelDoStmt::resultType>
Parse(ParseState & state)37 EndDoStmtForCapturedLabelDoStmt::Parse(ParseState &state) {
38   static constexpr auto parser{
39       statement(indirect(construct<EndDoStmt>("END DO" >> maybe(name))))};
40   if (auto enddo{parser.Parse(state)}) {
41     if (enddo->label) {
42       if (const auto *ustate{state.userState()}) {
43         if (ustate->IsDoLabel(enddo->label.value())) {
44           return enddo;
45         }
46       }
47     }
48   }
49   return std::nullopt;
50 }
51 
Parse(ParseState & state)52 std::optional<Success> EnterNonlabelDoConstruct::Parse(ParseState &state) {
53   if (auto *ustate{state.userState()}) {
54     ustate->EnterNonlabelDoConstruct();
55   }
56   return {Success{}};
57 }
58 
Parse(ParseState & state)59 std::optional<Success> LeaveDoConstruct::Parse(ParseState &state) {
60   if (auto ustate{state.userState()}) {
61     ustate->LeaveDoConstruct();
62   }
63   return {Success{}};
64 }
65 
Parse(ParseState & state)66 std::optional<Name> OldStructureComponentName::Parse(ParseState &state) {
67   if (std::optional<Name> n{name.Parse(state)}) {
68     if (const auto *ustate{state.userState()}) {
69       if (ustate->IsOldStructureComponent(n->source)) {
70         return n;
71       }
72     }
73   }
74   return std::nullopt;
75 }
76 
Parse(ParseState & state)77 std::optional<DataComponentDefStmt> StructureComponents::Parse(
78     ParseState &state) {
79   static constexpr auto stmt{Parser<DataComponentDefStmt>{}};
80   std::optional<DataComponentDefStmt> defs{stmt.Parse(state)};
81   if (defs) {
82     if (auto *ustate{state.userState()}) {
83       for (const auto &decl : std::get<std::list<ComponentDecl>>(defs->t)) {
84         ustate->NoteOldStructureComponent(std::get<Name>(decl.t).source);
85       }
86     }
87   }
88   return defs;
89 }
90 } // namespace Fortran::parser
91