1 // Copyright (c) Facebook, Inc. and its affiliates.
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
6 use emit_fatal::raise_fatal_runtime;
8 use hhas_param::HhasParam;
9 use hhbc_ast::{FcallArgs, FcallFlags};
10 use hhbc_id::function;
11 use instruction_sequence::{instr, InstrSeq, Result};
14 use oxidized::{aast::FunParam, ast::Expr, pos::Pos};
18 pub const MEMOIZE_SUFFIX: &str = "$memoize_impl";
20 pub fn get_memo_key_list<'arena>(
21 alloc: &'arena bumpalo::Bump,
24 name: impl AsRef<str>,
25 ) -> Vec<InstrSeq<'arena>> {
27 instr::getmemokeyl(alloc, Local::Named(Str::new_str(alloc, name.as_ref()))),
28 instr::setl(alloc, Local::Unnamed(local + index)),
33 pub fn param_code_sets<'arena>(
34 alloc: &'arena bumpalo::Bump,
35 params: &[(HhasParam<'arena>, Option<(Label, Expr)>)],
37 ) -> InstrSeq<'arena> {
43 .map(|(i, (param, _))| get_memo_key_list(alloc, local, i, ¶m.name.unsafe_as_str()))
49 pub fn param_code_gets<'arena>(
50 alloc: &'arena bumpalo::Bump,
51 params: &[(HhasParam<'arena>, Option<(Label, Expr)>)],
52 ) -> InstrSeq<'arena> {
60 Local::Named(Str::new_str(alloc, param.name.unsafe_as_str())),
67 pub fn check_memoize_possible<Ex, En>(
69 params: &[FunParam<Ex, En>],
72 if !is_method && params.iter().any(|param| param.is_variadic) {
73 return Err(raise_fatal_runtime(
75 String::from("<<__Memoize>> cannot be used on functions with variable arguments"),
81 pub fn get_implicit_context_memo_key<'arena>(
82 alloc: &'arena bumpalo::Bump,
84 ) -> InstrSeq<'arena> {
88 instr::nulluninit(alloc),
89 instr::nulluninit(alloc),
93 FcallFlags::default(),
101 function::from_raw_string(
103 "HH\\ImplicitContext\\_Private\\get_implicit_context_memo_key",
106 instr::setl(alloc, Local::Unnamed(local)),