CIRCT 20.0.0git
Loading...
Searching...
No Matches
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 "mlir/IR/BuiltinOps.h"
16#include "mlir/IR/ImplicitLocOpBuilder.h"
17#include "llvm/ADT/StringSet.h"
18#include "llvm/ADT/TypeSwitch.h"
19
20namespace circt {
21
22/// A helper union that can represent a `StringAttr`, `StringRef`, or `Twine`.
23/// It is intended to be used as arguments to an op's `build` function. This
24/// allows a single builder to accept any flavor value for a string attribute.
25/// The `get` function can then be used to obtain a `StringAttr` from any of the
26/// possible variants `StringAttrOrRef` can take.
28 std::variant<StringAttr, StringRef, Twine, const char *> value;
29
30public:
32 StringAttrOrRef(StringAttr attr) : value(attr) {}
33 StringAttrOrRef(const StringRef &str) : value(str) {}
34 StringAttrOrRef(const char *ptr) : value(ptr) {}
35 StringAttrOrRef(const std::string &str) : value(StringRef(str)) {}
36 StringAttrOrRef(const Twine &twine) : value(twine) {}
37
38 /// Return the represented string as a `StringAttr`.
39 StringAttr get(MLIRContext *context) const {
40 if (auto *attr = std::get_if<StringAttr>(&value))
41 return *attr;
42 if (auto *ref = std::get_if<StringRef>(&value))
43 return StringAttr::get(context, *ref);
44 if (auto *twine = std::get_if<Twine>(&value))
45 return StringAttr::get(context, *twine);
46 if (auto *ptr = std::get_if<const char *>(&value))
47 return StringAttr::get(context, *ptr);
48 return StringAttr{};
49 }
50};
51
52} // namespace circt
53
54#endif // CIRCT_SUPPORT_BUILDERUTILS_H
A helper union that can represent a StringAttr, StringRef, or Twine.
StringAttr get(MLIRContext *context) const
Return the represented string as a StringAttr.
std::variant< StringAttr, StringRef, Twine, const char * > value
StringAttrOrRef(const std::string &str)
StringAttrOrRef(const char *ptr)
StringAttrOrRef(StringAttr attr)
StringAttrOrRef(const StringRef &str)
StringAttrOrRef(const Twine &twine)
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.