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 auto emitError = [&parser] {
24 return InFlightDiagnosticWrapper(parser.emitError(parser.getCurrentLocation()));
25 };
26 FailureOr<SmallVector<Attribute>> res = forceIntAttrTypes(parseResult->getValue(), emitError);
27 if (failed(res)) {
28 return failure();
29 }
30 value = parser.getBuilder().getArrayAttr(*res);
31 return success();
32}
33
34void printStructParams(AsmPrinter &printer, ArrayAttr value) {
35 printer << '[';
36 printAttrs(printer, value.getValue(), ", ");
37 printer << ']';
38}
39
40LogicalResult StructType::verify(
41 function_ref<InFlightDiagnostic()> emitError, SymbolRefAttr nameRef, ArrayAttr params
42) {
44}
45
46FailureOr<SymbolLookupResult<StructDefOp>>
47StructType::getDefinition(SymbolTableCollection &symbolTable, Operation *op) const {
48 // First ensure this StructType passes verification
49 ArrayAttr typeParams = this->getParams();
50 if (failed(StructType::verify([op] { return op->emitError(); }, getNameRef(), typeParams))) {
51 return failure();
52 }
53 // Perform lookup and ensure the symbol references a StructDefOp
54 auto res = lookupTopLevelSymbol<StructDefOp>(symbolTable, getNameRef(), op);
55 if (failed(res) || !res.value()) {
56 return op->emitError() << "could not find '" << StructDefOp::getOperationName() << "' named \""
57 << getNameRef() << '"';
58 }
59 // If this StructType contains parameters, make sure they match the number from the StructDefOp.
60 if (typeParams) {
61 auto defParams = res.value().get().getConstParams();
62 size_t numExpected = defParams ? defParams->size() : 0;
63 if (typeParams.size() != numExpected) {
64 return op->emitError() << '\'' << StructType::name << "' type has " << typeParams.size()
65 << " parameters but \"" << res.value().get().getSymName()
66 << "\" expects " << numExpected;
67 }
68 }
69 return res;
70}
71
72LogicalResult StructType::verifySymbolRef(SymbolTableCollection &symbolTable, Operation *op) {
73 return getDefinition(symbolTable, op);
74}
75
76LogicalResult StructType::hasColumns(SymbolTableCollection &symbolTable, Operation *op) const {
77 auto lookup = getDefinition(symbolTable, op);
78 if (failed(lookup)) {
79 return lookup;
80 }
81 return lookup->get().hasColumns();
82}
83
84} // namespace llzk::component
Wrapper around InFlightDiagnostic that can either be a regular InFlightDiagnostic or a special versio...
Definition ErrorHelper.h:26
static constexpr ::llvm::StringLiteral getOperationName()
Definition Ops.h.inc:1152
::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:76
::llvm::LogicalResult verify(::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError, ::mlir::SymbolRefAttr nameRef, ::mlir::ArrayAttr params)
Definition Types.cpp:40
::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:47
::mlir::LogicalResult verifySymbolRef(::mlir::SymbolTableCollection &symbolTable, ::mlir::Operation *op)
Definition Types.cpp:72
static constexpr ::llvm::StringLiteral name
Definition Types.h.inc:38
void printStructParams(AsmPrinter &printer, ArrayAttr value)
Definition Types.cpp:34
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)
void printAttrs(AsmPrinter &printer, ArrayRef< Attribute > attrs, const StringRef &separator)
FailureOr< SmallVector< Attribute > > forceIntAttrTypes(ArrayRef< Attribute > attrList, EmitErrorFn emitError)
OwningEmitErrorFn wrapNonNullableInFlightDiagnostic(llvm::function_ref< mlir::InFlightDiagnostic()> emitError)
LogicalResult verifyStructTypeParams(EmitErrorFn emitError, ArrayAttr params)