xref: /llvm-project-15.0.7/mlir/lib/IR/Types.cpp (revision 56bb1d17)
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 Dialect &Type::getDialect() const {
25   return impl->getAbstractType().getDialect();
26 }
27 
28 MLIRContext *Type::getContext() const { return getDialect().getContext(); }
29 
30 unsigned Type::getSubclassData() const { return impl->getSubclassData(); }
31 void Type::setSubclassData(unsigned val) { impl->setSubclassData(val); }
32 
33 //===----------------------------------------------------------------------===//
34 // FunctionType
35 //===----------------------------------------------------------------------===//
36 
37 FunctionType FunctionType::get(ArrayRef<Type> inputs, ArrayRef<Type> results,
38                                MLIRContext *context) {
39   return Base::get(context, Type::Kind::Function, inputs, results);
40 }
41 
42 ArrayRef<Type> FunctionType::getInputs() const {
43   return getImpl()->getInputs();
44 }
45 
46 unsigned FunctionType::getNumResults() const { return getImpl()->numResults; }
47 
48 ArrayRef<Type> FunctionType::getResults() const {
49   return getImpl()->getResults();
50 }
51 
52 //===----------------------------------------------------------------------===//
53 // OpaqueType
54 //===----------------------------------------------------------------------===//
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, 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(Location loc,
76                                                        Identifier dialect,
77                                                        StringRef typeData) {
78   if (!Dialect::isValidNamespace(dialect.strref()))
79     return emitError(loc, "invalid dialect namespace '") << dialect << "'";
80   return success();
81 }
82