1 // Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
19 #ifndef RUST_CONTEXT_STACK_H
20 #define RUST_CONTEXT_STACK_H
22 #include "rust-system.h"
27 * Context stack util class. This class is useful for situations where you can
28 * enter the same kind of context multiple times. For example, when dealing with
29 * unsafe contexts, you might be tempted to simply keep a boolean value.
33 * unsafe { // we set the boolean to true
34 * // Now unsafe operations are allowed!
35 * let b = *(&a as *const i32);
36 * let c = std::mem::transmute<i32, f32>(b); // Urgh!
37 * } // we set it to false
40 * However, since the language allows nested unsafe blocks, you may run into
44 * unsafe { // we set the boolean to true
45 * unsafe { // we set the boolean to true
46 * } // we set it to false
48 * // Now unsafe operations are forbidden again, the boolean is false
49 * let f = std::mem::transmute<i32, f32>(15); // Error!
50 * } // we set it to false
53 template <typename T
> class StackedContexts
57 * Enter a special context
59 void enter (T value
) { stack
.emplace_back (value
); }
62 * Exit a special context
66 rust_assert (!stack
.empty ());
68 auto last
= stack
.back ();
75 * Are we currently inside of a special context?
77 bool is_in_context () const { return !stack
.empty (); }
86 #endif /* !RUST_CONTEXT_STACK_H */