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/call_expression.h"
16
17 #include "src/program_builder.h"
18
19 TINT_INSTANTIATE_TYPEINFO(tint::ast::CallExpression);
20
21 namespace tint {
22 namespace ast {
23
24 namespace {
ToTarget(const IdentifierExpression * name)25 CallExpression::Target ToTarget(const IdentifierExpression* name) {
26 CallExpression::Target target;
27 target.name = name;
28 return target;
29 }
ToTarget(const Type * type)30 CallExpression::Target ToTarget(const Type* type) {
31 CallExpression::Target target;
32 target.type = type;
33 return target;
34 }
35 } // namespace
36
CallExpression(ProgramID pid,const Source & src,const IdentifierExpression * name,ExpressionList a)37 CallExpression::CallExpression(ProgramID pid,
38 const Source& src,
39 const IdentifierExpression* name,
40 ExpressionList a)
41 : Base(pid, src), target(ToTarget(name)), args(a) {
42 TINT_ASSERT(AST, name);
43 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, name, program_id);
44 for (auto* arg : args) {
45 TINT_ASSERT(AST, arg);
46 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, arg, program_id);
47 }
48 }
49
CallExpression(ProgramID pid,const Source & src,const Type * type,ExpressionList a)50 CallExpression::CallExpression(ProgramID pid,
51 const Source& src,
52 const Type* type,
53 ExpressionList a)
54 : Base(pid, src), target(ToTarget(type)), args(a) {
55 TINT_ASSERT(AST, type);
56 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, type, program_id);
57 for (auto* arg : args) {
58 TINT_ASSERT(AST, arg);
59 TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, arg, program_id);
60 }
61 }
62
63 CallExpression::CallExpression(CallExpression&&) = default;
64
65 CallExpression::~CallExpression() = default;
66
Clone(CloneContext * ctx) const67 const CallExpression* CallExpression::Clone(CloneContext* ctx) const {
68 // Clone arguments outside of create() call to have deterministic ordering
69 auto src = ctx->Clone(source);
70 auto p = ctx->Clone(args);
71 return target.name
72 ? ctx->dst->create<CallExpression>(src, ctx->Clone(target.name), p)
73 : ctx->dst->create<CallExpression>(src, ctx->Clone(target.type),
74 p);
75 }
76
77 } // namespace ast
78 } // namespace tint
79