1d8b37de8SMax Kazantsev //===- GCStrategy.cpp - Garbage Collector Description ---------------------===//
2d8b37de8SMax Kazantsev //
3d8b37de8SMax Kazantsev // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4d8b37de8SMax Kazantsev // See https://llvm.org/LICENSE.txt for license information.
5d8b37de8SMax Kazantsev // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d8b37de8SMax Kazantsev //
7d8b37de8SMax Kazantsev //===----------------------------------------------------------------------===//
8d8b37de8SMax Kazantsev //
9d8b37de8SMax Kazantsev // This file implements the policy object GCStrategy which describes the
10d8b37de8SMax Kazantsev // behavior of a given garbage collector.
11d8b37de8SMax Kazantsev //
12d8b37de8SMax Kazantsev //===----------------------------------------------------------------------===//
13d8b37de8SMax Kazantsev 
14d8b37de8SMax Kazantsev #include "llvm/IR/GCStrategy.h"
15*4acc0235SDmitry Vassiliev #include "llvm/ADT/Twine.h"
16d8b37de8SMax Kazantsev 
17d8b37de8SMax Kazantsev using namespace llvm;
18d8b37de8SMax Kazantsev 
19d8b37de8SMax Kazantsev LLVM_INSTANTIATE_REGISTRY(GCRegistry)
20d8b37de8SMax Kazantsev 
21d8b37de8SMax Kazantsev GCStrategy::GCStrategy() = default;
22c5b63714SMax Kazantsev 
getGCStrategy(const StringRef Name)23c5b63714SMax Kazantsev std::unique_ptr<GCStrategy> llvm::getGCStrategy(const StringRef Name) {
24c5b63714SMax Kazantsev   for (auto &S : GCRegistry::entries())
25c5b63714SMax Kazantsev     if (S.getName() == Name)
26c5b63714SMax Kazantsev       return S.instantiate();
27c5b63714SMax Kazantsev 
28c5b63714SMax Kazantsev   if (GCRegistry::begin() == GCRegistry::end()) {
29c5b63714SMax Kazantsev     // In normal operation, the registry should not be empty.  There should
30c5b63714SMax Kazantsev     // be the builtin GCs if nothing else.  The most likely scenario here is
31c5b63714SMax Kazantsev     // that we got here without running the initializers used by the Registry
32c5b63714SMax Kazantsev     // itself and it's registration mechanism.
33c5b63714SMax Kazantsev     const std::string error =
34c5b63714SMax Kazantsev         std::string("unsupported GC: ") + Name.str() +
35c5b63714SMax Kazantsev         " (did you remember to link and initialize the library?)";
36*4acc0235SDmitry Vassiliev     report_fatal_error(Twine(error));
37c5b63714SMax Kazantsev   } else
38*4acc0235SDmitry Vassiliev     report_fatal_error(Twine(std::string("unsupported GC: ") + Name.str()));
39c5b63714SMax Kazantsev }
40