CIRCT  18.0.0git
BuilderUtils.h
Go to the documentation of this file.
1 //===- BuilderUtils.h - Operation builder utilities -------------*- 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 #ifndef CIRCT_SUPPORT_BUILDERUTILS_H
10 #define CIRCT_SUPPORT_BUILDERUTILS_H
11 
12 #include <variant>
13 
14 #include "circt/Support/LLVM.h"
15 #include "llvm/ADT/TypeSwitch.h"
16 
17 namespace circt {
18 
19 /// A helper union that can represent a `StringAttr`, `StringRef`, or `Twine`.
20 /// It is intended to be used as arguments to an op's `build` function. This
21 /// allows a single builder to accept any flavor value for a string attribute.
22 /// The `get` function can then be used to obtain a `StringAttr` from any of the
23 /// possible variants `StringAttrOrRef` can take.
25  std::variant<StringAttr, StringRef, Twine, const char *> value;
26 
27 public:
29  StringAttrOrRef(StringAttr attr) : value(attr) {}
30  StringAttrOrRef(const StringRef &str) : value(str) {}
31  StringAttrOrRef(const char *ptr) : value(ptr) {}
32  StringAttrOrRef(const std::string &str) : value(StringRef(str)) {}
33  StringAttrOrRef(const Twine &twine) : value(twine) {}
34 
35  /// Return the represented string as a `StringAttr`.
36  StringAttr get(MLIRContext *context) const {
37  if (auto *attr = std::get_if<StringAttr>(&value))
38  return *attr;
39  if (auto *ref = std::get_if<StringRef>(&value))
40  return StringAttr::get(context, *ref);
41  if (auto *twine = std::get_if<Twine>(&value))
42  return StringAttr::get(context, *twine);
43  if (auto *ptr = std::get_if<const char *>(&value))
44  return StringAttr::get(context, *ptr);
45  return StringAttr{};
46  }
47 };
48 
49 } // namespace circt
50 
51 #endif // CIRCT_SUPPORT_BUILDERUTILS_H
A helper union that can represent a StringAttr, StringRef, or Twine.
Definition: BuilderUtils.h:24
StringAttr get(MLIRContext *context) const
Return the represented string as a StringAttr.
Definition: BuilderUtils.h:36
std::variant< StringAttr, StringRef, Twine, const char * > value
Definition: BuilderUtils.h:25
StringAttrOrRef(const std::string &str)
Definition: BuilderUtils.h:32
StringAttrOrRef(const char *ptr)
Definition: BuilderUtils.h:31
StringAttrOrRef(StringAttr attr)
Definition: BuilderUtils.h:29
StringAttrOrRef(const StringRef &str)
Definition: BuilderUtils.h:30
StringAttrOrRef(const Twine &twine)
Definition: BuilderUtils.h:33
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:53
This file defines an intermediate representation for circuits acting as an abstraction for constraint...
Definition: DebugAnalysis.h:21