xref: /llvm-project-15.0.7/mlir/lib/IR/Types.cpp (revision ff6e7cf5)
1 //===- Types.cpp - MLIR Type Classes --------------------------------------===//
2 //
3 // Copyright 2019 The MLIR Authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //   http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // =============================================================================
17 
18 #include "mlir/IR/Types.h"
19 #include "TypeDetail.h"
20 #include "mlir/IR/Diagnostics.h"
21 #include "mlir/IR/Dialect.h"
22 #include "llvm/ADT/Twine.h"
23 
24 using namespace mlir;
25 using namespace mlir::detail;
26 
27 unsigned Type::getKind() const { return type->getKind(); }
28 
29 /// Get the dialect this type is registered to.
30 const Dialect &Type::getDialect() const { return type->getDialect(); }
31 
32 MLIRContext *Type::getContext() const { return getDialect().getContext(); }
33 
34 unsigned Type::getSubclassData() const { return type->getSubclassData(); }
35 void Type::setSubclassData(unsigned val) { type->setSubclassData(val); }
36 
37 /// Function Type.
38 
39 FunctionType FunctionType::get(ArrayRef<Type> inputs, ArrayRef<Type> results,
40                                MLIRContext *context) {
41   return Base::get(context, Type::Kind::Function, inputs, results);
42 }
43 
44 ArrayRef<Type> FunctionType::getInputs() const {
45   return getImpl()->getInputs();
46 }
47 
48 unsigned FunctionType::getNumResults() const { return getImpl()->numResults; }
49 
50 ArrayRef<Type> FunctionType::getResults() const {
51   return getImpl()->getResults();
52 }
53 
54 /// OpaqueType
55 
56 OpaqueType OpaqueType::get(Identifier dialect, StringRef typeData,
57                            MLIRContext *context) {
58   return Base::get(context, Type::Kind::Opaque, dialect, typeData);
59 }
60 
61 OpaqueType OpaqueType::getChecked(Identifier dialect, StringRef typeData,
62                                   MLIRContext *context, Location location) {
63   return Base::getChecked(location, context, Kind::Opaque, dialect, typeData);
64 }
65 
66 /// Returns the dialect namespace of the opaque type.
67 Identifier OpaqueType::getDialectNamespace() const {
68   return getImpl()->dialectNamespace;
69 }
70 
71 /// Returns the raw type data of the opaque type.
72 StringRef OpaqueType::getTypeData() const { return getImpl()->typeData; }
73 
74 /// Verify the construction of an opaque type.
75 LogicalResult OpaqueType::verifyConstructionInvariants(
76     llvm::Optional<Location> loc, MLIRContext *context, Identifier dialect,
77     StringRef typeData) {
78   if (!Dialect::isValidNamespace(dialect.strref())) {
79     if (loc)
80       context->emitError(*loc, "invalid dialect namespace '" +
81                                    dialect.strref() + "'");
82     return failure();
83   }
84   return success();
85 }
86