• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/function.h"
16 
17 #include "src/ast/stage_decoration.h"
18 #include "src/ast/workgroup_decoration.h"
19 #include "src/program_builder.h"
20 
21 TINT_INSTANTIATE_TYPEINFO(tint::ast::Function);
22 
23 namespace tint {
24 namespace ast {
25 
Function(ProgramID pid,const Source & src,Symbol sym,VariableList parameters,const Type * return_ty,const BlockStatement * b,DecorationList decos,DecorationList return_type_decos)26 Function::Function(ProgramID pid,
27                    const Source& src,
28                    Symbol sym,
29                    VariableList parameters,
30                    const Type* return_ty,
31                    const BlockStatement* b,
32                    DecorationList decos,
33                    DecorationList return_type_decos)
34     : Base(pid, src),
35       symbol(sym),
36       params(std::move(parameters)),
37       return_type(return_ty),
38       body(b),
39       decorations(std::move(decos)),
40       return_type_decorations(std::move(return_type_decos)) {
41   TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, symbol, program_id);
42   TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, body, program_id);
43   for (auto* param : params) {
44     TINT_ASSERT(AST, param && param->is_const);
45     TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, param, program_id);
46   }
47   TINT_ASSERT(AST, symbol.IsValid());
48   TINT_ASSERT(AST, return_type);
49   for (auto* deco : decorations) {
50     TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, deco, program_id);
51   }
52   for (auto* deco : return_type_decorations) {
53     TINT_ASSERT_PROGRAM_IDS_EQUAL_IF_VALID(AST, deco, program_id);
54   }
55 }
56 
57 Function::Function(Function&&) = default;
58 
59 Function::~Function() = default;
60 
PipelineStage() const61 PipelineStage Function::PipelineStage() const {
62   if (auto* stage = GetDecoration<StageDecoration>(decorations)) {
63     return stage->stage;
64   }
65   return PipelineStage::kNone;
66 }
67 
Clone(CloneContext * ctx) const68 const Function* Function::Clone(CloneContext* ctx) const {
69   // Clone arguments outside of create() call to have deterministic ordering
70   auto src = ctx->Clone(source);
71   auto sym = ctx->Clone(symbol);
72   auto p = ctx->Clone(params);
73   auto* ret = ctx->Clone(return_type);
74   auto* b = ctx->Clone(body);
75   auto decos = ctx->Clone(decorations);
76   auto ret_decos = ctx->Clone(return_type_decorations);
77   return ctx->dst->create<Function>(src, sym, p, ret, b, decos, ret_decos);
78 }
79 
Find(Symbol sym) const80 const Function* FunctionList::Find(Symbol sym) const {
81   for (auto* func : *this) {
82     if (func->symbol == sym) {
83       return func;
84     }
85   }
86   return nullptr;
87 }
88 
Find(Symbol sym,PipelineStage stage) const89 const Function* FunctionList::Find(Symbol sym, PipelineStage stage) const {
90   for (auto* func : *this) {
91     if (func->symbol == sym && func->PipelineStage() == stage) {
92       return func;
93     }
94   }
95   return nullptr;
96 }
97 
HasStage(ast::PipelineStage stage) const98 bool FunctionList::HasStage(ast::PipelineStage stage) const {
99   for (auto* func : *this) {
100     if (func->PipelineStage() == stage) {
101       return true;
102     }
103   }
104   return false;
105 }
106 
107 }  // namespace ast
108 }  // namespace tint
109