1 // Copyright 2021 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0(the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/sem/constant.h"
16
17 #include <utility>
18
19 #include "src/debug.h"
20 #include "src/program_builder.h"
21 #include "src/sem/type.h"
22
23 namespace tint {
24 namespace sem {
25
26 namespace {
27
ElemType(const Type * ty,size_t num_elements)28 const Type* ElemType(const Type* ty, size_t num_elements) {
29 diag::List diag;
30 if (ty->is_scalar()) {
31 if (num_elements != 1) {
32 TINT_ICE(Semantic, diag)
33 << "sem::Constant() type <-> num_element mismatch. type: '"
34 << ty->type_name() << "' num_elements: " << num_elements;
35 }
36 return ty;
37 }
38 if (auto* vec = ty->As<Vector>()) {
39 if (num_elements != vec->Width()) {
40 TINT_ICE(Semantic, diag)
41 << "sem::Constant() type <-> num_element mismatch. type: '"
42 << ty->type_name() << "' num_elements: " << num_elements;
43 }
44 TINT_ASSERT(Semantic, vec->type()->is_scalar());
45 return vec->type();
46 }
47 TINT_UNREACHABLE(Semantic, diag) << "Unsupported sem::Constant type";
48 return nullptr;
49 }
50
51 } // namespace
52
Constant()53 Constant::Constant() {}
54
Constant(const sem::Type * ty,Scalars els)55 Constant::Constant(const sem::Type* ty, Scalars els)
56 : type_(ty), elem_type_(ElemType(ty, els.size())), elems_(std::move(els)) {}
57
58 Constant::Constant(const Constant&) = default;
59
60 Constant::~Constant() = default;
61
62 Constant& Constant::operator=(const Constant& rhs) = default;
63
64 } // namespace sem
65 } // namespace tint
66