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 
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 }
22 TypeRange::TypeRange(OperandRange values)
23     : TypeRange(values.begin().getBase(), values.size()) {}
24 TypeRange::TypeRange(ResultRange values)
25     : TypeRange(values.getBase(), values.size()) {}
26 TypeRange::TypeRange(ArrayRef<Value> values)
27     : TypeRange(values.data(), values.size()) {}
28 TypeRange::TypeRange(ValueRange values) : TypeRange(OwnerT(), values.size()) {
29   if (count == 0)
30     return;
31   ValueRange::OwnerT owner = values.begin().getBase();
32   if (auto *result = owner.dyn_cast<detail::OpResultImpl *>())
33     this->base = result;
34   else if (auto *operand = owner.dyn_cast<OpOperand *>())
35     this->base = operand;
36   else
37     this->base = owner.get<const Value *>();
38 }
39 
40 /// See `llvm::detail::indexed_accessor_range_base` for details.
41 TypeRange::OwnerT TypeRange::offset_base(OwnerT object, ptrdiff_t index) {
42   if (const auto *value = object.dyn_cast<const Value *>())
43     return {value + index};
44   if (auto *operand = object.dyn_cast<OpOperand *>())
45     return {operand + index};
46   if (auto *result = object.dyn_cast<detail::OpResultImpl *>())
47     return {result->getNextResultAtOffset(index)};
48   return {object.dyn_cast<const Type *>() + index};
49 }
50 
51 /// See `llvm::detail::indexed_accessor_range_base` for details.
52 Type TypeRange::dereference_iterator(OwnerT object, ptrdiff_t index) {
53   if (const auto *value = object.dyn_cast<const Value *>())
54     return (value + index)->getType();
55   if (auto *operand = object.dyn_cast<OpOperand *>())
56     return (operand + index)->get().getType();
57   if (auto *result = object.dyn_cast<detail::OpResultImpl *>())
58     return result->getNextResultAtOffset(index)->getType();
59   return object.dyn_cast<const Type *>()[index];
60 }
61