1 //===- Attributes.cpp - MLIR Affine Expr Classes --------------------------===// 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 #include "mlir/IR/Attributes.h" 10 #include "mlir/IR/Dialect.h" 11 12 using namespace mlir; 13 using namespace mlir::detail; 14 15 //===----------------------------------------------------------------------===// 16 // AttributeStorage 17 //===----------------------------------------------------------------------===// 18 19 AttributeStorage::AttributeStorage(Type type) 20 : type(type.getAsOpaquePointer()) {} 21 AttributeStorage::AttributeStorage() : type(nullptr) {} 22 23 Type AttributeStorage::getType() const { 24 return Type::getFromOpaquePointer(type); 25 } 26 void AttributeStorage::setType(Type newType) { 27 type = newType.getAsOpaquePointer(); 28 } 29 30 //===----------------------------------------------------------------------===// 31 // Attribute 32 //===----------------------------------------------------------------------===// 33 34 /// Return the type of this attribute. 35 Type Attribute::getType() const { return impl->getType(); } 36 37 /// Return the context this attribute belongs to. 38 MLIRContext *Attribute::getContext() const { return getDialect().getContext(); } 39 40 /// Get the dialect this attribute is registered to. 41 Dialect &Attribute::getDialect() const { 42 return impl->getAbstractAttribute().getDialect(); 43 } 44 45 //===----------------------------------------------------------------------===// 46 // NamedAttribute 47 //===----------------------------------------------------------------------===// 48 49 bool mlir::operator<(const NamedAttribute &lhs, const NamedAttribute &rhs) { 50 return strcmp(lhs.first.data(), rhs.first.data()) < 0; 51 } 52 bool mlir::operator<(const NamedAttribute &lhs, StringRef rhs) { 53 // This is correct even when attr.first.data()[name.size()] is not a zero 54 // string terminator, because we only care about a less than comparison. 55 // This can't use memcmp, because it doesn't guarantee that it will stop 56 // reading both buffers if one is shorter than the other, even if there is 57 // a difference. 58 return strncmp(lhs.first.data(), rhs.data(), rhs.size()) < 0; 59 } 60