PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/system/idl/idl.php

https://github.com/tstarling/hiphop-php
PHP | 328 lines | 197 code | 75 blank | 56 comment | 21 complexity | c896828bb90b1e08ed444198708a97b5 MD5 | raw file
  1. <?php
  2. include_once __DIR__ . '/base.php';
  3. /**
  4. * Possible values for 'format':
  5. *
  6. * cpp: generating .h and .cpp for implementing the functions.
  7. * inc: generating .inc for system/builtin_symbols.cpp
  8. * test: generating .h and .cpp for unit tests
  9. * param: synchronize parameter types and names
  10. * extmap: sep extension's prototype map
  11. */
  12. $format = $argv[1];
  13. $input = $argv[2];
  14. $mode = '';
  15. // format-mode: for "sep" detachable extensions
  16. if (strpos($format, '-') > 0) {
  17. list($format, $mode) = explode('-', $format);
  18. }
  19. if (preg_match('/\.idl\.json/', $input)) {
  20. $full_name = preg_replace('/\.idl\.json/', '', $input);
  21. $name = preg_replace('%^.*/%', '', $full_name);
  22. if ($full_name != $name) $mode = 'remote';
  23. } else {
  24. throw new Exception("wrong IDL or schema file $input");
  25. }
  26. ReadIDLFile($input);
  27. $name = preg_replace('|[/\.]|', '_', $name);
  28. $NAME = strtoupper($name);
  29. $Name = ucfirst($name);
  30. $PREFIX = ($mode == 'sep') ? 'SEPEXT' : 'EXT';
  31. $format_function = 'idl_format_' . $format;
  32. if (function_exists($format_function)) {
  33. call_user_func_array($format_function, array_slice($argv, 3));
  34. }
  35. /*****************************************************************************/
  36. function idl_format_cpp($header, $impl) {
  37. idl_format_cpp_header($header);
  38. idl_format_cpp_impl($impl);
  39. }
  40. /*****************************************************************************/
  41. function idl_format_cpp_header($header) {
  42. global $preamble, $funcs, $constants, $classes, $PREFIX, $NAME;
  43. ($f = fopen($header, 'w')) || die("cannot open $header");
  44. fprintf($f,
  45. <<<EOT
  46. #ifndef incl_${PREFIX}_${NAME}_H_
  47. #define incl_${PREFIX}_${NAME}_H_
  48. #include "hphp/runtime/base/base-includes.h"
  49. EOT
  50. );
  51. if (isset($preamble)) {
  52. fprintf($f, $preamble);
  53. }
  54. fprintf($f,
  55. <<<EOT
  56. namespace HPHP {
  57. ///////////////////////////////////////////////////////////////////////////////
  58. EOT
  59. );
  60. foreach ($funcs as $func) {
  61. generateFuncCPPHeader($func, $f);
  62. }
  63. foreach ($constants as $const) {
  64. generateConstCPPHeader($const, $f);
  65. }
  66. foreach ($classes as $class) {
  67. generateClassCPPHeader($class, $f);
  68. }
  69. fprintf($f,
  70. <<<EOT
  71. ///////////////////////////////////////////////////////////////////////////////
  72. }
  73. #endif // incl_${PREFIX}_${NAME}_H_
  74. EOT
  75. );
  76. fclose($f);
  77. }
  78. /*****************************************************************************/
  79. function idl_format_cpp_impl($impl) {
  80. global $funcs, $name, $mode, $constants, $classes;
  81. ($f = fopen($impl, 'w')) || die("cannot open $impl");
  82. if ($mode) {
  83. $header_file = "\"ext_${name}.h\"";
  84. } else {
  85. $header_file = "<runtime/ext/ext_${name}.h>";
  86. }
  87. fprintf($f,
  88. <<<EOT
  89. #include $header_file
  90. namespace HPHP {
  91. ///////////////////////////////////////////////////////////////////////////////
  92. EOT
  93. );
  94. foreach ($constants as $const) {
  95. generateConstCPPImplementation($const, $f);
  96. }
  97. foreach ($funcs as $func) {
  98. generateFuncCPPImplementation($func, $f);
  99. }
  100. foreach ($classes as $class) {
  101. generateClassCPPImplementation($class, $f);
  102. }
  103. fprintf($f,
  104. <<<EOT
  105. ///////////////////////////////////////////////////////////////////////////////
  106. }
  107. EOT
  108. );
  109. }
  110. /*****************************************************************************/
  111. function idl_format_test($header, $impl) {
  112. idl_format_test_header($header);
  113. idl_format_test_impl($header,$impl);
  114. }
  115. /*****************************************************************************/
  116. function idl_format_test_header($test_header) {
  117. global $funcs, $classes, $PREFIX, $NAME, $Name;
  118. $ext_header = substr($test_header, 5);
  119. ($f = fopen($test_header, 'w')) || die("cannot open $test_header");
  120. fprintf($f,
  121. <<<EOT
  122. #ifndef incl_TEST_${PREFIX}_${NAME}_H_
  123. #define incl_TEST_${PREFIX}_${NAME}_H_
  124. #include "hphp/test/test_cpp_ext.h"
  125. ///////////////////////////////////////////////////////////////////////////////
  126. class TestExt${Name} : public TestCppExt {
  127. public:
  128. virtual bool RunTests(const std::string &which);
  129. EOT
  130. );
  131. foreach ($funcs as $func) {
  132. fprintf($f, " bool test_".$func['name']."();\n");
  133. }
  134. foreach ($classes as $class) {
  135. fprintf($f, " bool test_".$class['name']."();\n");
  136. }
  137. fprintf($f,
  138. <<<EOT
  139. };
  140. ///////////////////////////////////////////////////////////////////////////////
  141. #endif // incl_TEST_${PREFIX}_${NAME}_H_
  142. EOT
  143. );
  144. fclose($f);
  145. }
  146. /*****************************************************************************/
  147. function idl_format_test_impl($test_header, $test_impl) {
  148. global $funcs, $classes, $Name, $name, $mode;
  149. ($f = fopen($test_impl, 'w')) || die("cannot open $test_impl");
  150. if ($mode == 'sep' || $mode == 'remote') {
  151. $inc_file1 = "\"$test_header\"";
  152. $inc_file2 = "\"ext_${name}.h\"";
  153. } else {
  154. $inc_file1 = "<test/$test_header>";
  155. $inc_file2 = "<runtime/ext/ext_${name}.h>";
  156. }
  157. fprintf($f,
  158. <<<EOT
  159. #include $inc_file1
  160. #include $inc_file2
  161. IMPLEMENT_SEP_EXTENSION_TEST($Name);
  162. ///////////////////////////////////////////////////////////////////////////////
  163. bool TestExt${Name}::RunTests(const std::string &which) {
  164. bool ret = true;
  165. EOT
  166. );
  167. foreach ($funcs as $func) {
  168. fprintf($f, " RUN_TEST(test_".$func['name'].");\n");
  169. }
  170. foreach ($classes as $class) {
  171. fprintf($f, " RUN_TEST(test_".$class['name'].");\n");
  172. }
  173. fprintf($f, <<<EOT
  174. return ret;
  175. }
  176. ///////////////////////////////////////////////////////////////////////////////
  177. EOT
  178. );
  179. foreach ($funcs + $classes as $item) {
  180. $item_name = $item['name'];
  181. fprintf($f, <<<EOT
  182. bool TestExt${Name}::test_$item_name() {
  183. return Count(true);
  184. }
  185. EOT
  186. );
  187. }
  188. fclose($f);
  189. }
  190. /*****************************************************************************/
  191. function idl_format_param($header, $impl) {
  192. replaceParams($header, true);
  193. replaceParams($impl, false);
  194. }
  195. /*****************************************************************************/
  196. function idl_format_extmap($header, $impl) {
  197. idl_format_extmap_header($header);
  198. idl_format_extmap_impl($impl);
  199. }
  200. /*****************************************************************************/
  201. function idl_format_extmap_header($map_header) {
  202. global $name;
  203. ($f = fopen($map_header, 'w')) || die("cannot open $map_header");
  204. fprintf($f,
  205. <<<EOT
  206. #include <util/deprecated/base.h>
  207. ///////////////////////////////////////////////////////////////////////////////
  208. extern "C" {
  209. extern const char **${name}_map[];
  210. }
  211. EOT
  212. );
  213. fclose($f);
  214. }
  215. /*****************************************************************************/
  216. function idl_format_extmap_impl($map_impl) {
  217. global $name;
  218. ($f = fopen($map_impl, 'w')) || die("cannot open $map_impl");
  219. fprintf($f,
  220. <<<EOT
  221. #include "extmap_${name}.h"
  222. #include <compiler/analysis/type.h>
  223. ///////////////////////////////////////////////////////////////////////////////
  224. static const char *${name}_extension_functions[] = {
  225. #define S(n) (const char *)n
  226. #define T(t) (const char *)HPHP::Type::KindOf ## t
  227. #define EXT_TYPE 0
  228. #include "${name}.inc"
  229. NULL,
  230. };
  231. #undef EXT_TYPE
  232. static const char *${name}_extension_constants[] = {
  233. #define EXT_TYPE 1
  234. #include "${name}.inc"
  235. NULL,
  236. };
  237. #undef EXT_TYPE
  238. static const char *${name}_extension_classes[] = {
  239. #define EXT_TYPE 2
  240. #include "${name}.inc"
  241. NULL,
  242. };
  243. #undef EXT_TYPE
  244. ///////////////////////////////////////////////////////////////////////////////
  245. const char **${name}_map[] = {
  246. ${name}_extension_functions,
  247. ${name}_extension_constants,
  248. ${name}_extension_classes,
  249. };
  250. EOT
  251. );
  252. fclose($f);
  253. }