PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/hphp/system/idl/schema.php

http://github.com/facebook/hiphop-php
PHP | 348 lines | 214 code | 52 blank | 82 comment | 44 complexity | 0c2ed1e7d0c88f5302e7f5756da1fff8 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?php
  2. require_once 'base.php';
  3. $ext = $argv[1];
  4. if (file_exists($ext) || preg_match('/\.idl\.php/', $ext)) {
  5. require_once $ext;
  6. $ext = preg_replace('/\.idl\.php/', '', $ext);
  7. } else {
  8. require_once "$ext.idl.php";
  9. }
  10. $net = isset($argv[2]) ? $argv[2] : -1; // 1: always phpnet; 0; auto; -1; no
  11. ///////////////////////////////////////////////////////////////////////////////
  12. $output = <<<CODE
  13. <?php
  14. /**
  15. * Automatically generated by running "php schema.php $ext".
  16. *
  17. * You may modify the file, but re-running schema.php against this file will
  18. * standardize the format without losing your schema changes. It does lose
  19. * any changes that are not part of schema. Use "note" field to comment on
  20. * schema itself, and "note" fields are not used in any code generation but
  21. * only staying within this file.
  22. *
  23. * @nolint
  24. */
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // Preamble: C++ code inserted at beginning of ext_{name}.h
  27. DefinePreamble(<<<CPP
  28. $preamble
  29. CPP
  30. );
  31. ///////////////////////////////////////////////////////////////////////////////
  32. // Constants
  33. //
  34. // array (
  35. // 'name' => name of the constant
  36. // 'type' => type of the constant
  37. // 'note' => additional note about this constant's schema
  38. // )
  39. CODE;
  40. define_constants($constants);
  41. $output .= <<<CODE
  42. ///////////////////////////////////////////////////////////////////////////////
  43. // Functions
  44. //
  45. // array (
  46. // 'name' => name of the function
  47. // 'desc' => description of the function's purpose
  48. // 'flags' => attributes of the function, see base.php for possible values
  49. // 'opt' => optimization callback function's name for compiler
  50. // 'note' => additional note about this function's schema
  51. // 'return' =>
  52. // array (
  53. // 'type' => return type, use Reference for ref return
  54. // 'desc' => description of the return value
  55. // )
  56. // 'args' => arguments
  57. // array (
  58. // 'name' => name of the argument
  59. // 'type' => type of the argument, use Reference for output parameter
  60. // 'value' => default value of the argument
  61. // 'desc' => description of the argument
  62. // )
  63. // )
  64. CODE;
  65. foreach ($funcs as $func) {
  66. define_function($func);
  67. }
  68. $output .= <<<CODE
  69. ///////////////////////////////////////////////////////////////////////////////
  70. // Classes
  71. //
  72. // BeginClass
  73. // array (
  74. // 'name' => name of the class
  75. // 'desc' => description of the class's purpose
  76. // 'flags' => attributes of the class, see base.php for possible values
  77. // 'note' => additional note about this class's schema
  78. // 'parent' => parent class name, if any
  79. // 'ifaces' => array of interfaces tihs class implements
  80. // 'bases' => extra internal and special base classes this class requires
  81. // 'footer' => extra C++ inserted at end of class declaration
  82. // )
  83. //
  84. // DefineConstant(..)
  85. // DefineConstant(..)
  86. // ...
  87. // DefineFunction(..)
  88. // DefineFunction(..)
  89. // ...
  90. // DefineProperty
  91. // DefineProperty
  92. //
  93. // array (
  94. // 'name' => name of the property
  95. // 'type' => type of the property
  96. // 'flags' => attributes of the property
  97. // 'desc' => description of the property
  98. // 'note' => additional note about this property's schema
  99. // )
  100. //
  101. // EndClass()
  102. CODE;
  103. foreach ($classes as $class) {
  104. define_class($class);
  105. }
  106. ///////////////////////////////////////////////////////////////////////////////
  107. print $output;
  108. ///////////////////////////////////////////////////////////////////////////////
  109. // output helpers
  110. function idx_flags($arr, $name, $global_function) {
  111. return get_flag_names($arr, $name, $global_function);
  112. }
  113. function idx_type($arr, $name) {
  114. return !empty($arr[$name]) ? get_idl_name($arr[$name]) : '';
  115. }
  116. function idx_string($arr, $name) {
  117. // not empty() testing to allow a string of '0'
  118. return array_key_exists($name, $arr) ? $arr[$name] : '';
  119. }
  120. function idx_array($arr, $name) {
  121. if (!empty($arr[$name])) {
  122. return "array('" . implode("', '", $arr[$name]) . "')";
  123. }
  124. return '';
  125. }
  126. function begin_function($name) {
  127. global $indent, $output;
  128. $output .= str_repeat(' ', $indent) . "$name(\n";
  129. $indent += 2;
  130. }
  131. function end_function() {
  132. global $indent, $output;
  133. $indent -= 2;
  134. $output .= str_repeat(' ', $indent) . ");\n\n";
  135. }
  136. function begin_array($leading = true) {
  137. global $indent, $output;
  138. if ($leading) {
  139. $output .= str_repeat(' ', $indent);
  140. }
  141. $output .= "array(\n";
  142. $indent += 2;
  143. }
  144. function end_array($trailing_comma=true) {
  145. global $indent, $output;
  146. $indent -= 2;
  147. $output .= str_repeat(' ', $indent) . ")";
  148. if ($trailing_comma) $output .= ",\n";
  149. }
  150. function push_globals($inc=4) {
  151. global $indent, $output, $saved;
  152. $saved = $output; $output = '';
  153. $indent += $inc;
  154. }
  155. function pop_globals($dec=4) {
  156. global $indent, $output, $saved;
  157. $ret = $output; $output = $saved;
  158. $indent -= $dec;
  159. return $ret;
  160. }
  161. function out_str($name, $var, $required=false, $formatted=false, $doc=false) {
  162. global $indent, $output;
  163. if ($required && ($var === null || $var === '')) {
  164. throw new Exception("missing definition for $name");
  165. }
  166. if ($required || !($var === null || $var === '')) {
  167. $name = str_pad("'$name'", 8);
  168. if (!$formatted) {
  169. $var = '"'.escape_php($var).'"';
  170. }
  171. $output .= str_repeat(' ', $indent) . "$name => ";
  172. if ($doc) {
  173. $output .= "<<<EOT\n$var\nEOT\n,\n";
  174. } else {
  175. $output .= "$var,\n";
  176. }
  177. }
  178. }
  179. function out_fmt($name, $var, $required=false) {
  180. return out_str($name, $var, $required, true);
  181. }
  182. function out_doc($name, $var, $required=false) {
  183. return out_str($name, $var, $required, true, true);
  184. }
  185. function define_class($class) {
  186. global $output, $net;
  187. $name = $class['name'];
  188. $desc = idx_string($class, 'desc');
  189. if ((empty($desc) || $net == 1) && $net != -1) {
  190. $desc = phpnet_get_class_desc($name);
  191. }
  192. $output .= "////////////////////////////////////////".
  193. "///////////////////////////////////////\n\n";
  194. begin_function('BeginClass');
  195. begin_array();
  196. out_str('name', $name, true);
  197. out_str('parent', $class['parent']);
  198. out_fmt('ifaces', idx_array ($class, 'ifaces'));
  199. out_fmt('bases', idx_array ($class, 'bases'));
  200. out_str('desc', $desc);
  201. out_fmt('flags', idx_flags ($class, 'flags', false));
  202. out_str('note', idx_string($class, 'note'));
  203. out_doc('footer', idx_string($class, 'footer'));
  204. end_array(false);
  205. end_function();
  206. define_constants($class['consts']);
  207. foreach ($class['methods'] as $func) {
  208. define_function($func, $name);
  209. }
  210. define_properties($class['properties']);
  211. begin_function('EndClass');
  212. end_function();
  213. }
  214. function define_function($func, $clsname = 'function') {
  215. global $net;
  216. $phpnet = false;
  217. $desc = idx_string($func, 'desc');
  218. if ((empty($desc) || $net == 1) && $net != -1) {
  219. $phpnet = phpnet_get_function_info($func['name'], $clsname);
  220. if (!empty($phpnet['desc'])) $desc = ($phpnet['desc']);
  221. }
  222. // prepare return type
  223. $ret_desc = idx_string($func, 'ret_desc');
  224. if ((empty($ret_desc) || $net == 1) && $net != -1) {
  225. if (!empty($phpnet['ret'])) $ret_desc = $phpnet['ret'];
  226. }
  227. push_globals();
  228. begin_array(false);
  229. out_fmt('type', get_idl_name($func['return'], 'null'));
  230. out_str('desc', $ret_desc);
  231. end_array(false);
  232. $return = pop_globals();
  233. if ($func['args']) {
  234. push_globals();
  235. begin_array(false);
  236. $i = -1;
  237. foreach ($func['args'] as $arg) {
  238. $i++;
  239. $arg_desc = idx_string($arg, 'desc');
  240. if ((empty($arg_desc) || $net == 1) && $net != -1) {
  241. if (!empty($phpnet['params'][$i])) {
  242. $arg_desc = $phpnet['params'][$i];
  243. }
  244. }
  245. begin_array();
  246. out_str('name', $arg['name'], true);
  247. out_fmt('type', idx_type($arg, 'type'));
  248. out_str('value', idx_string($arg, 'default'));
  249. out_str('desc', $arg_desc);
  250. end_array();
  251. }
  252. end_array(false);
  253. $args = pop_globals();
  254. } else {
  255. $args = '';
  256. }
  257. begin_function('DefineFunction');
  258. begin_array();
  259. out_str('name', $func['name'], true);
  260. out_str('desc', $desc);
  261. out_fmt('flags', idx_flags($func, 'flags', $clsname == 'function'));
  262. out_str('opt', idx_string($func, 'opt'));
  263. out_fmt('return', $return);
  264. out_fmt('args', $args);
  265. out_str('note', idx_string($func, 'note'));
  266. end_array(false);
  267. end_function();
  268. }
  269. function define_constants($consts) {
  270. foreach ($consts as $constant) {
  271. begin_function('DefineConstant');
  272. begin_array();
  273. out_str('name', $constant['name'], true);
  274. if (array_key_exists('value', $constant)) {
  275. out_fmt('value', php_escape_val($constant['value'], true), false);
  276. } else {
  277. out_fmt('type', idx_type($constant, 'type'), true);
  278. }
  279. out_str('note', idx_string($constant, 'note'));
  280. end_array(false);
  281. end_function();
  282. }
  283. }
  284. function define_properties($properties) {
  285. foreach ($properties as $property) {
  286. begin_function('DefineProperty');
  287. begin_array();
  288. out_str('name', $property['name'], true);
  289. out_fmt('type', idx_type ($property, 'type'));
  290. out_fmt('flags', idx_flags ($property, 'flags', false));
  291. out_str('desc', idx_string($property, 'desc'));
  292. out_str('note', idx_string($property, 'note'));
  293. end_array(false);
  294. end_function();
  295. }
  296. }