summaryrefslogtreecommitdiff
path: root/lib/EventFlow/src/evfl/Flowchart.cpp
blob: 84c0af05329ec9c2cc94f589a8187212b2e612d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
#include <algorithm>
#include <evfl/Action.h>
#include <evfl/Flowchart.h>
#include <evfl/Param.h>
#include <evfl/Query.h>
#include <evfl/ResFlowchart.h>
#include <iterator>
#include <ore/Array.h>
#include <ore/BitUtils.h>
#include <ore/IterRange.h>
#include <ore/ResDic.h>
#include <ore/StringView.h>

namespace evfl {

int FlowchartContext::s_GlobalCounter{};

FlowchartContext::FlowchartContext() {
    m_handlers.SetOffset(ActionDoneHandler::GetListNodeOffset());
}

void FlowchartContext::Start(MetaDataPack* pack) {
    auto& obj = m_objs[m_obj_idx];
    const auto& entry_point = obj.GetFlowchart()->entry_points.Get()[m_active_entry_point_idx];

    const auto main_event_idx = entry_point.main_event_idx;
    if (main_event_idx == 0xffff)
        return;

    m_metadata_pack = pack;

    const int node_idx = AllocNode();
    auto& node = m_nodes[node_idx];
    node.m_obj = &obj;
    node.m_event_idx = main_event_idx;
    node.m_next_node_idx = -1;
    node.m_idx = node_idx;
    node.m_state = FlowchartContextNode::State::kNotInvoked;
    AllocVariablePack(node, entry_point);

    ProcessContext();
}

int FlowchartContext::AllocNode() {
    int idx = m_next_node_idx;

    if (idx == 0xffff) {
        idx = m_nodes.size();
        m_nodes.Resize(2 * m_nodes.size());
        for (int i = idx, n = m_nodes.size(); i < n - 1; ++i) {
            m_nodes[i].m_next_node_idx = i + 1;
            m_nodes[i].m_state = FlowchartContextNode::State::kFree;
        }
    }

    auto& node = m_nodes[idx];
    m_next_node_idx = node.m_next_node_idx;
    node.m_event_idx = -1;
    node.m_next_node_idx = -1;
    node.m_idx = -1;
    node.m_owns_variable_pack = false;
    node.m_obj = nullptr;
    node.m_variable_pack = nullptr;
    node.m_node_counter = ++s_GlobalCounter;
    node.m_state = FlowchartContextNode::State::kInvalid;
    ++m_num_allocated_nodes;
    return idx;
}

void FlowchartContext::AllocVariablePack(FlowchartContextNode& node,
                                         const ResEntryPoint& entry_point) {
    FreeVariablePack(node);

    if (entry_point.num_variable_defs != 0) {
        auto* pack = m_allocator.New<VariablePack>();
        pack->Init(m_allocator.GetArg(), &entry_point);
        node.m_variable_pack = pack;
        node.m_owns_variable_pack = true;
    }
}

void FlowchartContext::ProcessContext() {
    if (m_is_processing) {
        _91 = 1;
    } else {
        m_is_processing = true;
        do {
            _91 = 0;
            for (int i = 0, n = m_nodes.size(); i < n; ++i) {
                if (!m_nodes[i].IsInvalidOrFree())
                    _91 = (ProcessContextNode(i) | (_91 != 0)) & 1;
            }
        } while (_91);
        m_is_processing = false;
    }
}

FlowchartObj* FlowchartContext::FindFlowchartObj(ore::StringView name) {
    auto it = std::find_if(m_objs.begin(), m_objs.end(), [&](const FlowchartObj& obj) {
        return name == *obj.GetFlowchart()->name.Get();
    });
    return it == m_objs.end() ? nullptr : it;
}

const FlowchartObj* FlowchartContext::FindFlowchartObj(ore::StringView name) const {
    auto it = std::find_if(m_objs.begin(), m_objs.end(), [&](const FlowchartObj& obj) {
        return name == *obj.GetFlowchart()->name.Get();
    });
    return it == m_objs.end() ? nullptr : it;
}

void FlowchartContext::UnbindAll() {
    Clear();
    for (auto it = m_objs.begin(); it != m_objs.end(); ++it)
        it->GetActBinder().UnbindAll();
}

void FlowchartContext::Clear() {
    for (int i = 0, n = m_nodes.size(); i < n; ++i) {
        FreeVariablePack(m_nodes[i]);
        m_nodes[i].Reset();
        if (i < n - 1) {
            m_nodes[i].m_next_node_idx = i + 1;
            m_nodes[i].m_state = FlowchartContextNode::State::kFree;
        }
    }

    m_next_node_idx = 0;
    m_num_allocated_nodes = 0;
    m_metadata_pack = nullptr;

    while (!m_handlers.Empty()) {
        auto* handler = m_handlers.Front();
        handler->m_list_node.Erase();
        handler->Reset();
    }
}

void FlowchartContext::FreeVariablePack(FlowchartContextNode& node) {
    if (node.m_variable_pack && node.m_owns_variable_pack)
        m_allocator.DeleteAndNull(node.m_variable_pack);

    node.m_variable_pack = nullptr;
    node.m_owns_variable_pack = false;
}

void FlowchartContext::CopyVariablePack(FlowchartContextNode& src, FlowchartContextNode& dst) {
    FreeVariablePack(dst);
    dst.m_variable_pack = src.m_variable_pack;
    dst.m_owns_variable_pack = false;
}

bool FlowchartContext::ProcessContextNode(int node_idx) {
    using State = FlowchartContextNode::State;
    auto& node = GetNode(node_idx);
    while (true) {
        auto* obj = node.m_obj;
        const auto* flowchart = obj->GetFlowchart();
        const auto& event = flowchart->events.Get()[node.m_event_idx];

        int next_event_idx = -1;

        switch (event.type) {
        case ResEvent::EventType::kAction: {
            switch (node.m_state) {
            case State::kNotInvoked: {
                node.m_state = State::kInvoked;

                const auto actor_idx = event.actor_idx;
#ifdef EVFL_VER_LABO
                const auto& actor = flowchart->actors.Get()[actor_idx];
#else
                const auto& actor = obj->GetFlowchart()->actors.Get()[actor_idx];
#endif
                const auto* actions = actor.actions.Get();
                const auto* arg_name = actor.argument_name.Get();
                const auto action_idx = event.actor_action_idx;

                const auto* binding = &obj->GetActBinder().GetBindings()[actor_idx];
                if (!arg_name->empty()) {
                    binding = TrackBackArgumentActor(node_idx, *arg_name);
                    if (!binding)
                        return false;
                }

                const auto* action = binding->GetAction(*actions[action_idx].name.Get());
                const ActionArg arg(this, node_idx, binding->GetUserData(), action->user_data,
                                    node.m_variable_pack, &event);
                ActionDoneHandler done_handler(obj, this, node_idx);
                m_handlers.InsertFront(&done_handler);
                action->handler(arg, std::move(done_handler));
                return false;
            }  // action case State::kNotInvoked
            case State::kDone:
                next_event_idx = event.next_event_idx;
                break;
            default:
                return false;
            }
            break;
        }  // case ResEvent::EventType::kAction

        case ResEvent::EventType::kSwitch: {
            if (node.m_state != State::kNotInvoked)
                return false;

            const auto actor_idx = event.actor_idx;
            const auto& actor = flowchart->actors.Get()[actor_idx];
            const auto* queries = actor.queries.Get();
            const auto* arg_name = actor.argument_name.Get();
            const auto query_idx = event.actor_query_idx;

            const auto* binding = &obj->GetActBinder().GetBindings()[actor_idx];
            if (!arg_name->empty()) {
                binding = TrackBackArgumentActor(node_idx, *arg_name);
                if (!binding)
                    return false;
            }

            const auto* query = binding->GetQuery(*queries[query_idx].name.Get());
            const QueryArg arg(this, node_idx, binding->GetUserData(), query->user_data, &event,
                               node.m_variable_pack);
            const int result = query->handler(arg);

            next_event_idx = 0xffff;
            ore::Array<const ResCase> cases{event.cases.Get(), event.num_cases};
            for (const auto& case_ : cases) {
                if (case_.value == result) {
                    next_event_idx = case_.event_idx;
                    break;
                }
            }
            break;
        }  // case ResEvent::EventType::kSwitch

        case ResEvent::EventType::kFork: {
            if (node.m_state != State::kNotInvoked)
                return false;

            node.m_event_idx = event.join_event_idx;
            node.m_state = State::kInvoked;
            UpdateNodeCounter(node_idx);

            int prev_fork_node_idx = -1;
            int first_fork_node_idx = -1;
            int last_fork_node_idx = -1;

            const ore::Array<const u16> forks{event.fork_event_indices.Get(), event.num_forks};
            for (const auto& fork : forks) {
                last_fork_node_idx = AllocNode();
                auto& fork_node = GetNode(last_fork_node_idx);
                fork_node.m_obj = obj;
                fork_node.m_event_idx = fork;
                fork_node.m_next_node_idx = node_idx;
                fork_node.m_idx = prev_fork_node_idx;
                fork_node.m_state = State::kNotInvoked;
                if (first_fork_node_idx == -1)
                    first_fork_node_idx = last_fork_node_idx;
                CopyVariablePack(node, fork_node);
                prev_fork_node_idx = last_fork_node_idx;
            }

            GetNode(first_fork_node_idx).m_idx = u16(last_fork_node_idx);
            return true;
        }  // case ResEvent::EventType::kFork

        case ResEvent::EventType::kJoin: {
            if (node.m_state != State::kDone)
                return false;
            next_event_idx = event.next_event_idx;
            break;
        }  // case ResEvent::EventType::kJoin

        case ResEvent::EventType::kSubFlow: {
            bool called;
            bool valid_parameters;
            bool tried_invoking = false;
            switch (node.m_state) {
            case State::kNotInvoked: {
                tried_invoking = true;
                node.m_state = State::kInvoked;

                const ore::StringView sub_flow_flowchart = *event.sub_flow_flowchart.Get();
                if (!sub_flow_flowchart.empty()) {
                    obj = FindFlowchartObj(sub_flow_flowchart);
                    if (obj == nullptr) {
                        called = false;
                        valid_parameters = false;
                        break;
                    }
                }

                const int entry_point_idx = obj->GetFlowchart()->entry_point_names.Get()->FindIndex(
                    *event.sub_flow_entry_point.Get());
                if (entry_point_idx == -1) {
                    called = false;
                    valid_parameters = false;
                    break;
                }

                const auto& entry_point = obj->GetFlowchart()->entry_points.Get()[entry_point_idx];

                const u16 main_event_idx = entry_point.main_event_idx;
                if (main_event_idx == 0xffff) {
                    node.m_state = State::kDone;
                    called = false;
                    valid_parameters = true;
                    break;
                }

                // Optimization: if this is a tail call, we don't need to allocate a new node.
                if (event.next_event_idx == 0xffff && event.params.Get() == nullptr) {
                    node.m_obj = obj;
                    node.m_event_idx = main_event_idx;
                    node.m_state = State::kNotInvoked;
                    AllocVariablePack(node, entry_point);
                    UpdateNodeCounter(node_idx);
                } else {
                    const auto sub_flow_node_idx = AllocNode();
                    auto& sub_flow_node = GetNode(sub_flow_node_idx);
                    sub_flow_node.m_obj = obj;
                    sub_flow_node.m_event_idx = main_event_idx;
                    sub_flow_node.m_next_node_idx = node_idx;
                    sub_flow_node.m_idx = sub_flow_node_idx;
                    sub_flow_node.m_state = State::kNotInvoked;
                    AllocVariablePack(sub_flow_node, entry_point);
                }
                CallSubFlowCallback(flowchart, &event, SubFlowCallbackType::kEnter);
                called = true;
                /// @bug valid_parameters should have been initialized to true here.
                /// This bug causes LLVM to generate dumb code like
                ///     mov w8, wzr; mov w9, wzr; orr w8, w8, w9   (for the failure cases above)
                /// or more worryingly:
                ///     mov w8, #1;  orr w8, w8, w9
                /// where w9 is actually undefined!
#ifdef AVOID_UB
                valid_parameters = true;
#endif
                break;
            }  // subflow case State::kNotInvoked
            case State::kInvoked:
                return false;
            case State::kDone:
                next_event_idx = event.next_event_idx;
                CallSubFlowCallback(flowchart, &event, SubFlowCallbackType::kLeave);
                break;
            default:
                break;
            }
            if (tried_invoking)
                return valid_parameters || called;
            break;
        }  // case ResEvent::EventType::kSubFlow
        }

        // We are checking for 0xffff (a 0xffff that comes from the ResEvent data), *not* -1.
        if (next_event_idx == 0xffff) {
            bool ready = false;
            auto* node_2 = &node;
            if (node.m_idx != node_idx) {
                do {
                    node_2 = &GetNode(node_2->m_idx);
                    ready |= node_2->m_state != State::kWaiting;
                } while (node_2->m_idx != node_idx);
            }

            if (!ready) {
                if (node.m_next_node_idx != 0xffff)
                    GetNode(node.m_next_node_idx).m_state = State::kDone;

                int next_node_idx = node_idx;
                int next;
                do {
                    auto& node_to_free = GetNode(next_node_idx);
                    next = node_to_free.m_idx;
                    FreeVariablePack(node_to_free);
                    node_to_free.Reset();
                    node_to_free.m_next_node_idx = m_next_node_idx;
                    node_to_free.m_state = State::kFree;
                    m_next_node_idx = next_node_idx;
                    --m_num_allocated_nodes;
                    next_node_idx = next;
                } while (next != node_idx);
                return true;
            }

            if (event.type == ResEvent::EventType::kAction) {
                node.m_state = State::kWaiting;
                return true;
            }

            node_2->m_idx = node.m_idx;
            auto& node_to_free = GetNode(node_idx);
            FreeVariablePack(node_to_free);
            node_to_free.Reset();
            node_to_free.m_next_node_idx = m_next_node_idx;
            node_to_free.m_state = State::kFree;
            m_next_node_idx = node_idx;
            --m_num_allocated_nodes;
            return true;
        }

        node.m_event_idx = next_event_idx;
        node.m_state = State::kNotInvoked;
        UpdateNodeCounter(node_idx);
    }
}

ActorBinding* FlowchartContext::TrackBackArgumentActor(int node_idx, const ore::StringView& name) {
    if (node_idx == -1)
        return nullptr;

    const ore::ResMetaData* metadata;
    const MetaDataPack* metadata_pack;
    const VariablePack* variable_pack;
    FlowchartObj* obj;
    const ParamAccessor accessor{this, GetNode(node_idx).GetNextNodeIdx()};
    const auto real_name =
        accessor.TrackBackArgument(&metadata, &metadata_pack, &variable_pack, &obj, name);

    if (real_name.empty() || !metadata || !obj)
        return nullptr;

    const auto* param = metadata->Get(real_name, ore::ResMetaData::DataType::kActorIdentifier);
    if (!param)
        return nullptr;

    const ore::StringView actor_name = *param->value.actor.name.Get();
    const ore::StringView actor_sub_name = *param->value.actor.sub_name.Get();

    auto actor = obj->GetFlowchart()->actors.Get();
    for (int i = 0; i < int(obj->GetFlowchart()->num_actors); ++i, ++actor) {
        if (*actor->name.Get() == actor_name && *actor->secondary_name.Get() == actor_sub_name) {
            if (!actor->argument_name.Get()->empty())
                return nullptr;
            return &obj->GetActBinder().GetBindings()[i];
        }
    }

    return nullptr;
}

bool FlowchartContext::IsUsing(const ResFlowchart* flowchart) const {
    auto* obj = FindFlowchartObj(*flowchart->name.Get());
    return obj && obj->GetActBinder().IsUsed();
}

// NON_MATCHING: if (((state | 4) & 7) != 4) -- extremely weird check
bool FlowchartContext::IsPlaying(const ResFlowchart* flowchart) const {
    int state = 2;
    for (int i = 0, n = m_nodes.size(); i < n; ++i) {
        if (m_nodes[i].IsInvalidOrFree()) {
            state = 4;
        } else {
            const auto flowchart_name = ore::StringView(*flowchart->name.Get());
            const auto node_flowchart_name =
                ore::StringView(*m_nodes[i].m_obj->GetFlowchart()->name.Get());
            if (node_flowchart_name == flowchart_name) {
                state = 1;
            } else {
                state = 0;
            }
        }

        if (((state | 4) & 7) != 4)
            break;
    }
    return state != 2;
}

const ore::Array<ActorBinding>*
FlowchartContext::GetUsedResActors(ore::StringView flowchart_name) const {
    auto* obj = FindFlowchartObj(flowchart_name);
    if (!obj)
        return nullptr;
    return obj->GetActBinder().GetUsedResActors();
}

/// Recursively checks subflow calls in the specified entry point for missing flowcharts
/// or entry points.
/// @param result  Optional.
/// @param visited  Visited entry points (one BitArray per flowchart)
/// @param flowchart_idx   Index of the flowchart to which the entry point belongs.
/// @param entry_point_idx  Index of the entry point to be checked.
/// @returns true on success, false on failure
bool CheckSubFlowCalls(FlowchartContext::Builder::BuildResult* result,
                       const ore::IterRange<const ResFlowchart* const*>& flowcharts,
                       ore::Array<ore::BitArray>& visited, int flowchart_idx, int entry_point_idx) {
    if (visited[flowchart_idx].Test(entry_point_idx))
        return true;
    visited[flowchart_idx].Set(entry_point_idx);

    const auto* flowchart = *std::next(flowcharts.begin(), flowchart_idx);
    const auto entry_point_name =
        flowchart->entry_point_names.Get()->GetEntries()[1 + entry_point_idx].GetKey();
    const auto* entry_point = flowchart->GetEntryPoint(entry_point_name);

    for (u16 i = 0; i != entry_point->num_sub_flow_event_indices; ++i) {
        const auto& event = flowchart->events.Get()[entry_point->sub_flow_event_indices.Get()[i]];
        ore::StringView sub_flow_flowchart = *event.sub_flow_flowchart.Get();
        const ore::StringView sub_flow_entry_point = *event.sub_flow_entry_point.Get();

        auto sub_flowchart_idx = flowchart_idx;
        auto* sub_flowchart_res = flowchart;

        if (!sub_flow_flowchart.empty()) {
            const auto it =
                std::find_if(flowcharts.begin(), flowcharts.end(), [=](const ResFlowchart* f) {
                    return sub_flow_flowchart == *f->name.Get();
                });

            if (it == flowcharts.end()) {
                if (result) {
                    result->result =
                        FlowchartContext::Builder::BuildResultType::kResFlowchartNotFound;
                    result->missing_flowchart_name = sub_flow_flowchart;
                    result->missing_entry_point_name = {};
                }
                return false;
            }

            sub_flowchart_idx = std::distance(flowcharts.begin(), it);
            sub_flowchart_res = *it;

        } else {
            sub_flow_flowchart = *flowchart->name.Get();
        }

        const int sub_entry_point_idx =
            sub_flowchart_res->entry_point_names.Get()->FindIndex(sub_flow_entry_point);

        if (sub_entry_point_idx == -1) {
            if (result) {
                result->result = FlowchartContext::Builder::BuildResultType::kEntryPointNotFound;
                result->missing_flowchart_name = sub_flow_flowchart;
                result->missing_entry_point_name = sub_flow_entry_point;
            }
            return false;
        }

        if (!CheckSubFlowCalls(result, flowcharts, visited, sub_flowchart_idx, sub_entry_point_idx))
            return false;
    }

    if (result) {
        result->result = FlowchartContext::Builder::BuildResultType::kSuccess;
        result->missing_flowchart_name = {};
        result->missing_entry_point_name = {};
    }
    return true;
}

bool FlowchartContext::Builder::SetEntryPoint(const ore::StringView& flowchart_name,
                                              const ore::StringView& entry_point_name) {
    return SetEntryPoint(nullptr, flowchart_name, entry_point_name);
}

bool FlowchartContext::Builder::SetEntryPoint(BuildResult* result,
                                              const ore::StringView& flowchart_name,
                                              const ore::StringView& entry_point_name) {
    const auto* flowchart_it =
        std::find_if(m_flowcharts.begin(), m_flowcharts.end(), [=](const ResFlowchart* flowchart) {
            return flowchart_name == *flowchart->name.Get();
        });

    if (flowchart_it == m_flowcharts.end()) {
        if (result) {
            result->result = BuildResultType::kResFlowchartNotFound;
            result->missing_flowchart_name = flowchart_name;
            result->missing_entry_point_name = {};
        }
        return false;
    }

    const auto entry_point_idx =
        (*flowchart_it)->entry_point_names.Get()->FindIndex(entry_point_name);

    if (entry_point_idx == -1) {
        if (result) {
            result->result = BuildResultType::kEntryPointNotFound;
            result->missing_flowchart_name = flowchart_name;
            result->missing_entry_point_name = entry_point_name;
        }
        return false;
    }

    m_flowchart_idx = std::distance(m_flowcharts.begin(), flowchart_it);
    m_entry_point_idx = entry_point_idx;
    if (result) {
        result->result = BuildResultType::kSuccess;
        result->missing_flowchart_name = {};
        result->missing_entry_point_name = {};
    }
    return true;
}

bool FlowchartContext::Builder::BuildImpl(BuildResult* result, FlowchartRange flowcharts,
                                          FlowchartContext* context, AllocateArg allocate_arg,
                                          ore::Buffer flowchart_obj_buffer) {
    context->m_nodes.Reset();
    EvflAllocator allocator{allocate_arg};
    context->m_allocator = allocator;

    const int num_flowcharts = flowcharts.size();

    ore::Array<ore::BitArray> visited_entry_points;
    visited_entry_points.SetBuffer(num_flowcharts, &allocator);
    visited_entry_points.UninitializedDefaultConstructElements();

    const auto clean_up_visited_entry_points = [&] {
        if (auto* data = visited_entry_points.data()) {
            for (auto it = data; it != data + visited_entry_points.size(); ++it)
                it->FreeBufferIfNeeded(&allocator);

            visited_entry_points.DestructElements();
            allocator.Free(data);
        }
    };

    for (int i = 0; i < num_flowcharts; ++i) {
        visited_entry_points[i].AllocateBuffer(&allocator,
                                               (flowcharts.begin()[i])->num_entry_points);
    }

    if (!CheckSubFlowCalls(result, flowcharts, visited_entry_points, m_flowchart_idx,
                           m_entry_point_idx)) {
        clean_up_visited_entry_points();
        return false;
    }

    context->m_objs.ConstructElements(flowchart_obj_buffer);

    for (int i = 0; i < num_flowcharts; ++i) {
        auto* obj = &context->m_objs[i];
        FlowchartObj::Builder obj_builder{flowcharts.begin()[i], &visited_entry_points[i]};

        if (!obj_builder.Build(obj, &context->m_allocator, flowcharts)) {
            clean_up_visited_entry_points();
            context->m_objs.ClearWithoutFreeing();

            if (result) {
                const auto* flowchart = flowcharts.begin()[m_flowchart_idx];
                const ore::StringView name = *flowchart->name.Get();
                const ore::StringView ep_name = flowchart->GetEntryPointName(m_entry_point_idx);
                result->result = BuildResultType::kInvalidOperation;
                result->missing_flowchart_name = name;
                result->missing_entry_point_name = ep_name;
            }
            return false;
        }
    }

    context->m_obj_idx = m_flowchart_idx;
    context->m_active_entry_point_idx = m_entry_point_idx;
    context->m_nodes.Init(&context->m_allocator, 16);
    context->m_nodes.Resize(16);
    context->Clear();
    clean_up_visited_entry_points();

    if (result) {
        result->result = BuildResultType::kSuccess;
        result->missing_flowchart_name = {};
        result->missing_entry_point_name = {};
    }
    return true;
}

bool FlowchartContext::Builder::Build(FlowchartContext* context, AllocateArg allocate_arg) {
    return Build(nullptr, context, allocate_arg);
}

bool FlowchartContext::Builder::Build(BuildResult* result, FlowchartContext* context,
                                      AllocateArg allocate_arg) {
    context->Dispose();

    EvflAllocator allocator{allocate_arg};

    ore::DynArrayList<const ResFlowchart*> flowcharts{&allocator};
    flowcharts.Init(&allocator);
    flowcharts.DeduplicateCopy(m_flowcharts);

    FlowchartRange range{flowcharts};
    ore::Buffer obj_buffer{};
    obj_buffer.Allocate<FlowchartObj>(&allocator, flowcharts.size());

    if (!BuildImpl(result, range, context, allocate_arg, obj_buffer)) {
        obj_buffer.Free(&allocator);
        return false;
    }

    return true;
}

}  // namespace evfl