interfaceMakerPythonNative.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file interfaceMakerPythonNative.h
  10. */
  11. #ifndef INTERFACEMAKERPYTHONNATIVE_H
  12. #define INTERFACEMAKERPYTHONNATIVE_H
  13. #include "map"
  14. #include "set"
  15. #include "dtoolbase.h"
  16. #include "interfaceMakerPython.h"
  17. #include "interrogate_interface.h"
  18. #include "cppStructType.h"
  19. class FunctionRemap;
  20. /**
  21. * An InterfaceMaker for generating complex Python function wrappers around
  22. * C++ code.
  23. */
  24. class InterfaceMakerPythonNative : public InterfaceMakerPython {
  25. public:
  26. InterfaceMakerPythonNative(InterrogateModuleDef *def);
  27. virtual ~InterfaceMakerPythonNative();
  28. virtual void write_prototypes(ostream &out, ostream *out_h);
  29. void write_prototypes_class(ostream &out, ostream *out_h, Object *obj) ;
  30. void write_prototypes_class_external(ostream &out, Object *obj);
  31. virtual void write_functions(ostream &out);
  32. virtual void write_module(ostream &out, ostream *out_h, InterrogateModuleDef *def);
  33. virtual void write_module_support(ostream &out, ostream *out_h, InterrogateModuleDef *def);
  34. void write_module_class(ostream &out, Object *cls);
  35. virtual void write_sub_module(ostream &out, Object *obj);
  36. virtual bool synthesize_this_parameter();
  37. virtual bool separate_overloading();
  38. virtual Object *record_object(TypeIndex type_index);
  39. protected:
  40. virtual string get_wrapper_prefix();
  41. virtual string get_unique_prefix();
  42. virtual void record_function_wrapper(InterrogateFunction &ifunc,
  43. FunctionWrapperIndex wrapper_index);
  44. virtual void generate_wrappers();
  45. private:
  46. // This enum defines the various prototypes that must be generated for the
  47. // specialty functions that Python requires, especially for the slotted
  48. // functions.
  49. enum WrapperType {
  50. WT_none,
  51. WT_no_params,
  52. WT_one_param,
  53. WT_binary_operator,
  54. WT_setattr,
  55. WT_getattr,
  56. WT_sequence_getitem,
  57. WT_sequence_setitem,
  58. WT_sequence_size,
  59. WT_mapping_setitem,
  60. WT_inquiry,
  61. WT_getbuffer,
  62. WT_releasebuffer,
  63. WT_iter_next,
  64. WT_ternary_operator,
  65. WT_inplace_binary_operator,
  66. WT_inplace_ternary_operator,
  67. WT_traverse,
  68. WT_compare,
  69. WT_hash,
  70. };
  71. // This enum is passed to the wrapper generation functions to indicate what
  72. // sort of values the wrapper function is expected to return.
  73. enum ReturnFlags {
  74. // -1 on failure, 0 on success.
  75. RF_int = 0x100,
  76. // Like RF_int, but special case that it returns -1, 0, or 1.
  77. RF_compare = RF_int | 0x200,
  78. // Returns the actual return value as PyObject*.
  79. RF_pyobject = 0x010,
  80. // Returns a reference to self.
  81. RF_self = 0x020,
  82. // Assign to the coerced argument, in the case of a coercion constructor.
  83. RF_coerced = 0x040,
  84. // Don't automatically map NULL to None
  85. RF_preserve_null = 0x080,
  86. // These indicate what should be returned on error.
  87. RF_err_notimplemented = 0x002,
  88. RF_err_null = 0x004,
  89. RF_err_false = 0x008,
  90. // Decref temporary args object before returning.
  91. RF_decref_args = 0x1000,
  92. };
  93. class SlottedFunctionDef {
  94. public:
  95. string _answer_location;
  96. WrapperType _wrapper_type;
  97. int _min_version;
  98. string _wrapper_name;
  99. set<FunctionRemap*> _remaps;
  100. bool _keep_method;
  101. };
  102. typedef std::map<string, SlottedFunctionDef> SlottedFunctions;
  103. static bool get_slotted_function_def(Object *obj, Function *func, FunctionRemap *remap, SlottedFunctionDef &def);
  104. static void write_function_slot(ostream &out, int indent_level,
  105. const SlottedFunctions &slots,
  106. const string &slot, const string &def = "0");
  107. void write_prototype_for_name(ostream &out, Function *func, const std::string &name);
  108. void write_prototype_for(ostream &out, Function *func);
  109. void write_function_for_top(ostream &out, Object *obj, Function *func);
  110. void write_function_for_name(ostream &out, Object *obj,
  111. const Function::Remaps &remaps,
  112. const std::string &name, string &expected_params,
  113. bool coercion_allowed,
  114. ArgsType args_type, int return_flags);
  115. void write_coerce_constructor(ostream &out, Object *obj, bool is_const);
  116. int collapse_default_remaps(std::map<int, std::set<FunctionRemap *> > &map_sets,
  117. int max_required_args);
  118. void write_function_forset(ostream &out,
  119. const std::set<FunctionRemap*> &remaps,
  120. int min_num_args, int max_num_args,
  121. string &expected_params, int indent_level,
  122. bool coercion_allowed, bool report_errors,
  123. ArgsType args_type, int return_flags,
  124. bool check_exceptions = true,
  125. bool verify_const = true,
  126. const string &first_expr = string());
  127. void write_function_instance(ostream &out, FunctionRemap *remap,
  128. int min_num_args, int max_num_args,
  129. string &expected_params, int indent_level,
  130. bool coercion_allowed, bool report_errors,
  131. ArgsType args_type, int return_flags,
  132. bool check_exceptions = true,
  133. const string &first_pexpr = string());
  134. void error_return(ostream &out, int indent_level, int return_flags);
  135. void error_raise_return(ostream &out, int indent_level, int return_flags,
  136. const string &exc_type, const string &message,
  137. const string &format_args = "");
  138. void pack_return_value(ostream &out, int indent_level, FunctionRemap *remap,
  139. std::string return_expr, int return_flags);
  140. void write_make_seq(ostream &out, Object *obj, const std::string &ClassName,
  141. const std::string &cClassName, MakeSeq *make_seq);
  142. void write_getset(ostream &out, Object *obj, Property *property);
  143. void write_class_prototypes(ostream &out) ;
  144. void write_class_declarations(ostream &out, ostream *out_h, Object *obj);
  145. void write_class_details(ostream &out, Object *obj);
  146. public:
  147. bool is_remap_legal(FunctionRemap *remap);
  148. int has_coerce_constructor(CPPStructType *type);
  149. bool is_remap_coercion_possible(FunctionRemap *remap);
  150. bool is_function_legal(Function *func);
  151. bool is_cpp_type_legal(CPPType *ctype);
  152. bool isExportThisRun(CPPType *ctype);
  153. bool isExportThisRun(Function *func);
  154. bool isFunctionWithThis( Function *func);
  155. bool IsRunTimeTyped(const InterrogateType &itype);
  156. // comunicates the cast capabilites among methods..
  157. struct CastDetails {
  158. CPPStructType *_structType;
  159. std::string _to_class_name;
  160. std::string _up_cast_string;
  161. bool _can_downcast;
  162. bool _is_legal_py_class;
  163. };
  164. void get_valid_child_classes(std::map<std::string, CastDetails> &answer, CPPStructType *inclass, const std::string &upcast_seed = "", bool can_downcast = true);
  165. bool DoesInheritFromIsClass(const CPPStructType * inclass, const std::string &name);
  166. bool IsPandaTypedObject(CPPStructType * inclass) { return DoesInheritFromIsClass(inclass,"TypedObject"); };
  167. void write_python_instance(ostream &out, int indent_level, const std::string &return_expr, bool owns_memory, const InterrogateType &itype, bool is_const);
  168. bool has_get_class_type_function(CPPType *type);
  169. bool has_init_type_function(CPPType *type);
  170. int NeedsAStrFunction(const InterrogateType &itype_class);
  171. int NeedsAReprFunction(const InterrogateType &itype_class);
  172. bool NeedsARichCompareFunction(const InterrogateType &itype_class);
  173. void output_quoted(ostream &out, int indent_level, const std::string &str,
  174. bool first_line=true);
  175. // stash the forward declarations for this compile pass..
  176. std::set<CPPType *> _external_imports;
  177. };
  178. #endif