FLANG
user-state.h
1//===-- include/flang/Parser/user-state.h -----------------------*- C++ -*-===//
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#ifndef FORTRAN_PARSER_USER_STATE_H_
10#define FORTRAN_PARSER_USER_STATE_H_
11
12// Instances of ParseState (parse-state.h) incorporate instances of this
13// UserState class, which encapsulates any semantic information necessary for
14// parse tree construction so as to avoid any need for representing
15// state in static data.
16
17#include "flang/Parser/char-block.h"
18#include "flang/Parser/parse-tree.h"
19#include "flang/Support/Fortran-features.h"
20#include "llvm/Support/raw_ostream.h"
21#include <cinttypes>
22#include <optional>
23#include <set>
24#include <unordered_map>
25
26namespace Fortran::parser {
27
29class ParsingLog;
30class ParseState;
31
32class Success {}; // for when one must return something that's present
33
34class UserState {
35public:
36 UserState(const AllCookedSources &allCooked,
38 : allCooked_{allCooked}, features_{features} {}
39
40 const AllCookedSources &allCooked() const { return allCooked_; }
41 const common::LanguageFeatureControl &features() const { return features_; }
42
43 llvm::raw_ostream *debugOutput() const { return debugOutput_; }
44 UserState &set_debugOutput(llvm::raw_ostream &out) {
45 debugOutput_ = &out;
46 return *this;
47 }
48
49 ParsingLog *log() const { return log_; }
50 UserState &set_log(ParsingLog *log) {
51 log_ = log;
52 return *this;
53 }
54
55 bool instrumentedParse() const { return instrumentedParse_; }
56 UserState &set_instrumentedParse(bool yes) {
57 instrumentedParse_ = yes;
58 return *this;
59 }
60
61 void NewSubprogram() {
62 doLabels_.clear();
63 nonlabelDoConstructNestingDepth_ = 0;
64 oldStructureComponents_.clear();
65 }
66
67 using Label = std::uint64_t;
68 bool IsDoLabel(Label label) const {
69 auto iter{doLabels_.find(label)};
70 return iter != doLabels_.end() &&
71 iter->second >= nonlabelDoConstructNestingDepth_;
72 }
73 void NewDoLabel(Label label) {
74 doLabels_[label] = nonlabelDoConstructNestingDepth_;
75 }
76
77 void EnterNonlabelDoConstruct() { ++nonlabelDoConstructNestingDepth_; }
78 void LeaveDoConstruct() {
79 if (nonlabelDoConstructNestingDepth_ > 0) {
80 --nonlabelDoConstructNestingDepth_;
81 }
82 }
83
84 void NoteOldStructureComponent(const CharBlock &name) {
85 oldStructureComponents_.insert(name);
86 }
87 bool IsOldStructureComponent(const CharBlock &name) const {
88 return oldStructureComponents_.find(name) != oldStructureComponents_.end();
89 }
90
91private:
92 const AllCookedSources &allCooked_;
93
94 llvm::raw_ostream *debugOutput_{nullptr};
95
96 ParsingLog *log_{nullptr};
97 bool instrumentedParse_{false};
98
99 std::unordered_map<Label, int> doLabels_;
100 int nonlabelDoConstructNestingDepth_{0};
101
102 std::set<CharBlock> oldStructureComponents_;
103
105};
106
107// Definitions of parser classes that manipulate the UserState.
109 using resultType = Success;
110 static std::optional<Success> Parse(ParseState &);
111};
112
115 static std::optional<resultType> Parse(ParseState &);
116};
117
120 static std::optional<resultType> Parse(ParseState &);
121};
122
124 using resultType = Success;
125 static std::optional<Success> Parse(ParseState &);
126};
127
129 using resultType = Success;
130 static std::optional<Success> Parse(ParseState &);
131};
132
134 using resultType = Name;
135 static std::optional<Name> Parse(ParseState &);
136};
137
139 using resultType = DataComponentDefStmt;
140 static std::optional<DataComponentDefStmt> Parse(ParseState &);
141};
142
144 using resultType = StructureStmt;
145 static std::optional<StructureStmt> Parse(ParseState &);
146};
147} // namespace Fortran::parser
148#endif // FORTRAN_PARSER_USER_STATE_H_
Definition Fortran-features.h:98
Definition provenance.h:281
Definition char-block.h:26
Definition parse-state.h:31
Definition instrumented-parser.h:25
Definition user-state.h:32
Definition check-expression.h:19
Definition user-state.h:113
Definition parse-tree.h:1062
Definition user-state.h:128
Definition parse-tree.h:591
Definition user-state.h:143
Definition user-state.h:108
Definition parse-tree.h:361
Definition user-state.h:138
Definition parse-tree.h:3523