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