/
list_assignment.cpp
260 lines (236 loc) · 8.39 KB
/
list_assignment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2015 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#include "hphp/compiler/expression/list_assignment.h"
#include "hphp/compiler/expression/assignment_expression.h"
#include "hphp/compiler/expression/expression_list.h"
#include "hphp/compiler/analysis/file_scope.h"
#include "hphp/compiler/analysis/function_scope.h"
#include "hphp/compiler/expression/array_element_expression.h"
#include "hphp/compiler/expression/object_property_expression.h"
#include "hphp/compiler/expression/unary_op_expression.h"
#include "hphp/compiler/expression/binary_op_expression.h"
#include "hphp/parser/hphp.tab.hpp"
using namespace HPHP;
///////////////////////////////////////////////////////////////////////////////
// constructors/destructors
/*
Determine whether the rhs behaves normally, or abnormally.
1) If the expression is the silence operator, recurse on the inner expression.
2) If the expression is a list assignment expression, recurse on the
RHS of the expression.
3) If the expression is one of the following, then E behaves normally:
Simple/Dynamic variable (including $this and superglobals)
Array element expression
Property expression
Static variable expression
Function call expression
Preinc/predec expression (but not postinc/postdec)
Assignment expression
Assignment op expression
Binding assignment expression
Include/require expression
Eval expression
Array expression
Array cast expression
4) For all other expressions, E behaves abnormally. This includes:
All binary operator expressions
All unary operator expressions except silence and preinc/predec
Scalar expression of type null, bool, int, double, or string
Qop expression (?:)
Constant expression
Class constant expression
Isset or empty expression
Exit expression
Instanceof expression
Anonymous class expression
*/
static ListAssignment::RHSKind GetRHSKind(ExpressionPtr rhs) {
switch (rhs->getKindOf()) {
case Construct::KindOfSimpleVariable:
case Construct::KindOfPipeVariable:
case Construct::KindOfDynamicVariable:
case Construct::KindOfArrayElementExpression:
case Construct::KindOfObjectPropertyExpression:
case Construct::KindOfStaticMemberExpression:
case Construct::KindOfSimpleFunctionCall:
case Construct::KindOfDynamicFunctionCall:
case Construct::KindOfObjectMethodExpression:
case Construct::KindOfNewObjectExpression:
case Construct::KindOfAssignmentExpression:
case Construct::KindOfExpressionList:
case Construct::KindOfIncludeExpression:
case Construct::KindOfYieldExpression:
case Construct::KindOfYieldFromExpression:
case Construct::KindOfAwaitExpression:
case Construct::KindOfClassExpression:
return ListAssignment::Regular;
case Construct::KindOfListAssignment:
return GetRHSKind(static_pointer_cast<ListAssignment>(rhs)->getArray());
case Construct::KindOfUnaryOpExpression: {
auto u = static_pointer_cast<UnaryOpExpression>(rhs);
switch (u->getOp()) {
case '@':
return GetRHSKind(u->getExpression());
case T_INC:
case T_DEC:
return u->getFront() ?
ListAssignment::Regular : ListAssignment::Checked;
case T_EVAL:
case T_ARRAY:
case T_ARRAY_CAST:
return ListAssignment::Regular;
default:
return ListAssignment::Null;
}
break;
}
case Construct::KindOfBinaryOpExpression: {
auto b = static_pointer_cast<BinaryOpExpression>(rhs);
if (b->getOp() == T_PIPE) return GetRHSKind(b->getExp2());
if (b->isAssignmentOp() ||
b->getOp() == '+' ||
b->getOp() == T_COLLECTION) {
return ListAssignment::Regular;
}
return ListAssignment::Null;
}
case Construct::KindOfQOpExpression:
case Construct::KindOfNullCoalesceExpression:
return ListAssignment::Checked;
// invalid context
case Construct::KindOfExpression:
case Construct::KindOfArrayPairExpression:
case Construct::KindOfParameterExpression:
case Construct::KindOfModifierExpression:
case Construct::KindOfUserAttribute:
always_assert(false);
// non-arrays
case Construct::KindOfScalarExpression:
case Construct::KindOfConstantExpression:
case Construct::KindOfClassConstantExpression:
case Construct::KindOfEncapsListExpression:
case Construct::KindOfClosureExpression:
return ListAssignment::Null;
#define STATEMENT_CASE(x) case Construct::KindOf##x:
DECLARE_STATEMENT_TYPES(STATEMENT_CASE) {
always_assert(false);
}
#undef STATEMENT_CASE
}
// unreachable for known expression kinds
always_assert(false);
}
ListAssignment::ListAssignment
(EXPRESSION_CONSTRUCTOR_PARAMETERS,
ExpressionListPtr variables, ExpressionPtr array, bool rhsFirst /* = false */)
: Expression(EXPRESSION_CONSTRUCTOR_PARAMETER_VALUES(ListAssignment)),
m_variables(variables), m_array(array), m_rhsKind(Regular),
m_rhsFirst(rhsFirst) {
setLValue();
if (m_array) {
m_rhsKind = GetRHSKind(m_array);
if (m_array->is(KindOfSimpleVariable)) {
m_array->setContext(LValue);
}
}
}
ExpressionPtr ListAssignment::clone() {
ListAssignmentPtr exp(new ListAssignment(*this));
Expression::deepCopy(exp);
exp->m_variables = Clone(m_variables);
exp->m_array = Clone(m_array);
return exp;
}
void ListAssignment::setLValue() {
if (m_variables) {
for (int i = 0; i < m_variables->getCount(); i++) {
ExpressionPtr exp = (*m_variables)[i];
if (exp) {
if (exp->is(Construct::KindOfListAssignment)) {
auto sublist = dynamic_pointer_cast<ListAssignment>(exp);
sublist->setLValue();
} else {
// Magic contexts I took from assignment expression
exp->setContext(Expression::DeepAssignmentLHS);
exp->setContext(Expression::AssignmentLHS);
exp->setContext(Expression::LValue);
exp->setContext(Expression::NoLValueWrapper);
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// parser functions
///////////////////////////////////////////////////////////////////////////////
// static analysis functions
void ListAssignment::analyzeProgram(AnalysisResultPtr ar) {
if (m_variables) m_variables->analyzeProgram(ar);
if (m_array) m_array->analyzeProgram(ar);
FunctionScopePtr func = getFunctionScope();
if (ar->getPhase() == AnalysisResult::AnalyzeFinal) {
if (m_variables) {
for (int i = 0; i < m_variables->getCount(); i++) {
ExpressionPtr exp = (*m_variables)[i];
if (exp) {
if (!exp->is(Construct::KindOfListAssignment)) {
CheckNeeded(exp, ExpressionPtr());
}
}
}
}
}
}
ConstructPtr ListAssignment::getNthKid(int n) const {
switch (m_rhsFirst ? 1 - n : n) {
case 0:
return m_variables;
case 1:
return m_array;
default:
assert(false);
break;
}
return ConstructPtr();
}
int ListAssignment::getKidCount() const {
return 2;
}
void ListAssignment::setNthKid(int n, ConstructPtr cp) {
switch (m_rhsFirst ? 1 - n : n) {
case 0:
m_variables = dynamic_pointer_cast<ExpressionList>(cp);
break;
case 1:
m_array = dynamic_pointer_cast<Expression>(cp);
break;
default:
assert(false);
break;
}
}
///////////////////////////////////////////////////////////////////////////////
// code generation functions
void ListAssignment::outputPHP(CodeGenerator &cg, AnalysisResultPtr ar) {
cg_printf("list(");
if (m_variables) m_variables->outputPHP(cg, ar);
if (m_array) {
cg_printf(") = ");
m_array->outputPHP(cg, ar);
} else {
cg_printf(")");
}
}