FLANG
StatementContext.h
1//===-- StatementContext.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// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_LOWER_STATEMENTCONTEXT_H
14#define FORTRAN_LOWER_STATEMENTCONTEXT_H
15
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/SmallVector.h"
18#include <functional>
19#include <optional>
20
21namespace mlir {
22class Location;
23class Region;
24} // namespace mlir
25
26namespace fir {
27class FirOpBuilder;
28}
29
30namespace Fortran::lower {
31
47public:
48 explicit StatementContext(bool cleanupProhibited = false) {
49 if (cleanupProhibited)
50 return;
51 cufs.push_back({});
52 }
53
55 if (!cufs.empty())
57 assert(cufs.empty() && "invalid StatementContext destructor call");
58 }
59
60 using CleanupFunction = std::function<void()>;
61
63 void pushScope() {
64 assert(!cufs.empty() && "invalid pushScope statement context");
65 cufs.push_back({});
66 }
67
69 void attachCleanup(CleanupFunction cuf) {
70 assert(!cufs.empty() && "invalid attachCleanup statement context");
71 if (cufs.back()) {
72 CleanupFunction oldCleanup = *cufs.back();
73 cufs.back() = [=]() {
74 cuf();
75 oldCleanup();
76 };
77 } else {
78 cufs.back() = cuf;
79 }
80 }
81
84 assert(!cufs.empty() && "invalid finalize statement context");
85 if (cufs.back())
86 (*cufs.back())();
87 }
88
92 cufs.back().reset();
93 }
94
96 void pop() { cufs.pop_back(); }
97
101 pop();
102 }
103
104 bool hasCode() const {
105 return !cufs.empty() && llvm::any_of(cufs, [](auto &opt) -> bool {
106 return opt.has_value();
107 });
108 }
109
110private:
111 // A statement context should never be copied or moved.
112 StatementContext(const StatementContext &) = delete;
113 StatementContext &operator=(const StatementContext &) = delete;
114 StatementContext(StatementContext &&) = delete;
115
116 // Stack of cleanup function "lists" (nested cleanup function calls).
118};
119
122void genCleanUpInRegionIfAny(mlir::Location loc, fir::FirOpBuilder &builder,
123 mlir::Region &region, StatementContext &context);
124
125} // namespace Fortran::lower
126
127#endif // FORTRAN_LOWER_STATEMENTCONTEXT_H
Definition: StatementContext.h:46
void finalizeAndKeep()
Make cleanup calls. Retain the stack top list for a repeat call.
Definition: StatementContext.h:83
void finalizeAndReset()
Make cleanup calls. Clear the stack top list.
Definition: StatementContext.h:90
void pop()
Pop the stack top list.
Definition: StatementContext.h:96
void attachCleanup(CleanupFunction cuf)
Append a cleanup function to the "list" of cleanup functions.
Definition: StatementContext.h:69
void pushScope()
Push a context subscope.
Definition: StatementContext.h:63
void finalizeAndPop()
Make cleanup calls. Pop the stack top list.
Definition: StatementContext.h:99
Definition: FIRBuilder.h:55
Definition: OpenACC.h:20
Definition: AbstractConverter.h:59
void genCleanUpInRegionIfAny(mlir::Location loc, fir::FirOpBuilder &builder, mlir::Region &region, StatementContext &context)
Definition: Bridge.cpp:6221
Definition: ConvertVariable.h:26
Definition: AbstractConverter.h:31
Definition: AbstractConverter.h:27