[Perf] Optimize injection
[hiphop-php.git] / src / runtime / eval / ast / name.cpp
blobe962a3b40f842a496de80b928326b64ba992e883
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include <runtime/eval/ast/name.h>
18 #include <runtime/eval/ast/expression.h>
19 #include <runtime/base/complex_types.h>
20 #include <util/util.h>
22 namespace HPHP {
23 namespace Eval {
24 using namespace std;
25 ///////////////////////////////////////////////////////////////////////////////
27 Name::Name(CONSTRUCT_ARGS) : Construct(CONSTRUCT_PASS) {}
29 int64 Name::hash() const {
30 return -1;
32 int64 Name::hashLwr() const {
33 return -1;
36 bool Name::isSp() const { return false; }
38 NamePtr Name::fromString(CONSTRUCT_ARGS, const string &name,
39 bool isSp /* = false */) {
40 return NamePtr(new StringName(CONSTRUCT_PASS, name, isSp));
43 NamePtr Name::fromExp(CONSTRUCT_ARGS, ExpressionPtr e) {
44 return NamePtr(new ExprName(CONSTRUCT_PASS, e));
46 NamePtr Name::fromStaticClassExp(CONSTRUCT_ARGS, ExpressionPtr e) {
47 return NamePtr(new StaticClassExprName(CONSTRUCT_PASS, e));
49 NamePtr Name::LateStatic(CONSTRUCT_ARGS) {
50 return NamePtr(new LateStaticName(CONSTRUCT_PASS));
53 StringName::StringName(CONSTRUCT_ARGS, const string &name,
54 bool isSp /* = false */)
55 : Name(CONSTRUCT_PASS), m_hash(hash_string(name.c_str(), name.size())),
56 m_hashLwr(hash_string_i(name.c_str(), name.size())),
57 m_name(name), m_isSp(isSp) {
60 String StringName::get(VariableEnvironment &env) const {
61 return getStatic();
64 String StringName::getStatic() const {
65 return String(m_name.c_str(), m_name.size(), AttachLiteral);
68 int64 StringName::hash() const {
69 return m_hash;
71 int64 StringName::hashLwr() const {
72 return m_hashLwr;
75 bool StringName::isSp() const { return m_isSp; }
77 void StringName::dump(std::ostream &out) const {
78 out << m_name;
81 ExprName::ExprName(CONSTRUCT_ARGS, ExpressionPtr name)
82 : Name(CONSTRUCT_PASS), m_name(name) {}
84 String ExprName::get(VariableEnvironment &env) const {
85 return m_name->eval(env).toString();
88 void ExprName::dump(std::ostream &out) const {
89 if (m_name) {
90 m_name->dump(out);
94 ExpressionPtr ExprName::getExp() {
95 return m_name;
98 void ExprName::setExp(ExpressionPtr name) {
99 m_name = name;
102 StaticClassExprName::StaticClassExprName(CONSTRUCT_ARGS, ExpressionPtr name)
103 : ExprName(CONSTRUCT_PASS, name) {}
105 String StaticClassExprName::get(VariableEnvironment &env) const {
106 return get_static_class_name(m_name->eval(env));
109 LateStaticName::LateStaticName(CONSTRUCT_ARGS) : Name(CONSTRUCT_PASS) {}
111 String LateStaticName::get(VariableEnvironment &env) const {
112 return FrameInjection::GetStaticClassName(ThreadInfo::s_threadInfo.get());
115 void LateStaticName::dump(std::ostream &out) const {
116 out << "static";
119 ///////////////////////////////////////////////////////////////////////////////