Fixes for Code Model for query expressions.
[hiphop-php.git] / hphp / compiler / expression / join_clause.cpp
blob96335645e43bfc39e590f80a3fddc0e974ca17c4
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/join_clause.h"
18 #include "hphp/compiler/analysis/code_error.h"
19 #include "hphp/runtime/base/complex-types.h"
21 using namespace HPHP;
23 ///////////////////////////////////////////////////////////////////////////////
24 // constructors/destructors
26 JoinClause::JoinClause
27 (EXPRESSION_CONSTRUCTOR_PARAMETERS,
28 const std::string &var, ExpressionPtr coll, ExpressionPtr left,
29 ExpressionPtr right, const std::string &group)
30 : Expression(EXPRESSION_CONSTRUCTOR_PARAMETER_VALUES(JoinClause)),
31 m_var(var), m_coll(coll), m_left(left),
32 m_right(right), m_group(group) {
35 ExpressionPtr JoinClause::clone() {
36 JoinClausePtr exp(new JoinClause(*this));
37 Expression::deepCopy(exp);
38 exp->m_var = m_var;
39 exp->m_coll = Clone(m_coll);
40 exp->m_left = Clone(m_left);
41 exp->m_right = Clone(m_right);
42 exp->m_group = m_group;
43 return exp;
46 ///////////////////////////////////////////////////////////////////////////////
47 // parser functions
49 ///////////////////////////////////////////////////////////////////////////////
50 // static analysis functions
52 void JoinClause::analyzeProgram(AnalysisResultPtr ar) {
53 m_coll->analyzeProgram(ar);
54 m_left->analyzeProgram(ar);
55 m_right->analyzeProgram(ar);
58 ConstructPtr JoinClause::getNthKid(int n) const {
59 switch (n) {
60 case 0:
61 return m_coll;
62 case 1:
63 return m_left;
64 case 2:
65 return m_right;
66 default:
67 assert(false);
68 break;
70 return ConstructPtr();
73 int JoinClause::getKidCount() const {
74 return 3;
77 void JoinClause::setNthKid(int n, ConstructPtr cp) {
78 switch (n) {
79 case 0:
80 m_coll = dynamic_pointer_cast<Expression>(cp);
81 break;
82 case 1:
83 m_left = dynamic_pointer_cast<Expression>(cp);
84 break;
85 case 2:
86 m_right = dynamic_pointer_cast<Expression>(cp);
87 break;
88 default:
89 break;
93 TypePtr JoinClause::inferTypes(AnalysisResultPtr ar, TypePtr type,
94 bool coerce) {
95 m_coll->inferAndCheck(ar, Type::Some, false);
96 m_left->inferAndCheck(ar, Type::Some, false);
97 m_right->inferAndCheck(ar, Type::Some, false);
98 return Type::Object;
101 ///////////////////////////////////////////////////////////////////////////////
103 void JoinClause::outputCodeModel(CodeGenerator &cg) {
104 auto numProps = 5;
105 if (!m_group.empty()) numProps++;
106 cg.printObjectHeader("JoinClause", numProps);
107 cg.printPropertyHeader("identifier");
108 cg.printValue(m_var);
109 cg.printPropertyHeader("collection");
110 m_coll->outputCodeModel(cg);
111 cg.printPropertyHeader("left");
112 m_left->outputCodeModel(cg);
113 cg.printPropertyHeader("right");
114 m_right->outputCodeModel(cg);
115 if (!m_group.empty()) {
116 cg.printPropertyHeader("group");
117 cg.printValue(m_group);
119 cg.printPropertyHeader("sourceLocation");
120 cg.printLocation(this->getLocation());
121 cg.printObjectFooter();
124 ///////////////////////////////////////////////////////////////////////////////
125 // code generation functions
127 void JoinClause::outputPHP(CodeGenerator &cg, AnalysisResultPtr ar) {
128 cg_printf("join %s in ", m_var.c_str());
129 m_coll->outputPHP(cg, ar);
130 cg_printf(" on ");
131 m_left->outputPHP(cg, ar);
132 cg_printf(" equals ");
133 m_right->outputPHP(cg, ar);
134 if (!m_group.empty()) {
135 cg_printf(" into %s", m_group.c_str());