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 "flang/Support/LangOptions.h"
21#include "llvm/Support/raw_ostream.h"
22#include <cinttypes>
23#include <optional>
24#include <set>
25#include <unordered_map>
26
27namespace Fortran::parser {
28
30class ParsingLog;
31class ParseState;
32
33class Success {}; // for when one must return something that's present
34
35class UserState {
36public:
37 UserState(const AllCookedSources &allCooked,
39 : allCooked_{allCooked}, features_{features} {}
40
41 const AllCookedSources &allCooked() const { return allCooked_; }
42 const common::LanguageFeatureControl &features() const { return features_; }
43
44 llvm::raw_ostream *debugOutput() const { return debugOutput_; }
45 UserState &set_debugOutput(llvm::raw_ostream &out) {
46 debugOutput_ = &out;
47 return *this;
48 }
49
50 ParsingLog *log() const { return log_; }
51 UserState &set_log(ParsingLog *log) {
52 log_ = log;
53 return *this;
54 }
55
56 bool instrumentedParse() const { return instrumentedParse_; }
57 UserState &set_instrumentedParse(bool yes) {
58 instrumentedParse_ = yes;
59 return *this;
60 }
61
62 void NewSubprogram() {
63 doLabels_.clear();
64 nonlabelDoConstructNestingDepth_ = 0;
65 oldStructureComponents_.clear();
66 }
67
68 using Label = std::uint64_t;
69 bool IsDoLabel(Label label) const {
70 auto iter{doLabels_.find(label)};
71 return iter != doLabels_.end() &&
72 iter->second >= nonlabelDoConstructNestingDepth_;
73 }
74 void NewDoLabel(Label label) {
75 doLabels_[label] = nonlabelDoConstructNestingDepth_;
76 }
77
78 void EnterNonlabelDoConstruct() { ++nonlabelDoConstructNestingDepth_; }
79 void LeaveDoConstruct() {
80 if (nonlabelDoConstructNestingDepth_ > 0) {
81 --nonlabelDoConstructNestingDepth_;
82 }
83 }
84
85 void NoteOldStructureComponent(const CharBlock &name) {
86 oldStructureComponents_.insert(name);
87 }
88 bool IsOldStructureComponent(const CharBlock &name) const {
89 return oldStructureComponents_.find(name) != oldStructureComponents_.end();
90 }
91
92 const common::LangOptions &langOptions() const { return langOptions_; }
93 UserState &set_langOptions(const common::LangOptions &langOptions) {
94 langOptions_ = langOptions;
95 return *this;
96 }
97
98private:
99 const AllCookedSources &allCooked_;
100
101 llvm::raw_ostream *debugOutput_{nullptr};
102
103 ParsingLog *log_{nullptr};
104 bool instrumentedParse_{false};
105
106 std::unordered_map<Label, int> doLabels_;
107 int nonlabelDoConstructNestingDepth_{0};
108
109 std::set<CharBlock> oldStructureComponents_;
110
112 common::LangOptions langOptions_;
113};
114
115// Definitions of parser classes that manipulate the UserState.
117 using resultType = Success;
118 static std::optional<Success> Parse(ParseState &);
119};
120
123 static std::optional<resultType> Parse(ParseState &);
124};
125
128 static std::optional<resultType> Parse(ParseState &);
129};
130
132 using resultType = Success;
133 static std::optional<Success> Parse(ParseState &);
134};
135
137 using resultType = Success;
138 static std::optional<Success> Parse(ParseState &);
139};
140
142 using resultType = Name;
143 static std::optional<Name> Parse(ParseState &);
144};
145
147 using resultType = DataComponentDefStmt;
148 static std::optional<DataComponentDefStmt> Parse(ParseState &);
149};
150
152 using resultType = StructureStmt;
153 static std::optional<StructureStmt> Parse(ParseState &);
154};
155} // namespace Fortran::parser
156#endif // FORTRAN_PARSER_USER_STATE_H_
Definition LangOptions.h:70
Definition Fortran-features.h:99
Definition provenance.h:286
Definition char-block.h:26
Definition parse-state.h:31
Definition instrumented-parser.h:25
Definition user-state.h:33
Definition check-expression.h:19
Definition user-state.h:121
Definition parse-tree.h:1062
Definition user-state.h:136
Definition parse-tree.h:591
Definition user-state.h:151
Definition user-state.h:116
Definition parse-tree.h:361
Definition user-state.h:146
Definition parse-tree.h:3525