37ParseResult GlobalDefOp::parseGlobalInitialValue(
38 OpAsmParser &parser, Attribute &initialValue, TypeAttr typeAttr
40 if (parser.parseOptionalEqual()) {
44 Type specifiedType = typeAttr.getValue();
48 if (isa<FeltType>(specifiedType)) {
49 FeltConstAttr feltConstAttr;
50 if (parser.parseCustomAttributeWithFallback<FeltConstAttr>(feltConstAttr)) {
53 initialValue = feltConstAttr;
57 if (failed(parser.parseAttribute(initialValue, specifiedType))) {
63void GlobalDefOp::printGlobalInitialValue(
64 OpAsmPrinter &p,
GlobalDefOp , Attribute initialValue, TypeAttr
70 if (FeltConstAttr feltConstAttr = llvm::dyn_cast<FeltConstAttr>(initialValue)) {
71 p.printStrippedAttrOrType<FeltConstAttr>(feltConstAttr);
73 p.printAttributeWithoutType(initialValue);
86 EmitErrorFn errFn, Type rootType,
const Twine &aspect,
const Twine &expected,
const Twine &found
89 "with type ", rootType,
" expected ", expected,
" ", aspect,
" but found ", found
94 EmitErrorFn errFn, Type rootType,
const Twine &aspect,
const Twine &expected, Attribute found
96 return reportMismatch(errFn, rootType, aspect, expected, found.getAbstractAttribute().getName());
99LogicalResult ensureAttrTypeMatch(
100 Type type, Attribute valAttr,
const OwningEmitErrorFn &errFn, Type rootType,
const Twine &aspect
104 return errFn().append(
105 "attribute 'type' failed to satisfy constraint: type attribute of "
106 "any LLZK type except non-constant types"
109 if (type.isSignlessInteger(1)) {
110 if (IntegerAttr ia = llvm::dyn_cast<IntegerAttr>(valAttr)) {
111 APInt val = ia.getValue();
112 if (!val.isZero() && !val.isOne()) {
113 return errFn().append(
"integer constant out of range for attribute");
115 }
else if (!llvm::isa<BoolAttr>(valAttr)) {
116 return reportMismatch(errFn, rootType, aspect,
"builtin.bool or builtin.integer", valAttr);
118 }
else if (llvm::isa<IndexType>(type)) {
121 bool isBool = llvm::isa<BoolAttr>(valAttr);
122 if (isBool || !llvm::isa<IntegerAttr>(valAttr)) {
123 return reportMismatch(
124 errFn, rootType, aspect,
"builtin.index",
125 isBool ?
"builtin.bool" : valAttr.getAbstractAttribute().getName()
128 }
else if (llvm::isa<FeltType>(type)) {
129 if (!llvm::isa<FeltConstAttr, IntegerAttr>(valAttr)) {
130 return reportMismatch(errFn, rootType, aspect,
"felt.type", valAttr);
132 }
else if (llvm::isa<StringType>(type)) {
133 if (!llvm::isa<StringAttr>(valAttr)) {
134 return reportMismatch(errFn, rootType, aspect,
"builtin.string", valAttr);
136 }
else if (ArrayType arrTy = llvm::dyn_cast<ArrayType>(type)) {
137 if (ArrayAttr arrVal = llvm::dyn_cast<ArrayAttr>(valAttr)) {
139 assert(arrTy.hasStaticShape() &&
"implied by earlier isValidGlobalType() check");
140 int64_t expectedCount = arrTy.getNumElements();
141 size_t actualCount = arrVal.size();
142 if (std::cmp_not_equal(actualCount, expectedCount)) {
143 return reportMismatch(
144 errFn, rootType, Twine(aspect) +
" to contain " + Twine(expectedCount) +
" elements",
145 "builtin.array", Twine(actualCount)
151 bool hasFailure =
false;
152 Type expectedElemTy = arrTy.getElementType();
153 for (Attribute e : arrVal.getValue()) {
155 failed(ensureAttrTypeMatch(expectedElemTy, e, errFn, rootType,
"array element"));
161 return reportMismatch(errFn, rootType, aspect,
"builtin.array", valAttr);
164 return errFn().append(
"expected a valid LLZK type but found ", type);
175 return ensureAttrTypeMatch(ty, initValAttr, errFn, ty,
"attribute value");
179 return emitOpError(
"marked as 'const' must be assigned a value");
188FailureOr<SymbolLookupResult<GlobalDefOp>>
195FailureOr<SymbolLookupResult<GlobalDefOp>>
203 Type globalType = tgt->get().getType();
204 if (!
typesUnify(refOp.
getVal().getType(), globalType, tgt->getIncludeSymNames())) {
205 return refOp->emitOpError() <<
"has wrong type; expected " << globalType <<
", got "
206 << refOp.
getVal().getType();
214 if (failed(verifySymbolUsesImpl(*
this, tables))) {
222 auto tgt = verifySymbolUsesImpl(*
this, tables);
226 if (tgt->get().isConstant()) {
227 return emitOpError().append(