CIRCT 24.0.0git
Loading...
Searching...
No Matches
FieldInfo.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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// This file defines FieldInfo, a name and type pair describing the fields of
10// aggregate types such as `!hw.struct` and `!sim.variant`, along with helpers
11// to parse and print lists of fields.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef CIRCT_SUPPORT_FIELDINFO_H
16#define CIRCT_SUPPORT_FIELDINFO_H
17
18#include "circt/Support/LLVM.h"
19#include "mlir/IR/AttrTypeSubElements.h"
20#include "mlir/IR/BuiltinAttributes.h"
21#include "mlir/IR/Types.h"
22#include "llvm/ADT/Hashing.h"
23
24namespace circt {
25
26/// A named and typed field of an aggregate type.
27struct FieldInfo {
28 mlir::StringAttr name;
29 mlir::Type type;
30};
31
32inline bool operator==(const FieldInfo &a, const FieldInfo &b) {
33 return a.name == b.name && a.type == b.type;
34}
35inline llvm::hash_code hash_value(const FieldInfo &fi) {
36 return llvm::hash_combine(fi.name, fi.type);
37}
38
39/// Parse a list of named fields within `<>`, e.g. `<foo: i7, bar: i8>`. Field
40/// names may be bare keywords or quoted strings. The location and name of every
41/// field whose name was already used by a preceding field is appended to
42/// `duplicates`, leaving it to the caller to report them.
43ParseResult
44parseFieldList(AsmParser &p, SmallVectorImpl<FieldInfo> &fields,
45 SmallVectorImpl<std::pair<llvm::SMLoc, StringAttr>> &duplicates);
46
47/// Print a list of fields within `<>`, e.g. `<foo: i7, bar: i8>`.
48void printFieldList(AsmPrinter &p, ArrayRef<FieldInfo> fields);
49
50} // namespace circt
51
52namespace mlir {
53/// Expose the names and types of fields to the generic attribute and type
54/// walking and replacement infrastructure. This allows walkers to recurse into
55/// the fields of aggregate types, and replacers such as
56/// `mlir::AttrTypeReplacer` to replace types nested within the fields.
57template <>
58struct AttrTypeSubElementHandler<circt::FieldInfo> {
59 static void walk(const circt::FieldInfo &param,
60 AttrTypeImmediateSubElementWalker &walker) {
61 walker.walk(param.name);
62 walker.walk(param.type);
63 }
65 AttrSubElementReplacements &attrRepls,
66 TypeSubElementReplacements &typeRepls) {
67 return {cast<StringAttr>(attrRepls.take_front(1)[0]),
68 typeRepls.take_front(1)[0]};
69 }
70};
71} // namespace mlir
72
73#endif // CIRCT_SUPPORT_FIELDINFO_H
static bool operator==(const ModulePort &a, const ModulePort &b)
Definition HWTypes.h:64
static llvm::hash_code hash_value(const ModulePort &port)
Definition HWTypes.h:67
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
ParseResult parseFieldList(AsmParser &p, SmallVectorImpl< FieldInfo > &fields, SmallVectorImpl< std::pair< llvm::SMLoc, StringAttr > > &duplicates)
Parse a list of named fields within <>, e.g.
Definition FieldInfo.cpp:15
void printFieldList(AsmPrinter &p, ArrayRef< FieldInfo > fields)
Print a list of fields within <>, e.g. <foo: i7, bar: i8>.
Definition FieldInfo.cpp:38
A named and typed field of an aggregate type.
Definition FieldInfo.h:27
mlir::Type type
Definition FieldInfo.h:29
mlir::StringAttr name
Definition FieldInfo.h:28
static circt::FieldInfo replace(const circt::FieldInfo &param, AttrSubElementReplacements &attrRepls, TypeSubElementReplacements &typeRepls)
Definition FieldInfo.h:64
static void walk(const circt::FieldInfo &param, AttrTypeImmediateSubElementWalker &walker)
Definition FieldInfo.h:59