Simplify Ex in uast.expr
[hiphop-php.git] / hphp / hack / src / hhbc / bytecode_printer / special_class_resolver.rs
blob86606bd74f424f5eaa44f8a8d41adba918f6ff4b
1 // Copyright (c) Facebook, Inc. and its affiliates.
2 //
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 std::borrow::Cow;
8 use decl_provider::DeclProvider;
9 use hhbc_by_ref_ast_class_expr::ClassExpr;
10 use hhbc_by_ref_env::emitter::Emitter;
11 use hhbc_by_ref_hhas_body::HhasBodyEnv;
12 use oxidized::{ast, ast_defs, pos::Pos};
14 pub trait SpecialClassResolver {
15     fn resolve<'a>(&self, env: Option<&'a HhasBodyEnv>, id: &'a str) -> Cow<'a, str>;
18 impl<'arena, 'decl, D: DeclProvider<'decl>> SpecialClassResolver for Emitter<'arena, 'decl, D> {
19     fn resolve<'a>(&self, env: Option<&'a HhasBodyEnv>, id: &'a str) -> Cow<'a, str> {
20         let class_expr = match env {
21             None => ClassExpr::expr_to_class_expr_(
22                 self,
23                 true,
24                 true,
25                 None,
26                 None,
27                 ast::Expr(
28                     (),
29                     Pos::make_none(),
30                     ast::Expr_::mk_id(ast_defs::Id(Pos::make_none(), id.into())),
31                 ),
32             ),
33             Some(body_env) => ClassExpr::expr_to_class_expr_(
34                 self,
35                 true,
36                 true,
37                 body_env
38                     .class_info
39                     .as_ref()
40                     .map(|(k, s)| (k.clone(), s.as_str())),
41                 body_env.parent_name.clone(),
42                 ast::Expr(
43                     (),
44                     Pos::make_none(),
45                     ast::Expr_::mk_id(ast_defs::Id(Pos::make_none(), id.into())),
46                 ),
47             ),
48         };
49         match class_expr {
50             ClassExpr::Id(ast_defs::Id(_, name)) => Cow::Owned(name),
51             _ => Cow::Borrowed(id),
52         }
53     }