1 /**
2 * Copyright 2019 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "ir/dtype.h"
18 #include <cstdlib>
19 #include "utils/log_adapter.h"
20 #include "utils/ms_utils.h"
21
22 namespace mindspore {
DeepCopy() const23 TypePtr Keyword::DeepCopy() const {
24 if (IsGeneric()) {
25 return std::make_shared<Keyword>();
26 } else {
27 MS_EXCEPTION_IF_NULL(value_);
28 std::string key = key_;
29 return std::make_shared<Keyword>(key, value_->DeepCopy());
30 }
31 }
32
ToString() const33 std::string Keyword::ToString() const {
34 std::ostringstream buffer;
35 if (IsGeneric()) {
36 buffer << "Keyword";
37 } else {
38 MS_EXCEPTION_IF_NULL(value_);
39 buffer << "Keyword[";
40 buffer << "key : " << key_;
41 buffer << ", value : " << value_->ToString();
42 buffer << "]";
43 }
44 return buffer.str();
45 }
46
operator ==(const Type & other) const47 bool Keyword::operator==(const Type &other) const {
48 if (!IsSameObjectType(*this, other)) {
49 return false;
50 }
51 const auto &other_keyword = static_cast<const Keyword &>(other);
52 return (other_keyword.key_ == key_ && *other_keyword.value_ == *value_);
53 }
54
DumpText() const55 std::string Keyword::DumpText() const { return ToString(); }
56
DeepCopy() const57 TypePtr Slice::DeepCopy() const {
58 if (IsGeneric()) {
59 return std::make_shared<Slice>();
60 } else {
61 MS_EXCEPTION_IF_NULL(start_);
62 MS_EXCEPTION_IF_NULL(stop_);
63 MS_EXCEPTION_IF_NULL(step_);
64 auto copy = std::make_shared<Slice>(start_->DeepCopy(), stop_->DeepCopy(), step_->DeepCopy());
65 return copy;
66 }
67 }
68
ToString() const69 std::string Slice::ToString() const {
70 std::ostringstream buffer;
71 if (IsGeneric()) {
72 buffer << "Slice";
73 } else {
74 MS_EXCEPTION_IF_NULL(start_);
75 MS_EXCEPTION_IF_NULL(stop_);
76 MS_EXCEPTION_IF_NULL(step_);
77 buffer << "Slice[";
78 buffer << start_->ToString() << " : ";
79 buffer << stop_->ToString() << " : ";
80 buffer << step_->ToString();
81 buffer << "]";
82 }
83 return buffer.str();
84 }
85
operator ==(const Type & other) const86 bool Slice::operator==(const Type &other) const {
87 if (!IsSameObjectType(*this, other)) {
88 return false;
89 }
90 auto &other_slice = static_cast<const Slice &>(other);
91 return common::IsEqual(start_, other_slice.start_) && common::IsEqual(stop_, other_slice.stop_) &&
92 common::IsEqual(step_, other_slice.step_);
93 }
94
DumpText() const95 std::string Slice::DumpText() const { return ToString(); }
96
Function()97 Function::Function() : Object(kObjectTypeFunction) {
98 args_ = std::vector<TypePtr>();
99 retval_ = nullptr;
100 }
101
Function(const std::vector<TypePtr> & args,const TypePtr retval)102 Function::Function(const std::vector<TypePtr> &args, const TypePtr retval)
103 : Object(kObjectTypeFunction, false), args_(args), retval_(retval) {}
104
DeepCopy() const105 TypePtr Function::DeepCopy() const {
106 if (IsGeneric()) {
107 return std::make_shared<Function>();
108 } else {
109 TypePtrList args;
110 TypePtr retval = nullptr;
111 (void)std::transform(args_.begin(), args_.end(), std::back_inserter(args),
112 [](const TypePtr &arg) { return arg->DeepCopy(); });
113 if (retval_ != nullptr) {
114 retval = retval_->DeepCopy();
115 }
116 return std::make_shared<Function>(args, retval);
117 }
118 }
119
operator ==(const Type & other) const120 bool Function::operator==(const Type &other) const {
121 if (!IsSameObjectType(*this, other)) {
122 return false;
123 }
124
125 const auto &other_function = static_cast<const Function &>(other);
126 if ((retval_ != nullptr) && (other_function.retval_ != nullptr)) {
127 if (*retval_ != *other_function.retval_) {
128 return false;
129 }
130 } else if ((retval_ == nullptr) && (other_function.retval_ != nullptr)) {
131 return false;
132 }
133 if (args_.size() != other_function.args_.size()) {
134 return false;
135 }
136 for (size_t i = 0; i < args_.size(); ++i) {
137 MS_EXCEPTION_IF_NULL(args_[i]);
138 MS_EXCEPTION_IF_NULL(other_function.args_[i]);
139 if (*args_[i] != *other_function.args_[i]) {
140 return false;
141 }
142 }
143 return true;
144 }
145
ToString() const146 std::string Function::ToString() const {
147 std::ostringstream buffer;
148 if (IsGeneric()) {
149 buffer << "Func";
150 } else {
151 buffer << "Func[(";
152 bool begin = true;
153 for (auto &attr : args_) {
154 if (!begin) {
155 buffer << ", ";
156 } else {
157 begin = false;
158 }
159 buffer << attr->ToString();
160 }
161 buffer << ")";
162 if (retval_ != nullptr) {
163 buffer << ", " << retval_->ToString() << "]";
164 } else {
165 buffer << "]";
166 }
167 }
168 return buffer.str();
169 }
170
DeepCopy() const171 TypePtr JTagged::DeepCopy() const {
172 MS_EXCEPTION_IF_NULL(subtype_);
173 if (IsGeneric()) {
174 return std::make_shared<JTagged>();
175 } else {
176 auto subtype = subtype_->DeepCopy();
177 return std::make_shared<JTagged>(subtype);
178 }
179 }
180
ToString() const181 std::string JTagged::ToString() const {
182 MS_EXCEPTION_IF_NULL(subtype_);
183 std::ostringstream buffer;
184 if (IsGeneric()) {
185 buffer << "JT";
186 } else {
187 buffer << "JT[";
188 buffer << subtype_->ToString() << "]";
189 }
190 return buffer.str();
191 }
192
DumpText() const193 std::string JTagged::DumpText() const {
194 MS_EXCEPTION_IF_NULL(subtype_);
195 std::ostringstream buffer;
196 if (IsGeneric()) {
197 buffer << "JT";
198 } else {
199 buffer << "JT[";
200 buffer << subtype_->DumpText() << "]";
201 }
202 return buffer.str();
203 }
204
operator <<(std::ostream & os,const std::shared_ptr<Problem> problem)205 std::ostream &operator<<(std::ostream &os, const std::shared_ptr<Problem> problem) {
206 MS_EXCEPTION_IF_NULL(problem);
207 os << problem->ToString();
208 return os;
209 }
210 } // namespace mindspore
211