Fixes to get the C binding generator to work

This commit is contained in:
rdb 2015-07-05 16:07:08 +02:00
parent 41b44683b6
commit 7026854cc2
22 changed files with 153 additions and 56 deletions

View File

@ -217,11 +217,11 @@ is_equal(const CPPDeclaration *other) const {
if (*_bounds != *ot->_bounds) {
return false;
}
} else if (_bounds == NULL || ot->_bounds == NULL) {
} else if ((_bounds == NULL) != (ot->_bounds == NULL)) {
return false;
}
return _element_type == ot->_element_type;
return *_element_type == *ot->_element_type;
}
@ -241,10 +241,13 @@ is_less(const CPPDeclaration *other) const {
if (*_bounds != *ot->_bounds) {
return *_bounds < *ot->_bounds;
}
} else if (_bounds == NULL || ot->_bounds == NULL) {
} else if ((_bounds == NULL) != (ot->_bounds == NULL)) {
return _bounds < ot->_bounds;
}
return _element_type < ot->_element_type;
if (*_element_type != *ot->_element_type) {
return *_element_type < *ot->_element_type;
}
return false;
}

View File

@ -724,7 +724,7 @@ typedef_declaration:
inst->_storage_class |= (current_storage_class | $1);
current_scope->add_declaration(inst, global_scope, current_lexer, @2);
CPPTypedefType *typedef_type = new CPPTypedefType(inst->_type, inst->_ident, current_scope);
current_scope->add_declaration(typedef_type, global_scope, current_lexer, @2);
current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @2);
}
}
}
@ -735,13 +735,13 @@ typedef_instance_identifiers:
{
CPPType *target_type = current_type;
CPPTypedefType *typedef_type = new CPPTypedefType(target_type, $1, current_scope, @1.file);
current_scope->add_declaration(typedef_type, global_scope, current_lexer, @1);
current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @1);
}
| instance_identifier maybe_initialize ',' typedef_instance_identifiers
{
CPPType *target_type = current_type;
CPPTypedefType *typedef_type = new CPPTypedefType(target_type, $1, current_scope, @1.file);
current_scope->add_declaration(typedef_type, global_scope, current_lexer, @1);
current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @1);
}
;
@ -751,14 +751,14 @@ typedef_const_instance_identifiers:
$1->add_modifier(IIT_const);
CPPType *target_type = current_type;
CPPTypedefType *typedef_type = new CPPTypedefType(target_type, $1, current_scope, @1.file);
current_scope->add_declaration(typedef_type, global_scope, current_lexer, @1);
current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @1);
}
| instance_identifier maybe_initialize ',' typedef_const_instance_identifiers
{
$1->add_modifier(IIT_const);
CPPType *target_type = current_type;
CPPTypedefType *typedef_type = new CPPTypedefType(target_type, $1, current_scope, @1.file);
current_scope->add_declaration(typedef_type, global_scope, current_lexer, @1);
current_scope->add_declaration(CPPType::new_type(typedef_type), global_scope, current_lexer, @1);
}
;

View File

