From 9381ab39e7540081ae2caaba18a77e35c1fcf1c6 Mon Sep 17 00:00:00 2001 From: Sara Golemon Date: Tue, 9 Apr 2013 13:08:21 -0700 Subject: [PATCH] Improve idl.php generator Output constants and class implementations in .cpp file Include base.php via __DIR__ rather than cwd for calling from outside hphp/idl --- hphp/idl/base.php | 50 ++++++++++++++++++++++++++++++++++++++++++++--- hphp/idl/idl.php | 19 ++++++++++++------ 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/hphp/idl/base.php b/hphp/idl/base.php index c630832a4..87f57926c 100644 --- a/hphp/idl/base.php +++ b/hphp/idl/base.php @@ -629,6 +629,25 @@ function generateConstCPPHeader($const, $f) { fprintf($f, "extern const %s k_%s;\n", $name, $const['name']); } +function generateConstCPPImplementation($const, $f, $prefix = 'k_') { + $name = typename($const['type']); + if ($name == 'String') { + $name = 'StaticString'; + } + $def = ''; + if (isset($const['value'])) { + if ($name == 'StaticString') { + $def = '"' . addslashes($const['value']) . '"'; + } else if ($name == 'bool') { + $def = $const['value'] ? 'true' : 'false'; + } else { + $def = $const['value']; + } + $def = " = $def"; + } + fprintf($f, "const %s %s%s%s;\n", $name, $prefix, $const['name'], $def); +} + function generateClassCPPHeader($class, $f) { global $MAGIC_METHODS; $clsname = $class['name']; @@ -725,6 +744,16 @@ EOT fprintf($f, "\n};\n"); } +function generateClassCPPImplementation($class, $f) { + foreach ($class['consts'] as $k) { + generateConstCPPImplementation($k, $f, "q_{$class['name']}$$"); + } + + foreach ($class['methods'] as $m) { + generateMethodCPPImplementation($m, $class, $f); + } +} + function generateMethodCPPHeader($method, $class, $f) { global $MAGIC_METHODS; fprintf($f, " public: "); @@ -733,6 +762,17 @@ function generateMethodCPPHeader($method, $class, $f) { $method['flags'] & IsStatic, $class); } +function generateMethodCPPImplementation($method, $class, $f) { + if ($method['flags'] & IsStatic) { + $prefix = "c_{$class['name']}::ti_"; + $sprop = 'const char *cls'; + } else { + $prefix = "c_{$class['name']}::t_"; + $sprop = ''; + } + generateFuncCPPImplementation($method, $f, $prefix, $sprop); +} + function generatePropertyCPPHeader($property, $f) { fprintf($f, " public: "); fprintf($f, "%s m_%s;\n", typename($property['type']), @@ -754,7 +794,7 @@ function generatePreImplemented($method, $class, $f) { } } -function generateFuncCPPImplementation($func, $f) { +function generateFuncCPPImplementation($func, $f, $prefix = 'f_', $sprop = '') { $schema = ""; $schema_no = 0; if ($func['return'] == Object || $func['return'] == Resource) { @@ -763,15 +803,19 @@ function generateFuncCPPImplementation($func, $f) { $output = ''; $need_ret = false; - fprintf($f, '%s f_%s(', typename($func['return']), $func['name']); + fprintf($f, '%s %s%s(', typename($func['return']), $prefix, $func['name']); $var_arg = ($func['flags'] & VarArgsMask); if ($var_arg) fprintf($f, 'int _argc'); if ($var_arg && count($func['args']) > 0) fprintf($f, ', '); $params = ""; $params_no = 0; + if ($sprop) { + // For static class methods to inject class name + fprintf($f, $sprop); + } for ($i = 0; $i < count($func['args']); $i++) { $arg = $func['args'][$i]; - if ($i > 0) fprintf($f, ', '); + if ($sprop || ($i > 0)) fprintf($f, ', '); fprintf($f, '%s %s', param_typename($arg), $arg['name']); if (isset($arg['default'])) { diff --git a/hphp/idl/idl.php b/hphp/idl/idl.php index 871d220f4..c15d7ee2d 100644 --- a/hphp/idl/idl.php +++ b/hphp/idl/idl.php @@ -1,6 +1,6 @@ "; + $header_file = ""; } fprintf($f, <<