1 /** 2 * Copyright 2019-2022 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "ir/scope.h" 18 namespace mindspore { 19 const ScopePtr kDefaultScope = std::make_shared<Scope>("Default"); 20 GetInstance()21ScopeManager &ScopeManager::GetInstance() noexcept { 22 static ScopeManager instance; 23 return instance; 24 } 25 EnterScope(const ScopePtr & scope)26void ScopeManager::EnterScope(const ScopePtr &scope) { 27 if (scope != kDefaultScope) { 28 scope_stack_.push(scope); 29 } 30 } 31 LeaveScope(const ScopePtr & scope)32void ScopeManager::LeaveScope(const ScopePtr &scope) noexcept { 33 if (scope != kDefaultScope && !scope_stack_.empty()) { 34 scope_stack_.pop(); 35 } 36 } GetCurrentScope()37ScopePtr ScopeManager::GetCurrentScope() { 38 // if the scope stack is empty, return the default scope 39 if (scope_stack_.empty()) { 40 return kDefaultScope; 41 } 42 return scope_stack_.top(); 43 } ClearScope()44void ScopeManager::ClearScope() { 45 while (!scope_stack_.empty()) { 46 scope_stack_.pop(); 47 } 48 } 49 } // namespace mindspore 50