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 //===----------------------------------------------------------------------===//
41 // NamedAttribute
42 //===----------------------------------------------------------------------===//
43 
44 bool mlir::operator<(const NamedAttribute &lhs, const NamedAttribute &rhs) {
45   return strcmp(lhs.first.data(), rhs.first.data()) < 0;
46 }
47 bool mlir::operator<(const NamedAttribute &lhs, StringRef rhs) {
48   // This is correct even when attr.first.data()[name.size()] is not a zero
49   // string terminator, because we only care about a less than comparison.
50   // This can't use memcmp, because it doesn't guarantee that it will stop
51   // reading both buffers if one is shorter than the other, even if there is
52   // a difference.
53   return strncmp(lhs.first.data(), rhs.data(), rhs.size()) < 0;
54 }
55