@ -323,6 +323,12 @@ new_type(CPPType *type) {
return type;
}
// If this triggers, we probably messed up by defining is_less()
// incorrectly; they provide a relative ordering even though they
// are equal to each other. Or, we provided an is_equal() that
// gives false negatives.
assert(**result.first == *type);
// The insertion has not taken place; thus, there was previously
// another equivalent type declared.
if (*result.first != type) {

View File

@ -381,7 +381,7 @@ is_equal(const CPPDeclaration *other) const {
const CPPTypedefType *ot = ((CPPDeclaration *)other)->as_typedef_type();
assert(ot != NULL);
return (_type == ot->_type) && (*_ident == *ot->_ident);
return (*_type == *ot->_type) && (*_ident == *ot->_ident);
}
////////////////////////////////////////////////////////////////////

View File

@ -94,9 +94,29 @@ get_parameter_name(int n) const {
// Access: Public
// Description: Writes a sequence of commands to the given output
// stream to call the wrapped function. The parameter
// values are taken from pexprs, if it is nonempty, or
// are assumed to be simply the names of the parameters,
// if it is empty.
// values are assumed to be simply the names of the
// parameters.
//
// The return value is the expression to return, if we
// are returning a value, or the empty string if we
// return nothing.
////////////////////////////////////////////////////////////////////
string FunctionRemap::
call_function(ostream &out, int indent_level, bool convert_result,
const string &container) const {
vector_string pexprs;
for (int i = 0; i < _parameters.size(); ++i) {
pexprs.push_back(get_parameter_name(i));
}
return call_function(out, indent_level, convert_result, container, pexprs);
}
////////////////////////////////////////////////////////////////////
// Function: FunctionRemap::call_function
// Access: Public
// Description: Writes a sequence of commands to the given output
// stream to call the wrapped function. The parameter
// values are taken from pexprs.
//
// The return value is the expression to return, if we
// are returning a value, or the empty string if we
@ -219,7 +239,7 @@ call_function(ostream &out, int indent_level, bool convert_result,
string call = get_call_str(container, pexprs);
if (!convert_result) {
return_expr = get_call_str(container, pexprs);
return_expr = call;
} else {
//if (_return_type->return_value_should_be_simple()) {
@ -371,23 +391,35 @@ get_call_str(const string &container, const vector_string &pexprs) const {
// Getters and setters are a special case.
if (_type == T_getter) {
if (!container.empty()) {
if (_has_this && !container.empty()) {
call << "(" << container << ")->" << _expression;
} else {
call << _expression;
}
} else if (_type == T_setter) {
if (!container.empty()) {
call << "(" << container << ")->" << _expression;
string expr;
if (_has_this && !container.empty()) {
expr = "(" + container + ")->" + _expression;
} else {
call << _expression;
expr = _expression;
}
// It's not possible to assign arrays in C++, we have to copy them.
CPPArrayType *array_type = _parameters[_first_true_parameter]._remap->get_orig_type()->as_array_type();
if (array_type != NULL) {
call << "std::copy(" << expr << ", " << expr << " + " << *array_type->_bounds << ", ";
} else {
call << expr << " = ";
}
call << " = ";
_parameters[_first_true_parameter]._remap->pass_parameter(call,
get_parameter_expr(_first_true_parameter, pexprs));
if (array_type != NULL) {
call << ')';
}
} else {
const char *separator = "";

View File

@ -52,9 +52,11 @@ public:
~FunctionRemap();
string get_parameter_name(int n) const;
string call_function(ostream &out, int indent_level,
bool convert_result, const string &container) const;
string call_function(ostream &out, int indent_level,
bool convert_result, const string &container,
const vector_string &pexprs = vector_string()) const;
const vector_string &pexprs) const;
void write_orig_prototype(ostream &out, int indent_level, bool local=false,
int num_default_args=0) const;

View File

@ -442,8 +442,8 @@ remap_parameter(CPPType *struct_type, CPPType *param_type) {
return new ParameterRemapEnumToInt(param_type);
*/
} else if (TypeManager::is_const_simple(param_type)) {
return new ParameterRemapConstToNonConst(param_type);
//} else if (TypeManager::is_const_simple(param_type)) {
// return new ParameterRemapConstToNonConst(param_type);
} else if (TypeManager::is_const_ref_to_simple(param_type)) {
return new ParameterRemapReferenceToConcrete(param_type);
@ -833,9 +833,9 @@ manage_return_value(ostream &out, int indent_level,
indent(out, indent_level)
<< "if (" << return_expr << " != ("
<< remap->_return_type->get_new_type()->get_local_name(&parser) << ")0) {\n";
<< remap->_return_type->get_new_type()->get_local_name(&parser) << ")NULL) {\n";
indent(out, indent_level + 2)
<< return_expr << "->ref();\n";
<< "(" << return_expr << ")->ref();\n";
indent(out, indent_level)
<< "}\n";
output_ref(out, indent_level, remap, "refcount");

View File

@ -173,13 +173,17 @@ write_function_for(ostream &out, InterfaceMaker::Function *func) {
void InterfaceMakerC::
write_function_instance(ostream &out, InterfaceMaker::Function *func,
FunctionRemap *remap) {
if (remap->_extension || (remap->_flags & FunctionRemap::F_explicit_self)) {
return;
}
out << "/*\n"
<< " * C wrapper for\n"
<< " * ";
remap->write_orig_prototype(out, 0);
out << "\n"
<< " */\n";
if (!output_function_names) {
// If we're not saving the function names, don't export it from
// the library.
@ -192,17 +196,17 @@ write_function_instance(ostream &out, InterfaceMaker::Function *func,
if (generate_spam) {
write_spam_message(out, remap);
}
string return_expr =
string return_expr =
remap->call_function(out, 2, true, "param0");
return_expr = manage_return_value(out, 2, remap, return_expr);
if (!return_expr.empty()) {
out << " return " << return_expr << ";\n";
}
out << "}\n\n";
}
////////////////////////////////////////////////////////////////////
// Function: InterfaceMakerC::write_function_header
// Access: Private
@ -212,6 +216,10 @@ write_function_instance(ostream &out, InterfaceMaker::Function *func,
void InterfaceMakerC::
write_function_header(ostream &out, InterfaceMaker::Function *func,
FunctionRemap *remap, bool newline) {
if (remap->_extension || (remap->_flags & FunctionRemap::F_explicit_self)) {
return;
}
if (remap->_void_return) {
out << "void";
} else {

View File

@ -4748,8 +4748,7 @@ write_function_instance(ostream &out, FunctionRemap *remap,
<< "PyUnicode_AsWideChar(" << param_name << ", " << param_name << "_str, " << param_name << "_len);\n"
<< "#endif\n";
pexpr_string = "std::wstring(" +
param_name + "_str, " + param_name + "_len)";
pexpr_string = param_name + "_str, " + param_name + "_len";
extra_cleanup
<< "#if PY_VERSION_HEX >= 0x03030000\n"
@ -4774,8 +4773,7 @@ write_function_instance(ostream &out, FunctionRemap *remap,
<< "PyUnicode_AsWideChar(" << param_name << ", " << param_name << "_str, " << param_name << "_len);\n"
<< "#endif\n";
pexpr_string = "&std::wstring(" +
param_name + "_str, " + param_name + "_len)";
pexpr_string = param_name + "_str, " + param_name + "_len";
extra_cleanup
<< "#if PY_VERSION_HEX >= 0x03030000\n"
@ -4822,13 +4820,12 @@ write_function_instance(ostream &out, FunctionRemap *remap,
+ "_str, &" + param_name + "_len";
}
if (TypeManager::is_const_ptr_to_basic_string_char(orig_type)) {
pexpr_string = "&std::string(" +
param_name + "_str, " + param_name + "_len)";
} else {
pexpr_string = "std::string(" +
param_name + "_str, " + param_name + "_len)";
}
// if (TypeManager::is_const_ptr_to_basic_string_char(orig_type)) {
// pexpr_string = "&std::string(" +
// param_name + "_str, " + param_name + "_len)";
// } else {
pexpr_string = param_name + "_str, " + param_name + "_len";
// }
expected_params += "str";
}
// Remember to clear the TypeError that any of the above methods raise.

View File

@ -1537,6 +1537,12 @@ get_getter(CPPType *expr_type, string expression,
assert(expr_type != (CPPType *)NULL);
}
// We can't return an array from a function, but we can decay it
// into a pointer.
while (expr_type->get_subtype() == CPPDeclaration::ST_array) {
expr_type = CPPType::new_type(new CPPPointerType(expr_type->as_array_type()->_element_type));
}
// Make up a CPPFunctionType.
CPPParameterList *params = new CPPParameterList;
CPPFunctionType *ftype = new CPPFunctionType(expr_type, params, 0);

View File

@ -23,6 +23,12 @@ ParameterRemapBasicStringPtrToString::
ParameterRemapBasicStringPtrToString(CPPType *orig_type) :
ParameterRemapToString(orig_type)
{
static CPPType *const_char_star_type = (CPPType *)NULL;
if (const_char_star_type == (CPPType *)NULL) {
const_char_star_type = parser.parse_type("const char *");
}
_new_type = const_char_star_type;
}
////////////////////////////////////////////////////////////////////
@ -34,7 +40,7 @@ ParameterRemapBasicStringPtrToString(CPPType *orig_type) :
////////////////////////////////////////////////////////////////////
void ParameterRemapBasicStringPtrToString::
pass_parameter(ostream &out, const string &variable_name) {
out << "&" << variable_name;
out << "&std::string(" << variable_name << ")";
}
////////////////////////////////////////////////////////////////////
@ -58,6 +64,12 @@ ParameterRemapBasicWStringPtrToWString::
ParameterRemapBasicWStringPtrToWString(CPPType *orig_type) :
ParameterRemapToWString(orig_type)
{
static CPPType *const_wchar_star_type = (CPPType *)NULL;
if (const_wchar_star_type == (CPPType *)NULL) {
const_wchar_star_type = parser.parse_type("const wchar_t *");
}
_new_type = const_wchar_star_type;
}
////////////////////////////////////////////////////////////////////
@ -69,7 +81,7 @@ ParameterRemapBasicWStringPtrToWString(CPPType *orig_type) :
////////////////////////////////////////////////////////////////////
void ParameterRemapBasicWStringPtrToWString::
pass_parameter(ostream &out, const string &variable_name) {
out << "&" << variable_name;
out << "&std::wstring(" << variable_name << ")";
}
////////////////////////////////////////////////////////////////////

View File

@ -23,6 +23,12 @@ ParameterRemapBasicStringRefToString::
ParameterRemapBasicStringRefToString(CPPType *orig_type) :
ParameterRemapToString(orig_type)
{
static CPPType *const_char_star_type = (CPPType *)NULL;
if (const_char_star_type == (CPPType *)NULL) {
const_char_star_type = parser.parse_type("const char *");
}
_new_type = const_char_star_type;
}
////////////////////////////////////////////////////////////////////
@ -34,7 +40,7 @@ ParameterRemapBasicStringRefToString(CPPType *orig_type) :
////////////////////////////////////////////////////////////////////
void ParameterRemapBasicStringRefToString::
pass_parameter(ostream &out, const string &variable_name) {
out << variable_name;
out << "std::string(" << variable_name << ")";
}
////////////////////////////////////////////////////////////////////
@ -58,6 +64,12 @@ ParameterRemapBasicWStringRefToWString::
ParameterRemapBasicWStringRefToWString(CPPType *orig_type) :
ParameterRemapToWString(orig_type)
{
static CPPType *const_wchar_star_type = (CPPType *)NULL;
if (const_wchar_star_type == (CPPType *)NULL) {
const_wchar_star_type = parser.parse_type("const wchar_t *");
}
_new_type = const_wchar_star_type;
}
////////////////////////////////////////////////////////////////////
@ -69,7 +81,7 @@ ParameterRemapBasicWStringRefToWString(CPPType *orig_type) :
////////////////////////////////////////////////////////////////////
void ParameterRemapBasicWStringRefToWString::
pass_parameter(ostream &out, const string &variable_name) {
out << variable_name;
out << "std::wstring(" << variable_name << ")";
}
////////////////////////////////////////////////////////////////////

View File

@ -24,6 +24,12 @@ ParameterRemapBasicStringToString::
ParameterRemapBasicStringToString(CPPType *orig_type) :
ParameterRemapToString(orig_type)
{
static CPPType *const_char_star_type = (CPPType *)NULL;
if (const_char_star_type == (CPPType *)NULL) {
const_char_star_type = parser.parse_type("const char *");
}
_new_type = const_char_star_type;
}
////////////////////////////////////////////////////////////////////
@ -35,7 +41,7 @@ ParameterRemapBasicStringToString(CPPType *orig_type) :
////////////////////////////////////////////////////////////////////
void ParameterRemapBasicStringToString::
pass_parameter(ostream &out, const string &variable_name) {
out << variable_name;
out << "std::string(" << variable_name << ")";
}
////////////////////////////////////////////////////////////////////
@ -73,8 +79,14 @@ get_return_expr(const string &expression) {
////////////////////////////////////////////////////////////////////
ParameterRemapBasicWStringToWString::
ParameterRemapBasicWStringToWString(CPPType *orig_type) :
ParameterRemapToString(orig_type)
ParameterRemapToWString(orig_type)
{
static CPPType *const_wchar_star_type = (CPPType *)NULL;
if (const_wchar_star_type == (CPPType *)NULL) {
const_wchar_star_type = parser.parse_type("const wchar_t *");
}
_new_type = const_wchar_star_type;
}
////////////////////////////////////////////////////////////////////
@ -86,7 +98,7 @@ ParameterRemapBasicWStringToWString(CPPType *orig_type) :
////////////////////////////////////////////////////////////////////
void ParameterRemapBasicWStringToWString::
pass_parameter(ostream &out, const string &variable_name) {
out << variable_name;
out << "std::wstring(" << variable_name << ")";
}
////////////////////////////////////////////////////////////////////

View File

@ -39,7 +39,7 @@ public:
// Description : Maps a concrete basic_string<wchar_t> to an atomic
// string.
////////////////////////////////////////////////////////////////////
class ParameterRemapBasicWStringToWString : public ParameterRemapToString {
class ParameterRemapBasicWStringToWString : public ParameterRemapToWString {
public:
ParameterRemapBasicWStringToWString(CPPType *orig_type);

View File

@ -35,7 +35,7 @@ ParameterRemapToString(CPPType *orig_type) :
const_char_star_type = parser.parse_type("const char *");
}
if (TypeManager::is_const(orig_type)) {
if (TypeManager::is_const_char_pointer(orig_type) || TypeManager::is_string(orig_type)) {
_new_type = const_char_star_type;
} else {
_new_type = char_star_type;

View File

@ -59,6 +59,10 @@ __published:
bool bad() const;
void clear();
};
// We actually want to wrap streampos as streamoff.
#define streampos streamoff
class ostream : virtual public ios {
__published:
void put(char c);
@ -104,6 +108,6 @@ extern istream cin;
extern ostream cout;
extern ostream cerr;
typedef int streampos;
typedef long streamoff;
#endif

View File

@ -15,7 +15,7 @@
#ifndef _STDINT_H
#define _STDINT_H
#ifdef _LP64
#if defined(_LP64) || defined(_WIN64)
#define __WORDSIZE 64
#else
#define __WORDSIZE 32

View File

@ -20,8 +20,8 @@
#ifndef STDTYPEDEFS_H
#define STDTYPEDEFS_H
#ifndef __APPLE__
typedef unsigned int size_t;
typedef int ssize_t;
typedef unsigned long size_t;
typedef long ssize_t;
typedef int off_t;
typedef unsigned int time_t;
typedef int clock_t;

View File

@ -77,7 +77,6 @@ PUBLISHED:
void clear_direct_host();
void add_direct_host(const string &hostname);
void get_proxies_for_url(const URLSpec &url, pvector<URLSpec> &proxies) const;
string get_proxies_for_url(const URLSpec &url) const;
void set_username(const string &server, const string &realm, const string &username);
@ -134,6 +133,8 @@ PUBLISHED:
static HTTPClient *get_global_ptr();
public:
void get_proxies_for_url(const URLSpec &url, pvector<URLSpec> &proxies) const;
SSL_CTX *get_ssl_ctx();
private:

View File

@ -34,6 +34,8 @@ PUBLISHED:
INLINE string get_data();
INLINE void set_data(const string &data);
public:
INLINE void swap_data(pvector<unsigned char> &data);
private:

View File

@ -1018,7 +1018,7 @@ tag_end() const {
////////////////////////////////////////////////////////////////////
// Function: EggGroup::tag_size
// Access: Published
// Access: Public
// Description: Returns the number of elements between tag_begin()
// and tag_end().
//

View File

@ -36,7 +36,7 @@ class GraphicsStateGuardianBase;
////////////////////////////////////////////////////////////////////
class EXPCL_PANDA_PGRAPH GeomNode : public PandaNode {
PUBLISHED:
GeomNode(const string &name);
explicit GeomNode(const string &name);
protected:
GeomNode(const GeomNode &copy);