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 
AttributeStorage(Type type)19 AttributeStorage::AttributeStorage(Type type)
20     : type(type.getAsOpaquePointer()) {}
AttributeStorage()21 AttributeStorage::AttributeStorage() : type(nullptr) {}
22 
getType() const23 Type AttributeStorage::getType() const {
24   return Type::getFromOpaquePointer(type);
25 }
setType(Type newType)26 void AttributeStorage::setType(Type newType) {
27   type = newType.getAsOpaquePointer();
28 }
29 
30 //===----------------------------------------------------------------------===//
31 // Attribute
32 //===----------------------------------------------------------------------===//
33 
34 /// Return the type of this attribute.
getType() const35 Type Attribute::getType() const { return impl->getType(); }
36 
37 /// Return the context this attribute belongs to.
getContext() const38 MLIRContext *Attribute::getContext() const { return getDialect().getContext(); }
39 
40 /// Get the dialect this attribute is registered to.
getDialect() const41 Dialect &Attribute::getDialect() const {
42   return impl->getAbstractAttribute().getDialect();
43 }
44 
45 //===----------------------------------------------------------------------===//
46 // NamedAttribute
47 //===----------------------------------------------------------------------===//
48 
operator <(const NamedAttribute & lhs,const NamedAttribute & rhs)49 bool mlir::operator<(const NamedAttribute &lhs, const NamedAttribute &rhs) {
50   return strcmp(lhs.first.data(), rhs.first.data()) < 0;
51 }
operator <(const NamedAttribute & lhs,StringRef rhs)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