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 getImpl()->getInputs(); 45 } 46 47 unsigned FunctionType::getNumResults() const { return getImpl()->numResults; } 48 49 ArrayRef<Type> FunctionType::getResults() const { 50 return getImpl()->getResults(); 51 } 52 53 /// UnknownType 54 55 UnknownType UnknownType::get(Identifier dialect, StringRef typeData, 56 MLIRContext *context) { 57 return Base::get(context, Type::Kind::Unknown, dialect, typeData); 58 } 59 60 UnknownType UnknownType::getChecked(Identifier dialect, StringRef typeData, 61 MLIRContext *context, Location location) { 62 return Base::getChecked(location, context, Kind::Unknown, dialect, typeData); 63 } 64 65 /// Returns the dialect namespace of the unknown type. 66 Identifier UnknownType::getDialectNamespace() const { 67 return getImpl()->dialectNamespace; 68 } 69 70 /// Returns the raw type data of the unknown type. 71 StringRef UnknownType::getTypeData() const { return getImpl()->typeData; } 72 73 /// Verify the construction of an unknown type. 74 LogicalResult UnknownType::verifyConstructionInvariants( 75 llvm::Optional<Location> loc, MLIRContext *context, Identifier dialect, 76 StringRef typeData) { 77 if (!Dialect::isValidNamespace(dialect.strref())) { 78 if (loc) 79 context->emitError(*loc, "invalid dialect namespace '" + 80 dialect.strref() + "'"); 81 return failure(); 82 } 83 return success(); 84 } 85