Fixes for Code Model for query expressions.
[hiphop-php.git] / hphp / compiler / expression / ordering.cpp
blob3dfa1de64b943ba93822818058ad5528a1fe7933
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/ordering.h"
18 #include "hphp/compiler/analysis/code_error.h"
19 #include "hphp/runtime/base/complex-types.h"
20 #include "hphp/compiler/code_model_enums.h"
22 using namespace HPHP;
24 ///////////////////////////////////////////////////////////////////////////////
25 // constructors/destructors
27 Ordering::Ordering
28 (EXPRESSION_CONSTRUCTOR_PARAMETERS,
29 ExpressionPtr key, std::string direction)
30 : Expression(EXPRESSION_CONSTRUCTOR_PARAMETER_VALUES(Ordering)),
31 m_key(key) {
32 if (direction == "") {
33 m_direction = PHP_NOT_SPECIFIED;
34 } else if (strcasecmp(direction.c_str(), "ascending") == 0) {
35 m_direction = PHP_ASCENDING;
36 } else {
37 assert(strcasecmp(direction.c_str(), "descending") == 0);
38 m_direction = PHP_DESCENDING;
42 ExpressionPtr Ordering::clone() {
43 OrderingPtr exp(new Ordering(*this));
44 Expression::deepCopy(exp);
45 exp->m_key = Clone(m_key);
46 exp->m_direction = m_direction;
47 return exp;
50 ///////////////////////////////////////////////////////////////////////////////
51 // parser functions
53 ///////////////////////////////////////////////////////////////////////////////
54 // static analysis functions
56 void Ordering::analyzeProgram(AnalysisResultPtr ar) {
57 m_key->analyzeProgram(ar);
60 ConstructPtr Ordering::getNthKid(int n) const {
61 switch (n) {
62 case 0:
63 return m_key;
64 default:
65 assert(false);
66 break;
68 return ConstructPtr();
71 int Ordering::getKidCount() const {
72 return 1;
75 void Ordering::setNthKid(int n, ConstructPtr cp) {
76 switch (n) {
77 case 0:
78 m_key = dynamic_pointer_cast<Expression>(cp);
79 break;
80 default:
81 break;
85 TypePtr Ordering::inferTypes(AnalysisResultPtr ar, TypePtr type,
86 bool coerce) {
87 m_key->inferAndCheck(ar, Type::Some, false);
88 return Type::Object;
91 ///////////////////////////////////////////////////////////////////////////////
93 void Ordering::outputCodeModel(CodeGenerator &cg) {
94 cg.printObjectHeader("Ordering", 3);
95 cg.printPropertyHeader("expression");
96 m_key->outputCodeModel(cg);
97 cg.printPropertyHeader("order");
98 cg.printValue(m_direction);
99 cg.printPropertyHeader("sourceLocation");
100 cg.printLocation(this->getLocation());
101 cg.printObjectFooter();
104 ///////////////////////////////////////////////////////////////////////////////
105 // code generation functions
107 void Ordering::outputPHP(CodeGenerator &cg, AnalysisResultPtr ar) {
108 m_key->outputPHP(cg, ar);
109 switch (m_direction) {
110 case PHP_ASCENDING:
111 cg_printf(" ascending");
112 break;
113 case PHP_DESCENDING:
114 cg_printf(" decending");
115 break;
116 default:
117 break;