xref: /llvm-project-15.0.7/mlir/lib/IR/Types.cpp (revision ca23bd78)
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 //===----------------------------------------------------------------------===//
28 // Type
29 //===----------------------------------------------------------------------===//
30 
31 unsigned Type::getKind() const { return impl->getKind(); }
32 
33 /// Get the dialect this type is registered to.
34 Dialect &Type::getDialect() const { return impl->getDialect(); }
35 
36 MLIRContext *Type::getContext() const { return getDialect().getContext(); }
37 
38 unsigned Type::getSubclassData() const { return impl->getSubclassData(); }
39 void Type::setSubclassData(unsigned val) { impl->setSubclassData(val); }
40 
41 //===----------------------------------------------------------------------===//
42 // FunctionType
43 //===----------------------------------------------------------------------===//
44 
45 FunctionType FunctionType::get(ArrayRef<Type> inputs, ArrayRef<Type> results,
46                                MLIRContext *context) {
47   return Base::get(context, Type::Kind::Function, inputs, results);
48 }
49 
50 ArrayRef<Type> FunctionType::getInputs() const {
51   return getImpl()->getInputs();
52 }
53 
54 unsigned FunctionType::getNumResults() const { return getImpl()->numResults; }
55 
56 ArrayRef<Type> FunctionType::getResults() const {
57   return getImpl()->getResults();
58 }
59 
60 //===----------------------------------------------------------------------===//
61 // OpaqueType
62 //===----------------------------------------------------------------------===//
63 
64 OpaqueType OpaqueType::get(Identifier dialect, StringRef typeData,
65                            MLIRContext *context) {
66   return Base::get(context, Type::Kind::Opaque, dialect, typeData);
67 }
68 
69 OpaqueType OpaqueType::getChecked(Identifier dialect, StringRef typeData,
70                                   MLIRContext *context, Location location) {
71   return Base::getChecked(location, context, Kind::Opaque, dialect, typeData);
72 }
73 
74 /// Returns the dialect namespace of the opaque type.
75 Identifier OpaqueType::getDialectNamespace() const {
76   return getImpl()->dialectNamespace;
77 }
78 
79 /// Returns the raw type data of the opaque type.
80 StringRef OpaqueType::getTypeData() const { return getImpl()->typeData; }
81 
82 /// Verify the construction of an opaque type.
83 LogicalResult OpaqueType::verifyConstructionInvariants(Optional<Location> loc,
84                                                        MLIRContext *context,
85                                                        Identifier dialect,
86                                                        StringRef typeData) {
87   if (!Dialect::isValidNamespace(dialect.strref()))
88     return emitOptionalError(loc, "invalid dialect namespace '", dialect, "'");
89   return success();
90 }
91