xref: /aosp_15_r20/external/stg/scope.h (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2022-2024 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License.  You may obtain a copy of the License at
9 //
10 //     https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Ignes Simeonova
19 // Author: Aleksei Vetrov
20 
21 #ifndef STG_SCOPE_H_
22 #define STG_SCOPE_H_
23 
24 #include <cstddef>
25 #include <string>
26 
27 namespace stg {
28 
29 struct Scope {
30   std::string name;
31   bool named = true;
32 };
33 
34 class PushScopeName {
35  public:
36   template <typename Kind>
PushScopeName(Scope & scope,Kind && kind,const std::string & name)37   PushScopeName(Scope& scope, Kind&& kind, const std::string& name)
38       : scope_(scope), old_size_(scope_.name.size()),
39         old_named_(scope_.named) {
40     if (name.empty()) {
41       scope_.name += "<unnamed ";
42       scope_.name += kind;
43       scope_.name += ">::";
44       scope_.named = false;
45     } else {
46       scope_.name += name;
47       scope_.name += "::";
48     }
49   }
50 
51   PushScopeName(const PushScopeName& other) = delete;
52   PushScopeName& operator=(const PushScopeName& other) = delete;
~PushScopeName()53   ~PushScopeName() {
54     scope_.name.resize(old_size_);
55     scope_.named = old_named_;
56   }
57 
58  private:
59   Scope& scope_;
60   const size_t old_size_;
61   const bool old_named_;
62 };
63 
64 }  // namespace stg
65 
66 #endif  // STG_SCOPE_H_
67