Files
sdk/runtime/vm/closure_functions_cache.h
T
Alexander Markov ffa7e990f0 [vm,dart2bytecode] Use local function IDs to register and find functions
TEST=ci

Change-Id: I275a7f9e81a8fa69a004518b150c7dfcf1fe7bd6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/483844
Commit-Queue: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
2026-03-02 07:55:38 -08:00

70 lines
2.4 KiB
C++

// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_CLOSURE_FUNCTIONS_CACHE_H_
#define RUNTIME_VM_CLOSURE_FUNCTIONS_CACHE_H_
#include <functional>
#include "vm/allocation.h"
#include "vm/token_position.h"
namespace dart {
class Class;
class Function;
class FunctionPtr;
// Implementation of cache for inner closure functions.
//
// This cache is populated lazily by the compiler: When compiling a function,
// the flow graph builder will recursively traverse the kernel AST for the
// function and any inner functions. This will cause the lazy-creation of inner
// closure functions.
//
// The cache is currently implemented as a 2-level
// Map<OutermostMemberFunction, Map<LocalFunctionId, Function>>.
//
// The function is also added to the growable list in order to
// satisfy the following requirements:
// * closure functions list can grow while iterating
// * the index of closure function must be stable
//
class ClosureFunctionsCache : public AllStatic {
public:
static constexpr int kInvalidLocalFunctionId = -1;
static FunctionPtr LookupClosureFunction(const Function& member_function,
intptr_t local_function_id);
static FunctionPtr LookupClosureFunctionLocked(
const Function& member_function,
intptr_t local_function_id);
// Normally implicit closure functions are not added to this cache, however
// during AOT compilation we might add those implicit closure functions
// that have their original functions shaken to allow ProgramWalker to
// discover them.
static void AddClosureFunctionLocked(
const Function& function,
intptr_t local_function_id,
bool allow_implicit_closure_functions = false);
static intptr_t FindClosureIndex(const Function& needle);
static FunctionPtr ClosureFunctionFromIndex(intptr_t idx);
// Visits all closure functions registered in the object store.
//
// Iterates in-order, thereby allowing new closures being added during the
// iteration.
//
// The iteration continues until either [callback] returns `false` or all
// closure functions have been visited.
static void ForAllClosureFunctions(
std::function<bool(const Function&)> callback);
};
} // namespace dart
#endif // RUNTIME_VM_CLOSURE_FUNCTIONS_CACHE_H_