Blender V5.3
FN_multi_function_procedure.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include "FN_multi_function.hh"
12
14
15class Variable;
16class Instruction;
17class CallInstruction;
22class Procedure;
23
32
38 public:
47
48 private:
49 Type type_ = None;
50 Instruction *instruction_ = nullptr;
51 /* Only used when it is a branch instruction. */
52 bool branch_output_ = false;
53
54 public:
55 InstructionCursor() = default;
60
62
63 Instruction *next(Procedure &procedure) const;
64 void set_next(Procedure &procedure, Instruction *new_instruction) const;
65
66 Instruction *instruction() const;
67
68 Type type() const;
69
70 friend bool operator==(const InstructionCursor &a, const InstructionCursor &b) = default;
71};
72
79 private:
80 DataType data_type_;
82 std::string name_;
83 int index_in_graph_;
84
85 friend Procedure;
86 friend CallInstruction;
87 friend BranchInstruction;
88 friend DestructInstruction;
89
90 public:
91 DataType data_type() const;
93
94 StringRefNull name() const;
95 void set_name(std::string name);
96
97 int index_in_procedure() const;
98};
99
122
128 private:
129 const MultiFunction *fn_ = nullptr;
130 Instruction *next_ = nullptr;
132
133 friend Procedure;
134
135 public:
136 const MultiFunction &fn() const;
137
138 Instruction *next();
139 const Instruction *next() const;
140 void set_next(Instruction *instruction);
141
142 void set_param_variable(int param_index, Variable *variable);
143 void set_params(Span<Variable *> variables);
144
147};
148
154 private:
155 Variable *condition_ = nullptr;
156 Instruction *branch_true_ = nullptr;
157 Instruction *branch_false_ = nullptr;
158
159 friend Procedure;
160
161 public:
163 const Variable *condition() const;
164 void set_condition(Variable *variable);
165
167 const Instruction *branch_true() const;
168 void set_branch_true(Instruction *instruction);
169
171 const Instruction *branch_false() const;
172 void set_branch_false(Instruction *instruction);
173};
174
182 private:
183 Variable *variable_ = nullptr;
184 Instruction *next_ = nullptr;
185
186 friend Procedure;
187
188 public:
190 const Variable *variable() const;
192
193 Instruction *next();
194 const Instruction *next() const;
195 void set_next(Instruction *instruction);
196};
197
202 private:
203 Instruction *next_ = nullptr;
204
205 friend Procedure;
206
207 public:
208 Instruction *next();
209 const Instruction *next() const;
210 void set_next(Instruction *instruction);
211};
212
217
225
230
240 private:
241 LinearAllocator<> allocator_;
242 Vector<CallInstruction *> call_instructions_;
243 Vector<BranchInstruction *> branch_instructions_;
244 Vector<DestructInstruction *> destruct_instructions_;
245 Vector<DummyInstruction *> dummy_instructions_;
246 Vector<ReturnInstruction *> return_instructions_;
247 Vector<Variable *> variables_;
248 Vector<Parameter> params_;
249 Vector<destruct_ptr<MultiFunction>> owned_functions_;
250 Instruction *entry_ = nullptr;
251
252 friend class ProcedureDotExport;
253
254 public:
255 Procedure() = default;
256 ~Procedure();
257
258 Variable &new_variable(DataType data_type, std::string name = "");
264
265 void add_parameter(ParamType::InterfaceType interface_type, Variable &variable);
267
268 template<typename T, typename... Args> const MultiFunction &construct_function(Args &&...args);
269
271 const Instruction *entry() const;
273
276
277 std::string to_dot() const;
278
279 bool validate() const;
280
282
283 private:
284 bool validate_all_instruction_pointers_set() const;
285 bool validate_all_params_provided() const;
286 bool validate_same_variables_in_one_call() const;
287 bool validate_parameters() const;
288 bool validate_initialization() const;
289
290 struct InitState {
291 bool can_be_initialized = false;
292 bool can_be_uninitialized = false;
293 };
294
295 InitState find_initialization_state_before_instruction(const Instruction &target_instruction,
296 const Variable &variable) const;
297};
298
299/* -------------------------------------------------------------------- */
302
307
312
314 : type_(Branch), instruction_(&instruction), branch_output_(branch_output)
315{
316}
317
322
324{
325 InstructionCursor cursor;
326 cursor.type_ = Type::Entry;
327 return cursor;
328}
329
331{
332 /* This isn't really const correct unfortunately, because to make it correct we'll need a const
333 * version of #InstructionCursor. */
334 return instruction_;
335}
336
338{
339 return type_;
340}
341
343
344/* -------------------------------------------------------------------- */
347
349{
350 return data_type_;
351}
352
354{
355 return users_;
356}
357
359{
360 return name_;
361}
362
364{
365 return index_in_graph_;
366}
367
369
370/* -------------------------------------------------------------------- */
373
375{
376 return type_;
377}
378
380{
381 return prev_;
382}
383
385
386/* -------------------------------------------------------------------- */
389
391{
392 return *fn_;
393}
394
396{
397 return next_;
398}
399
401{
402 return next_;
403}
404
406{
407 return params_;
408}
409
411{
412 return params_;
413}
414
416
417/* -------------------------------------------------------------------- */
420
422{
423 return condition_;
424}
425
427{
428 return condition_;
429}
430
432{
433 return branch_true_;
434}
435
437{
438 return branch_true_;
439}
440
442{
443 return branch_false_;
444}
445
447{
448 return branch_false_;
449}
450
452
453/* -------------------------------------------------------------------- */
456
458{
459 return variable_;
460}
461
463{
464 return variable_;
465}
466
468{
469 return next_;
470}
471
473{
474 return next_;
475}
476
478
479/* -------------------------------------------------------------------- */
482
484{
485 return next_;
486}
487
489{
490 return next_;
491}
492
494
495/* -------------------------------------------------------------------- */
498
500{
501 static_assert(sizeof(Parameter) == sizeof(ConstParameter));
502 return params_.as_span().cast<ConstParameter>();
503}
504
506{
507 return entry_;
508}
509
510inline const Instruction *Procedure::entry() const
511{
512 return entry_;
513}
514
516{
517 return variables_;
518}
519
521{
522 return variables_;
523}
524
525template<typename T, typename... Args>
526inline const MultiFunction &Procedure::construct_function(Args &&...args)
527{
528 destruct_ptr<T> fn = allocator_.construct<T>(std::forward<Args>(args)...);
529 const MultiFunction &fn_ref = *fn;
530 owned_functions_.append(std::move(fn));
531 return fn_ref;
532}
533
535
536} // namespace blender::fn::multi_function
Span< NewT > constexpr cast() const
Definition BLI_span.hh:422
NonCopyable(const NonCopyable &other)=delete
NonMovable(NonMovable &&other)=delete
void set_param_variable(int param_index, Variable *variable)
friend bool operator==(const InstructionCursor &a, const InstructionCursor &b)=default
void set_next(Procedure &procedure, Instruction *new_instruction) const
Instruction * next(Procedure &procedure) const
CallInstruction & new_call_instruction(const MultiFunction &fn)
const MultiFunction & construct_function(Args &&...args)
void add_parameter(ParamType::InterfaceType interface_type, Variable &variable)
Variable & new_variable(DataType data_type, std::string name="")
#define T
std::unique_ptr< T, DestructValueAtAddress< T > > destruct_ptr
@ Return
Definition types.hh:115