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
Esse commit está contido em:
+47
-3
@@ -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'])) {
|
||||
|
||||
+13
-6
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
include_once 'base.php';
|
||||
include_once __DIR__ . '/base.php';
|
||||
|
||||
/**
|
||||
* Possible values for 'format':
|
||||
@@ -102,19 +102,19 @@ EOT
|
||||
|
||||
/*****************************************************************************/
|
||||
function idl_format_cpp_impl($impl) {
|
||||
global $funcs, $name, $mode;
|
||||
global $funcs, $name, $mode, $constants, $classes;
|
||||
($f = fopen($impl, 'w')) || die("cannot open $impl");
|
||||
|
||||
if ($mode == 'sep' || $mode == 'remote') {
|
||||
$inc_file = "\"ext_${name}.h\"";
|
||||
if ($mode) {
|
||||
$header_file = "\"ext_${name}.h\"";
|
||||
} else {
|
||||
$inc_file = "<runtime/ext/ext_${name}.h>";
|
||||
$header_file = "<runtime/ext/ext_${name}.h>";
|
||||
}
|
||||
|
||||
fprintf($f,
|
||||
<<<EOT
|
||||
|
||||
#include $inc_file
|
||||
#include $header_file
|
||||
|
||||
namespace HPHP {
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
@@ -122,9 +122,16 @@ namespace HPHP {
|
||||
|
||||
EOT
|
||||
);
|
||||
|
||||
foreach ($constants as $const) {
|
||||
generateConstCPPImplementation($const, $f);
|
||||
}
|
||||
foreach ($funcs as $func) {
|
||||
generateFuncCPPImplementation($func, $f);
|
||||
}
|
||||
foreach ($classes as $class) {
|
||||
generateClassCPPImplementation($class, $f);
|
||||
}
|
||||
fprintf($f,
|
||||
<<<EOT
|
||||
|
||||
|
||||
Referência em uma Nova Issue
Bloquear um usuário