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