PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - Zend - zend_ast.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 232 233 99.6 %
Date: 2015-01-26 Functions: 20 20 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2015 Zend Technologies Ltd. (http://www.zend.com) |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt.                                |
      11             :    | If you did not receive a copy of the Zend license and are unable to  |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@zend.com so we can mail you a copy immediately.              |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Bob Weinand <bwoebi@php.net>                                |
      16             :    |          Dmitry Stogov <dmitry@zend.com>                             |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #include "zend_ast.h"
      23             : #include "zend_API.h"
      24             : #include "zend_operators.h"
      25             : 
      26             : ZEND_API zend_ast_process_t zend_ast_process = NULL;
      27             : 
      28     4528600 : static inline void *zend_ast_alloc(size_t size) {
      29     4528600 :         return zend_arena_alloc(&CG(ast_arena), size);
      30             : }
      31             : 
      32       68903 : static inline void *zend_ast_realloc(void *old, size_t old_size, size_t new_size) {
      33       68903 :         void *new = zend_ast_alloc(new_size);
      34       68903 :         memcpy(new, old, old_size);
      35       68903 :         return new;
      36             : }
      37             : 
      38     1946369 : static inline size_t zend_ast_size(uint32_t children) {
      39     1946369 :         return sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
      40             : }
      41             : 
      42      804572 : static inline size_t zend_ast_list_size(uint32_t children) {
      43      804572 :         return sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
      44             : }
      45             : 
      46       17128 : ZEND_API zend_ast *zend_ast_create_znode(znode *node) {
      47             :         zend_ast_znode *ast;
      48             : 
      49       17128 :         ast = zend_ast_alloc(sizeof(zend_ast_znode));
      50       17128 :         ast->kind = ZEND_AST_ZNODE;
      51       17128 :         ast->attr = 0;
      52       17128 :         ast->lineno = CG(zend_lineno);
      53       17128 :         ast->node = *node;
      54       17128 :         return (zend_ast *) ast;
      55             : }
      56             : 
      57     1786534 : ZEND_API zend_ast *zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr) {
      58             :         zend_ast_zval *ast;
      59             : 
      60     1786534 :         ast = zend_ast_alloc(sizeof(zend_ast_zval));
      61     1786534 :         ast->kind = ZEND_AST_ZVAL;
      62     1786534 :         ast->attr = attr;
      63     1786534 :         ZVAL_COPY_VALUE(&ast->val, zv);
      64     1786534 :         ast->val.u2.lineno = CG(zend_lineno);
      65     1786534 :         return (zend_ast *) ast;
      66             : }
      67             : 
      68       43180 : ZEND_API zend_ast *zend_ast_create_decl(
      69             :         zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
      70             :         zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2
      71             : ) {
      72             :         zend_ast_decl *ast;
      73             : 
      74       43180 :         ast = zend_ast_alloc(sizeof(zend_ast_decl));
      75       43180 :         ast->kind = kind;
      76       43180 :         ast->attr = 0;
      77       43180 :         ast->start_lineno = start_lineno;
      78       43180 :         ast->end_lineno = CG(zend_lineno);
      79       43180 :         ast->flags = flags;
      80       43180 :         ast->lex_pos = LANG_SCNG(yy_text);
      81       43180 :         ast->doc_comment = doc_comment;
      82       43180 :         ast->name = name;
      83       43180 :         ast->child[0] = child0;
      84       43180 :         ast->child[1] = child1;
      85       43180 :         ast->child[2] = child2;
      86             : 
      87       43180 :         return (zend_ast *) ast;
      88             : }
      89             : 
      90     1946188 : static zend_ast *zend_ast_create_from_va_list(zend_ast_kind kind, zend_ast_attr attr, va_list va) {
      91     1946188 :         uint32_t i, children = kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
      92             :         zend_ast *ast;
      93             : 
      94     1946188 :         ast = zend_ast_alloc(zend_ast_size(children));
      95     1946188 :         ast->kind = kind;
      96     1946188 :         ast->attr = attr;
      97     1946188 :         ast->lineno = (uint32_t) -1;
      98             : 
      99     5108559 :         for (i = 0; i < children; ++i) {
     100     3162371 :                 ast->child[i] = va_arg(va, zend_ast *);
     101     3162371 :                 if (ast->child[i] != NULL) {
     102     5816810 :                         uint32_t lineno = zend_ast_get_lineno(ast->child[i]);
     103     2908405 :                         if (lineno < ast->lineno) {
     104     1934862 :                                 ast->lineno = lineno;
     105             :                         }
     106             :                 }
     107             :         }
     108             : 
     109     1946188 :         if (ast->lineno == UINT_MAX) {
     110       11971 :                 ast->lineno = CG(zend_lineno);
     111             :         }
     112             : 
     113     1946188 :         return ast;
     114             : }
     115             : 
     116      224180 : ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ...) {
     117             :         va_list va;
     118             :         zend_ast *ast;
     119             : 
     120      224180 :         va_start(va, attr);
     121      224180 :         ast = zend_ast_create_from_va_list(kind, attr, va);
     122      224180 :         va_end(va);
     123             : 
     124      224180 :         return ast;
     125             : }
     126             : 
     127     1722008 : ZEND_API zend_ast *zend_ast_create(zend_ast_kind kind, ...) {
     128             :         va_list va;
     129             :         zend_ast *ast;
     130             : 
     131     1722008 :         va_start(va, kind);
     132     1722008 :         ast = zend_ast_create_from_va_list(kind, 0, va);
     133     1722008 :         va_end(va);
     134             : 
     135     1722008 :         return ast;
     136             : }
     137             : 
     138      666667 : ZEND_API zend_ast *zend_ast_create_list(uint32_t init_children, zend_ast_kind kind, ...) {
     139             :         zend_ast *ast;
     140             :         zend_ast_list *list;
     141             : 
     142      666667 :         ast = zend_ast_alloc(zend_ast_list_size(4));
     143      666667 :         list = (zend_ast_list *) ast;
     144      666667 :         list->kind = kind;
     145      666667 :         list->attr = 0;
     146      666667 :         list->lineno = CG(zend_lineno);
     147      666667 :         list->children = 0;
     148             : 
     149             :         {
     150             :                 va_list va;
     151             :                 uint32_t i;
     152      666667 :                 va_start(va, kind);
     153     1136335 :                 for (i = 0; i < init_children; ++i) {
     154      469668 :                         ast = zend_ast_list_add(ast, va_arg(va, zend_ast *));
     155             :                 }
     156      666667 :                 va_end(va);
     157             :         }
     158             : 
     159      666667 :         return ast;
     160             : }
     161             : 
     162      360021 : static inline zend_bool is_power_of_two(uint32_t n) {
     163      360021 :         return ((n != 0) && (n == (n & (~n + 1))));
     164             : }
     165             : 
     166     1397358 : ZEND_API zend_ast *zend_ast_list_add(zend_ast *ast, zend_ast *op) {
     167     1397358 :         zend_ast_list *list = zend_ast_get_list(ast);
     168     1397358 :         if (list->children >= 4 && is_power_of_two(list->children)) {
     169       68903 :                         list = zend_ast_realloc(list,
     170             :                         zend_ast_list_size(list->children), zend_ast_list_size(list->children * 2));
     171             :         }
     172     1397358 :         list->child[list->children++] = op;
     173     1397358 :         return (zend_ast *) list;
     174             : }
     175             : 
     176         117 : static void zend_ast_add_array_element(zval *result, zval *offset, zval *expr)
     177             : {
     178         117 :         switch (Z_TYPE_P(offset)) {
     179             :                 case IS_UNDEF:
     180          17 :                         zend_hash_next_index_insert(Z_ARRVAL_P(result), expr);
     181          17 :                         break;
     182             :                 case IS_STRING:
     183          34 :                         zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(offset), expr);
     184             :                         zval_dtor(offset);
     185          34 :                         break;
     186             :                 case IS_NULL:
     187           1 :                         zend_symtable_update(Z_ARRVAL_P(result), STR_EMPTY_ALLOC(), expr);
     188           1 :                         break;
     189             :                 case IS_LONG:
     190          61 :                         zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(offset), expr);
     191          61 :                         break;
     192             :                 case IS_FALSE:
     193           1 :                         zend_hash_index_update(Z_ARRVAL_P(result), 0, expr);
     194           1 :                         break;
     195             :                 case IS_TRUE:
     196           1 :                         zend_hash_index_update(Z_ARRVAL_P(result), 1, expr);
     197           1 :                         break;
     198             :                 case IS_DOUBLE:
     199           2 :                         zend_hash_index_update(Z_ARRVAL_P(result), zend_dval_to_lval(Z_DVAL_P(offset)), expr);
     200           1 :                         break;
     201             :                 default:
     202           1 :                         zend_error(E_ERROR, "Illegal offset type");
     203             :                         break;
     204             :         }
     205         116 : }
     206             : 
     207         459 : ZEND_API void zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope)
     208             : {
     209             :         zval op1, op2;
     210             : 
     211         459 :         switch (ast->kind) {
     212             :                 case ZEND_AST_BINARY_OP:
     213             :                 {
     214          13 :                         binary_op_type op = get_binary_op(ast->attr);
     215          13 :                         zend_ast_evaluate(&op1, ast->child[0], scope);
     216          13 :                         zend_ast_evaluate(&op2, ast->child[1], scope);
     217          13 :                         op(result, &op1, &op2);
     218             :                         zval_dtor(&op1);
     219             :                         zval_dtor(&op2);
     220          13 :                         break;
     221             :                 }
     222             :                 case ZEND_AST_GREATER:
     223             :                 case ZEND_AST_GREATER_EQUAL:
     224             :                 {
     225             :                         /* op1 > op2 is the same as op2 < op1 */
     226           2 :                         binary_op_type op = ast->kind == ZEND_AST_GREATER
     227           2 :                                 ? is_smaller_function : is_smaller_or_equal_function;
     228           2 :                         zend_ast_evaluate(&op1, ast->child[0], scope);
     229           2 :                         zend_ast_evaluate(&op2, ast->child[1], scope);
     230           2 :                         op(result, &op2, &op1);
     231             :                         zval_dtor(&op1);
     232             :                         zval_dtor(&op2);
     233           2 :                         break;
     234             :                 }
     235             :                 case ZEND_AST_UNARY_OP:
     236             :                 {
     237           2 :                         unary_op_type op = get_unary_op(ast->attr);
     238           2 :                         zend_ast_evaluate(&op1, ast->child[0], scope);
     239           2 :                         op(result, &op1);
     240             :                         zval_dtor(&op1);
     241           2 :                         break;
     242             :                 }
     243             :                 case ZEND_AST_ZVAL:
     244             :                 {
     245         310 :                         zval *zv = zend_ast_get_zval(ast);
     246         310 :                         if (scope) {
     247             :                                 /* class constants may be updated in-place */
     248           9 :                                 if (Z_OPT_CONSTANT_P(zv)) {
     249           6 :                                         zval_update_constant_ex(zv, 1, scope);
     250             :                                 }
     251           6 :                                 ZVAL_DUP(result, zv);
     252             :                         } else {
     253         301 :                                 ZVAL_DUP(result, zv);
     254         301 :                                 if (Z_OPT_CONSTANT_P(result)) {
     255         183 :                                         zval_update_constant_ex(result, 1, scope);
     256             :                                 }
     257             :                         }
     258         291 :                         break;
     259             :                 }
     260             :                 case ZEND_AST_AND:
     261           4 :                         zend_ast_evaluate(&op1, ast->child[0], scope);
     262           4 :                         if (zend_is_true(&op1)) {
     263           3 :                                 zend_ast_evaluate(&op2, ast->child[1], scope);
     264           3 :                                 ZVAL_BOOL(result, zend_is_true(&op2));
     265             :                                 zval_dtor(&op2);
     266             :                         } else {
     267           1 :                                 ZVAL_FALSE(result);
     268             :                         }
     269             :                         zval_dtor(&op1);
     270           4 :                         break;
     271             :                 case ZEND_AST_OR:
     272           4 :                         zend_ast_evaluate(&op1, ast->child[0], scope);
     273           4 :                         if (zend_is_true(&op1)) {
     274           2 :                                 ZVAL_TRUE(result);
     275             :                         } else {
     276           2 :                                 zend_ast_evaluate(&op2, ast->child[1], scope);
     277           2 :                                 ZVAL_BOOL(result, zend_is_true(&op2));
     278             :                                 zval_dtor(&op2);
     279             :                         }
     280             :                         zval_dtor(&op1);
     281           4 :                         break;
     282             :                 case ZEND_AST_CONDITIONAL:
     283           6 :                         zend_ast_evaluate(&op1, ast->child[0], scope);
     284           6 :                         if (zend_is_true(&op1)) {
     285           3 :                                 if (!ast->child[1]) {
     286           2 :                                         *result = op1;
     287             :                                 } else {
     288           1 :                                         zend_ast_evaluate(result, ast->child[1], scope);
     289             :                                         zval_dtor(&op1);
     290             :                                 }
     291             :                         } else {
     292           3 :                                 zend_ast_evaluate(result, ast->child[2], scope);
     293             :                                 zval_dtor(&op1);
     294             :                         }
     295           6 :                         break;
     296             :                 case ZEND_AST_UNARY_PLUS:
     297           2 :                         ZVAL_LONG(&op1, 0);
     298           2 :                         zend_ast_evaluate(&op2, ast->child[0], scope);
     299           2 :                         add_function(result, &op1, &op2);
     300             :                         zval_dtor(&op2);
     301           2 :                         break;
     302             :                 case ZEND_AST_UNARY_MINUS:
     303           3 :                         ZVAL_LONG(&op1, 0);
     304           3 :                         zend_ast_evaluate(&op2, ast->child[0], scope);
     305           3 :                         sub_function(result, &op1, &op2);
     306             :                         zval_dtor(&op2);
     307           3 :                         break;
     308             :                 case ZEND_AST_ARRAY:
     309         102 :                         array_init(result);
     310             :                         {
     311             :                                 uint32_t i;
     312         102 :                                 zend_ast_list *list = zend_ast_get_list(ast);
     313         218 :                                 for (i = 0; i < list->children; i++) {
     314         136 :                                         zend_ast *elem = list->child[i];
     315         136 :                                         if (elem->child[1]) {
     316         111 :                                                 zend_ast_evaluate(&op1, elem->child[1], scope);
     317             :                                         } else {
     318          25 :                                                 ZVAL_UNDEF(&op1);
     319             :                                         }
     320         130 :                                         zend_ast_evaluate(&op2, elem->child[0], scope);
     321         117 :                                         zend_ast_add_array_element(result, &op1, &op2);
     322             :                                 }
     323             :                         }
     324          82 :                         break;
     325             :                 case ZEND_AST_DIM:
     326          11 :                         zend_ast_evaluate(&op1, ast->child[0], scope);
     327          11 :                         zend_ast_evaluate(&op2, ast->child[1], scope);
     328             :                         {
     329             :                                 zval tmp;
     330          11 :                                 zend_fetch_dimension_by_zval(&tmp, &op1, &op2);
     331          33 :                                 ZVAL_ZVAL(result, &tmp, 1, 1);
     332             :                         }
     333             :                         zval_dtor(&op1);
     334             :                         zval_dtor(&op2);
     335          11 :                         break;
     336             :                 default:
     337           0 :                         zend_error(E_ERROR, "Unsupported constant expression");
     338             :         }
     339         420 : }
     340             : 
     341         627 : ZEND_API zend_ast *zend_ast_copy(zend_ast *ast)
     342             : {
     343         627 :         if (ast == NULL) {
     344          26 :                 return NULL;
     345         601 :         } else if (ast->kind == ZEND_AST_ZVAL) {
     346         321 :                 zend_ast_zval *new = emalloc(sizeof(zend_ast_zval));
     347         321 :                 new->kind = ZEND_AST_ZVAL;
     348         321 :                 new->attr = ast->attr;
     349         642 :                 ZVAL_COPY(&new->val, zend_ast_get_zval(ast));
     350         321 :                 return (zend_ast *) new;
     351         280 :         } else if (zend_ast_is_list(ast)) {
     352          99 :                 zend_ast_list *list = zend_ast_get_list(ast);
     353          99 :                 zend_ast_list *new = emalloc(zend_ast_list_size(list->children));
     354             :                 uint32_t i;
     355          99 :                 new->kind = list->kind;
     356          99 :                 new->attr = list->attr;
     357          99 :                 new->children = list->children;
     358         232 :                 for (i = 0; i < list->children; i++) {
     359         133 :                         new->child[i] = zend_ast_copy(list->child[i]);
     360             :                 }
     361          99 :                 return (zend_ast *) new;
     362             :         } else {
     363         181 :                 uint32_t i, children = zend_ast_get_num_children(ast);
     364         181 :                 zend_ast *new = emalloc(zend_ast_size(children));
     365         181 :                 new->kind = ast->kind;
     366         181 :                 new->attr = ast->attr;
     367         542 :                 for (i = 0; i < children; i++) {
     368         361 :                         new->child[i] = zend_ast_copy(ast->child[i]);
     369             :                 }
     370         181 :                 return new;
     371             :         }
     372             : }
     373             : 
     374     4705133 : static void zend_ast_destroy_ex(zend_ast *ast, zend_bool free) {
     375     4705133 :         if (!ast) {
     376      328836 :                 return;
     377             :         }
     378             : 
     379     4376297 :         switch (ast->kind) {
     380             :                 case ZEND_AST_ZVAL:
     381             :                         /* Destroy value without using GC: When opcache moves arrays into SHM it will
     382             :                          * free the zend_array structure, so references to it from outside the op array
     383             :                          * become invalid. GC would cause such a reference in the root buffer. */
     384             :                         zval_ptr_dtor_nogc(zend_ast_get_zval(ast));
     385     1724313 :                         break;
     386             :                 case ZEND_AST_FUNC_DECL:
     387             :                 case ZEND_AST_CLOSURE:
     388             :                 case ZEND_AST_METHOD:
     389             :                 case ZEND_AST_CLASS:
     390             :                 {
     391       42750 :                         zend_ast_decl *decl = (zend_ast_decl *) ast;
     392       42750 :                         zend_string_release(decl->name);
     393       42750 :                         if (decl->doc_comment) {
     394        2455 :                                 zend_string_release(decl->doc_comment);
     395             :                         }
     396       42750 :                         zend_ast_destroy_ex(decl->child[0], free);
     397       42750 :                         zend_ast_destroy_ex(decl->child[1], free);
     398       42750 :                         zend_ast_destroy_ex(decl->child[2], free);
     399       42750 :                         break;
     400             :                 }
     401             :                 default:
     402     2609234 :                         if (zend_ast_is_list(ast)) {
     403      665248 :                                 zend_ast_list *list = zend_ast_get_list(ast);
     404             :                                 uint32_t i;
     405     2061053 :                                 for (i = 0; i < list->children; i++) {
     406     1395805 :                                         zend_ast_destroy_ex(list->child[i], free);
     407             :                                 }
     408             :                         } else {
     409     1943986 :                                 uint32_t i, children = zend_ast_get_num_children(ast);
     410     5068162 :                                 for (i = 0; i < children; i++) {
     411     3124176 :                                         zend_ast_destroy_ex(ast->child[i], free);
     412             :                                 }
     413             :                         }
     414             :         }
     415             : 
     416     4376297 :         if (free) {
     417         595 :                 efree(ast);
     418             :         }
     419             : }
     420             : 
     421       56770 : ZEND_API void zend_ast_destroy(zend_ast *ast) {
     422       56770 :         zend_ast_destroy_ex(ast, 0);
     423       56770 : }
     424         132 : ZEND_API void zend_ast_destroy_and_free(zend_ast *ast) {
     425         132 :         zend_ast_destroy_ex(ast, 1);
     426         132 : }
     427             : 
     428         275 : ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn) {
     429         275 :         if (zend_ast_is_list(ast)) {
     430          97 :                 zend_ast_list *list = zend_ast_get_list(ast);
     431             :                 uint32_t i;
     432         227 :                 for (i = 0; i < list->children; ++i) {
     433         130 :                         fn(&list->child[i]);
     434             :                 }
     435             :         } else {
     436         178 :                 uint32_t i, children = zend_ast_get_num_children(ast);
     437         533 :                 for (i = 0; i < children; ++i) {
     438         355 :                         fn(&ast->child[i]);
     439             :                 }
     440             :         }
     441         275 : }

Generated by: LCOV version 1.10

Generated at Mon, 26 Jan 2015 14:46:40 +0000 (2 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.