xref: /llvm-project-15.0.7/mlir/lib/IR/Types.cpp (revision 4bee2afc)
1 //===- Types.cpp - MLIR Type 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/Types.h"
10 #include "TypeDetail.h"
11 #include "mlir/IR/Diagnostics.h"
12 #include "mlir/IR/Dialect.h"
13 #include "llvm/ADT/Twine.h"
14 
15 using namespace mlir;
16 using namespace mlir::detail;
17 
18 //===----------------------------------------------------------------------===//
19 // Type
20 //===----------------------------------------------------------------------===//
21 
22 unsigned Type::getKind() const { return impl->getKind(); }
23 
24 /// Get the dialect this type is registered to.
25 Dialect &Type::getDialect() const { return impl->getDialect(); }
26 
27 MLIRContext *Type::getContext() const { return getDialect().getContext(); }
28 
29 unsigned Type::getSubclassData() const { return impl->getSubclassData(); }
30 void Type::setSubclassData(unsigned val) { impl->setSubclassData(val); }
31 
32 //===----------------------------------------------------------------------===//
33 // FunctionType
34 //===----------------------------------------------------------------------===//
35 
36 FunctionType FunctionType::get(ArrayRef<Type> inputs, ArrayRef<Type> results,
37                                MLIRContext *context) {
38   return Base::get(context, Type::Kind::Function, inputs, results);
39 }
40 
41 ArrayRef<Type> FunctionType::getInputs() const {
42   return getImpl()->getInputs();
43 }
44 
45 unsigned FunctionType::getNumResults() const { return getImpl()->numResults; }
46 
47 ArrayRef<Type> FunctionType::getResults() const {
48   return getImpl()->getResults();
49 }
50 
51 //===----------------------------------------------------------------------===//
52 // OpaqueType
53 //===----------------------------------------------------------------------===//
54 
55 OpaqueType OpaqueType::get(Identifier dialect, StringRef typeData,
56                            MLIRContext *context) {
57   return Base::get(context, Type::Kind::Opaque, dialect, typeData);
58 }
59 
60 OpaqueType OpaqueType::getChecked(Identifier dialect, StringRef typeData,
61                                   MLIRContext *context, Location location) {
62   return Base::getChecked(location, Kind::Opaque, dialect, typeData);
63 }
64 
65 /// Returns the dialect namespace of the opaque type.
66 Identifier OpaqueType::getDialectNamespace() const {
67   return getImpl()->dialectNamespace;
68 }
69 
70 /// Returns the raw type data of the opaque type.
71 StringRef OpaqueType::getTypeData() const { return getImpl()->typeData; }
72 
73 /// Verify the construction of an opaque type.
74 LogicalResult OpaqueType::verifyConstructionInvariants(Location loc,
75                                                        Identifier dialect,
76                                                        StringRef typeData) {
77   if (!Dialect::isValidNamespace(dialect.strref()))
78     return emitError(loc, "invalid dialect namespace '") << dialect << "'";
79   return success();
80 }
81