1//===- MatchDataInfo.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 "MatchDataInfo.h"
13#include "llvm/Support/Debug.h"
14#include "llvm/Support/raw_ostream.h"
15
16namespace llvm {
17namespace gi {
18
19StringMap<std::vector<std::string>> AllMatchDataVars;
20
21StringRef MatchDataInfo::getVariableName() const {
22  assert(hasVariableName());
23  return VarName;
24}
25
26void MatchDataInfo::print(raw_ostream &OS) const {
27  OS << "(MatchDataInfo pattern_symbol:" << PatternSymbol << " type:'" << Type
28     << "' var_name:" << (VarName.empty() ? "<unassigned>" : VarName) << ")";
29}
30
31void MatchDataInfo::dump() const { print(dbgs()); }
32
33void AssignMatchDataVariables(MutableArrayRef<MatchDataInfo> Infos) {
34  static unsigned NextVarID = 0;
35
36  StringMap<unsigned> SeenTypes;
37  for (auto &Info : Infos) {
38    unsigned &NumSeen = SeenTypes[Info.getType()];
39    auto &ExistingVars = AllMatchDataVars[Info.getType()];
40
41    if (NumSeen == ExistingVars.size())
42      ExistingVars.push_back("MDInfo" + std::to_string(NextVarID++));
43
44    Info.setVariableName(ExistingVars[NumSeen++]);
45  }
46}
47
48} // namespace gi
49} // namespace llvm
50