Identify direct children of yield expressions
[hiphop-php.git] / hphp / compiler / expression / encaps_list_expression.cpp
bloba86c3bd911868ef2de5d6af2077db428393d3d65
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 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 "hphp/compiler/expression/encaps_list_expression.h"
18 #include "hphp/compiler/expression/expression_list.h"
19 #include "hphp/compiler/expression/binary_op_expression.h"
20 #include "hphp/compiler/analysis/code_error.h"
21 #include "hphp/runtime/base/builtin_functions.h"
23 using namespace HPHP;
25 ///////////////////////////////////////////////////////////////////////////////
26 // constructors/destructors
28 EncapsListExpression::EncapsListExpression
29 (EXPRESSION_CONSTRUCTOR_PARAMETERS, int type, ExpressionListPtr expList)
30 : Expression(EXPRESSION_CONSTRUCTOR_PARAMETER_VALUES(EncapsListExpression)),
31 m_type(type), m_exps(expList) {
34 ExpressionPtr EncapsListExpression::clone() {
35 EncapsListExpressionPtr exp(new EncapsListExpression(*this));
36 Expression::deepCopy(exp);
37 exp->m_exps = Clone(m_exps);
38 return exp;
41 ///////////////////////////////////////////////////////////////////////////////
42 // parser functions
44 ///////////////////////////////////////////////////////////////////////////////
45 // static analysis functions
47 void EncapsListExpression::analyzeProgram(AnalysisResultPtr ar) {
48 if (m_exps) {
49 for (int i = 0; i < m_exps->getCount(); i++) {
50 (*m_exps)[i]->analyzeProgram(ar);
55 ConstructPtr EncapsListExpression::getNthKid(int n) const {
56 switch (n) {
57 case 0:
58 return m_exps;
59 default:
60 assert(false);
61 break;
63 return ConstructPtr();
66 int EncapsListExpression::getKidCount() const {
67 return 1;
70 void EncapsListExpression::setNthKid(int n, ConstructPtr cp) {
71 switch (n) {
72 case 0:
73 m_exps = boost::dynamic_pointer_cast<ExpressionList>(cp);
74 break;
75 default:
76 assert(false);
77 break;
81 void EncapsListExpression::stripConcat() {
82 m_exps->stripConcat();
85 ExpressionPtr EncapsListExpression::preOptimize(AnalysisResultConstPtr ar) {
86 if (m_type != '`' && m_type != '\'' && m_exps) {
87 int count = m_exps->getCount();
88 // turn into cascaded concat
89 if (count > 1) {
90 ExpressionPtr exp =
91 BinaryOpExpressionPtr(new BinaryOpExpression(
92 getScope(), getLocation(),
93 (*m_exps)[0], (*m_exps)[1], '.'));
94 for (int i = 2; i < count; i++) {
95 exp =
96 BinaryOpExpressionPtr(new BinaryOpExpression(
97 getScope(), getLocation(),
98 exp, (*m_exps)[i], '.'));
100 return exp;
103 return ExpressionPtr();
106 TypePtr EncapsListExpression::inferTypes(AnalysisResultPtr ar, TypePtr type,
107 bool coerce) {
108 if (m_exps) {
109 for (int i = 0; i < m_exps->getCount(); i++) {
110 (*m_exps)[i]->inferAndCheck(ar, Type::String, false);
113 return Type::String;
116 bool EncapsListExpression::canonCompare(ExpressionPtr e) const {
117 if (!Expression::canonCompare(e)) return false;
118 EncapsListExpressionPtr el = static_pointer_cast<EncapsListExpression>(e);
119 return m_type == el->m_type;
122 ///////////////////////////////////////////////////////////////////////////////
123 // code generation functions
125 void EncapsListExpression::outputPHP(CodeGenerator &cg, AnalysisResultPtr ar) {
126 if (m_type == '`') cg_printf("shell_exec(");
128 if (m_exps) {
129 for (int i = 0; i < m_exps->getCount(); i++) {
130 if (i > 0) cg_printf(" . ");
131 (*m_exps)[i]->outputPHP(cg, ar);
133 } else {
134 cg_printf("''");
137 if (m_type == '`') cg_printf(")");