interfaceMakerPythonNative.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Filename: InterfaceMakerPythonNative.h
  2. ////////////////////////////////////////////////////////////////////
  3. //
  4. // PANDA 3D SOFTWARE
  5. // Copyright (c) Carnegie Mellon University. All rights reserved.
  6. //
  7. // All use of this software is subject to the terms of the revised BSD
  8. // license. You should have received a copy of this license along
  9. // with this source code in a file named "LICENSE."
  10. //
  11. ////////////////////////////////////////////////////////////////////
  12. #ifndef INTERFACEMAKERPYTHONNATIVE_H
  13. #define INTERFACEMAKERPYTHONNATIVE_H
  14. #include "map"
  15. #include "set"
  16. #include "dtoolbase.h"
  17. #include "interfaceMakerPython.h"
  18. #include "interrogate_interface.h"
  19. #include "cppStructType.h"
  20. class FunctionRemap;
  21. ////////////////////////////////////////////////////////////////////
  22. // Class : InterfaceMakerPythonNative
  23. // Description : An InterfaceMaker for generating complex Python
  24. // function wrappers around C++ code.
  25. ////////////////////////////////////////////////////////////////////
  26. class InterfaceMakerPythonNative : public InterfaceMakerPython {
  27. public:
  28. InterfaceMakerPythonNative(InterrogateModuleDef *def);
  29. virtual ~InterfaceMakerPythonNative();
  30. virtual void write_prototypes(ostream &out, ostream *out_h);
  31. void write_prototypes_class(ostream &out, ostream *out_h, Object *obj) ;
  32. void write_prototypes_class_external(ostream &out, Object *obj);
  33. virtual void write_functions(ostream &out);
  34. virtual void write_module(ostream &out, ostream *out_h, InterrogateModuleDef *def);
  35. virtual void write_module_support(ostream &out, ostream *out_h, InterrogateModuleDef *def);
  36. void write_module_class(ostream &out, Object *cls);
  37. virtual void write_sub_module(ostream &out, Object *obj);
  38. virtual bool synthesize_this_parameter();
  39. virtual bool separate_overloading();
  40. virtual Object *record_object(TypeIndex type_index);
  41. protected:
  42. virtual string get_wrapper_prefix();
  43. virtual string get_unique_prefix();
  44. virtual void record_function_wrapper(InterrogateFunction &ifunc,
  45. FunctionWrapperIndex wrapper_index);
  46. virtual void generate_wrappers();
  47. private:
  48. // This enum defines the various prototypes that must be generated
  49. // for the specialty functions that Python requires, especially for
  50. // the slotted functions.
  51. enum WrapperType {
  52. WT_none,
  53. WT_no_params,
  54. WT_one_param,
  55. WT_binary_operator,
  56. WT_setattr,
  57. WT_getattr,
  58. WT_sequence_getitem,
  59. WT_sequence_setitem,
  60. WT_sequence_size,
  61. WT_mapping_setitem,
  62. WT_inquiry,
  63. WT_getbuffer,
  64. WT_releasebuffer,
  65. WT_iter_next,
  66. WT_ternary_operator,
  67. WT_inplace_binary_operator,
  68. WT_inplace_ternary_operator,
  69. WT_traverse,
  70. WT_compare,
  71. WT_hash,
  72. };
  73. // This enum is passed to the wrapper generation functions to indicate
  74. // what sort of values the wrapper function is expected to return.
  75. enum ReturnFlags {
  76. // -1 on failure, 0 on success.
  77. RF_int = 0x100,
  78. // Like RF_int, but special case that it returns -1, 0, or 1.
  79. RF_compare = RF_int | 0x200,
  80. // Returns the actual return value as PyObject*.
  81. RF_pyobject = 0x010,
  82. // Returns a reference to self.
  83. RF_self = 0x020,
  84. // Assign to the coerced argument, in the case of a coercion constructor.
  85. RF_coerced = 0x040,
  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);
  140. void write_make_seq(ostream &out, Object *obj, const std::string &ClassName,
  141. const std::string &cClassName, MakeSeq *make_seq);
  142. void write_class_prototypes(ostream &out) ;
  143. void write_class_declarations(ostream &out, ostream *out_h, Object *obj);
  144. void write_class_details(ostream &out, Object *obj);
  145. public:
  146. bool is_remap_legal(FunctionRemap *remap);
  147. int has_coerce_constructor(CPPStructType *type);
  148. bool is_remap_coercion_possible(FunctionRemap *remap);
  149. bool is_function_legal(Function *func);
  150. bool is_cpp_type_legal(CPPType *ctype);
  151. bool isExportThisRun(CPPType *ctype);
  152. bool isExportThisRun(Function *func);
  153. bool isFunctionWithThis( Function *func);
  154. bool IsRunTimeTyped(const InterrogateType &itype);
  155. // comunicates the cast capabilites among methods..
  156. struct CastDetails {
  157. CPPStructType *_structType;
  158. std::string _to_class_name;
  159. std::string _up_cast_string;
  160. bool _can_downcast;
  161. bool _is_legal_py_class;
  162. };
  163. void get_valid_child_classes(std::map<std::string, CastDetails> &answer, CPPStructType *inclass, const std::string &upcast_seed = "", bool can_downcast = true);
  164. bool DoesInheritFromIsClass(const CPPStructType * inclass, const std::string &name);
  165. bool IsPandaTypedObject(CPPStructType * inclass) { return DoesInheritFromIsClass(inclass,"TypedObject"); };
  166. void write_python_instance(ostream &out, int indent_level, const std::string &return_expr, bool owns_memory, const InterrogateType &itype, bool is_const);
  167. bool HasAGetClassTypeFunction(CPPType *type);
  168. int NeedsAStrFunction(const InterrogateType &itype_class);
  169. int NeedsAReprFunction(const InterrogateType &itype_class);
  170. bool NeedsARichCompareFunction(const InterrogateType &itype_class);
  171. void output_quoted(ostream &out, int indent_level, const std::string &str,
  172. bool first_line=true);
  173. // stash the forward declarations for this compile pass..
  174. std::set<CPPType *> _external_imports;
  175. };
  176. #endif