CIRCT 24.0.0git
Loading...
Searching...
No Matches
RTGTypes.cpp
Go to the documentation of this file.
1//===- RTGTypes.cpp -------------------------------------------------------===//
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
12#include "mlir/IR/Builders.h"
13#include "mlir/IR/DialectImplementation.h"
14#include "llvm/ADT/StringSet.h"
15#include "llvm/ADT/TypeSwitch.h"
16
17using namespace circt;
18using namespace rtg;
19
20#define GET_TYPEDEF_CLASSES
21#include "circt/Dialect/RTG/IR/RTGTypes.cpp.inc"
22
23//===----------------------------------------------------------------------===//
24// DictType
25//===----------------------------------------------------------------------===/
26
27LogicalResult DictType::verify(function_ref<InFlightDiagnostic()> emitError,
28 ArrayRef<DictEntry> entries) {
29 StringAttr last;
30 for (auto entry : entries) {
31 if (entry.name.empty())
32 return emitError() << "empty strings not allowed as entry names";
33
34 if (last && entry.name.getValue() <= last.getValue())
35 return emitError() << "dictionary must be sorted by names and contain no "
36 "duplicates, first violation at entry '"
37 << entry.name.getValue() << "'";
38
39 last = entry.name;
40 }
41
42 return success();
43}
44
45Type DictType::parse(AsmParser &p) {
46 SmallVector<DictEntry> entries;
47 auto loc = p.getCurrentLocation();
48
49 auto parseResult = p.parseCommaSeparatedList(
50 mlir::AsmParser::Delimiter::LessGreater, [&]() -> ParseResult {
51 std::string name;
52 Type type;
53 loc = p.getCurrentLocation();
54
55 if (p.parseKeywordOrString(&name) || p.parseColon() ||
56 p.parseType(type))
57 return failure();
58
59 DictEntry entry;
60 entry.name = StringAttr::get(p.getContext(), name);
61 entry.type = type;
62 entries.emplace_back(entry);
63 return success();
64 });
65
66 if (failed(parseResult))
67 return Type();
68
69 auto emitError = [&]() { return p.emitError(loc); };
70
71 // Call 'getChecked' here such that we do not have to repeat the verification
72 // checks in the parser here, but still get the errors reported at meaningful
73 // locations.
74 return getChecked(emitError, p.getContext(), entries);
75}
76
77LogicalResult
78StructType::verifySymbolUses(Operation *op,
79 SymbolTableCollection &symbolTable) const {
80 if (!symbolTable.lookupNearestSymbolFrom<StructDeclOp>(op, getDeclaration()))
81 return op->emitOpError()
82 << "references unknown struct declaration " << getDeclaration();
83 return success();
84}
85
86LogicalResult
87VariantType::verifySymbolUses(Operation *op,
88 SymbolTableCollection &symbolTable) const {
89 if (!symbolTable.lookupNearestSymbolFrom<VariantDeclOp>(op, getDeclaration()))
90 return op->emitOpError()
91 << "references unknown variant declaration " << getDeclaration();
92 return success();
93}
94
95void DictType::print(AsmPrinter &p) const {
96 p << '<';
97 llvm::interleaveComma(getEntries(), p, [&](auto entry) {
98 p.printKeywordOrString(entry.name.getValue());
99 p << ": " << entry.type;
100 });
101 p << ">";
102}
103
104bool DictType::entryTypesMatch(TypeRange types) const {
105 return llvm::equal(getEntries(), types,
106 [](const DictEntry &entry, const Type &type) {
107 return entry.type == type;
108 });
109}
110
111void circt::rtg::RTGDialect::registerTypes() {
112 addTypes<
113#define GET_TYPEDEF_LIST
114#include "circt/Dialect/RTG/IR/RTGTypes.cpp.inc"
115 >();
116}
false return failure()
Parse an optional keyword or string and set instance into 'result'.
return success()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition rtg.py:1