1 //===- CXXPredicates.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //
10 //===----------------------------------------------------------------------===//
11 
12 #include "CXXPredicates.h"
13 #include "llvm/ADT/STLExtras.h"
14 
15 namespace llvm {
16 namespace gi {
17 
18 std::vector<const CXXPredicateCode *>
getSorted(const CXXPredicateCodePool & Pool)19 CXXPredicateCode::getSorted(const CXXPredicateCodePool &Pool) {
20   std::vector<const CXXPredicateCode *> Out;
21   std::transform(Pool.begin(), Pool.end(), std::back_inserter(Out),
22                  [&](auto &Elt) { return Elt.second.get(); });
23   sort(Out, [](const auto *A, const auto *B) { return A->ID < B->ID; });
24   return Out;
25 }
26 
get(CXXPredicateCodePool & Pool,std::string Code)27 const CXXPredicateCode &CXXPredicateCode::get(CXXPredicateCodePool &Pool,
28                                               std::string Code) {
29   // Check if we already have an identical piece of code, if not, create an
30   // entry in the pool.
31   const auto CodeHash = hash_value(Code);
32   if (auto It = Pool.find(CodeHash); It != Pool.end())
33     return *It->second;
34 
35   const auto ID = Pool.size();
36   auto OwnedData = std::unique_ptr<CXXPredicateCode>(
37       new CXXPredicateCode(std::move(Code), ID));
38   const auto &DataRef = *OwnedData;
39   Pool[CodeHash] = std::move(OwnedData);
40   return DataRef;
41 }
42 
43 // TODO: Make BaseEnumName prefix configurable.
CXXPredicateCode(std::string Code,unsigned ID)44 CXXPredicateCode::CXXPredicateCode(std::string Code, unsigned ID)
45     : Code(Code), ID(ID), BaseEnumName("GICombiner" + std::to_string(ID)) {}
46 
47 CXXPredicateCode::CXXPredicateCodePool CXXPredicateCode::AllCXXMatchCode;
48 CXXPredicateCode::CXXPredicateCodePool CXXPredicateCode::AllCXXApplyCode;
49 
50 } // namespace gi
51 } // namespace llvm
52