LLZK 0.1.0
Veridise's ZK Language IR
Loading...
Searching...
No Matches
Types.cpp
Go to the documentation of this file.
1//===-- Types.cpp - Struct type implementations -----------------*- C++ -*-===//
2//
3// Part of the LLZK Project, under the Apache License v2.0.
4// See LICENSE.txt for license information.
5// Copyright 2025 Veridise Inc.
6// SPDX-License-Identifier: Apache-2.0
7//
8//===----------------------------------------------------------------------===//
9
13
14using namespace mlir;
15
16namespace llzk::component {
17
18ParseResult parseStructParams(AsmParser &parser, ArrayAttr &value) {
19 auto parseResult = FieldParser<ArrayAttr>::parse(parser);
20 if (failed(parseResult)) {
21 return parser.emitError(parser.getCurrentLocation(), "failed to parse struct parameters");
22 }
23 SmallVector<Attribute> own = forceIntAttrTypes(parseResult->getValue());
24 value = parser.getBuilder().getArrayAttr(own);
25 return success();
26}
27
28void printStructParams(AsmPrinter &printer, ArrayAttr value) {
29 printer << '[';
30 printAttrs(printer, value.getValue(), ", ");
31 printer << ']';
32}
33
34LogicalResult StructType::verify(EmitErrorFn emitError, SymbolRefAttr nameRef, ArrayAttr params) {
35 return verifyStructTypeParams(emitError, params);
36}
37
38FailureOr<SymbolLookupResult<StructDefOp>>
39StructType::getDefinition(SymbolTableCollection &symbolTable, Operation *op) const {
40 // First ensure this StructType passes verification
41 ArrayAttr typeParams = this->getParams();
42 if (failed(StructType::verify([op] { return op->emitError(); }, getNameRef(), typeParams))) {
43 return failure();
44 }
45 // Perform lookup and ensure the symbol references a StructDefOp
46 auto res = lookupTopLevelSymbol<StructDefOp>(symbolTable, getNameRef(), op);
47 if (failed(res) || !res.value()) {
48 return op->emitError() << "could not find '" << StructDefOp::getOperationName() << "' named \""
49 << getNameRef() << "\"";
50 }
51 // If this StructType contains parameters, make sure they match the number from the StructDefOp.
52 if (typeParams) {
53 auto defParams = res.value().get().getConstParams();
54 size_t numExpected = defParams ? defParams->size() : 0;
55 if (typeParams.size() != numExpected) {
56 return op->emitError() << "'" << StructType::name << "' type has " << typeParams.size()
57 << " parameters but \"" << res.value().get().getSymName()
58 << "\" expects " << numExpected;
59 }
60 }
61 return res;
62}
63
64LogicalResult StructType::verifySymbolRef(SymbolTableCollection &symbolTable, Operation *op) {
65 return getDefinition(symbolTable, op);
66}
67
68LogicalResult StructType::hasColumns(SymbolTableCollection &symbolTable, Operation *op) const {
69 auto lookup = getDefinition(symbolTable, op);
70 if (failed(lookup)) {
71 return lookup;
72 }
73 return lookup->get().hasColumns();
74}
75
76} // namespace llzk::component
static constexpr ::llvm::StringLiteral getOperationName()
Definition Ops.h.inc:919
::mlir::SymbolRefAttr getNameRef() const
mlir::LogicalResult hasColumns(mlir::SymbolTableCollection &symbolTable, mlir::Operation *op) const
Returns wether the struct this type refers to has fields marked as columns.
Definition Types.cpp:68
::mlir::LogicalResult verify(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::SymbolRefAttr nameRef, ::mlir::ArrayAttr params)
Definition Types.cpp:34
::mlir::ArrayAttr getParams() const
::mlir::FailureOr< SymbolLookupResult< StructDefOp > > getDefinition(::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op) const
Gets the struct op that defines this struct.
Definition Types.cpp:39
::mlir::LogicalResult verifySymbolRef(::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op)
Definition Types.cpp:64
static constexpr ::llvm::StringLiteral name
Definition Types.h.inc:38
void printStructParams(AsmPrinter &printer, ArrayAttr value)
Definition Types.cpp:28
ParseResult parseStructParams(AsmParser &parser, ArrayAttr &value)
Definition Types.cpp:18
mlir::FailureOr< SymbolLookupResultUntyped > lookupTopLevelSymbol(mlir::SymbolTableCollection &tables, mlir::SymbolRefAttr symbol, mlir::Operation *origin, bool reportMissing=true)
llvm::function_ref< mlir::InFlightDiagnostic()> EmitErrorFn
Definition ErrorHelper.h:18
SmallVector< Attribute > forceIntAttrTypes(ArrayRef< Attribute > attrList)
void printAttrs(AsmPrinter &printer, ArrayRef< Attribute > attrs, const StringRef &separator)
LogicalResult verifyStructTypeParams(EmitErrorFn emitError, ArrayAttr params)