xref: /llvm-project-15.0.7/mlir/lib/IR/Types.cpp (revision dd0fdf80)
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 //===----------------------------------------------------------------------===//
31 // FunctionType
32 //===----------------------------------------------------------------------===//
33 
34 FunctionType FunctionType::get(TypeRange inputs, TypeRange results,
35                                MLIRContext *context) {
36   return Base::get(context, Type::Kind::Function, inputs, results);
37 }
38 
39 unsigned FunctionType::getNumInputs() const { return getImpl()->numInputs; }
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