1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2 // -*- mode: C++ -*- 3 // 4 // Copyright 2022-2023 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 using Scope = std::string; 30 31 class PushScopeName { 32 public: 33 template <typename Kind> PushScopeName(Scope & scope_,Kind && kind,const std::string & name)34 PushScopeName(Scope& scope_, Kind&& kind, const std::string& name) 35 : scope_name_(scope_), old_size_(scope_name_.size()) { 36 if (name.empty()) { 37 scope_name_ += "<unnamed "; 38 scope_name_ += kind; 39 scope_name_ += ">::"; 40 } else { 41 scope_name_ += name; 42 scope_name_ += "::"; 43 } 44 } 45 46 PushScopeName(const PushScopeName& other) = delete; 47 PushScopeName& operator=(const PushScopeName& other) = delete; ~PushScopeName()48 ~PushScopeName() { 49 scope_name_.resize(old_size_); 50 } 51 52 private: 53 std::string& scope_name_; 54 const size_t old_size_; 55 }; 56 57 } // namespace stg 58 59 #endif // STG_SCOPE_H_ 60