zend_ast.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 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@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend_ast.h"
  20. #include "zend_API.h"
  21. #include "zend_operators.h"
  22. #include "zend_language_parser.h"
  23. #include "zend_smart_str.h"
  24. #include "zend_exceptions.h"
  25. #include "zend_constants.h"
  26. ZEND_API zend_ast_process_t zend_ast_process = NULL;
  27. static inline void *zend_ast_alloc(size_t size) {
  28. return zend_arena_alloc(&CG(ast_arena), size);
  29. }
  30. static inline void *zend_ast_realloc(void *old, size_t old_size, size_t new_size) {
  31. void *new = zend_ast_alloc(new_size);
  32. memcpy(new, old, old_size);
  33. return new;
  34. }
  35. static inline size_t zend_ast_size(uint32_t children) {
  36. return sizeof(zend_ast) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
  37. }
  38. static inline size_t zend_ast_list_size(uint32_t children) {
  39. return sizeof(zend_ast_list) - sizeof(zend_ast *) + sizeof(zend_ast *) * children;
  40. }
  41. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_znode(znode *node) {
  42. zend_ast_znode *ast;
  43. ast = zend_ast_alloc(sizeof(zend_ast_znode));
  44. ast->kind = ZEND_AST_ZNODE;
  45. ast->attr = 0;
  46. ast->lineno = CG(zend_lineno);
  47. ast->node = *node;
  48. return (zend_ast *) ast;
  49. }
  50. static zend_always_inline zend_ast * zend_ast_create_zval_int(zval *zv, uint32_t attr, uint32_t lineno) {
  51. zend_ast_zval *ast;
  52. ast = zend_ast_alloc(sizeof(zend_ast_zval));
  53. ast->kind = ZEND_AST_ZVAL;
  54. ast->attr = attr;
  55. ZVAL_COPY_VALUE(&ast->val, zv);
  56. Z_LINENO(ast->val) = lineno;
  57. return (zend_ast *) ast;
  58. }
  59. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_with_lineno(zval *zv, uint32_t lineno) {
  60. return zend_ast_create_zval_int(zv, 0, lineno);
  61. }
  62. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_ex(zval *zv, zend_ast_attr attr) {
  63. return zend_ast_create_zval_int(zv, attr, CG(zend_lineno));
  64. }
  65. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval(zval *zv) {
  66. return zend_ast_create_zval_int(zv, 0, CG(zend_lineno));
  67. }
  68. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_str(zend_string *str) {
  69. zval zv;
  70. ZVAL_STR(&zv, str);
  71. return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno));
  72. }
  73. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_zval_from_long(zend_long lval) {
  74. zval zv;
  75. ZVAL_LONG(&zv, lval);
  76. return zend_ast_create_zval_int(&zv, 0, CG(zend_lineno));
  77. }
  78. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_constant(zend_string *name, zend_ast_attr attr) {
  79. zend_ast_zval *ast;
  80. ast = zend_ast_alloc(sizeof(zend_ast_zval));
  81. ast->kind = ZEND_AST_CONSTANT;
  82. ast->attr = attr;
  83. ZVAL_STR(&ast->val, name);
  84. Z_LINENO(ast->val) = CG(zend_lineno);
  85. return (zend_ast *) ast;
  86. }
  87. ZEND_API zend_ast *zend_ast_create_decl(
  88. zend_ast_kind kind, uint32_t flags, uint32_t start_lineno, zend_string *doc_comment,
  89. zend_string *name, zend_ast *child0, zend_ast *child1, zend_ast *child2, zend_ast *child3
  90. ) {
  91. zend_ast_decl *ast;
  92. ast = zend_ast_alloc(sizeof(zend_ast_decl));
  93. ast->kind = kind;
  94. ast->attr = 0;
  95. ast->start_lineno = start_lineno;
  96. ast->end_lineno = CG(zend_lineno);
  97. ast->flags = flags;
  98. ast->lex_pos = LANG_SCNG(yy_text);
  99. ast->doc_comment = doc_comment;
  100. ast->name = name;
  101. ast->child[0] = child0;
  102. ast->child[1] = child1;
  103. ast->child[2] = child2;
  104. ast->child[3] = child3;
  105. return (zend_ast *) ast;
  106. }
  107. #if ZEND_AST_SPEC
  108. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_0(zend_ast_kind kind) {
  109. zend_ast *ast;
  110. ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 0);
  111. ast = zend_ast_alloc(zend_ast_size(0));
  112. ast->kind = kind;
  113. ast->attr = 0;
  114. ast->lineno = CG(zend_lineno);
  115. return ast;
  116. }
  117. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_1(zend_ast_kind kind, zend_ast *child) {
  118. zend_ast *ast;
  119. uint32_t lineno;
  120. ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 1);
  121. ast = zend_ast_alloc(zend_ast_size(1));
  122. ast->kind = kind;
  123. ast->attr = 0;
  124. ast->child[0] = child;
  125. if (child) {
  126. lineno = zend_ast_get_lineno(child);
  127. } else {
  128. lineno = CG(zend_lineno);
  129. }
  130. ast->lineno = lineno;
  131. ast->lineno = lineno;
  132. return ast;
  133. }
  134. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) {
  135. zend_ast *ast;
  136. uint32_t lineno;
  137. ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 2);
  138. ast = zend_ast_alloc(zend_ast_size(2));
  139. ast->kind = kind;
  140. ast->attr = 0;
  141. ast->child[0] = child1;
  142. ast->child[1] = child2;
  143. if (child1) {
  144. lineno = zend_ast_get_lineno(child1);
  145. } else if (child2) {
  146. lineno = zend_ast_get_lineno(child2);
  147. } else {
  148. lineno = CG(zend_lineno);
  149. }
  150. ast->lineno = lineno;
  151. return ast;
  152. }
  153. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_3(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3) {
  154. zend_ast *ast;
  155. uint32_t lineno;
  156. ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 3);
  157. ast = zend_ast_alloc(zend_ast_size(3));
  158. ast->kind = kind;
  159. ast->attr = 0;
  160. ast->child[0] = child1;
  161. ast->child[1] = child2;
  162. ast->child[2] = child3;
  163. if (child1) {
  164. lineno = zend_ast_get_lineno(child1);
  165. } else if (child2) {
  166. lineno = zend_ast_get_lineno(child2);
  167. } else if (child3) {
  168. lineno = zend_ast_get_lineno(child3);
  169. } else {
  170. lineno = CG(zend_lineno);
  171. }
  172. ast->lineno = lineno;
  173. return ast;
  174. }
  175. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_4(zend_ast_kind kind, zend_ast *child1, zend_ast *child2, zend_ast *child3, zend_ast *child4) {
  176. zend_ast *ast;
  177. uint32_t lineno;
  178. ZEND_ASSERT(kind >> ZEND_AST_NUM_CHILDREN_SHIFT == 4);
  179. ast = zend_ast_alloc(zend_ast_size(4));
  180. ast->kind = kind;
  181. ast->attr = 0;
  182. ast->child[0] = child1;
  183. ast->child[1] = child2;
  184. ast->child[2] = child3;
  185. ast->child[3] = child4;
  186. if (child1) {
  187. lineno = zend_ast_get_lineno(child1);
  188. } else if (child2) {
  189. lineno = zend_ast_get_lineno(child2);
  190. } else if (child3) {
  191. lineno = zend_ast_get_lineno(child3);
  192. } else if (child4) {
  193. lineno = zend_ast_get_lineno(child4);
  194. } else {
  195. lineno = CG(zend_lineno);
  196. }
  197. ast->lineno = lineno;
  198. return ast;
  199. }
  200. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_0(zend_ast_kind kind) {
  201. zend_ast *ast;
  202. zend_ast_list *list;
  203. ast = zend_ast_alloc(zend_ast_list_size(4));
  204. list = (zend_ast_list *) ast;
  205. list->kind = kind;
  206. list->attr = 0;
  207. list->lineno = CG(zend_lineno);
  208. list->children = 0;
  209. return ast;
  210. }
  211. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_1(zend_ast_kind kind, zend_ast *child) {
  212. zend_ast *ast;
  213. zend_ast_list *list;
  214. uint32_t lineno;
  215. ast = zend_ast_alloc(zend_ast_list_size(4));
  216. list = (zend_ast_list *) ast;
  217. list->kind = kind;
  218. list->attr = 0;
  219. list->children = 1;
  220. list->child[0] = child;
  221. if (child) {
  222. lineno = zend_ast_get_lineno(child);
  223. if (lineno > CG(zend_lineno)) {
  224. lineno = CG(zend_lineno);
  225. }
  226. } else {
  227. lineno = CG(zend_lineno);
  228. }
  229. list->lineno = lineno;
  230. return ast;
  231. }
  232. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_list_2(zend_ast_kind kind, zend_ast *child1, zend_ast *child2) {
  233. zend_ast *ast;
  234. zend_ast_list *list;
  235. uint32_t lineno;
  236. ast = zend_ast_alloc(zend_ast_list_size(4));
  237. list = (zend_ast_list *) ast;
  238. list->kind = kind;
  239. list->attr = 0;
  240. list->children = 2;
  241. list->child[0] = child1;
  242. list->child[1] = child2;
  243. if (child1) {
  244. lineno = zend_ast_get_lineno(child1);
  245. if (lineno > CG(zend_lineno)) {
  246. lineno = CG(zend_lineno);
  247. }
  248. } else if (child2) {
  249. lineno = zend_ast_get_lineno(child2);
  250. if (lineno > CG(zend_lineno)) {
  251. lineno = CG(zend_lineno);
  252. }
  253. } else {
  254. list->children = 0;
  255. lineno = CG(zend_lineno);
  256. }
  257. list->lineno = lineno;
  258. return ast;
  259. }
  260. #else
  261. static zend_ast *zend_ast_create_from_va_list(zend_ast_kind kind, zend_ast_attr attr, va_list va) {
  262. uint32_t i, children = kind >> ZEND_AST_NUM_CHILDREN_SHIFT;
  263. zend_ast *ast;
  264. ast = zend_ast_alloc(zend_ast_size(children));
  265. ast->kind = kind;
  266. ast->attr = attr;
  267. ast->lineno = (uint32_t) -1;
  268. for (i = 0; i < children; ++i) {
  269. ast->child[i] = va_arg(va, zend_ast *);
  270. if (ast->child[i] != NULL) {
  271. uint32_t lineno = zend_ast_get_lineno(ast->child[i]);
  272. if (lineno < ast->lineno) {
  273. ast->lineno = lineno;
  274. }
  275. }
  276. }
  277. if (ast->lineno == UINT_MAX) {
  278. ast->lineno = CG(zend_lineno);
  279. }
  280. return ast;
  281. }
  282. ZEND_API zend_ast *zend_ast_create_ex(zend_ast_kind kind, zend_ast_attr attr, ...) {
  283. va_list va;
  284. zend_ast *ast;
  285. va_start(va, attr);
  286. ast = zend_ast_create_from_va_list(kind, attr, va);
  287. va_end(va);
  288. return ast;
  289. }
  290. ZEND_API zend_ast *zend_ast_create(zend_ast_kind kind, ...) {
  291. va_list va;
  292. zend_ast *ast;
  293. va_start(va, kind);
  294. ast = zend_ast_create_from_va_list(kind, 0, va);
  295. va_end(va);
  296. return ast;
  297. }
  298. ZEND_API zend_ast *zend_ast_create_list(uint32_t init_children, zend_ast_kind kind, ...) {
  299. zend_ast *ast;
  300. zend_ast_list *list;
  301. ast = zend_ast_alloc(zend_ast_list_size(4));
  302. list = (zend_ast_list *) ast;
  303. list->kind = kind;
  304. list->attr = 0;
  305. list->lineno = CG(zend_lineno);
  306. list->children = 0;
  307. {
  308. va_list va;
  309. uint32_t i;
  310. va_start(va, kind);
  311. for (i = 0; i < init_children; ++i) {
  312. zend_ast *child = va_arg(va, zend_ast *);
  313. ast = zend_ast_list_add(ast, child);
  314. if (child != NULL) {
  315. uint32_t lineno = zend_ast_get_lineno(child);
  316. if (lineno < ast->lineno) {
  317. ast->lineno = lineno;
  318. }
  319. }
  320. }
  321. va_end(va);
  322. }
  323. return ast;
  324. }
  325. #endif
  326. static inline zend_bool is_power_of_two(uint32_t n) {
  327. return ((n != 0) && (n == (n & (~n + 1))));
  328. }
  329. ZEND_API zend_ast * ZEND_FASTCALL zend_ast_list_add(zend_ast *ast, zend_ast *op) {
  330. zend_ast_list *list = zend_ast_get_list(ast);
  331. if (list->children >= 4 && is_power_of_two(list->children)) {
  332. list = zend_ast_realloc(list,
  333. zend_ast_list_size(list->children), zend_ast_list_size(list->children * 2));
  334. }
  335. list->child[list->children++] = op;
  336. return (zend_ast *) list;
  337. }
  338. static int zend_ast_add_array_element(zval *result, zval *offset, zval *expr)
  339. {
  340. switch (Z_TYPE_P(offset)) {
  341. case IS_UNDEF:
  342. if (!zend_hash_next_index_insert(Z_ARRVAL_P(result), expr)) {
  343. zend_error(E_WARNING,
  344. "Cannot add element to the array as the next element is already occupied");
  345. zval_ptr_dtor_nogc(expr);
  346. }
  347. break;
  348. case IS_STRING:
  349. zend_symtable_update(Z_ARRVAL_P(result), Z_STR_P(offset), expr);
  350. zval_ptr_dtor_str(offset);
  351. break;
  352. case IS_NULL:
  353. zend_symtable_update(Z_ARRVAL_P(result), ZSTR_EMPTY_ALLOC(), expr);
  354. break;
  355. case IS_LONG:
  356. zend_hash_index_update(Z_ARRVAL_P(result), Z_LVAL_P(offset), expr);
  357. break;
  358. case IS_FALSE:
  359. zend_hash_index_update(Z_ARRVAL_P(result), 0, expr);
  360. break;
  361. case IS_TRUE:
  362. zend_hash_index_update(Z_ARRVAL_P(result), 1, expr);
  363. break;
  364. case IS_DOUBLE:
  365. zend_hash_index_update(Z_ARRVAL_P(result), zend_dval_to_lval(Z_DVAL_P(offset)), expr);
  366. break;
  367. case IS_RESOURCE:
  368. zend_error(E_NOTICE, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(offset), Z_RES_HANDLE_P(offset));
  369. zend_hash_index_update(Z_ARRVAL_P(result), Z_RES_HANDLE_P(offset), expr);
  370. break;
  371. default:
  372. zend_throw_error(NULL, "Illegal offset type");
  373. return FAILURE;
  374. }
  375. return SUCCESS;
  376. }
  377. ZEND_API int ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast, zend_class_entry *scope)
  378. {
  379. zval op1, op2;
  380. int ret = SUCCESS;
  381. switch (ast->kind) {
  382. case ZEND_AST_BINARY_OP:
  383. if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
  384. ret = FAILURE;
  385. } else if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
  386. zval_ptr_dtor_nogc(&op1);
  387. ret = FAILURE;
  388. } else {
  389. binary_op_type op = get_binary_op(ast->attr);
  390. ret = op(result, &op1, &op2);
  391. zval_ptr_dtor_nogc(&op1);
  392. zval_ptr_dtor_nogc(&op2);
  393. }
  394. break;
  395. case ZEND_AST_GREATER:
  396. case ZEND_AST_GREATER_EQUAL:
  397. if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
  398. ret = FAILURE;
  399. } else if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
  400. zval_ptr_dtor_nogc(&op1);
  401. ret = FAILURE;
  402. } else {
  403. /* op1 > op2 is the same as op2 < op1 */
  404. binary_op_type op = ast->kind == ZEND_AST_GREATER
  405. ? is_smaller_function : is_smaller_or_equal_function;
  406. ret = op(result, &op2, &op1);
  407. zval_ptr_dtor_nogc(&op1);
  408. zval_ptr_dtor_nogc(&op2);
  409. }
  410. break;
  411. case ZEND_AST_UNARY_OP:
  412. if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
  413. ret = FAILURE;
  414. } else {
  415. unary_op_type op = get_unary_op(ast->attr);
  416. ret = op(result, &op1);
  417. zval_ptr_dtor_nogc(&op1);
  418. }
  419. break;
  420. case ZEND_AST_ZVAL:
  421. {
  422. zval *zv = zend_ast_get_zval(ast);
  423. ZVAL_COPY(result, zv);
  424. break;
  425. }
  426. case ZEND_AST_CONSTANT:
  427. {
  428. zend_string *name = zend_ast_get_constant_name(ast);
  429. zval *zv = zend_get_constant_ex(name, scope, ast->attr);
  430. if (UNEXPECTED(zv == NULL)) {
  431. ZVAL_UNDEF(result);
  432. ret = zend_use_undefined_constant(name, ast->attr, result);
  433. break;
  434. }
  435. ZVAL_COPY_OR_DUP(result, zv);
  436. break;
  437. }
  438. case ZEND_AST_CONSTANT_CLASS:
  439. ZEND_ASSERT(EG(current_execute_data));
  440. if (scope && scope->name) {
  441. ZVAL_STR_COPY(result, scope->name);
  442. } else {
  443. ZVAL_EMPTY_STRING(result);
  444. }
  445. break;
  446. case ZEND_AST_AND:
  447. if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
  448. ret = FAILURE;
  449. break;
  450. }
  451. if (zend_is_true(&op1)) {
  452. if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
  453. zval_ptr_dtor_nogc(&op1);
  454. ret = FAILURE;
  455. break;
  456. }
  457. ZVAL_BOOL(result, zend_is_true(&op2));
  458. zval_ptr_dtor_nogc(&op2);
  459. } else {
  460. ZVAL_FALSE(result);
  461. }
  462. zval_ptr_dtor_nogc(&op1);
  463. break;
  464. case ZEND_AST_OR:
  465. if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
  466. ret = FAILURE;
  467. break;
  468. }
  469. if (zend_is_true(&op1)) {
  470. ZVAL_TRUE(result);
  471. } else {
  472. if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
  473. zval_ptr_dtor_nogc(&op1);
  474. ret = FAILURE;
  475. break;
  476. }
  477. ZVAL_BOOL(result, zend_is_true(&op2));
  478. zval_ptr_dtor_nogc(&op2);
  479. }
  480. zval_ptr_dtor_nogc(&op1);
  481. break;
  482. case ZEND_AST_CONDITIONAL:
  483. if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
  484. ret = FAILURE;
  485. break;
  486. }
  487. if (zend_is_true(&op1)) {
  488. if (!ast->child[1]) {
  489. *result = op1;
  490. } else {
  491. if (UNEXPECTED(zend_ast_evaluate(result, ast->child[1], scope) != SUCCESS)) {
  492. zval_ptr_dtor_nogc(&op1);
  493. ret = FAILURE;
  494. break;
  495. }
  496. zval_ptr_dtor_nogc(&op1);
  497. }
  498. } else {
  499. if (UNEXPECTED(zend_ast_evaluate(result, ast->child[2], scope) != SUCCESS)) {
  500. zval_ptr_dtor_nogc(&op1);
  501. ret = FAILURE;
  502. break;
  503. }
  504. zval_ptr_dtor_nogc(&op1);
  505. }
  506. break;
  507. case ZEND_AST_COALESCE:
  508. if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
  509. ret = FAILURE;
  510. break;
  511. }
  512. if (Z_TYPE(op1) > IS_NULL) {
  513. *result = op1;
  514. } else {
  515. if (UNEXPECTED(zend_ast_evaluate(result, ast->child[1], scope) != SUCCESS)) {
  516. zval_ptr_dtor_nogc(&op1);
  517. ret = FAILURE;
  518. break;
  519. }
  520. zval_ptr_dtor_nogc(&op1);
  521. }
  522. break;
  523. case ZEND_AST_UNARY_PLUS:
  524. if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[0], scope) != SUCCESS)) {
  525. ret = FAILURE;
  526. } else {
  527. ZVAL_LONG(&op1, 0);
  528. ret = add_function(result, &op1, &op2);
  529. zval_ptr_dtor_nogc(&op2);
  530. }
  531. break;
  532. case ZEND_AST_UNARY_MINUS:
  533. if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[0], scope) != SUCCESS)) {
  534. ret = FAILURE;
  535. } else {
  536. ZVAL_LONG(&op1, 0);
  537. ret = sub_function(result, &op1, &op2);
  538. zval_ptr_dtor_nogc(&op2);
  539. }
  540. break;
  541. case ZEND_AST_ARRAY:
  542. {
  543. uint32_t i;
  544. zend_ast_list *list = zend_ast_get_list(ast);
  545. if (!list->children) {
  546. ZVAL_EMPTY_ARRAY(result);
  547. break;
  548. }
  549. array_init(result);
  550. for (i = 0; i < list->children; i++) {
  551. zend_ast *elem = list->child[i];
  552. if (elem->child[1]) {
  553. if (UNEXPECTED(zend_ast_evaluate(&op1, elem->child[1], scope) != SUCCESS)) {
  554. zval_ptr_dtor_nogc(result);
  555. return FAILURE;
  556. }
  557. } else {
  558. ZVAL_UNDEF(&op1);
  559. }
  560. if (UNEXPECTED(zend_ast_evaluate(&op2, elem->child[0], scope) != SUCCESS)) {
  561. zval_ptr_dtor_nogc(&op1);
  562. zval_ptr_dtor_nogc(result);
  563. return FAILURE;
  564. }
  565. if (UNEXPECTED(zend_ast_add_array_element(result, &op1, &op2) != SUCCESS)) {
  566. zval_ptr_dtor_nogc(&op1);
  567. zval_ptr_dtor_nogc(&op2);
  568. zval_ptr_dtor_nogc(result);
  569. return FAILURE;
  570. }
  571. }
  572. }
  573. break;
  574. case ZEND_AST_DIM:
  575. if (ast->child[1] == NULL) {
  576. zend_error_noreturn(E_COMPILE_ERROR, "Cannot use [] for reading");
  577. }
  578. if (UNEXPECTED(zend_ast_evaluate(&op1, ast->child[0], scope) != SUCCESS)) {
  579. ret = FAILURE;
  580. } else if (UNEXPECTED(zend_ast_evaluate(&op2, ast->child[1], scope) != SUCCESS)) {
  581. zval_ptr_dtor_nogc(&op1);
  582. ret = FAILURE;
  583. } else {
  584. zend_fetch_dimension_const(result, &op1, &op2, (ast->attr == ZEND_DIM_IS) ? BP_VAR_IS : BP_VAR_R);
  585. zval_ptr_dtor_nogc(&op1);
  586. zval_ptr_dtor_nogc(&op2);
  587. }
  588. break;
  589. default:
  590. zend_throw_error(NULL, "Unsupported constant expression");
  591. ret = FAILURE;
  592. }
  593. return ret;
  594. }
  595. static size_t ZEND_FASTCALL zend_ast_tree_size(zend_ast *ast)
  596. {
  597. size_t size;
  598. if (ast->kind == ZEND_AST_ZVAL || ast->kind == ZEND_AST_CONSTANT) {
  599. size = sizeof(zend_ast_zval);
  600. } else if (zend_ast_is_list(ast)) {
  601. uint32_t i;
  602. zend_ast_list *list = zend_ast_get_list(ast);
  603. size = zend_ast_list_size(list->children);
  604. for (i = 0; i < list->children; i++) {
  605. if (list->child[i]) {
  606. size += zend_ast_tree_size(list->child[i]);
  607. }
  608. }
  609. } else {
  610. uint32_t i, children = zend_ast_get_num_children(ast);
  611. size = zend_ast_size(children);
  612. for (i = 0; i < children; i++) {
  613. if (ast->child[i]) {
  614. size += zend_ast_tree_size(ast->child[i]);
  615. }
  616. }
  617. }
  618. return size;
  619. }
  620. static void* ZEND_FASTCALL zend_ast_tree_copy(zend_ast *ast, void *buf)
  621. {
  622. if (ast->kind == ZEND_AST_ZVAL) {
  623. zend_ast_zval *new = (zend_ast_zval*)buf;
  624. new->kind = ZEND_AST_ZVAL;
  625. new->attr = ast->attr;
  626. ZVAL_COPY(&new->val, zend_ast_get_zval(ast));
  627. buf = (void*)((char*)buf + sizeof(zend_ast_zval));
  628. } else if (ast->kind == ZEND_AST_CONSTANT) {
  629. zend_ast_zval *new = (zend_ast_zval*)buf;
  630. new->kind = ZEND_AST_CONSTANT;
  631. new->attr = ast->attr;
  632. ZVAL_STR_COPY(&new->val, zend_ast_get_constant_name(ast));
  633. buf = (void*)((char*)buf + sizeof(zend_ast_zval));
  634. } else if (zend_ast_is_list(ast)) {
  635. zend_ast_list *list = zend_ast_get_list(ast);
  636. zend_ast_list *new = (zend_ast_list*)buf;
  637. uint32_t i;
  638. new->kind = list->kind;
  639. new->attr = list->attr;
  640. new->children = list->children;
  641. buf = (void*)((char*)buf + zend_ast_list_size(list->children));
  642. for (i = 0; i < list->children; i++) {
  643. if (list->child[i]) {
  644. new->child[i] = (zend_ast*)buf;
  645. buf = zend_ast_tree_copy(list->child[i], buf);
  646. } else {
  647. new->child[i] = NULL;
  648. }
  649. }
  650. } else {
  651. uint32_t i, children = zend_ast_get_num_children(ast);
  652. zend_ast *new = (zend_ast*)buf;
  653. new->kind = ast->kind;
  654. new->attr = ast->attr;
  655. buf = (void*)((char*)buf + zend_ast_size(children));
  656. for (i = 0; i < children; i++) {
  657. if (ast->child[i]) {
  658. new->child[i] = (zend_ast*)buf;
  659. buf = zend_ast_tree_copy(ast->child[i], buf);
  660. } else {
  661. new->child[i] = NULL;
  662. }
  663. }
  664. }
  665. return buf;
  666. }
  667. ZEND_API zend_ast_ref * ZEND_FASTCALL zend_ast_copy(zend_ast *ast)
  668. {
  669. size_t tree_size;
  670. zend_ast_ref *ref;
  671. ZEND_ASSERT(ast != NULL);
  672. tree_size = zend_ast_tree_size(ast) + sizeof(zend_ast_ref);
  673. ref = emalloc(tree_size);
  674. zend_ast_tree_copy(ast, GC_AST(ref));
  675. GC_SET_REFCOUNT(ref, 1);
  676. GC_TYPE_INFO(ref) = IS_CONSTANT_AST;
  677. return ref;
  678. }
  679. ZEND_API void ZEND_FASTCALL zend_ast_destroy(zend_ast *ast)
  680. {
  681. tail_call:
  682. if (!ast) {
  683. return;
  684. }
  685. if (EXPECTED(ast->kind >= ZEND_AST_VAR)) {
  686. uint32_t i, children = zend_ast_get_num_children(ast);
  687. for (i = 1; i < children; i++) {
  688. zend_ast_destroy(ast->child[i]);
  689. }
  690. ast = ast->child[0];
  691. goto tail_call;
  692. } else if (EXPECTED(ast->kind == ZEND_AST_ZVAL)) {
  693. zval_ptr_dtor_nogc(zend_ast_get_zval(ast));
  694. } else if (EXPECTED(zend_ast_is_list(ast))) {
  695. zend_ast_list *list = zend_ast_get_list(ast);
  696. if (list->children) {
  697. uint32_t i;
  698. for (i = 1; i < list->children; i++) {
  699. zend_ast_destroy(list->child[i]);
  700. }
  701. ast = list->child[0];
  702. goto tail_call;
  703. }
  704. } else if (EXPECTED(ast->kind == ZEND_AST_CONSTANT)) {
  705. zend_string_release_ex(zend_ast_get_constant_name(ast), 0);
  706. } else if (EXPECTED(ast->kind >= ZEND_AST_FUNC_DECL)) {
  707. zend_ast_decl *decl = (zend_ast_decl *) ast;
  708. if (decl->name) {
  709. zend_string_release_ex(decl->name, 0);
  710. }
  711. if (decl->doc_comment) {
  712. zend_string_release_ex(decl->doc_comment, 0);
  713. }
  714. zend_ast_destroy(decl->child[0]);
  715. zend_ast_destroy(decl->child[1]);
  716. zend_ast_destroy(decl->child[2]);
  717. ast = decl->child[3];
  718. goto tail_call;
  719. }
  720. }
  721. ZEND_API void ZEND_FASTCALL zend_ast_ref_destroy(zend_ast_ref *ast)
  722. {
  723. zend_ast_destroy(GC_AST(ast));
  724. efree(ast);
  725. }
  726. ZEND_API void zend_ast_apply(zend_ast *ast, zend_ast_apply_func fn) {
  727. if (zend_ast_is_list(ast)) {
  728. zend_ast_list *list = zend_ast_get_list(ast);
  729. uint32_t i;
  730. for (i = 0; i < list->children; ++i) {
  731. fn(&list->child[i]);
  732. }
  733. } else {
  734. uint32_t i, children = zend_ast_get_num_children(ast);
  735. for (i = 0; i < children; ++i) {
  736. fn(&ast->child[i]);
  737. }
  738. }
  739. }
  740. /*
  741. * Operator Precendence
  742. * ====================
  743. * priority associativity operators
  744. * ----------------------------------
  745. * 10 left include, include_once, eval, require, require_once
  746. * 20 left ,
  747. * 30 left or
  748. * 40 left xor
  749. * 50 left and
  750. * 60 right print
  751. * 70 right yield
  752. * 80 right =>
  753. * 85 right yield from
  754. * 90 right = += -= *= /= .= %= &= |= ^= <<= >>= **=
  755. * 100 left ? :
  756. * 110 right ??
  757. * 120 left ||
  758. * 130 left &&
  759. * 140 left |
  760. * 150 left ^
  761. * 160 left &
  762. * 170 non-associative == != === !==
  763. * 180 non-associative < <= > >= <=>
  764. * 190 left << >>
  765. * 200 left + - .
  766. * 210 left * / %
  767. * 220 right !
  768. * 230 non-associative instanceof
  769. * 240 right + - ++ -- ~ (type) @
  770. * 250 right **
  771. * 260 left [
  772. * 270 non-associative clone new
  773. */
  774. static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int priority, int indent);
  775. static ZEND_COLD void zend_ast_export_str(smart_str *str, zend_string *s)
  776. {
  777. size_t i;
  778. for (i = 0; i < ZSTR_LEN(s); i++) {
  779. unsigned char c = ZSTR_VAL(s)[i];
  780. if (c == '\'' || c == '\\') {
  781. smart_str_appendc(str, '\\');
  782. smart_str_appendc(str, c);
  783. } else {
  784. smart_str_appendc(str, c);
  785. }
  786. }
  787. }
  788. static ZEND_COLD void zend_ast_export_qstr(smart_str *str, char quote, zend_string *s)
  789. {
  790. size_t i;
  791. for (i = 0; i < ZSTR_LEN(s); i++) {
  792. unsigned char c = ZSTR_VAL(s)[i];
  793. if (c < ' ') {
  794. switch (c) {
  795. case '\n':
  796. smart_str_appends(str, "\\n");
  797. break;
  798. case '\r':
  799. smart_str_appends(str, "\\r");
  800. break;
  801. case '\t':
  802. smart_str_appends(str, "\\t");
  803. break;
  804. case '\f':
  805. smart_str_appends(str, "\\f");
  806. break;
  807. case '\v':
  808. smart_str_appends(str, "\\v");
  809. break;
  810. #ifdef ZEND_WIN32
  811. case VK_ESCAPE:
  812. #else
  813. case '\e':
  814. #endif
  815. smart_str_appends(str, "\\e");
  816. break;
  817. default:
  818. smart_str_appends(str, "\\0");
  819. smart_str_appendc(str, '0' + (c / 8));
  820. smart_str_appendc(str, '0' + (c % 8));
  821. break;
  822. }
  823. } else {
  824. if (c == quote || c == '$' || c == '\\') {
  825. smart_str_appendc(str, '\\');
  826. }
  827. smart_str_appendc(str, c);
  828. }
  829. }
  830. }
  831. static ZEND_COLD void zend_ast_export_indent(smart_str *str, int indent)
  832. {
  833. while (indent > 0) {
  834. smart_str_appends(str, " ");
  835. indent--;
  836. }
  837. }
  838. static ZEND_COLD void zend_ast_export_name(smart_str *str, zend_ast *ast, int priority, int indent)
  839. {
  840. if (ast->kind == ZEND_AST_ZVAL) {
  841. zval *zv = zend_ast_get_zval(ast);
  842. if (Z_TYPE_P(zv) == IS_STRING) {
  843. smart_str_append(str, Z_STR_P(zv));
  844. return;
  845. }
  846. }
  847. zend_ast_export_ex(str, ast, priority, indent);
  848. }
  849. static ZEND_COLD void zend_ast_export_ns_name(smart_str *str, zend_ast *ast, int priority, int indent)
  850. {
  851. if (ast->kind == ZEND_AST_ZVAL) {
  852. zval *zv = zend_ast_get_zval(ast);
  853. if (Z_TYPE_P(zv) == IS_STRING) {
  854. if (ast->attr == ZEND_NAME_FQ) {
  855. smart_str_appendc(str, '\\');
  856. } else if (ast->attr == ZEND_NAME_RELATIVE) {
  857. smart_str_appends(str, "namespace\\");
  858. }
  859. smart_str_append(str, Z_STR_P(zv));
  860. return;
  861. }
  862. }
  863. zend_ast_export_ex(str, ast, priority, indent);
  864. }
  865. static ZEND_COLD int zend_ast_valid_var_char(char ch)
  866. {
  867. unsigned char c = (unsigned char)ch;
  868. if (c != '_' && c < 127 &&
  869. (c < '0' || c > '9') &&
  870. (c < 'A' || c > 'Z') &&
  871. (c < 'a' || c > 'z')) {
  872. return 0;
  873. }
  874. return 1;
  875. }
  876. static ZEND_COLD int zend_ast_valid_var_name(const char *s, size_t len)
  877. {
  878. unsigned char c;
  879. size_t i;
  880. if (len == 0) {
  881. return 0;
  882. }
  883. c = (unsigned char)s[0];
  884. if (c != '_' && c < 127 &&
  885. (c < 'A' || c > 'Z') &&
  886. (c < 'a' || c > 'z')) {
  887. return 0;
  888. }
  889. for (i = 1; i < len; i++) {
  890. c = (unsigned char)s[i];
  891. if (c != '_' && c < 127 &&
  892. (c < '0' || c > '9') &&
  893. (c < 'A' || c > 'Z') &&
  894. (c < 'a' || c > 'z')) {
  895. return 0;
  896. }
  897. }
  898. return 1;
  899. }
  900. static ZEND_COLD int zend_ast_var_needs_braces(char ch)
  901. {
  902. return ch == '[' || zend_ast_valid_var_char(ch);
  903. }
  904. static ZEND_COLD void zend_ast_export_var(smart_str *str, zend_ast *ast, int priority, int indent)
  905. {
  906. if (ast->kind == ZEND_AST_ZVAL) {
  907. zval *zv = zend_ast_get_zval(ast);
  908. if (Z_TYPE_P(zv) == IS_STRING &&
  909. zend_ast_valid_var_name(Z_STRVAL_P(zv), Z_STRLEN_P(zv))) {
  910. smart_str_append(str, Z_STR_P(zv));
  911. return;
  912. }
  913. } else if (ast->kind == ZEND_AST_VAR) {
  914. zend_ast_export_ex(str, ast, 0, indent);
  915. return;
  916. }
  917. smart_str_appendc(str, '{');
  918. zend_ast_export_name(str, ast, 0, indent);
  919. smart_str_appendc(str, '}');
  920. }
  921. static ZEND_COLD void zend_ast_export_list(smart_str *str, zend_ast_list *list, int separator, int priority, int indent)
  922. {
  923. uint32_t i = 0;
  924. while (i < list->children) {
  925. if (i != 0 && separator) {
  926. smart_str_appends(str, ", ");
  927. }
  928. zend_ast_export_ex(str, list->child[i], priority, indent);
  929. i++;
  930. }
  931. }
  932. static ZEND_COLD void zend_ast_export_encaps_list(smart_str *str, char quote, zend_ast_list *list, int indent)
  933. {
  934. uint32_t i = 0;
  935. zend_ast *ast;
  936. while (i < list->children) {
  937. ast = list->child[i];
  938. if (ast->kind == ZEND_AST_ZVAL) {
  939. zval *zv = zend_ast_get_zval(ast);
  940. ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
  941. zend_ast_export_qstr(str, quote, Z_STR_P(zv));
  942. } else if (ast->kind == ZEND_AST_VAR &&
  943. ast->child[0]->kind == ZEND_AST_ZVAL &&
  944. (i + 1 == list->children ||
  945. list->child[i + 1]->kind != ZEND_AST_ZVAL ||
  946. !zend_ast_var_needs_braces(
  947. *Z_STRVAL_P(
  948. zend_ast_get_zval(list->child[i + 1]))))) {
  949. zend_ast_export_ex(str, ast, 0, indent);
  950. } else {
  951. smart_str_appendc(str, '{');
  952. zend_ast_export_ex(str, ast, 0, indent);
  953. smart_str_appendc(str, '}');
  954. }
  955. i++;
  956. }
  957. }
  958. static ZEND_COLD void zend_ast_export_name_list_ex(smart_str *str, zend_ast_list *list, int indent, const char *separator)
  959. {
  960. uint32_t i = 0;
  961. while (i < list->children) {
  962. if (i != 0) {
  963. smart_str_appends(str, separator);
  964. }
  965. zend_ast_export_name(str, list->child[i], 0, indent);
  966. i++;
  967. }
  968. }
  969. #define zend_ast_export_name_list(s, l, i) zend_ast_export_name_list_ex(s, l, i, ", ")
  970. #define zend_ast_export_catch_name_list(s, l, i) zend_ast_export_name_list_ex(s, l, i, "|")
  971. static ZEND_COLD void zend_ast_export_var_list(smart_str *str, zend_ast_list *list, int indent)
  972. {
  973. uint32_t i = 0;
  974. while (i < list->children) {
  975. if (i != 0) {
  976. smart_str_appends(str, ", ");
  977. }
  978. if (list->child[i]->attr) {
  979. smart_str_appendc(str, '&');
  980. }
  981. smart_str_appendc(str, '$');
  982. zend_ast_export_name(str, list->child[i], 20, indent);
  983. i++;
  984. }
  985. }
  986. static ZEND_COLD void zend_ast_export_stmt(smart_str *str, zend_ast *ast, int indent)
  987. {
  988. if (!ast) {
  989. return;
  990. }
  991. if (ast->kind == ZEND_AST_STMT_LIST ||
  992. ast->kind == ZEND_AST_TRAIT_ADAPTATIONS) {
  993. zend_ast_list *list = (zend_ast_list*)ast;
  994. uint32_t i = 0;
  995. while (i < list->children) {
  996. ast = list->child[i];
  997. zend_ast_export_stmt(str, ast, indent);
  998. i++;
  999. }
  1000. } else {
  1001. zend_ast_export_indent(str, indent);
  1002. zend_ast_export_ex(str, ast, 0, indent);
  1003. switch (ast->kind) {
  1004. case ZEND_AST_LABEL:
  1005. case ZEND_AST_IF:
  1006. case ZEND_AST_SWITCH:
  1007. case ZEND_AST_WHILE:
  1008. case ZEND_AST_TRY:
  1009. case ZEND_AST_FOR:
  1010. case ZEND_AST_FOREACH:
  1011. case ZEND_AST_FUNC_DECL:
  1012. case ZEND_AST_METHOD:
  1013. case ZEND_AST_CLASS:
  1014. case ZEND_AST_USE_TRAIT:
  1015. case ZEND_AST_NAMESPACE:
  1016. case ZEND_AST_DECLARE:
  1017. break;
  1018. default:
  1019. smart_str_appendc(str, ';');
  1020. break;
  1021. }
  1022. smart_str_appendc(str, '\n');
  1023. }
  1024. }
  1025. static ZEND_COLD void zend_ast_export_if_stmt(smart_str *str, zend_ast_list *list, int indent)
  1026. {
  1027. uint32_t i;
  1028. zend_ast *ast;
  1029. tail_call:
  1030. i = 0;
  1031. while (i < list->children) {
  1032. ast = list->child[i];
  1033. ZEND_ASSERT(ast->kind == ZEND_AST_IF_ELEM);
  1034. if (ast->child[0]) {
  1035. if (i == 0) {
  1036. smart_str_appends(str, "if (");
  1037. } else {
  1038. zend_ast_export_indent(str, indent);
  1039. smart_str_appends(str, "} elseif (");
  1040. }
  1041. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1042. smart_str_appends(str, ") {\n");
  1043. zend_ast_export_stmt(str, ast->child[1], indent + 1);
  1044. } else {
  1045. zend_ast_export_indent(str, indent);
  1046. smart_str_appends(str, "} else ");
  1047. if (ast->child[1] && ast->child[1]->kind == ZEND_AST_IF) {
  1048. list = (zend_ast_list*)ast->child[1];
  1049. goto tail_call;
  1050. } else {
  1051. smart_str_appends(str, "{\n");
  1052. zend_ast_export_stmt(str, ast->child[1], indent + 1);
  1053. }
  1054. }
  1055. i++;
  1056. }
  1057. zend_ast_export_indent(str, indent);
  1058. smart_str_appendc(str, '}');
  1059. }
  1060. static ZEND_COLD void zend_ast_export_zval(smart_str *str, zval *zv, int priority, int indent)
  1061. {
  1062. zend_long idx;
  1063. zend_string *key;
  1064. zval *val;
  1065. int first;
  1066. ZVAL_DEREF(zv);
  1067. switch (Z_TYPE_P(zv)) {
  1068. case IS_NULL:
  1069. smart_str_appends(str, "null");
  1070. break;
  1071. case IS_FALSE:
  1072. smart_str_appends(str, "false");
  1073. break;
  1074. case IS_TRUE:
  1075. smart_str_appends(str, "true");
  1076. break;
  1077. case IS_LONG:
  1078. smart_str_append_long(str, Z_LVAL_P(zv));
  1079. break;
  1080. case IS_DOUBLE:
  1081. key = zend_strpprintf(0, "%.*G", (int) EG(precision), Z_DVAL_P(zv));
  1082. smart_str_appendl(str, ZSTR_VAL(key), ZSTR_LEN(key));
  1083. zend_string_release_ex(key, 0);
  1084. break;
  1085. case IS_STRING:
  1086. smart_str_appendc(str, '\'');
  1087. zend_ast_export_str(str, Z_STR_P(zv));
  1088. smart_str_appendc(str, '\'');
  1089. break;
  1090. case IS_ARRAY:
  1091. smart_str_appendc(str, '[');
  1092. first = 1;
  1093. ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(zv), idx, key, val) {
  1094. if (first) {
  1095. first = 0;
  1096. } else {
  1097. smart_str_appends(str, ", ");
  1098. }
  1099. if (key) {
  1100. smart_str_appendc(str, '\'');
  1101. zend_ast_export_str(str, key);
  1102. smart_str_appends(str, "' => ");
  1103. } else {
  1104. smart_str_append_long(str, idx);
  1105. smart_str_appends(str, " => ");
  1106. }
  1107. zend_ast_export_zval(str, val, 0, indent);
  1108. } ZEND_HASH_FOREACH_END();
  1109. smart_str_appendc(str, ']');
  1110. break;
  1111. case IS_CONSTANT_AST:
  1112. zend_ast_export_ex(str, Z_ASTVAL_P(zv), priority, indent);
  1113. break;
  1114. EMPTY_SWITCH_DEFAULT_CASE();
  1115. }
  1116. }
  1117. static ZEND_COLD void zend_ast_export_class_no_header(smart_str *str, zend_ast_decl *decl, int indent) {
  1118. if (decl->child[0]) {
  1119. smart_str_appends(str, " extends ");
  1120. zend_ast_export_ns_name(str, decl->child[0], 0, indent);
  1121. }
  1122. if (decl->child[1]) {
  1123. smart_str_appends(str, " implements ");
  1124. zend_ast_export_ex(str, decl->child[1], 0, indent);
  1125. }
  1126. smart_str_appends(str, " {\n");
  1127. zend_ast_export_stmt(str, decl->child[2], indent + 1);
  1128. zend_ast_export_indent(str, indent);
  1129. smart_str_appends(str, "}");
  1130. }
  1131. #define BINARY_OP(_op, _p, _pl, _pr) do { \
  1132. op = _op; \
  1133. p = _p; \
  1134. pl = _pl; \
  1135. pr = _pr; \
  1136. goto binary_op; \
  1137. } while (0)
  1138. #define PREFIX_OP(_op, _p, _pl) do { \
  1139. op = _op; \
  1140. p = _p; \
  1141. pl = _pl; \
  1142. goto prefix_op; \
  1143. } while (0)
  1144. #define FUNC_OP(_op) do { \
  1145. op = _op; \
  1146. goto func_op; \
  1147. } while (0)
  1148. #define POSTFIX_OP(_op, _p, _pl) do { \
  1149. op = _op; \
  1150. p = _p; \
  1151. pl = _pl; \
  1152. goto postfix_op; \
  1153. } while (0)
  1154. #define APPEND_NODE_1(_op) do { \
  1155. op = _op; \
  1156. goto append_node_1; \
  1157. } while (0)
  1158. #define APPEND_STR(_op) do { \
  1159. op = _op; \
  1160. goto append_str; \
  1161. } while (0)
  1162. #define APPEND_DEFAULT_VALUE(n) do { \
  1163. p = n; \
  1164. goto append_default_value; \
  1165. } while (0)
  1166. static ZEND_COLD void zend_ast_export_ex(smart_str *str, zend_ast *ast, int priority, int indent)
  1167. {
  1168. zend_ast_decl *decl;
  1169. int p, pl, pr;
  1170. const char *op;
  1171. tail_call:
  1172. if (!ast) {
  1173. return;
  1174. }
  1175. switch (ast->kind) {
  1176. /* special nodes */
  1177. case ZEND_AST_ZVAL:
  1178. zend_ast_export_zval(str, zend_ast_get_zval(ast), priority, indent);
  1179. break;
  1180. case ZEND_AST_CONSTANT: {
  1181. zend_string *name = zend_ast_get_constant_name(ast);
  1182. smart_str_appendl(str, ZSTR_VAL(name), ZSTR_LEN(name));
  1183. break;
  1184. }
  1185. case ZEND_AST_CONSTANT_CLASS:
  1186. smart_str_appendl(str, "__CLASS__", sizeof("__CLASS__")-1);
  1187. break;
  1188. case ZEND_AST_ZNODE:
  1189. /* This AST kind is only used for temporary nodes during compilation */
  1190. ZEND_ASSERT(0);
  1191. break;
  1192. /* declaration nodes */
  1193. case ZEND_AST_FUNC_DECL:
  1194. case ZEND_AST_CLOSURE:
  1195. case ZEND_AST_METHOD:
  1196. decl = (zend_ast_decl *) ast;
  1197. if (decl->flags & ZEND_ACC_PUBLIC) {
  1198. smart_str_appends(str, "public ");
  1199. } else if (decl->flags & ZEND_ACC_PROTECTED) {
  1200. smart_str_appends(str, "protected ");
  1201. } else if (decl->flags & ZEND_ACC_PRIVATE) {
  1202. smart_str_appends(str, "private ");
  1203. }
  1204. if (decl->flags & ZEND_ACC_STATIC) {
  1205. smart_str_appends(str, "static ");
  1206. }
  1207. if (decl->flags & ZEND_ACC_ABSTRACT) {
  1208. smart_str_appends(str, "abstract ");
  1209. }
  1210. if (decl->flags & ZEND_ACC_FINAL) {
  1211. smart_str_appends(str, "final ");
  1212. }
  1213. smart_str_appends(str, "function ");
  1214. if (decl->flags & ZEND_ACC_RETURN_REFERENCE) {
  1215. smart_str_appendc(str, '&');
  1216. }
  1217. if (ast->kind != ZEND_AST_CLOSURE) {
  1218. smart_str_appendl(str, ZSTR_VAL(decl->name), ZSTR_LEN(decl->name));
  1219. }
  1220. smart_str_appendc(str, '(');
  1221. zend_ast_export_ex(str, decl->child[0], 0, indent);
  1222. smart_str_appendc(str, ')');
  1223. zend_ast_export_ex(str, decl->child[1], 0, indent);
  1224. if (decl->child[3]) {
  1225. smart_str_appends(str, ": ");
  1226. if (decl->child[3]->attr & ZEND_TYPE_NULLABLE) {
  1227. smart_str_appendc(str, '?');
  1228. }
  1229. zend_ast_export_ns_name(str, decl->child[3], 0, indent);
  1230. }
  1231. if (decl->child[2]) {
  1232. smart_str_appends(str, " {\n");
  1233. zend_ast_export_stmt(str, decl->child[2], indent + 1);
  1234. zend_ast_export_indent(str, indent);
  1235. smart_str_appendc(str, '}');
  1236. if (ast->kind != ZEND_AST_CLOSURE) {
  1237. smart_str_appendc(str, '\n');
  1238. }
  1239. } else {
  1240. smart_str_appends(str, ";\n");
  1241. }
  1242. break;
  1243. case ZEND_AST_CLASS:
  1244. decl = (zend_ast_decl *) ast;
  1245. if (decl->flags & ZEND_ACC_INTERFACE) {
  1246. smart_str_appends(str, "interface ");
  1247. } else if (decl->flags & ZEND_ACC_TRAIT) {
  1248. smart_str_appends(str, "trait ");
  1249. } else {
  1250. if (decl->flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) {
  1251. smart_str_appends(str, "abstract ");
  1252. }
  1253. if (decl->flags & ZEND_ACC_FINAL) {
  1254. smart_str_appends(str, "final ");
  1255. }
  1256. smart_str_appends(str, "class ");
  1257. }
  1258. smart_str_appendl(str, ZSTR_VAL(decl->name), ZSTR_LEN(decl->name));
  1259. zend_ast_export_class_no_header(str, decl, indent);
  1260. smart_str_appendc(str, '\n');
  1261. break;
  1262. /* list nodes */
  1263. case ZEND_AST_ARG_LIST:
  1264. case ZEND_AST_EXPR_LIST:
  1265. case ZEND_AST_PARAM_LIST:
  1266. simple_list:
  1267. zend_ast_export_list(str, (zend_ast_list*)ast, 1, 20, indent);
  1268. break;
  1269. case ZEND_AST_ARRAY:
  1270. smart_str_appendc(str, '[');
  1271. zend_ast_export_list(str, (zend_ast_list*)ast, 1, 20, indent);
  1272. smart_str_appendc(str, ']');
  1273. break;
  1274. case ZEND_AST_ENCAPS_LIST:
  1275. smart_str_appendc(str, '"');
  1276. zend_ast_export_encaps_list(str, '"', (zend_ast_list*)ast, indent);
  1277. smart_str_appendc(str, '"');
  1278. break;
  1279. case ZEND_AST_STMT_LIST:
  1280. case ZEND_AST_TRAIT_ADAPTATIONS:
  1281. zend_ast_export_stmt(str, ast, indent);
  1282. break;
  1283. case ZEND_AST_IF:
  1284. zend_ast_export_if_stmt(str, (zend_ast_list*)ast, indent);
  1285. break;
  1286. case ZEND_AST_SWITCH_LIST:
  1287. case ZEND_AST_CATCH_LIST:
  1288. zend_ast_export_list(str, (zend_ast_list*)ast, 0, 0, indent);
  1289. break;
  1290. case ZEND_AST_CLOSURE_USES:
  1291. smart_str_appends(str, " use(");
  1292. zend_ast_export_var_list(str, (zend_ast_list*)ast, indent);
  1293. smart_str_appendc(str, ')');
  1294. break;
  1295. case ZEND_AST_PROP_DECL:
  1296. if (ast->attr & ZEND_ACC_PUBLIC) {
  1297. smart_str_appends(str, "public ");
  1298. } else if (ast->attr & ZEND_ACC_PROTECTED) {
  1299. smart_str_appends(str, "protected ");
  1300. } else if (ast->attr & ZEND_ACC_PRIVATE) {
  1301. smart_str_appends(str, "private ");
  1302. }
  1303. if (ast->attr & ZEND_ACC_STATIC) {
  1304. smart_str_appends(str, "static ");
  1305. }
  1306. goto simple_list;
  1307. case ZEND_AST_CONST_DECL:
  1308. case ZEND_AST_CLASS_CONST_DECL:
  1309. smart_str_appends(str, "const ");
  1310. goto simple_list;
  1311. case ZEND_AST_NAME_LIST:
  1312. zend_ast_export_name_list(str, (zend_ast_list*)ast, indent);
  1313. break;
  1314. case ZEND_AST_USE:
  1315. smart_str_appends(str, "use ");
  1316. if (ast->attr == T_FUNCTION) {
  1317. smart_str_appends(str, "function ");
  1318. } else if (ast->attr == T_CONST) {
  1319. smart_str_appends(str, "const ");
  1320. }
  1321. goto simple_list;
  1322. /* 0 child nodes */
  1323. case ZEND_AST_MAGIC_CONST:
  1324. switch (ast->attr) {
  1325. case T_LINE: APPEND_STR("__LINE__");
  1326. case T_FILE: APPEND_STR("__FILE__");
  1327. case T_DIR: APPEND_STR("__DIR__");
  1328. case T_TRAIT_C: APPEND_STR("__TRAIT__");
  1329. case T_METHOD_C: APPEND_STR("__METHOD__");
  1330. case T_FUNC_C: APPEND_STR("__FUNCTION__");
  1331. case T_NS_C: APPEND_STR("__NAMESPACE__");
  1332. case T_CLASS_C: APPEND_STR("__CLASS__");
  1333. EMPTY_SWITCH_DEFAULT_CASE();
  1334. }
  1335. break;
  1336. case ZEND_AST_TYPE:
  1337. switch (ast->attr & ~ZEND_TYPE_NULLABLE) {
  1338. case IS_ARRAY: APPEND_STR("array");
  1339. case IS_CALLABLE: APPEND_STR("callable");
  1340. EMPTY_SWITCH_DEFAULT_CASE();
  1341. }
  1342. break;
  1343. /* 1 child node */
  1344. case ZEND_AST_VAR:
  1345. smart_str_appendc(str, '$');
  1346. zend_ast_export_var(str, ast->child[0], 0, indent);
  1347. break;
  1348. case ZEND_AST_CONST:
  1349. zend_ast_export_ns_name(str, ast->child[0], 0, indent);
  1350. break;
  1351. case ZEND_AST_UNPACK:
  1352. smart_str_appends(str, "...");
  1353. ast = ast->child[0];
  1354. goto tail_call;
  1355. case ZEND_AST_UNARY_PLUS: PREFIX_OP("+", 240, 241);
  1356. case ZEND_AST_UNARY_MINUS: PREFIX_OP("-", 240, 241);
  1357. case ZEND_AST_CAST:
  1358. switch (ast->attr) {
  1359. case IS_NULL: PREFIX_OP("(unset)", 240, 241);
  1360. case _IS_BOOL: PREFIX_OP("(bool)", 240, 241);
  1361. case IS_LONG: PREFIX_OP("(int)", 240, 241);
  1362. case IS_DOUBLE: PREFIX_OP("(double)", 240, 241);
  1363. case IS_STRING: PREFIX_OP("(string)", 240, 241);
  1364. case IS_ARRAY: PREFIX_OP("(array)", 240, 241);
  1365. case IS_OBJECT: PREFIX_OP("(object)", 240, 241);
  1366. EMPTY_SWITCH_DEFAULT_CASE();
  1367. }
  1368. break;
  1369. case ZEND_AST_EMPTY:
  1370. FUNC_OP("empty");
  1371. case ZEND_AST_ISSET:
  1372. FUNC_OP("isset");
  1373. case ZEND_AST_SILENCE:
  1374. PREFIX_OP("@", 240, 241);
  1375. case ZEND_AST_SHELL_EXEC:
  1376. smart_str_appendc(str, '`');
  1377. if (ast->child[0]->kind == ZEND_AST_ENCAPS_LIST) {
  1378. zend_ast_export_encaps_list(str, '`', (zend_ast_list*)ast->child[0], indent);
  1379. } else {
  1380. zval *zv;
  1381. ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_ZVAL);
  1382. zv = zend_ast_get_zval(ast->child[0]);
  1383. ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
  1384. zend_ast_export_qstr(str, '`', Z_STR_P(zv));
  1385. }
  1386. smart_str_appendc(str, '`');
  1387. break;
  1388. case ZEND_AST_CLONE:
  1389. PREFIX_OP("clone ", 270, 271);
  1390. case ZEND_AST_EXIT:
  1391. if (ast->child[0]) {
  1392. FUNC_OP("exit");
  1393. } else {
  1394. APPEND_STR("exit");
  1395. }
  1396. break;
  1397. case ZEND_AST_PRINT:
  1398. PREFIX_OP("print ", 60, 61);
  1399. case ZEND_AST_INCLUDE_OR_EVAL:
  1400. switch (ast->attr) {
  1401. case ZEND_INCLUDE_ONCE: FUNC_OP("include_once");
  1402. case ZEND_INCLUDE: FUNC_OP("include");
  1403. case ZEND_REQUIRE_ONCE: FUNC_OP("require_once");
  1404. case ZEND_REQUIRE: FUNC_OP("require");
  1405. case ZEND_EVAL: FUNC_OP("eval");
  1406. EMPTY_SWITCH_DEFAULT_CASE();
  1407. }
  1408. break;
  1409. case ZEND_AST_UNARY_OP:
  1410. switch (ast->attr) {
  1411. case ZEND_BW_NOT: PREFIX_OP("~", 240, 241);
  1412. case ZEND_BOOL_NOT: PREFIX_OP("!", 240, 241);
  1413. EMPTY_SWITCH_DEFAULT_CASE();
  1414. }
  1415. break;
  1416. case ZEND_AST_PRE_INC:
  1417. PREFIX_OP("++", 240, 241);
  1418. case ZEND_AST_PRE_DEC:
  1419. PREFIX_OP("--", 240, 241);
  1420. case ZEND_AST_POST_INC:
  1421. POSTFIX_OP("++", 240, 241);
  1422. case ZEND_AST_POST_DEC:
  1423. POSTFIX_OP("--", 240, 241);
  1424. case ZEND_AST_GLOBAL:
  1425. APPEND_NODE_1("global");
  1426. case ZEND_AST_UNSET:
  1427. FUNC_OP("unset");
  1428. case ZEND_AST_RETURN:
  1429. APPEND_NODE_1("return");
  1430. case ZEND_AST_LABEL:
  1431. zend_ast_export_name(str, ast->child[0], 0, indent);
  1432. smart_str_appendc(str, ':');
  1433. break;
  1434. case ZEND_AST_REF:
  1435. smart_str_appendc(str, '&');
  1436. ast = ast->child[0];
  1437. goto tail_call;
  1438. case ZEND_AST_HALT_COMPILER:
  1439. APPEND_STR("__HALT_COMPILER()");
  1440. case ZEND_AST_ECHO:
  1441. APPEND_NODE_1("echo");
  1442. case ZEND_AST_THROW:
  1443. APPEND_NODE_1("throw");
  1444. case ZEND_AST_GOTO:
  1445. smart_str_appends(str, "goto ");
  1446. zend_ast_export_name(str, ast->child[0], 0, indent);
  1447. break;
  1448. case ZEND_AST_BREAK:
  1449. APPEND_NODE_1("break");
  1450. case ZEND_AST_CONTINUE:
  1451. APPEND_NODE_1("continue");
  1452. /* 2 child nodes */
  1453. case ZEND_AST_DIM:
  1454. zend_ast_export_ex(str, ast->child[0], 260, indent);
  1455. smart_str_appendc(str, '[');
  1456. if (ast->child[1]) {
  1457. zend_ast_export_ex(str, ast->child[1], 0, indent);
  1458. }
  1459. smart_str_appendc(str, ']');
  1460. break;
  1461. case ZEND_AST_PROP:
  1462. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1463. smart_str_appends(str, "->");
  1464. zend_ast_export_var(str, ast->child[1], 0, indent);
  1465. break;
  1466. case ZEND_AST_STATIC_PROP:
  1467. zend_ast_export_ns_name(str, ast->child[0], 0, indent);
  1468. smart_str_appends(str, "::$");
  1469. zend_ast_export_var(str, ast->child[1], 0, indent);
  1470. break;
  1471. case ZEND_AST_CALL:
  1472. zend_ast_export_ns_name(str, ast->child[0], 0, indent);
  1473. smart_str_appendc(str, '(');
  1474. zend_ast_export_ex(str, ast->child[1], 0, indent);
  1475. smart_str_appendc(str, ')');
  1476. break;
  1477. case ZEND_AST_CLASS_CONST:
  1478. zend_ast_export_ns_name(str, ast->child[0], 0, indent);
  1479. smart_str_appends(str, "::");
  1480. zend_ast_export_name(str, ast->child[1], 0, indent);
  1481. break;
  1482. case ZEND_AST_ASSIGN: BINARY_OP(" = ", 90, 91, 90);
  1483. case ZEND_AST_ASSIGN_REF: BINARY_OP(" =& ", 90, 91, 90);
  1484. case ZEND_AST_ASSIGN_OP:
  1485. switch (ast->attr) {
  1486. case ZEND_ASSIGN_ADD: BINARY_OP(" += ", 90, 91, 90);
  1487. case ZEND_ASSIGN_SUB: BINARY_OP(" -= ", 90, 91, 90);
  1488. case ZEND_ASSIGN_MUL: BINARY_OP(" *= ", 90, 91, 90);
  1489. case ZEND_ASSIGN_DIV: BINARY_OP(" /= ", 90, 91, 90);
  1490. case ZEND_ASSIGN_MOD: BINARY_OP(" %= ", 90, 91, 90);
  1491. case ZEND_ASSIGN_SL: BINARY_OP(" <<= ", 90, 91, 90);
  1492. case ZEND_ASSIGN_SR: BINARY_OP(" >>= ", 90, 91, 90);
  1493. case ZEND_ASSIGN_CONCAT: BINARY_OP(" .= ", 90, 91, 90);
  1494. case ZEND_ASSIGN_BW_OR: BINARY_OP(" |= ", 90, 91, 90);
  1495. case ZEND_ASSIGN_BW_AND: BINARY_OP(" &= ", 90, 91, 90);
  1496. case ZEND_ASSIGN_BW_XOR: BINARY_OP(" ^= ", 90, 91, 90);
  1497. case ZEND_ASSIGN_POW: BINARY_OP(" **= ", 90, 91, 90);
  1498. EMPTY_SWITCH_DEFAULT_CASE();
  1499. }
  1500. break;
  1501. case ZEND_AST_BINARY_OP:
  1502. switch (ast->attr) {
  1503. case ZEND_ADD: BINARY_OP(" + ", 200, 200, 201);
  1504. case ZEND_SUB: BINARY_OP(" - ", 200, 200, 201);
  1505. case ZEND_MUL: BINARY_OP(" * ", 210, 210, 211);
  1506. case ZEND_DIV: BINARY_OP(" / ", 210, 210, 211);
  1507. case ZEND_MOD: BINARY_OP(" % ", 210, 210, 211);
  1508. case ZEND_SL: BINARY_OP(" << ", 190, 190, 191);
  1509. case ZEND_SR: BINARY_OP(" >> ", 190, 190, 191);
  1510. case ZEND_CONCAT: BINARY_OP(" . ", 200, 200, 201);
  1511. case ZEND_BW_OR: BINARY_OP(" | ", 140, 140, 141);
  1512. case ZEND_BW_AND: BINARY_OP(" & ", 160, 160, 161);
  1513. case ZEND_BW_XOR: BINARY_OP(" ^ ", 150, 150, 151);
  1514. case ZEND_IS_IDENTICAL: BINARY_OP(" === ", 170, 171, 171);
  1515. case ZEND_IS_NOT_IDENTICAL: BINARY_OP(" !== ", 170, 171, 171);
  1516. case ZEND_IS_EQUAL: BINARY_OP(" == ", 170, 171, 171);
  1517. case ZEND_IS_NOT_EQUAL: BINARY_OP(" != ", 170, 171, 171);
  1518. case ZEND_IS_SMALLER: BINARY_OP(" < ", 180, 181, 181);
  1519. case ZEND_IS_SMALLER_OR_EQUAL: BINARY_OP(" <= ", 180, 181, 181);
  1520. case ZEND_POW: BINARY_OP(" ** ", 250, 251, 250);
  1521. case ZEND_BOOL_XOR: BINARY_OP(" xor ", 40, 40, 41);
  1522. case ZEND_SPACESHIP: BINARY_OP(" <=> ", 180, 181, 181);
  1523. EMPTY_SWITCH_DEFAULT_CASE();
  1524. }
  1525. break;
  1526. case ZEND_AST_GREATER: BINARY_OP(" > ", 180, 181, 181);
  1527. case ZEND_AST_GREATER_EQUAL: BINARY_OP(" >= ", 180, 181, 181);
  1528. case ZEND_AST_AND: BINARY_OP(" && ", 130, 130, 131);
  1529. case ZEND_AST_OR: BINARY_OP(" || ", 120, 120, 121);
  1530. case ZEND_AST_ARRAY_ELEM:
  1531. if (ast->child[1]) {
  1532. zend_ast_export_ex(str, ast->child[1], 80, indent);
  1533. smart_str_appends(str, " => ");
  1534. }
  1535. if (ast->attr)
  1536. smart_str_appendc(str, '&');
  1537. zend_ast_export_ex(str, ast->child[0], 80, indent);
  1538. break;
  1539. case ZEND_AST_NEW:
  1540. smart_str_appends(str, "new ");
  1541. if (ast->child[0]->kind == ZEND_AST_CLASS) {
  1542. smart_str_appends(str, "class");
  1543. if (zend_ast_get_list(ast->child[1])->children) {
  1544. smart_str_appendc(str, '(');
  1545. zend_ast_export_ex(str, ast->child[1], 0, indent);
  1546. smart_str_appendc(str, ')');
  1547. }
  1548. zend_ast_export_class_no_header(str, (zend_ast_decl *) ast->child[0], indent);
  1549. } else {
  1550. zend_ast_export_ns_name(str, ast->child[0], 0, indent);
  1551. smart_str_appendc(str, '(');
  1552. zend_ast_export_ex(str, ast->child[1], 0, indent);
  1553. smart_str_appendc(str, ')');
  1554. }
  1555. break;
  1556. case ZEND_AST_INSTANCEOF:
  1557. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1558. smart_str_appends(str, " instanceof ");
  1559. zend_ast_export_ns_name(str, ast->child[1], 0, indent);
  1560. break;
  1561. case ZEND_AST_YIELD:
  1562. if (priority > 70) smart_str_appendc(str, '(');
  1563. smart_str_appends(str, "yield ");
  1564. if (ast->child[0]) {
  1565. if (ast->child[1]) {
  1566. zend_ast_export_ex(str, ast->child[1], 70, indent);
  1567. smart_str_appends(str, " => ");
  1568. }
  1569. zend_ast_export_ex(str, ast->child[0], 70, indent);
  1570. }
  1571. if (priority > 70) smart_str_appendc(str, ')');
  1572. break;
  1573. case ZEND_AST_YIELD_FROM:
  1574. PREFIX_OP("yield from ", 85, 86);
  1575. case ZEND_AST_COALESCE: BINARY_OP(" ?? ", 110, 111, 110);
  1576. case ZEND_AST_STATIC:
  1577. smart_str_appends(str, "static $");
  1578. zend_ast_export_name(str, ast->child[0], 0, indent);
  1579. APPEND_DEFAULT_VALUE(1);
  1580. case ZEND_AST_WHILE:
  1581. smart_str_appends(str, "while (");
  1582. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1583. smart_str_appends(str, ") {\n");
  1584. zend_ast_export_stmt(str, ast->child[1], indent + 1);
  1585. zend_ast_export_indent(str, indent);
  1586. smart_str_appendc(str, '}');
  1587. break;
  1588. case ZEND_AST_DO_WHILE:
  1589. smart_str_appends(str, "do {\n");
  1590. zend_ast_export_stmt(str, ast->child[0], indent + 1);
  1591. zend_ast_export_indent(str, indent);
  1592. smart_str_appends(str, "} while (");
  1593. zend_ast_export_ex(str, ast->child[1], 0, indent);
  1594. smart_str_appendc(str, ')');
  1595. break;
  1596. case ZEND_AST_IF_ELEM:
  1597. if (ast->child[0]) {
  1598. smart_str_appends(str, "if (");
  1599. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1600. smart_str_appends(str, ") {\n");
  1601. zend_ast_export_stmt(str, ast->child[1], indent + 1);
  1602. } else {
  1603. smart_str_appends(str, "else {\n");
  1604. zend_ast_export_stmt(str, ast->child[1], indent + 1);
  1605. }
  1606. zend_ast_export_indent(str, indent);
  1607. smart_str_appendc(str, '}');
  1608. break;
  1609. case ZEND_AST_SWITCH:
  1610. smart_str_appends(str, "switch (");
  1611. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1612. smart_str_appends(str, ") {\n");
  1613. zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
  1614. zend_ast_export_indent(str, indent);
  1615. smart_str_appendc(str, '}');
  1616. break;
  1617. case ZEND_AST_SWITCH_CASE:
  1618. zend_ast_export_indent(str, indent);
  1619. if (ast->child[0]) {
  1620. smart_str_appends(str, "case ");
  1621. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1622. smart_str_appends(str, ":\n");
  1623. } else {
  1624. smart_str_appends(str, "default:\n");
  1625. }
  1626. zend_ast_export_stmt(str, ast->child[1], indent + 1);
  1627. break;
  1628. case ZEND_AST_DECLARE:
  1629. smart_str_appends(str, "declare(");
  1630. ZEND_ASSERT(ast->child[0]->kind == ZEND_AST_CONST_DECL);
  1631. zend_ast_export_list(str, (zend_ast_list*)ast->child[0], 1, 0, indent);
  1632. smart_str_appendc(str, ')');
  1633. if (ast->child[1]) {
  1634. smart_str_appends(str, " {\n");
  1635. zend_ast_export_stmt(str, ast->child[1], indent + 1);
  1636. zend_ast_export_indent(str, indent);
  1637. smart_str_appendc(str, '}');
  1638. } else {
  1639. smart_str_appendc(str, ';');
  1640. }
  1641. break;
  1642. case ZEND_AST_PROP_ELEM:
  1643. smart_str_appendc(str, '$');
  1644. /* break missing intentionally */
  1645. case ZEND_AST_CONST_ELEM:
  1646. zend_ast_export_name(str, ast->child[0], 0, indent);
  1647. APPEND_DEFAULT_VALUE(1);
  1648. case ZEND_AST_USE_TRAIT:
  1649. smart_str_appends(str, "use ");
  1650. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1651. if (ast->child[1]) {
  1652. smart_str_appends(str, " {\n");
  1653. zend_ast_export_ex(str, ast->child[1], 0, indent + 1);
  1654. zend_ast_export_indent(str, indent);
  1655. smart_str_appends(str, "}");
  1656. } else {
  1657. smart_str_appends(str, ";");
  1658. }
  1659. break;
  1660. case ZEND_AST_TRAIT_PRECEDENCE:
  1661. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1662. smart_str_appends(str, " insteadof ");
  1663. zend_ast_export_ex(str, ast->child[1], 0, indent);
  1664. break;
  1665. case ZEND_AST_METHOD_REFERENCE:
  1666. if (ast->child[0]) {
  1667. zend_ast_export_name(str, ast->child[0], 0, indent);
  1668. smart_str_appends(str, "::");
  1669. }
  1670. zend_ast_export_name(str, ast->child[1], 0, indent);
  1671. break;
  1672. case ZEND_AST_NAMESPACE:
  1673. smart_str_appends(str, "namespace");
  1674. if (ast->child[0]) {
  1675. smart_str_appendc(str, ' ');
  1676. zend_ast_export_name(str, ast->child[0], 0, indent);
  1677. }
  1678. if (ast->child[1]) {
  1679. smart_str_appends(str, " {\n");
  1680. zend_ast_export_stmt(str, ast->child[1], indent + 1);
  1681. zend_ast_export_indent(str, indent);
  1682. smart_str_appends(str, "}\n");
  1683. } else {
  1684. smart_str_appendc(str, ';');
  1685. }
  1686. break;
  1687. case ZEND_AST_USE_ELEM:
  1688. case ZEND_AST_TRAIT_ALIAS:
  1689. zend_ast_export_name(str, ast->child[0], 0, indent);
  1690. if (ast->attr & ZEND_ACC_PUBLIC) {
  1691. smart_str_appends(str, " as public");
  1692. } else if (ast->attr & ZEND_ACC_PROTECTED) {
  1693. smart_str_appends(str, " as protected");
  1694. } else if (ast->attr & ZEND_ACC_PRIVATE) {
  1695. smart_str_appends(str, " as private");
  1696. } else if (ast->child[1]) {
  1697. smart_str_appends(str, " as");
  1698. }
  1699. if (ast->child[1]) {
  1700. smart_str_appendc(str, ' ');
  1701. zend_ast_export_name(str, ast->child[1], 0, indent);
  1702. }
  1703. break;
  1704. /* 3 child nodes */
  1705. case ZEND_AST_METHOD_CALL:
  1706. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1707. smart_str_appends(str, "->");
  1708. zend_ast_export_var(str, ast->child[1], 0, indent);
  1709. smart_str_appendc(str, '(');
  1710. zend_ast_export_ex(str, ast->child[2], 0, indent);
  1711. smart_str_appendc(str, ')');
  1712. break;
  1713. case ZEND_AST_STATIC_CALL:
  1714. zend_ast_export_ns_name(str, ast->child[0], 0, indent);
  1715. smart_str_appends(str, "::");
  1716. zend_ast_export_var(str, ast->child[1], 0, indent);
  1717. smart_str_appendc(str, '(');
  1718. zend_ast_export_ex(str, ast->child[2], 0, indent);
  1719. smart_str_appendc(str, ')');
  1720. break;
  1721. case ZEND_AST_CONDITIONAL:
  1722. if (priority > 100) smart_str_appendc(str, '(');
  1723. zend_ast_export_ex(str, ast->child[0], 100, indent);
  1724. if (ast->child[1]) {
  1725. smart_str_appends(str, " ? ");
  1726. zend_ast_export_ex(str, ast->child[1], 101, indent);
  1727. smart_str_appends(str, " : ");
  1728. } else {
  1729. smart_str_appends(str, " ?: ");
  1730. }
  1731. zend_ast_export_ex(str, ast->child[2], 101, indent);
  1732. if (priority > 100) smart_str_appendc(str, ')');
  1733. break;
  1734. case ZEND_AST_TRY:
  1735. smart_str_appends(str, "try {\n");
  1736. zend_ast_export_stmt(str, ast->child[0], indent + 1);
  1737. zend_ast_export_indent(str, indent);
  1738. zend_ast_export_ex(str, ast->child[1], 0, indent);
  1739. if (ast->child[2]) {
  1740. smart_str_appends(str, "} finally {\n");
  1741. zend_ast_export_stmt(str, ast->child[2], indent + 1);
  1742. zend_ast_export_indent(str, indent);
  1743. }
  1744. smart_str_appendc(str, '}');
  1745. break;
  1746. case ZEND_AST_CATCH:
  1747. smart_str_appends(str, "} catch (");
  1748. zend_ast_export_catch_name_list(str, zend_ast_get_list(ast->child[0]), indent);
  1749. smart_str_appends(str, " $");
  1750. zend_ast_export_var(str, ast->child[1], 0, indent);
  1751. smart_str_appends(str, ") {\n");
  1752. zend_ast_export_stmt(str, ast->child[2], indent + 1);
  1753. zend_ast_export_indent(str, indent);
  1754. break;
  1755. case ZEND_AST_PARAM:
  1756. if (ast->child[0]) {
  1757. if (ast->child[0]->attr & ZEND_TYPE_NULLABLE) {
  1758. smart_str_appendc(str, '?');
  1759. }
  1760. zend_ast_export_ns_name(str, ast->child[0], 0, indent);
  1761. smart_str_appendc(str, ' ');
  1762. }
  1763. if (ast->attr & ZEND_PARAM_REF) {
  1764. smart_str_appendc(str, '&');
  1765. }
  1766. if (ast->attr & ZEND_PARAM_VARIADIC) {
  1767. smart_str_appends(str, "...");
  1768. }
  1769. smart_str_appendc(str, '$');
  1770. zend_ast_export_name(str, ast->child[1], 0, indent);
  1771. APPEND_DEFAULT_VALUE(2);
  1772. /* 4 child nodes */
  1773. case ZEND_AST_FOR:
  1774. smart_str_appends(str, "for (");
  1775. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1776. smart_str_appendc(str, ';');
  1777. if (ast->child[1]) {
  1778. smart_str_appendc(str, ' ');
  1779. zend_ast_export_ex(str, ast->child[1], 0, indent);
  1780. }
  1781. smart_str_appendc(str, ';');
  1782. if (ast->child[2]) {
  1783. smart_str_appendc(str, ' ');
  1784. zend_ast_export_ex(str, ast->child[2], 0, indent);
  1785. }
  1786. smart_str_appends(str, ") {\n");
  1787. zend_ast_export_stmt(str, ast->child[3], indent + 1);
  1788. zend_ast_export_indent(str, indent);
  1789. smart_str_appendc(str, '}');
  1790. break;
  1791. case ZEND_AST_FOREACH:
  1792. smart_str_appends(str, "foreach (");
  1793. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1794. smart_str_appends(str, " as ");
  1795. if (ast->child[2]) {
  1796. zend_ast_export_ex(str, ast->child[2], 0, indent);
  1797. smart_str_appends(str, " => ");
  1798. }
  1799. zend_ast_export_ex(str, ast->child[1], 0, indent);
  1800. smart_str_appends(str, ") {\n");
  1801. zend_ast_export_stmt(str, ast->child[3], indent + 1);
  1802. zend_ast_export_indent(str, indent);
  1803. smart_str_appendc(str, '}');
  1804. break;
  1805. EMPTY_SWITCH_DEFAULT_CASE();
  1806. }
  1807. return;
  1808. binary_op:
  1809. if (priority > p) smart_str_appendc(str, '(');
  1810. zend_ast_export_ex(str, ast->child[0], pl, indent);
  1811. smart_str_appends(str, op);
  1812. zend_ast_export_ex(str, ast->child[1], pr, indent);
  1813. if (priority > p) smart_str_appendc(str, ')');
  1814. return;
  1815. prefix_op:
  1816. if (priority > p) smart_str_appendc(str, '(');
  1817. smart_str_appends(str, op);
  1818. zend_ast_export_ex(str, ast->child[0], pl, indent);
  1819. if (priority > p) smart_str_appendc(str, ')');
  1820. return;
  1821. postfix_op:
  1822. if (priority > p) smart_str_appendc(str, '(');
  1823. zend_ast_export_ex(str, ast->child[0], pl, indent);
  1824. smart_str_appends(str, op);
  1825. if (priority > p) smart_str_appendc(str, ')');
  1826. return;
  1827. func_op:
  1828. smart_str_appends(str, op);
  1829. smart_str_appendc(str, '(');
  1830. zend_ast_export_ex(str, ast->child[0], 0, indent);
  1831. smart_str_appendc(str, ')');
  1832. return;
  1833. append_node_1:
  1834. smart_str_appends(str, op);
  1835. if (ast->child[0]) {
  1836. smart_str_appendc(str, ' ');
  1837. ast = ast->child[0];
  1838. goto tail_call;
  1839. }
  1840. return;
  1841. append_str:
  1842. smart_str_appends(str, op);
  1843. return;
  1844. append_default_value:
  1845. if (ast->child[p]) {
  1846. smart_str_appends(str, " = ");
  1847. ast = ast->child[p];
  1848. goto tail_call;
  1849. }
  1850. return;
  1851. }
  1852. ZEND_API ZEND_COLD zend_string *zend_ast_export(const char *prefix, zend_ast *ast, const char *suffix)
  1853. {
  1854. smart_str str = {0};
  1855. smart_str_appends(&str, prefix);
  1856. zend_ast_export_ex(&str, ast, 0, 0);
  1857. smart_str_appends(&str, suffix);
  1858. smart_str_0(&str);
  1859. return str.s;
  1860. }
  1861. /*
  1862. * Local variables:
  1863. * tab-width: 4
  1864. * c-basic-offset: 4
  1865. * indent-tabs-mode: t
  1866. * End:
  1867. * vim600: sw=4 ts=4 fdm=marker
  1868. * vim<600: sw=4 ts=4
  1869. */