1 // Copyright 2020 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/ast/array.h"
16 
17 #include <cmath>
18 
19 #include "src/program_builder.h"
20 
21 TINT_INSTANTIATE_TYPEINFO(tint::ast::Array);
22 
23 namespace tint {
24 namespace ast {
25 
26 namespace {
27 // Returns the string representation of an array size expression.
SizeExprToString(const Expression* size, const SymbolTable& symbols)28 std::string SizeExprToString(const Expression* size,
29                              const SymbolTable& symbols) {
30   if (auto* ident = size->As<IdentifierExpression>()) {
31     return symbols.NameFor(ident->symbol);
32   }
33   if (auto* literal = size->As<IntLiteralExpression>()) {
34     return std::to_string(literal->ValueAsU32());
35   }
36   // This will never be exposed to the user as the Resolver will reject this
37   // expression for array size.
38   return "<invalid>";
39 }
40 }  // namespace
41 
Array(ProgramID pid, const Source& src, const Type* subtype, const Expression* cnt, DecorationList decos)42 Array::Array(ProgramID pid,
43              const Source& src,
44              const Type* subtype,
45              const Expression* cnt,
46              DecorationList decos)
47     : Base(pid, src), type(subtype), count(cnt), decorations(decos) {}
48 
49 Array::Array(Array&&) = default;
50 
51 Array::~Array() = default;
52 
FriendlyName(const SymbolTable& symbols) const53 std::string Array::FriendlyName(const SymbolTable& symbols) const {
54   std::ostringstream out;
55   for (auto* deco : decorations) {
56     if (auto* stride = deco->As<ast::StrideDecoration>()) {
57       out << "[[stride(" << stride->stride << ")]] ";
58     }
59   }
60   out << "array<" << type->FriendlyName(symbols);
61   if (!IsRuntimeArray()) {
62     out << ", " << SizeExprToString(count, symbols);
63   }
64   out << ">";
65   return out.str();
66 }
67 
Clone(CloneContext* ctx) const68 const Array* Array::Clone(CloneContext* ctx) const {
69   // Clone arguments outside of create() call to have deterministic ordering
70   auto src = ctx->Clone(source);
71   auto* ty = ctx->Clone(type);
72   auto* cnt = ctx->Clone(count);
73   auto decos = ctx->Clone(decorations);
74   return ctx->dst->create<Array>(src, ty, cnt, decos);
75 }
76 
77 }  // namespace ast
78 }  // namespace tint
79