1 //===- TypeRange.cpp ------------------------------------------------------===//
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/TypeRange.h"
10 #include "mlir/IR/Operation.h"
11
12 using namespace mlir;
13
14 //===----------------------------------------------------------------------===//
15 // TypeRange
16
TypeRange(ArrayRef<Type> types)17 TypeRange::TypeRange(ArrayRef<Type> types)
18 : TypeRange(types.data(), types.size()) {
19 assert(llvm::all_of(types, [](Type t) { return t; }) &&
20 "attempting to construct a TypeRange with null types");
21 }
TypeRange(OperandRange values)22 TypeRange::TypeRange(OperandRange values)
23 : TypeRange(values.begin().getBase(), values.size()) {}
TypeRange(ResultRange values)24 TypeRange::TypeRange(ResultRange values)
25 : TypeRange(values.getBase(), values.size()) {}
TypeRange(ValueRange values)26 TypeRange::TypeRange(ValueRange values) : TypeRange(OwnerT(), values.size()) {
27 if (count == 0)
28 return;
29 ValueRange::OwnerT owner = values.begin().getBase();
30 if (auto *result = owner.dyn_cast<detail::OpResultImpl *>())
31 this->base = result;
32 else if (auto *operand = owner.dyn_cast<OpOperand *>())
33 this->base = operand;
34 else
35 this->base = owner.get<const Value *>();
36 }
37
38 /// See `llvm::detail::indexed_accessor_range_base` for details.
offset_base(OwnerT object,ptrdiff_t index)39 TypeRange::OwnerT TypeRange::offset_base(OwnerT object, ptrdiff_t index) {
40 if (const auto *value = object.dyn_cast<const Value *>())
41 return {value + index};
42 if (auto *operand = object.dyn_cast<OpOperand *>())
43 return {operand + index};
44 if (auto *result = object.dyn_cast<detail::OpResultImpl *>())
45 return {result->getNextResultAtOffset(index)};
46 return {object.dyn_cast<const Type *>() + index};
47 }
48
49 /// See `llvm::detail::indexed_accessor_range_base` for details.
dereference_iterator(OwnerT object,ptrdiff_t index)50 Type TypeRange::dereference_iterator(OwnerT object, ptrdiff_t index) {
51 if (const auto *value = object.dyn_cast<const Value *>())
52 return (value + index)->getType();
53 if (auto *operand = object.dyn_cast<OpOperand *>())
54 return (operand + index)->get().getType();
55 if (auto *result = object.dyn_cast<detail::OpResultImpl *>())
56 return result->getNextResultAtOffset(index)->getType();
57 return object.dyn_cast<const Type *>()[index];
58 }
59