interfaceMakerPythonNative.cxx 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136
  1. // Filename: interfaceMakerPythonNative.cxx
  2. ////////////////////////////////////////////////////////////////////
  3. //
  4. // PANDA 3D SOFTWARE
  5. // Copyright (c) 2001 - 2004, Disney Enterprises, Inc. All rights reserved
  6. //
  7. // All use of this software is subject to the terms of the Panda 3d
  8. // Software license. You should have received a copy of this license
  9. // along with this source code; you will also find a current copy of
  10. // the license at http://etc.cmu.edu/panda3d/docs/license/ .
  11. //
  12. // To contact the maintainers of this program write to
  13. // [email protected] .
  14. //
  15. ////////////////////////////////////////////////////////////////////
  16. #include "interfaceMakerPythonNative.h"
  17. #include "interrogateBuilder.h"
  18. #include "interrogate.h"
  19. #include "functionRemap.h"
  20. #include "parameterRemapUnchanged.h"
  21. #include "typeManager.h"
  22. #include "interrogateDatabase.h"
  23. #include "interrogateType.h"
  24. #include "interrogateFunction.h"
  25. #include "cppFunctionType.h"
  26. #include "cppPointerType.h"
  27. #include "cppTypeDeclaration.h"
  28. #include "cppStructType.h"
  29. #include "vector"
  30. #include "cppParameterList.h"
  31. #include "algorithm"
  32. #include <set>
  33. #include <map>
  34. extern bool inside_python_native;
  35. extern InterrogateType dummy_type;
  36. extern std::string EXPORT_IMPORT_PREFEX;
  37. #define CLASS_PREFEX "Dtool_"
  38. #define INSTANCE_PREFEX "Dtool_"
  39. #define BASE_INSTANCE_NAME "Dtool_PyInstDef"
  40. /////////////////////////////////////////////////////////
  41. // Name Remaper...
  42. // Snagged from ffi py code....
  43. /////////////////////////////////////////////////////////
  44. struct RenameSet
  45. {
  46. char * _from;
  47. char * _to;
  48. int function_type;
  49. };
  50. struct FlagSet
  51. {
  52. char * _to;
  53. int function_type;
  54. };
  55. ///////////////////////////////////////////////////////////////////////////////////////
  56. RenameSet methodRenameDictionary[] = {
  57. { "operator==" , "eq", 0 },
  58. { "operator!=" , "ne", 0 },
  59. { "operator<<" , "__lshift__", 0 },
  60. { "operator>>" , "__rshift__", 0 },
  61. { "operator<" , "lessThan", 0 },
  62. { "operator>" , "greaterThan", 0 },
  63. { "operator<=" , "lessThanOrEqual", 0 },
  64. { "operator>=" , "greaterThanOrEqual", 0 },
  65. { "operator=" , "assign", 0 },
  66. { "operator()" , "__call__", 0 },
  67. { "operator[]" , "__getitem__", 0 },
  68. { "operator++unary" , "increment", 0 },
  69. { "operator++" , "increment", 0 },
  70. { "operator--unary" , "decrement", 0 },
  71. { "operator--" , "decrement", 0 },
  72. { "operator^" , "__xor__", 0 },
  73. { "operator%" , "__mod__", 0 },
  74. { "operator!" , "logicalNot", 0 },
  75. { "operator~unary" , "__invert__", 0 },
  76. { "operator&" , "__and__", 0 },
  77. { "operator&&" , "logicalAnd", 0 },
  78. { "operator|" , "__or__", 0 },
  79. { "operator||" , "logicalOr", 0 },
  80. { "operator+" , "__add__", 0 },
  81. { "operator-" , "__sub__", 0 },
  82. { "operator-unary", "__neg__", 0 },
  83. { "operator*" , "__mul__", 0 },
  84. { "operator/" , "__div__", 0 },
  85. { "operator+=" , "__iadd__", 1 },
  86. { "operator-=" , "__isub__", 1 },
  87. { "operator*=" , "__imul__", 1 },
  88. { "operator/=" , "__idiv__", 1 },
  89. { "operator," , "concatenate", 0 },
  90. { "operator|=" , "__ior__", 1 },
  91. { "operator&=" , "__iand__", 1 },
  92. { "operator^=" , "__ixor__", 1 },
  93. { "operator~=" , "bitwiseNotEqual", 0 },
  94. { "operator->" , "dereference", 0 },
  95. { "operator<<=" , "__ilshift__", 1 },
  96. { "operator>>=" , "__irshift__", 1 },
  97. { "print" , "Cprint", 0 },
  98. { "CInterval.setT" , "_priv__cSetT", 0 },
  99. { NULL, NULL, -1 }
  100. };
  101. char * InPlaceSet[] = {
  102. "__iadd__",
  103. "__isub__",
  104. "__imul__",
  105. "__idiv__",
  106. "__ior__",
  107. "__iand__",
  108. "__ixor__",
  109. "__ilshift__",
  110. "__irshift__",
  111. NULL,
  112. };
  113. ///////////////////////////////////////////////////////////////////////////////////////
  114. RenameSet classRenameDictionary[] = {
  115. { "Loader" , "PandaLoader",0 },
  116. { "String" , "CString",0 },
  117. { "LMatrix4f" , "Mat4",0 },
  118. { "LMatrix3f" , "Mat3",0 },
  119. { "LVecBase4f" , "VBase4",0 },
  120. { "LVector4f" , "Vec4",0 },
  121. { "LPoint4f" , "Point4",0 },
  122. { "LVecBase3f" , "VBase3",0 },
  123. { "LVector3f" , "Vec3",0 },
  124. { "LPoint3f" , "Point3",0 },
  125. { "LVecBase2f" , "VBase2",0 },
  126. { "LVector2f" , "Vec2",0 },
  127. { "LPoint2f" , "Point2",0 },
  128. { "LQuaternionf" , "Quat",0 },
  129. { "LMatrix4d" , "Mat4D",0 },
  130. { "LMatrix3d" , "Mat3D",0 },
  131. { "LVecBase4d" , "VBase4D",0 },
  132. { "LVector4d" , "Vec4D",0 },
  133. { "LPoint4d" , "Point4D",0 },
  134. { "LVecBase3d" , "VBase3D",0 },
  135. { "LVector3d" , "Vec3D",0 },
  136. { "LPoint3d" , "Point3D",0 },
  137. { "LVecBase2d" , "VBase2D",0 },
  138. { "LVector2d" , "Vec2D",0 },
  139. { "LPoint2d" , "Point2D",0 },
  140. { "LQuaterniond" , "QuatD",0 },
  141. { "Plane" , "PlaneBase",0 },
  142. { "Planef" , "Plane",0 },
  143. { "Planed" , "PlaneD",0 },
  144. { "Frustum" , "FrustumBase",0 },
  145. { "Frustumf" , "Frustum",0 },
  146. { "Frustumd" , "FrustumD",0 },
  147. { NULL,NULL,-1 }
  148. };
  149. ///////////////////////////////////////////////////////////////////////////////////////
  150. ///////////////////////////////////////////////////////////////////////////////////////
  151. char * pythonKeywords[] = {
  152. "and",
  153. "del",
  154. "for",
  155. "is",
  156. "raise",
  157. "assert",
  158. "elif",
  159. "from",
  160. "lambda",
  161. "return",
  162. "break",
  163. "else",
  164. "global",
  165. "not",
  166. "try",
  167. "class",
  168. "except",
  169. "if",
  170. "or",
  171. "while",
  172. "continue",
  173. "exec",
  174. "import",
  175. "pass",
  176. "def",
  177. "finally",
  178. "in",
  179. "print",
  180. NULL};
  181. ///////////////////////////////////////////////////////////////////////////////////////
  182. std::string checkKeyword(std::string & cppName)
  183. {
  184. for(int x = 0; pythonKeywords[x] != NULL; x++)
  185. {
  186. if(cppName == pythonKeywords[x])
  187. {
  188. return std::string("_")+cppName;
  189. }
  190. }
  191. return cppName;
  192. }
  193. ///////////////////////////////////////////////////////////////////////////////////////
  194. ///////////////////////////////////////////////////////////////////////////////////////
  195. std::string classNameFromCppName(const std::string &cppName)
  196. {
  197. //# initialize to empty string
  198. std::string className = "";
  199. //# These are the characters we want to strip out of the name
  200. const std::string badChars("!@#$%^&*()<>,.-=+~{}? ");
  201. int nextCap = 0;
  202. int firstChar = 1;
  203. for(std::string::const_iterator chr = cppName.begin(); chr != cppName.end(); chr++)
  204. {
  205. if (badChars.find(*chr) != std::string::npos)
  206. {
  207. }
  208. else if (*chr == '_')
  209. {
  210. nextCap = 1;
  211. }
  212. else if (nextCap || firstChar)
  213. {
  214. className += toupper(*chr);
  215. nextCap = 0;
  216. firstChar = 0;
  217. }
  218. else
  219. {
  220. className += * chr;
  221. }
  222. }
  223. for(int x = 0; classRenameDictionary[x]._from != NULL; x++)
  224. {
  225. if(className == classRenameDictionary[x]._from)
  226. className = classRenameDictionary[x]._to;
  227. }
  228. if (className.empty())
  229. {
  230. std::string text = "** ERROR ** Renaming class: " + cppName + " to empty string";
  231. printf("%s",text.c_str());
  232. }
  233. //# FFIConstants.notify.debug('Renaming class: ' + cppName + ' to: ' + className)
  234. //# Note we do not have to check for keywords because class name are capitalized
  235. return className;
  236. }
  237. ///////////////////////////////////////////////////////////////////////////////////////
  238. ///////////////////////////////////////////////////////////////////////////////////////
  239. std::string nonClassNameFromCppName(const std::string &cppName_in)
  240. {
  241. std::string className = classNameFromCppName(cppName_in);
  242. //# Make the first character lowercase
  243. std::string newName;
  244. int pass = 0;
  245. for(std::string::const_iterator chr = className.begin(); chr != className.end(); chr++)
  246. {
  247. if(pass == 0)
  248. newName += toupper(*chr);
  249. else
  250. newName += tolower(*chr);
  251. pass++;
  252. }
  253. //# Mangle names that happen to be python keywords so they are not anymore
  254. //newName = checkKeyword(newName)
  255. return newName;
  256. }
  257. ///////////////////////////////////////////////////////////////////////////////////////
  258. ///////////////////////////////////////////////////////////////////////////////////////
  259. std::string methodNameFromCppName(InterfaceMaker::Function *func, const std::string &className)
  260. {
  261. std::string cppName = func->_ifunc.get_name();
  262. if (func->_ifunc.is_unary_op()) {
  263. cppName += "unary";
  264. }
  265. std::string methodName;
  266. std::string badChars(" ");
  267. int nextCap = 0;
  268. for(std::string::const_iterator chr = cppName.begin(); chr != cppName.end(); chr++)
  269. {
  270. if (badChars.find(*chr) != std::string::npos)
  271. {
  272. }
  273. else if (*chr == '_')
  274. {
  275. nextCap = 1;
  276. }
  277. else if (nextCap)
  278. {
  279. methodName += toupper(*chr);
  280. nextCap = 0;
  281. }
  282. else
  283. {
  284. methodName += *chr;
  285. }
  286. }
  287. std::string LookUpName = methodName;
  288. for(int x = 0; classRenameDictionary[x]._from != NULL; x++)
  289. {
  290. if(methodName == methodRenameDictionary[x]._from || (cppName == methodRenameDictionary[x]._from && methodRenameDictionary[x].function_type != 0) )
  291. {
  292. methodName = methodRenameDictionary[x]._to;
  293. }
  294. }
  295. if(className.size() > 0)
  296. {
  297. LookUpName = className + '.' + methodName;
  298. for(int x = 0; classRenameDictionary[x]._from != NULL; x++)
  299. {
  300. if(methodName == methodRenameDictionary[x]._from)
  301. methodName = methodRenameDictionary[x]._to;
  302. }
  303. }
  304. // # Mangle names that happen to be python keywords so they are not anymore
  305. methodName = checkKeyword(methodName);
  306. return methodName;
  307. }
  308. ///////////////////////////////////////////////////////////////////////////////////////
  309. ///////////////////////////////////////////////////////////////////////////////////////
  310. ///////////////////////////////////////////////////////////////////////////////
  311. ///////////////////////////////////////////////////////////////////////////////
  312. ///////////////////////////////////////////////////////////////////////////////
  313. std::string make_safe_name(const std::string & name)
  314. {
  315. return InterrogateBuilder::clean_identifier(name);
  316. /*
  317. static const char safe_chars2[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
  318. std::string result = name;
  319. size_t pos = result.find_first_not_of(safe_chars2);
  320. while (pos != std::string::npos)
  321. {
  322. result[pos] = '_';
  323. pos = result.find_first_not_of(safe_chars2);
  324. }
  325. return result;
  326. */
  327. }
  328. bool isInplaceFunction(InterfaceMaker::Function *func)
  329. {
  330. std::string wname = methodNameFromCppName(func,"");
  331. for(int x = 0; InPlaceSet[x] != NULL; x++)
  332. if(InPlaceSet[x] == wname)
  333. return true;
  334. return false;
  335. }
  336. ///////////////////////////////////////////////////////////////////////////////
  337. ///////////////////////////////////////////////////////////////////////////////
  338. void InterfaceMakerPythonNative::GetValideChildClasses( std::map< std::string ,CastDetails > &answer, CPPStructType * inclass, const std::string &up_cast_seed, bool downcastposible)
  339. {
  340. if(inclass == NULL)
  341. return;
  342. CPPStructType::Derivation::const_iterator bi;
  343. for (bi = inclass->_derivation.begin();
  344. bi != inclass->_derivation.end();
  345. ++bi)
  346. {
  347. const CPPStructType::Base &base = (*bi);
  348. // if (base._vis <= V_public)
  349. // downcastposible = false;
  350. CPPStructType *base_type = TypeManager::resolve_type(base._base)->as_struct_type();
  351. if(base_type != NULL)
  352. {
  353. std::string scoped_name = base_type->get_fully_scoped_name();
  354. if(answer.find(scoped_name) == answer.end())
  355. {
  356. answer[scoped_name]._can_downcast = downcastposible;
  357. answer[scoped_name]._to_class_name = scoped_name;
  358. answer[scoped_name]._structType = base_type;
  359. if(base._is_virtual)
  360. answer[scoped_name]._can_downcast = false;
  361. std::string local_up_cast("( ");
  362. local_up_cast += scoped_name + " *)"+ up_cast_seed +"";
  363. answer[scoped_name]._up_cast_string = local_up_cast;
  364. answer[scoped_name]._is_legal_py_class = isCppTypeLegal(base_type);
  365. }
  366. else
  367. {
  368. answer[scoped_name]._can_downcast = false;
  369. }
  370. GetValideChildClasses(answer,base_type, answer[scoped_name]._up_cast_string,answer[scoped_name]._can_downcast);
  371. }
  372. }
  373. }
  374. ///////////////////////////////////////////////////////////////////////////////
  375. // Function : WriteReturnInstance
  376. //
  377. ///////////////////////////////////////////////////////////////////////////////
  378. void InterfaceMakerPythonNative::WriteReturnInstance(ostream &out, int indent_level, std::string &return_expr, std::string &ows_memory_flag, const std::string &class_name, CPPType *ctype, bool inplace)
  379. {
  380. if(inplace == true)
  381. {
  382. indent(out, indent_level)<<"Py_INCREF(self);\n";
  383. indent(out, indent_level)<<"return self;\n";
  384. }
  385. else
  386. {
  387. indent(out, indent_level)<<"if("<< return_expr<< " == NULL)\n";
  388. indent(out, indent_level)<<"{\n";
  389. indent(out, indent_level)<<" Py_INCREF(Py_None);\n";
  390. indent(out, indent_level)<<" return Py_None;\n";
  391. indent(out, indent_level)<<"}\n";
  392. if(IsPandaTypedObject(ctype->as_struct_type()))
  393. {
  394. std::string typestr = "((TypedObject *)" + return_expr + ")->get_type_index()";
  395. indent(out, indent_level)<<"return DTool_CreatePyInstanceTyped((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(class_name) << ","<< ows_memory_flag<<","<<typestr<<");\n";
  396. }
  397. else
  398. {
  399. // indent(out, indent_level)<< "if(" << return_expr <<"!= NULL)\n";
  400. indent(out, indent_level)
  401. <<"return DTool_CreatePyInstance((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(class_name) << ","<<ows_memory_flag<<");\n";
  402. }
  403. }
  404. }
  405. ////////////////////////////////////////////////////////////////////
  406. // Function: InterfaceMakerPythonNative::Constructor
  407. // Access: Public
  408. // Description:
  409. ////////////////////////////////////////////////////////////////////
  410. InterfaceMakerPythonNative::
  411. InterfaceMakerPythonNative(InterrogateModuleDef *def) :
  412. InterfaceMakerPython(def)
  413. {
  414. }
  415. ////////////////////////////////////////////////////////////////////
  416. // Function: InterfaceMakerPythonNative::Destructor
  417. // Access: Public, Virtual
  418. // Description:
  419. ////////////////////////////////////////////////////////////////////
  420. InterfaceMakerPythonNative::
  421. ~InterfaceMakerPythonNative() {
  422. }
  423. ////////////////////////////////////////////////////////////////////
  424. // Function: InterfaceMakerPythonNative::write_prototypes
  425. // Access: Public, Virtual
  426. // Description: Generates the list of function prototypes
  427. // corresponding to the functions that will be output in
  428. // write_functions().
  429. ////////////////////////////////////////////////////////////////////
  430. void InterfaceMakerPythonNative::write_prototypes(ostream &out_code,ostream *out_h)
  431. {
  432. inside_python_native = true;
  433. Functions::iterator fi;
  434. if(out_h != NULL)
  435. *out_h << "#include \"py_panda.h\"\n\n";
  436. out_code << "//********************************************************************\n";
  437. out_code << "//*** prototypes for .. Glabal\n";
  438. out_code << "//********************************************************************\n";
  439. /*
  440. for (fi = _functions.begin(); fi != _functions.end(); ++fi)
  441. {
  442. Function *func = (*fi);
  443. if(!func->_itype.is_global() && isFunctionLegal(func))
  444. write_prototype_for(out_code, func);
  445. }
  446. */
  447. Objects::iterator oi;
  448. for (oi = _objects.begin(); oi != _objects.end(); ++oi)
  449. {
  450. Object *object = (*oi).second;
  451. if(object->_itype.is_class() ||object->_itype.is_struct())
  452. {
  453. if(isCppTypeLegal(object->_itype._cpptype))
  454. {
  455. if(isExportThisRun(object->_itype._cpptype))
  456. {
  457. write_prototypes_class(out_code,out_h,object);
  458. }
  459. else
  460. //write_prototypes_class_external(out_code,object);
  461. _external_imports.insert(make_safe_name(object->_itype.get_scoped_name()));
  462. }
  463. }
  464. }
  465. out_code << "//********************************************************************\n";
  466. out_code << "//*** prototypes for .. Extrernal Objects \n";
  467. out_code << "//********************************************************************\n";
  468. for(std::set< std::string >::iterator ii = _external_imports.begin(); ii != _external_imports.end(); ii++)
  469. out_code << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" <<*ii <<";\n";
  470. inside_python_native = false;
  471. }
  472. /////////////////////////////////////////////////////////////////////////////////////////////
  473. // Function : write_prototypes_class_external
  474. //
  475. // Description : Output enough enformation to a declartion of a externally
  476. // generated dtool type object
  477. /////////////////////////////////////////////////////////////////////////////////////////////
  478. void InterfaceMakerPythonNative::write_prototypes_class_external(ostream &out, Object * obj)
  479. {
  480. std::string class_name = make_safe_name(obj->_itype.get_scoped_name());
  481. std::string c_class_name = obj->_itype.get_true_name();
  482. out << "//********************************************************************\n";
  483. out << "//*** prototypes for external.. " << class_name <<"\n";
  484. out << "//********************************************************************\n";
  485. out << "typedef "<< c_class_name <<" "<< class_name <<"_localtype;\n";
  486. out << "Define_Module_Class_Forward("<< _def->module_name << ", "<< class_name << "," << class_name <<"_localtype,"<< classNameFromCppName(c_class_name) << ");\n";
  487. }
  488. ///////////////////////////////////////// ////////////////////////////////////////////////////
  489. // Function : write_prototypes_class
  490. //
  491. /////////////////////////////////////////////////////////////////////////////////////////////
  492. void InterfaceMakerPythonNative::write_prototypes_class(ostream &out_code,ostream *out_h, Object * obj)
  493. {
  494. std::string ClassName = make_safe_name(obj->_itype.get_scoped_name());
  495. Functions::iterator fi;
  496. out_code << "//********************************************************************\n";
  497. out_code << "//*** prototypes for .. " << ClassName <<"\n";
  498. out_code << "//********************************************************************\n";
  499. /*
  500. for (fi = obj->_methods.begin(); fi != obj->_methods.end(); ++fi)
  501. {
  502. Function *func = (*fi);
  503. write_prototype_for(out_code, func);
  504. }
  505. */
  506. /*
  507. for (fi = obj->_constructors.begin(); fi != obj->_constructors.end(); ++fi)
  508. {
  509. Function *func = (*fi);
  510. std::string fname = "int Dtool_Init_"+ClassName+"(PyObject *self, PyObject *args, PyObject *kwds)";
  511. write_prototype_for_name(out_code, func,fname);
  512. }
  513. */
  514. write_ClasseDeclarations(out_code,out_h,obj);
  515. }
  516. ////////////////////////////////////////////////////////////////////
  517. // Function: InterfaceMakerPythonNative::write_functions
  518. // Access: Public, Virtual
  519. // Description: Generates the list of functions that are appropriate
  520. // for this interface. This function is called *before*
  521. // write_prototypes(), above.
  522. ////////////////////////////////////////////////////////////////////
  523. void InterfaceMakerPythonNative::write_functions(ostream &out)
  524. {
  525. inside_python_native = true;
  526. out << "//********************************************************************\n";
  527. out << "//*** Functions for .. Global \n" ;
  528. out << "//********************************************************************\n";
  529. Functions::iterator fi;
  530. for (fi = _functions.begin(); fi != _functions.end(); ++fi)
  531. {
  532. Function *func = (*fi);
  533. if(!func->_itype.is_global() && isFunctionLegal(func))
  534. write_function_for_top(out, func,"");
  535. }
  536. Objects::iterator oi;
  537. for (oi = _objects.begin(); oi != _objects.end(); ++oi)
  538. {
  539. Object *object = (*oi).second;
  540. if(object->_itype.is_class() ||object->_itype.is_struct())
  541. {
  542. if(isCppTypeLegal(object->_itype._cpptype))
  543. if(isExportThisRun(object->_itype._cpptype))
  544. write_ClasseDetails(out,object);
  545. }
  546. }
  547. // Objects::iterator oi;
  548. for (oi = _objects.begin(); oi != _objects.end(); ++oi)
  549. {
  550. Object *object = (*oi).second;
  551. if(!object->_itype.get_outer_class())
  552. {
  553. if(object->_itype.is_class() ||object->_itype.is_struct())
  554. if(isCppTypeLegal(object->_itype._cpptype))
  555. if(isExportThisRun(object->_itype._cpptype))
  556. write_module_class(out,object);
  557. }
  558. }
  559. inside_python_native = true;
  560. }
  561. ////////////////////////////////////////////////////////////
  562. // Function : write_ClasseDetails
  563. ////////////////////////////////////////////////////////////
  564. void InterfaceMakerPythonNative::write_ClasseDetails(ostream &out, Object * obj)
  565. {
  566. Functions::iterator fi;
  567. //std::string cClassName = obj->_itype.get_scoped_name();
  568. std::string ClassName = make_safe_name(obj->_itype.get_scoped_name());
  569. std::string cClassName = obj->_itype.get_true_name();
  570. out << "//********************************************************************\n";
  571. out << "//*** Functions for .. "<< cClassName <<" \n" ;
  572. out << "//********************************************************************\n";
  573. for (fi = obj->_methods.begin(); fi != obj->_methods.end(); ++fi)
  574. {
  575. Function *func = (*fi);
  576. if( (func))
  577. {
  578. ostringstream GetThis;
  579. GetThis << " "<<cClassName << " * local_this = NULL;\n";
  580. GetThis << " DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<< ClassName<<",(void **)&local_this);\n";
  581. GetThis << " if(local_this == NULL) {\n";
  582. GetThis << " // This might happen if Python called a reverse operator like __rsub__().\n";
  583. GetThis << " Py_INCREF(Py_NotImplemented);\n";
  584. GetThis << " return Py_NotImplemented;\n";
  585. GetThis << " }\n";
  586. write_function_for_top(out, func,GetThis.str());
  587. }
  588. }
  589. // bool AnyLeganConstructors;
  590. if(obj->_constructors.size() == 0)
  591. {
  592. std::string fname = "int Dtool_Init_"+ClassName+"(PyObject *self, PyObject *args, PyObject *kwds)";
  593. out << fname << "\n";
  594. out << "{\n";
  595. out << " PyErr_SetString(PyExc_TypeError, \"Error Can Not Init Constant Class (" << cClassName << ")\");\n";
  596. out << " return -1;\n" ;
  597. out << "}\n";
  598. }
  599. else
  600. {
  601. for (fi = obj->_constructors.begin(); fi != obj->_constructors.end(); ++fi)
  602. {
  603. Function *func = (*fi);
  604. std::string fname = "int Dtool_Init_"+ClassName+"(PyObject *self, PyObject *args, PyObject *kwds) ";
  605. write_function_for_name(out, func,fname,"",ClassName);
  606. }
  607. }
  608. std::map< string ,CastDetails > details;
  609. std::map< string ,CastDetails >::iterator di;
  610. builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(obj->_itype._cpptype)),false);
  611. GetValideChildClasses(details,obj->_itype._cpptype->as_struct_type());
  612. for(di = details.begin(); di != details.end(); di++)
  613. {
  614. //InterrogateType ptype =idb->get_type(di->first);
  615. if(di->second._is_legal_py_class && !isExportThisRun(di->second._structType))
  616. _external_imports.insert(make_safe_name(di->second._to_class_name));
  617. //out << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(di->second._to_class_name) <<";\n";
  618. }
  619. { // the Cast Converter
  620. out << "inline void * Dtool_UpcastInterface_"<< ClassName << "(PyObject *self, Dtool_PyTypedObject *requested_type)\n";
  621. out << "{\n";
  622. out << " Dtool_PyTypedObject *SelfType = ((Dtool_PyInstDef *)self)->_My_Type;\n";
  623. out << " if(SelfType != &Dtool_" << ClassName <<")\n";
  624. out << " {\n";
  625. out << " printf(\""<<ClassName<<" ** Bad Source Type-- Requesting Conversion from %s to %s\\n\",((Dtool_PyInstDef *)self)->_My_Type->_name,requested_type->_name);fflush(NULL);\n";;
  626. out << " return NULL;\n";
  627. out << " }\n";
  628. out << " \n";
  629. out << " "<<cClassName<<" * local_this = (" << cClassName<<" *)((Dtool_PyInstDef *)self)->_ptr_to_object;\n";
  630. out << " if(requested_type == &Dtool_"<<ClassName<<")\n";
  631. out << " return local_this;\n";
  632. for(di = details.begin(); di != details.end(); di++)
  633. {
  634. if(di->second._is_legal_py_class)
  635. {
  636. out << " if(requested_type == &Dtool_"<<make_safe_name(di->second._to_class_name)<<")\n";
  637. out << " return "<< di->second._up_cast_string << " local_this;\n";
  638. }
  639. }
  640. out << " return NULL;\n";
  641. out << "}\n";
  642. out << "inline void * Dtool_DowncastInterface_"<< ClassName << "(void *from_this, Dtool_PyTypedObject *from_type)\n";
  643. out << "{\n";
  644. out << " if(from_this == NULL || from_type == NULL)\n";
  645. out << " return NULL;\n";
  646. out << " if(from_type == &Dtool_" << ClassName<<")\n";
  647. out << " return from_this;\n";
  648. for(di = details.begin(); di != details.end(); di++)
  649. {
  650. if(di->second._can_downcast && di->second._is_legal_py_class)
  651. {
  652. out << " if(from_type == &Dtool_"<<make_safe_name(di->second._to_class_name)<<")\n";
  653. out << " {\n";
  654. out << " "<< di->second._to_class_name << "* other_this = ("<< di->second._to_class_name << "*)from_this;\n" ;
  655. out << " return ("<< cClassName << "*)other_this;\n";
  656. out << " }\n";
  657. }
  658. }
  659. out << " return (void *) NULL;\n";
  660. out << "}\n";
  661. }
  662. }
  663. ////////////////////////////////////////////////////////////
  664. /// Function : write_ClasseDeclarations
  665. //
  666. //
  667. ////////////////////////////////////////////////////////////
  668. void InterfaceMakerPythonNative::write_ClasseDeclarations(ostream &out, ostream *out_h,Object * obj )
  669. {
  670. const InterrogateType &itype = obj->_itype;
  671. std::string class_name = make_safe_name(obj->_itype.get_scoped_name());
  672. std::string c_class_name = itype.get_true_name();
  673. std::string class_struct_name = std::string(CLASS_PREFEX) +class_name;
  674. out << "typedef "<< c_class_name <<" "<< class_name <<"_localtype;\n";
  675. if(obj->_itype.has_destructor() ||
  676. obj->_itype.destructor_is_inherited())
  677. {
  678. if(TypeManager::is_reference_count(obj->_itype._cpptype))
  679. {
  680. out << "Define_Module_ClassRef("<< _def->module_name<<"," << class_name << "," << class_name <<"_localtype,"<< classNameFromCppName(c_class_name) <<");\n";
  681. }
  682. else
  683. {
  684. out << "Define_Module_Class("<<_def->module_name << "," << class_name << "," <<class_name <<"_localtype,"<< classNameFromCppName(c_class_name) <<");\n";
  685. }
  686. }
  687. else
  688. {
  689. if(TypeManager::is_reference_count(obj->_itype._cpptype))
  690. {
  691. out << "Define_Module_ClassRef_Private("<<_def->module_name << "," << class_name << "," << class_name <<"_localtype,"<< classNameFromCppName(c_class_name) <<");\n";
  692. }
  693. else
  694. {
  695. out << "Define_Module_Class_Private("<<_def->module_name<< "," << class_name << "," << class_name <<"_localtype,"<< classNameFromCppName(c_class_name) << ");\n";
  696. }
  697. }
  698. if(out_h != NULL)
  699. *out_h << "extern \"C\" " << EXPORT_IMPORT_PREFEX << " struct Dtool_PyTypedObject Dtool_" << class_name <<";\n";
  700. }
  701. ////////////////////////////////////////////////////////////////////
  702. // Function: InterfaceMakerPythonNative::write_module
  703. // Access: Public, Virtual
  704. // Description: Generates whatever additional code is required to
  705. // support a module file.
  706. ////////////////////////////////////////////////////////////////////
  707. void InterfaceMakerPythonNative::write_sub_module(ostream &out, Object *obj)
  708. {
  709. //Object * obj = _objects[_embeded_index] ;
  710. std::string ClassName = make_safe_name(obj->_itype.get_scoped_name());
  711. out << "//********************************************************************\n";
  712. out << "//*** Module Init Updcall .." << obj->_itype.get_scoped_name() << "\n";
  713. out << "//********************************************************************\n";
  714. out << " Dtool_PyModuleClassInit_"<< ClassName <<"(module);\n";
  715. }
  716. /////////////////////////////////////////////////////////////////////////////
  717. // Function : write_module_support
  718. /////////////////////////////////////////////////////////////////////////////
  719. void InterfaceMakerPythonNative::write_module_support(ostream &out,ostream *out_h,InterrogateModuleDef *moduledefdef)
  720. {
  721. out << "//********************************************************************\n";
  722. out << "//*** Module Object Linker .. \n";
  723. out << "//********************************************************************\n";
  724. out << "static void BuildInstants(PyObject * module)\n";
  725. out << "{\n";
  726. Objects::iterator oi;
  727. for (oi = _objects.begin(); oi != _objects.end(); ++oi)
  728. {
  729. Object *object = (*oi).second;
  730. if(!object->_itype.get_outer_class())
  731. {
  732. if(object->_itype.is_enum())
  733. {
  734. int enum_count = object->_itype.number_of_enum_values();
  735. if(enum_count > 0)
  736. {
  737. out << "//********************************************************************\n";
  738. out << "//*** Module Enums .." << object->_itype.get_scoped_name() << "\n";
  739. out << "//********************************************************************\n";
  740. }
  741. for(int xx = 0; xx< enum_count; xx++)
  742. out << " PyModule_AddIntConstant(module,\"" << classNameFromCppName(object->_itype.get_enum_value_name(xx)) <<"\","<< object->_itype.get_enum_value(xx) << ");\n";
  743. }
  744. }
  745. }
  746. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  747. int num_manifests = idb->get_num_global_manifests();
  748. for (int mi = 0; mi < num_manifests; mi++)
  749. {
  750. ManifestIndex manifest_index = idb->get_global_manifest(mi);
  751. const InterrogateManifest &iman = idb->get_manifest(manifest_index);
  752. if (iman.has_getter())
  753. {
  754. FunctionIndex func_index = iman.get_getter();
  755. record_function(dummy_type, func_index);
  756. }
  757. if(iman.has_int_value())
  758. out << " PyModule_AddIntConstant(module,\"" << classNameFromCppName(iman.get_name()) <<"\","<< iman.get_int_value() << ");\n";
  759. else
  760. out << " PyModule_AddStringConstant(module,\"" << classNameFromCppName(iman.get_name()) <<"\",\""<< iman.get_definition().c_str() << "\");\n";
  761. }
  762. for (oi = _objects.begin(); oi != _objects.end(); ++oi)
  763. {
  764. Object *object = (*oi).second;
  765. if(!object->_itype.get_outer_class())
  766. {
  767. if(object->_itype.is_class() ||object->_itype.is_struct())
  768. if(isCppTypeLegal(object->_itype._cpptype))
  769. if(isExportThisRun(object->_itype._cpptype))
  770. write_sub_module(out,object);
  771. }
  772. }
  773. out << "//********************************************************************\n";
  774. out << "//*** Module Init Updcall .. Externally Defined Class\n";
  775. out << "//********************************************************************\n";
  776. // for(std::set< std::string >::iterator ii = _external_imports.begin(); ii != _external_imports.end(); ii++)
  777. // out << "Dtool_" <<*ii <<"._Dtool_ClassInit(NULL);\n";
  778. out << "}\n";
  779. bool force_base_functions = true;
  780. out << "static PyMethodDef python_simple_funcs[] = {\n";
  781. Functions::iterator fi;
  782. for (fi = _functions.begin(); fi != _functions.end(); ++fi)
  783. {
  784. Function *func = (*fi);
  785. if(!func->_itype.is_global() && isFunctionLegal(func))
  786. {
  787. {
  788. out << " { \"" << methodNameFromCppName(func,"") << "\", (PyCFunction) &"
  789. << func->_name << ", METH_VARARGS| METH_KEYWORDS ," << func->_name << "_comment},\n";
  790. }
  791. }
  792. }
  793. if(force_base_functions)
  794. {
  795. out << " //Support Function For Dtool_types ... for know in each module ??\n";
  796. out << " {\"Dtool_BarrowThisRefrence\", &Dtool_BarrowThisRefrence,METH_VARARGS,\"Used to barrow 'this' poiner ( to, from)\\n Assumes no ownership\"}, \n";
  797. out << " {\"Dtool_AddToDictionary\", &Dtool_AddToDictionary,METH_VARARGS,\"Used to Items Into a types (tp_dict)\"}, \n";
  798. }
  799. out << " { NULL, NULL ,0,NULL}\n" << "};\n\n";
  800. out << "struct LibrayDef " << moduledefdef->library_name <<"_moddef = {python_simple_funcs,BuildInstants};\n";
  801. if(out_h != NULL)
  802. *out_h << "extern struct LibrayDef " << moduledefdef->library_name <<"_moddef;\n";
  803. }
  804. /////////////////////////////////////////////////////////////////////////////
  805. ///// Function : write_module
  806. /////////////////////////////////////////////////////////////////////////////
  807. void InterfaceMakerPythonNative::write_module(ostream &out,ostream *out_h, InterrogateModuleDef *moduledefdef)
  808. {
  809. InterfaceMakerPython::write_module(out,out_h, moduledefdef);
  810. Objects::iterator oi;
  811. out << "//********************************************************************\n";
  812. out << "//*** Py Init Code For .. GlobalScope\n" ;
  813. out << "//********************************************************************\n";
  814. out << "#ifdef _WIN32\n"
  815. << "extern \"C\" __declspec(dllexport) void init" << moduledefdef->module_name << "();\n"
  816. << "#else\n"
  817. << "extern \"C\" void init" << moduledefdef->module_name << "();\n"
  818. << "#endif\n\n";
  819. out << "void init" << moduledefdef->module_name << "() {\n";
  820. out << " LibrayDef * refs[] = {&" << moduledefdef->library_name << "_moddef,NULL};\n";
  821. out << " Dtool_PyModuleInitHelper(refs,\"" << moduledefdef->module_name << "\");\n";
  822. out << "}\n\n";
  823. }
  824. ////////////////////////////////////////////////////////////////////
  825. /// Function : GetSlotedFunctinDef
  826. //
  827. // This function is used to define special behavior for class functions..
  828. // main use is to encode the slot pointer logic and function call
  829. // conventions for the slaot interface..
  830. ////////////////////////////////////////////////////////////////////
  831. bool GetSlotedFunctinDef(const std::string &thimputstring, std::string &answer_location, int &wraper_type)
  832. {
  833. wraper_type = -1;
  834. if(thimputstring.size() > 4 && thimputstring[0] == '_' && thimputstring[1] == '_')
  835. {
  836. if(thimputstring == "__add__")
  837. {
  838. answer_location = "tp_as_number->nb_add";
  839. wraper_type = 3;
  840. return true;
  841. }
  842. if(thimputstring == "__sub__")
  843. {
  844. answer_location = "tp_as_number->nb_subtract";
  845. wraper_type = 3;
  846. return true;
  847. }
  848. if(thimputstring == "__neg__")
  849. {
  850. answer_location = "tp_as_number->nb_negative";
  851. wraper_type = 2;
  852. return true;
  853. }
  854. if(thimputstring == "__mul__")
  855. {
  856. answer_location = "tp_as_number->nb_multiply";
  857. wraper_type = 3;
  858. return true;
  859. }
  860. if(thimputstring == "__div__")
  861. {
  862. answer_location = "tp_as_number->nb_divide";
  863. wraper_type = 3;
  864. return true;
  865. }
  866. if(thimputstring == "__mod__")
  867. {
  868. answer_location = "tp_as_number->nb_remainder";
  869. wraper_type = 3;
  870. return true;
  871. }
  872. if(thimputstring == "__lshift__")
  873. {
  874. answer_location = "tp_as_number->nb_lshift";
  875. wraper_type = 3;
  876. return true;
  877. }
  878. if(thimputstring == "__rshift__")
  879. {
  880. answer_location = "tp_as_number->nb_rshift";
  881. wraper_type = 3;
  882. return true;
  883. }
  884. if(thimputstring == "__xor__")
  885. {
  886. answer_location = "tp_as_number->nb_xor";
  887. wraper_type = 3;
  888. return true;
  889. }
  890. if(thimputstring == "__invert__")
  891. {
  892. answer_location = "tp_as_number->nb_invert";
  893. wraper_type = 2;
  894. return true;
  895. }
  896. if(thimputstring == "__and__")
  897. {
  898. answer_location = "tp_as_number->nb_and";
  899. wraper_type = 3;
  900. return true;
  901. }
  902. if(thimputstring == "__or__")
  903. {
  904. answer_location = "tp_as_number->nb_or";
  905. wraper_type = 3;
  906. return true;
  907. }
  908. if(thimputstring == "__iadd__")
  909. {
  910. answer_location = "tp_as_number->nb_inplace_add";
  911. wraper_type = 3;
  912. return true;
  913. }
  914. if(thimputstring == "__isub__")
  915. {
  916. answer_location = "tp_as_number->nb_inplace_subtract";
  917. wraper_type = 3;
  918. return true;
  919. }
  920. if(thimputstring == "__imul__")
  921. {
  922. answer_location = "tp_as_number->nb_inplace_multiply";
  923. wraper_type = 3;
  924. return true;
  925. }
  926. if(thimputstring == "__idiv__")
  927. {
  928. answer_location = "tp_as_number->nb_inplace_divide";
  929. wraper_type = 3;
  930. return true;
  931. }
  932. if(thimputstring == "__imod__")
  933. {
  934. answer_location = ".tp_as_number->nb_inplace_remainder";
  935. wraper_type = 3;
  936. return true;
  937. }
  938. if(thimputstring == "__ilshift__")
  939. {
  940. answer_location = "tp_as_number->nb_inplace_lshift";
  941. wraper_type = 3;
  942. return true;
  943. }
  944. if(thimputstring == "__irshift__")
  945. {
  946. answer_location = "tp_as_number->nb_inplace_rshift";
  947. wraper_type = 3;
  948. return true;
  949. }
  950. if(thimputstring == "__iand__")
  951. {
  952. answer_location = "tp_as_number->nb_inplace_and";
  953. wraper_type = 3;
  954. return true;
  955. }
  956. if(thimputstring == "__ixor__")
  957. {
  958. answer_location = "tp_as_number->nb_inplace_xor";
  959. wraper_type = 3;
  960. return true;
  961. }
  962. if(thimputstring == "__int__")
  963. {
  964. answer_location = "tp_as_number->nb_int";
  965. wraper_type = 2;
  966. return true;
  967. }
  968. // if(thimputstring == "__coerce__")
  969. // {
  970. // answer_location = "tp_as_number->nb_coerce";
  971. // return true;
  972. // }
  973. // mapping methods
  974. if(thimputstring == "__getitem__")
  975. {
  976. answer_location = "tp_as_mapping->mp_subscript";
  977. wraper_type = 3;
  978. return true;
  979. }
  980. //Direct methods
  981. if(thimputstring == "__call__")
  982. {
  983. answer_location = "tp_call";
  984. //wraper_type = 1;
  985. return true;
  986. }
  987. }
  988. return false;
  989. };
  990. /////////////////////////////////////////////////////////////////////////////////////////////
  991. // Function :write_module_class
  992. /////////////////////////////////////////////////////////////////////////////////////////////
  993. void InterfaceMakerPythonNative::
  994. write_module_class(ostream &out, Object *obj) {
  995. bool has_local_hash = false;
  996. bool has_local_repr = false;
  997. bool has_local_str = false;
  998. {
  999. int num_nested = obj->_itype.number_of_nested_types();
  1000. for (int ni = 0; ni < num_nested; ni++)
  1001. {
  1002. TypeIndex nested_index = obj->_itype.get_nested_type(ni);
  1003. Object * nested_obj = _objects[nested_index];
  1004. if(nested_obj->_itype.is_class() ||nested_obj->_itype.is_struct())
  1005. {
  1006. write_module_class(out,nested_obj);
  1007. }
  1008. }
  1009. }
  1010. bool is_runtime_typed = IsPandaTypedObject(obj->_itype._cpptype->as_struct_type());
  1011. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  1012. std::string ClassName = make_safe_name(obj->_itype.get_scoped_name());
  1013. std::string cClassName = obj->_itype.get_true_name();
  1014. std::string export_calss_name = classNameFromCppName(obj->_itype.get_name());
  1015. Functions::iterator fi;
  1016. out << "//********************************************************************\n";
  1017. out << "//*** Py Init Code For .. "<< ClassName <<" | " << export_calss_name <<"\n" ;
  1018. out << "//********************************************************************\n";
  1019. out << "PyMethodDef Dtool_Methods_"<< ClassName << "[]= {\n";
  1020. std::map<int , Function * > static_functions;
  1021. std::map<Function *, std::string > normal_Operator_functions;
  1022. std::map<Function *, std::pair< std::string , int> > wraped_Operator_functions;
  1023. // function Table
  1024. int x;
  1025. for (x = 0, fi = obj->_methods.begin(); fi != obj->_methods.end(); ++fi,x++)
  1026. {
  1027. Function *func = (*fi);
  1028. std::string temp0;
  1029. int temp1;
  1030. if(!GetSlotedFunctinDef( methodNameFromCppName(func,export_calss_name),temp0,temp1))
  1031. {
  1032. out << " { \"" << methodNameFromCppName(func,export_calss_name) << "\",(PyCFunction ) &"
  1033. << func->_name << ", METH_VARARGS| METH_KEYWORDS ," << func->_name << "_comment},\n";
  1034. if(!isFunctionWithThis(func))
  1035. static_functions[x] = func;
  1036. }
  1037. else
  1038. {
  1039. if(temp1 > 0)
  1040. {
  1041. wraped_Operator_functions[func] = std::pair< std::string, int>(temp0,temp1);
  1042. out << " { \"" << methodNameFromCppName(func,export_calss_name) << "\",(PyCFunction ) &"
  1043. << func->_name << ", METH_VARARGS| METH_KEYWORDS ," << func->_name << "_comment},\n";
  1044. if(!isFunctionWithThis(func))
  1045. static_functions[x] = func;
  1046. }
  1047. else
  1048. {
  1049. normal_Operator_functions[func] = temp0;
  1050. out << " { \"" << methodNameFromCppName(func,export_calss_name) << "\",(PyCFunction ) &"
  1051. << func->_name << ", METH_VARARGS| METH_KEYWORDS ," << func->_name << "_comment},\n";
  1052. if(!isFunctionWithThis(func))
  1053. static_functions[x] = func;
  1054. }
  1055. }
  1056. }
  1057. out << " { NULL, NULL }\n"
  1058. << "};\n\n";
  1059. int num_derivations = obj->_itype.number_of_derivations();
  1060. int di;
  1061. for (di = 0; di < num_derivations; di++)
  1062. {
  1063. TypeIndex d_type_Index = obj->_itype.get_derivation(di);
  1064. if(!interrogate_type_is_unpublished(d_type_Index))
  1065. {
  1066. const InterrogateType &d_itype = idb->get_type(d_type_Index);
  1067. if(isCppTypeLegal(d_itype._cpptype))
  1068. {
  1069. if(!isExportThisRun(d_itype._cpptype))
  1070. {
  1071. _external_imports.insert(make_safe_name(d_itype.get_scoped_name().c_str()));
  1072. //out << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(d_itype.get_scoped_name().c_str()) <<";\n";
  1073. }
  1074. }
  1075. }
  1076. }
  1077. std::vector< std::string > bases;
  1078. for (di = 0; di < num_derivations; di++)
  1079. {
  1080. TypeIndex d_type_Index = obj->_itype.get_derivation(di);
  1081. if(!interrogate_type_is_unpublished(d_type_Index))
  1082. {
  1083. const InterrogateType &d_itype = idb->get_type(d_type_Index);
  1084. if(isCppTypeLegal(d_itype._cpptype))
  1085. {
  1086. bases.push_back(make_safe_name(d_itype.get_scoped_name().c_str()));
  1087. }
  1088. }
  1089. }
  1090. if(bases.empty())
  1091. bases.push_back("DTOOL_SUPPER_BASE");
  1092. {
  1093. std::map<Function *, std::pair< std::string , int> >::iterator rfi; // wraped_Operator_functions;
  1094. for(rfi = wraped_Operator_functions.begin(); rfi != wraped_Operator_functions.end(); rfi++)
  1095. {
  1096. if(rfi->second.second == 1)
  1097. {
  1098. Function *func = rfi->first;
  1099. out << "//////////////////\n";
  1100. out << "// Required TO Convert the calling Conventions.. \n";
  1101. out << "// " <<ClassName<< " ..." << rfi->second.first <<" = "<< methodNameFromCppName(func,export_calss_name) <<"\n";
  1102. out << "//////////////////\n";
  1103. out << "static PyObject * " << func->_name << methodNameFromCppName(func,export_calss_name) << "( PyObject * self, PyObject * args, PyObject *dict)\n";
  1104. out << "{\n";
  1105. out << " return "<< func->_name <<"(self,args);\n";
  1106. out << "}\n\n";
  1107. }
  1108. else if(rfi->second.second == 2)
  1109. {
  1110. Function *func = rfi->first;
  1111. out << "//////////////////\n";
  1112. out << "// Required TO Convert the calling Conventions.. \n";
  1113. out << "// " <<ClassName<< " ..." << rfi->second.first <<" = "<< methodNameFromCppName(func,export_calss_name) <<"\n";
  1114. out << "//////////////////\n";
  1115. out << "static PyObject * " << func->_name << methodNameFromCppName(func,export_calss_name) << "( PyObject * self)\n";
  1116. out << "{\n";
  1117. out << " return "<< func->_name <<"(self,Py_None,Py_None);\n";
  1118. out << "}\n\n";
  1119. }
  1120. if(rfi->second.second == 3)
  1121. {
  1122. Function *func = rfi->first;
  1123. out << "//////////////////\n";
  1124. out << "// Required TO Convert the calling Conventions.. \n";
  1125. out << "// " <<ClassName<< " ..." << rfi->second.first <<" = "<< methodNameFromCppName(func,export_calss_name) <<"\n";
  1126. out << "//////////////////\n";
  1127. out << "static PyObject * " << func->_name << methodNameFromCppName(func,export_calss_name) << "( PyObject * self, PyObject * args)\n";
  1128. out << "{\n";
  1129. out << " return "<< func->_name <<"(self,args,Py_None);\n";
  1130. out << "}\n\n";
  1131. }
  1132. }
  1133. if(HasAGetKeyFunction(obj->_itype))
  1134. {
  1135. out << "//////////////////\n";
  1136. out << "// A LocalHash(getKey) Function for this type";
  1137. out << "// " <<ClassName << "\n";
  1138. out << "//////////////////\n";
  1139. out << "static long DTool_HashKey_"<<ClassName << "(PyObject * self)\n";
  1140. out << "{\n";
  1141. out << " "<<cClassName << " * local_this = NULL;\n";
  1142. out << " DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<< ClassName<<",(void **)&local_this);\n";
  1143. out << " if(local_this == NULL)\n";
  1144. out << " {\n";
  1145. out << " return -1;\n";
  1146. out << " };\n";
  1147. out << " return local_this->get_key();\n";
  1148. out << "}\n\n";
  1149. has_local_hash = true;
  1150. }
  1151. else
  1152. {
  1153. if(bases.size() == 0)
  1154. {
  1155. out << "//////////////////\n";
  1156. out << "// A LocalHash(This Pointer) Function for this type";
  1157. out << "// " <<ClassName << "\n";
  1158. out << "//////////////////\n";
  1159. out << "static long DTool_HashKey_"<<ClassName << "(PyObject * self)\n";
  1160. out << "{\n";
  1161. out << " "<<cClassName << " * local_this = NULL;\n";
  1162. out << " DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<< ClassName<<",(void **)&local_this);\n";
  1163. out << " if(local_this == NULL)\n";
  1164. out << " {\n";
  1165. out << " return -1;\n";
  1166. out << " };\n";
  1167. out << " return (long)local_this;\n";
  1168. out << "}\n\n";
  1169. has_local_hash = true;
  1170. }
  1171. }
  1172. int need_repr = NeedsAReprFunction(obj->_itype);
  1173. if(need_repr > 0)
  1174. {
  1175. out << "//////////////////\n";
  1176. out << "// A __repr__ Function\n";
  1177. out << "// " <<ClassName << "\n";
  1178. out << "//////////////////\n";
  1179. out << "static PyObject * Dtool_Repr_"<<ClassName << "(PyObject * self)\n";
  1180. out << "{\n";
  1181. out << " "<<cClassName << " * local_this = NULL;\n";
  1182. out << " DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<< ClassName<<",(void **)&local_this);\n";
  1183. out << " if(local_this != NULL)\n";
  1184. out << " {\n";
  1185. out << " ostringstream os;\n";
  1186. if (need_repr == 2) {
  1187. out << " local_this->output(os);\n";
  1188. } else {
  1189. out << " local_this->python_repr(os, \""
  1190. << classNameFromCppName(ClassName) << "\");\n";
  1191. }
  1192. out << " std::string ss = os.str();\n";
  1193. out << " return PyString_FromStringAndSize(ss.data(),ss.length());\n";
  1194. out << " };\n";
  1195. out << " return Py_BuildValue(\"\");\n";
  1196. out << "}\n";
  1197. has_local_repr = true;
  1198. }
  1199. int need_str = NeedsAStrFunction(obj->_itype);
  1200. if(need_str > 0)
  1201. {
  1202. out << "//////////////////\n";
  1203. out << "// A __str__ Function\n";
  1204. out << "// " <<ClassName << "\n";
  1205. out << "//////////////////\n";
  1206. out << "static PyObject * Dtool_Str_"<<ClassName << "(PyObject * self)\n";
  1207. out << "{\n";
  1208. out << " "<<cClassName << " * local_this = NULL;\n";
  1209. out << " DTOOL_Call_ExtractThisPointerForType(self,&Dtool_"<< ClassName<<",(void **)&local_this);\n";
  1210. out << " if(local_this != NULL)\n";
  1211. out << " {\n";
  1212. out << " ostringstream os;\n";
  1213. if(need_str == 2)
  1214. out << " local_this->write(os,0);\n";
  1215. else
  1216. out << " local_this->write(os);\n";
  1217. out << " std::string ss = os.str();\n";
  1218. out << " return PyString_FromStringAndSize(ss.data(),ss.length());\n";
  1219. out << " };\n";
  1220. out << " return Py_BuildValue(\"\");\n";
  1221. out << "}\n";
  1222. has_local_str = true;
  1223. }
  1224. }
  1225. out << "void Dtool_PyModuleClassInit_" << ClassName << "(PyObject *module)\n";
  1226. out << "{\n";
  1227. out << " static bool initdone = false;\n";
  1228. out << " if(!initdone)\n";
  1229. out << " {\n";
  1230. out << " initdone = true;\n";
  1231. // out << " memset(Dtool_"<< ClassName << ".As_PyTypeObject().tp_as_number,0,sizeof(PyNumberMethods));\n";
  1232. // out << " memset(Dtool_"<< ClassName << ".As_PyTypeObject().tp_as_mapping,0,sizeof(PyMappingMethods));\n";
  1233. // out << " static Dtool_PyTypedObject *InheritsFrom[] = {";
  1234. // add doc string
  1235. if (obj->_itype.has_comment()) {
  1236. out << "#ifndef NDEBUG\n";
  1237. out << " // Class documentation string\n";
  1238. out << " Dtool_" << ClassName
  1239. << ".As_PyTypeObject().tp_doc =\n";
  1240. output_quoted(out, 10, obj->_itype.get_comment());
  1241. out << ";\n"
  1242. << "#endif\n";
  1243. }
  1244. // add bases///
  1245. if(bases.size() > 0)
  1246. {
  1247. out << " // Dependent Objects \n";
  1248. std::string format1= "";
  1249. std::string format2= "";
  1250. for(std::vector< std::string >::iterator bi = bases.begin(); bi != bases.end(); bi++)
  1251. {
  1252. format1 += "O";
  1253. format2 += ",&Dtool_" + *bi + ".As_PyTypeObject()";
  1254. out << " Dtool_"<< make_safe_name(*bi) << "._Dtool_ClassInit(NULL);\n";
  1255. }
  1256. out << " Dtool_"<<ClassName<<".As_PyTypeObject().tp_bases = Py_BuildValue(\"(" << format1 << ")\""<< format2 << ");\n";
  1257. }
  1258. // get dictionary
  1259. out << " Dtool_" << ClassName << ".As_PyTypeObject().tp_dict = PyDict_New();\n";
  1260. out << " PyDict_SetItemString(Dtool_"<<ClassName <<".As_PyTypeObject().tp_dict,\"DtoolClassDict\",Dtool_"<<ClassName <<".As_PyTypeObject().tp_dict);\n";
  1261. // the standard call functions
  1262. std::map<Function *, std::string >::iterator ofi;
  1263. for(ofi = normal_Operator_functions.begin(); ofi != normal_Operator_functions.end(); ofi++)
  1264. {
  1265. Function *func = ofi->first;
  1266. out << " // " << ofi->second <<" = "<< methodNameFromCppName(func,export_calss_name) <<"\n";
  1267. out << " Dtool_" << ClassName <<".As_PyTypeObject()." << ofi->second <<" = &" << func->_name <<";\n";
  1268. }
  1269. // wraped functions...
  1270. {
  1271. std::map<Function *, std::pair< std::string , int> >::iterator rfi; // wraped_Operator_functions;
  1272. for(rfi = wraped_Operator_functions.begin(); rfi != wraped_Operator_functions.end(); rfi++)
  1273. {
  1274. Function *func = rfi->first;
  1275. out << " // " << rfi->second.first <<" = "<< methodNameFromCppName(func,export_calss_name) <<"\n";
  1276. out << " Dtool_" << ClassName <<".As_PyTypeObject()." << rfi->second.first <<" = &" << func->_name << methodNameFromCppName(func,export_calss_name)<<";\n";
  1277. }
  1278. }
  1279. // compare and hash work together in PY inherit behavior hmm grrr
  1280. // __hash__
  1281. if(has_local_hash == true)
  1282. {
  1283. out << " // __hash__\n";
  1284. out << " Dtool_" << ClassName <<".As_PyTypeObject().tp_hash = &DTool_HashKey_"<<ClassName <<";\n";
  1285. out << " Dtool_" << ClassName <<".As_PyTypeObject().tp_compare = &DTOOL_PyObject_Compare;\n";
  1286. }
  1287. if(has_local_repr == true)
  1288. {
  1289. out << " // __repr__\n";
  1290. out << " Dtool_" << ClassName <<".As_PyTypeObject().tp_repr = & Dtool_Repr_"<<ClassName <<";\n";
  1291. }
  1292. if(has_local_str == true)
  1293. {
  1294. out << " // __str__\n";
  1295. out << " Dtool_" << ClassName <<".As_PyTypeObject().tp_str = & Dtool_Str_"<<ClassName <<";\n";
  1296. }
  1297. else if(has_local_repr == true)
  1298. {
  1299. out << " // __str__ Repr Proxy\n";
  1300. out << " Dtool_" << ClassName <<".As_PyTypeObject().tp_str = & Dtool_Repr_"<<ClassName <<";\n";
  1301. }
  1302. int num_nested = obj->_itype.number_of_nested_types();
  1303. for (int ni = 0; ni < num_nested; ni++)
  1304. {
  1305. TypeIndex nested_index = obj->_itype.get_nested_type(ni);
  1306. Object * nested_obj = _objects[nested_index];
  1307. if(nested_obj->_itype.is_class() ||nested_obj->_itype.is_struct())
  1308. {
  1309. std::string ClassName1 = make_safe_name(nested_obj->_itype.get_scoped_name());
  1310. std::string ClassName2 = make_safe_name(nested_obj->_itype.get_name());
  1311. out << " // Nested Object "<< ClassName1 << ";\n";
  1312. out << " Dtool_" << ClassName1 << "._Dtool_ClassInit(NULL);\n";
  1313. out << " PyDict_SetItemString(Dtool_" << ClassName << ".As_PyTypeObject().tp_dict,\"" << classNameFromCppName(ClassName2) <<"\",(PyObject *)&Dtool_" << ClassName1 << ".As_PyTypeObject());\n";
  1314. }
  1315. else
  1316. {
  1317. if(nested_obj->_itype.is_enum())
  1318. {
  1319. out << " // Enum "<< nested_obj->_itype.get_scoped_name() << ";\n";
  1320. int enum_count = nested_obj->_itype.number_of_enum_values();
  1321. for(int xx = 0; xx< enum_count; xx++)
  1322. out << " PyDict_SetItemString(Dtool_" << ClassName << ".As_PyTypeObject().tp_dict,\"" << classNameFromCppName(nested_obj->_itype.get_enum_value_name(xx)) <<"\",PyInt_FromLong("<< nested_obj->_itype.get_enum_value(xx) << "));\n";
  1323. }
  1324. }
  1325. }
  1326. out << " if(PyType_Ready(&Dtool_"<< ClassName << ".As_PyTypeObject()) < 0)\n";
  1327. out << " {\n";
  1328. out << " PyErr_SetString(PyExc_TypeError, \"PyType_Ready("<< ClassName << ")\");\n";
  1329. out << " printf(\" Error In PyType_Ready" << ClassName << "\");\n";
  1330. out << " return;\n";
  1331. out << " }\n";
  1332. out << " Py_INCREF(&Dtool_"<< ClassName << ".As_PyTypeObject());\n";
  1333. out << " PyDict_SetItemString(Dtool_"<<ClassName <<".As_PyTypeObject().tp_dict,\""<<export_calss_name<< "\",&Dtool_"<<ClassName <<".As_PyObject());\n";
  1334. // static function into dictionary with bogus self..
  1335. //
  1336. std::map<int , Function * >::iterator sfi;
  1337. for(sfi= static_functions.begin(); sfi != static_functions.end(); sfi++)
  1338. {
  1339. out << " // Static Method " << methodNameFromCppName(sfi->second,export_calss_name) << "\n";
  1340. out << " PyDict_SetItemString(Dtool_" << ClassName << ".As_PyTypeObject().tp_dict,\"" ;
  1341. out << methodNameFromCppName(sfi->second,export_calss_name) ;
  1342. out << "\",PyCFunction_New(&Dtool_Methods_"<< ClassName <<"[" << sfi->first << "],&Dtool_"<< ClassName<< ".As_PyObject()));\n";
  1343. }
  1344. if(is_runtime_typed)
  1345. out << " RegisterRuntimeClass(&Dtool_"<<ClassName<<","<< cClassName <<"::get_class_type().get_index());\n";
  1346. else
  1347. out << " RegisterRuntimeClass(&Dtool_"<<ClassName<<",-1);\n";
  1348. out << " }\n";
  1349. out << " if(module != NULL)\n";
  1350. out << " {\n";
  1351. out << " Py_INCREF(&Dtool_"<< ClassName << ".As_PyTypeObject());\n";
  1352. out << " PyModule_AddObject(module, \""<<export_calss_name<<"\",(PyObject *)&Dtool_"<< ClassName << ".As_PyTypeObject());\n";
  1353. out << " }\n";
  1354. out << "}\n";
  1355. }
  1356. ////////////////////////////////////////////////////////////////////
  1357. // Function: InterfaceMakerPythonNative::synthesize_this_parameter
  1358. // Access: Public, Virtual
  1359. // Description: This method should be overridden and redefined to
  1360. // return true for interfaces that require the implicit
  1361. // "this" parameter, if present, to be passed as the
  1362. // first parameter to any wrapper functions.
  1363. ////////////////////////////////////////////////////////////////////
  1364. bool InterfaceMakerPythonNative::
  1365. synthesize_this_parameter() {
  1366. return true;
  1367. }
  1368. ////////////////////////////////////////////////////////////////////
  1369. // Function: InterfaceMakerPythonNative::get_wrapper_prefix
  1370. // Access: Protected, Virtual
  1371. // Description: Returns the prefix string used to generate wrapper
  1372. // function names.
  1373. ////////////////////////////////////////////////////////////////////
  1374. string InterfaceMakerPythonNative::
  1375. get_wrapper_prefix() {
  1376. return "Dtool_";
  1377. }
  1378. ////////////////////////////////////////////////////////////////////
  1379. // Function: InterfaceMakerPythonNative::get_unique_prefix
  1380. // Access: Protected, Virtual
  1381. // Description: Returns the prefix string used to generate unique
  1382. // symbolic names, which are not necessarily C-callable
  1383. // function names.
  1384. ////////////////////////////////////////////////////////////////////
  1385. string InterfaceMakerPythonNative::get_unique_prefix()
  1386. {
  1387. return "Dtool_";
  1388. }
  1389. ////////////////////////////////////////////////////////////////////
  1390. // Function: InterfaceMakerPythonNative::record_function_wrapper
  1391. // Access: Protected, Virtual
  1392. // Description: Associates the function wrapper with its function in
  1393. // the appropriate structures in the database.
  1394. ////////////////////////////////////////////////////////////////////
  1395. void InterfaceMakerPythonNative::record_function_wrapper(InterrogateFunction &ifunc, FunctionWrapperIndex wrapper_index)
  1396. {
  1397. ifunc._python_wrappers.push_back(wrapper_index);
  1398. }
  1399. ////////////////////////////////////////////////////////////////////
  1400. // Function: InterfaceMakerPythonNative::write_prototype_for
  1401. // Access: Private
  1402. // Description: Writes the prototype for the indicated function.
  1403. ////////////////////////////////////////////////////////////////////
  1404. void InterfaceMakerPythonNative::write_prototype_for(ostream &out, InterfaceMaker::Function *func)
  1405. {
  1406. std::string fname = "PyObject *"+func->_name+"(PyObject *self, PyObject *args)";
  1407. write_prototype_for_name(out,func,fname);
  1408. }
  1409. ////////////////////////////////////////////////////////////////////
  1410. ////////////////////////////////////////////////////////////////////
  1411. ////////////////////////////////////////////////////////////////////
  1412. void InterfaceMakerPythonNative::write_prototype_for_name(ostream &out, InterfaceMaker::Function *func, const std::string &function_namename)
  1413. {
  1414. Function::Remaps::const_iterator ri;
  1415. // for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri) {
  1416. // FunctionRemap *remap = (*ri);
  1417. if (!output_function_names) {
  1418. // If we're not saving the function names, don't export it from
  1419. // the library.
  1420. out << "static ";
  1421. } else {
  1422. out << "extern \"C\" ";
  1423. }
  1424. out << function_namename <<";\n";
  1425. // }
  1426. }
  1427. ////////////////////////////////////////////////////////////////////
  1428. // Function: InterfaceMakerPythonNative::write_function_for
  1429. // Access: Private
  1430. // Description: Writes the definition for a function that will call
  1431. // the indicated C++ function or method.
  1432. ////////////////////////////////////////////////////////////////////
  1433. void InterfaceMakerPythonNative::write_function_for_top(ostream &out, InterfaceMaker::Function *func, const std::string &PreProcess)
  1434. {
  1435. std::string fname = "static PyObject *"+func->_name+"(PyObject *self, PyObject *args,PyObject *kwds)";
  1436. write_function_for_name(out,func,fname,PreProcess,"");
  1437. }
  1438. ////////////////////////////////////////////////////////////////////
  1439. /// Function : write_function_for_name
  1440. //
  1441. // Wrap a complete name override function for Py.....
  1442. ////////////////////////////////////////////////////////////////////
  1443. void InterfaceMakerPythonNative::
  1444. write_function_for_name(ostream &out1, InterfaceMaker::Function *func,
  1445. const std::string &function_name,
  1446. const std::string &PreProcess,
  1447. const std::string &ClassName) {
  1448. ostringstream forward_decl;
  1449. ostringstream out;
  1450. std::map<int , std::set<FunctionRemap *> > MapSets;
  1451. std::map<int , std::set<FunctionRemap *> >::iterator mii;
  1452. std::set<FunctionRemap *>::iterator sii;
  1453. Function::Remaps::const_iterator ri;
  1454. out1 << "/******************************************************************\n" << " * Python type method wrapper for\n";
  1455. for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri) {
  1456. FunctionRemap *remap = (*ri);
  1457. if (isRemapLegal(*remap)) {
  1458. int parameter_size = remap->_parameters.size();
  1459. if(remap->_has_this && remap->_type != FunctionRemap::T_constructor)
  1460. parameter_size --;
  1461. MapSets[parameter_size].insert(remap);
  1462. out1 << " * ";
  1463. remap->write_orig_prototype(out1, 0);
  1464. out1 << "\n";
  1465. } else {
  1466. out1 << " * Rejected Remap [";
  1467. remap->write_orig_prototype(out1, 0);
  1468. out1 << "]\n";
  1469. }
  1470. }
  1471. out1 << " *******************************************************************/\n";
  1472. out << function_name << " {\n";
  1473. if (isFunctionWithThis(func)) {
  1474. out << PreProcess;
  1475. }
  1476. bool is_inplace = isInplaceFunction(func);
  1477. if (MapSets.empty()) {
  1478. return;
  1479. }
  1480. std::string FunctionComment = func->_ifunc._comment;
  1481. std::string FunctionComment1;
  1482. if (FunctionComment.size() > 2) {
  1483. FunctionComment += "\n";
  1484. }
  1485. bool constructor = false;
  1486. if (MapSets.size() > 1) {
  1487. string expected_params;
  1488. indent(out,4) << "int parameter_count = 1;\n";
  1489. indent(out,4) << "if(PyTuple_Check(args))\n";
  1490. indent(out,4) << "{\n";
  1491. indent(out,4) << " parameter_count = PyTuple_Size(args);\n" ;
  1492. indent(out,4) << " if(kwds != NULL && PyDict_Check(kwds))\n";
  1493. indent(out,4) << " parameter_count += PyDict_Size(kwds);\n" ;
  1494. indent(out,4) << "}\n";
  1495. indent(out,4) << "switch(parameter_count)\n";
  1496. indent(out,4) << "{\n";
  1497. for (mii = MapSets.begin(); mii != MapSets.end(); mii ++) {
  1498. indent(out,4) << "case(" << mii->first << "):\n";
  1499. indent(out,8) << "{\n";
  1500. write_function_forset(out,func,mii->second,expected_params,8,forward_decl,ClassName + function_name, is_inplace);
  1501. if ((*mii->second.begin())->_type == FunctionRemap::T_constructor) {
  1502. constructor = true;
  1503. }
  1504. indent(out,8)<< "}\n";
  1505. indent(out,8)<< "break;\n";
  1506. }
  1507. indent(out,4)<< "default:\n";
  1508. indent(out,8)<< "{\n";
  1509. indent(out,12)
  1510. << "PyErr_Format(PyExc_TypeError, \""
  1511. << func->_remaps[0]->_cppfunc->get_local_name(&parser)
  1512. << "() takes ";
  1513. // We add one to the parameter count for "self", following the
  1514. // Python convention.
  1515. int add_self = func->_has_this ? 1 : 0;
  1516. size_t mic;
  1517. for (mic = 0, mii = MapSets.begin();
  1518. mii != MapSets.end();
  1519. ++mii, ++mic) {
  1520. if (mic == MapSets.size() - 1) {
  1521. if (mic == 1) {
  1522. out << " or ";
  1523. } else {
  1524. out << ", or ";
  1525. }
  1526. } else if (mic != 0) {
  1527. out << ", ";
  1528. }
  1529. out << mii->first + add_self;
  1530. }
  1531. out << " arguments (%d given)\", parameter_count + " << add_self << ");\n";
  1532. if (constructor)
  1533. indent(out,12) << "return -1;\n";
  1534. else
  1535. indent(out,12) << "return (PyObject *) NULL; \n";
  1536. indent(out,8)<< "}\n";
  1537. indent(out,8)<< "break;\n";
  1538. indent(out,4)<< "}\n";
  1539. out << " if(!PyErr_Occurred()) // let error pass on \n";
  1540. out << " PyErr_SetString(PyExc_TypeError, \n";
  1541. out << " \"Arguments must match one of:\\n\"\n";
  1542. output_quoted(out, 10, expected_params);
  1543. out << ");\n";
  1544. if (constructor)
  1545. indent(out,4) << "return -1;\n";
  1546. else
  1547. indent(out,4) << "return (PyObject *) NULL; \n";
  1548. if(!expected_params.empty() && FunctionComment1.empty())
  1549. FunctionComment1 += "C++ Interface:\n";
  1550. FunctionComment1 += expected_params;
  1551. } else {
  1552. string expected_params = "";
  1553. for (mii = MapSets.begin(); mii != MapSets.end(); mii ++) {
  1554. write_function_forset(out,func,mii->second,expected_params,4,forward_decl,ClassName + function_name,is_inplace);
  1555. if((*mii->second.begin())->_type == FunctionRemap::T_constructor) {
  1556. constructor = true;
  1557. }
  1558. }
  1559. out << " if(!PyErr_Occurred())\n";
  1560. out << " PyErr_SetString(PyExc_TypeError,\n";
  1561. out << " \"Must Match :\\n\"\n";
  1562. output_quoted(out, 10, expected_params);
  1563. out << ");\n";
  1564. if (constructor)
  1565. indent(out,4) << "return -1;\n";
  1566. else
  1567. indent(out,4) << "return (PyObject *) NULL; \n";
  1568. if (!expected_params.empty() && FunctionComment1.empty()) {
  1569. FunctionComment1 += "C++ Interface:\n";
  1570. }
  1571. FunctionComment1 += expected_params;
  1572. }
  1573. out << "}\n\n";
  1574. if (!FunctionComment1.empty()) {
  1575. FunctionComment = FunctionComment1 + "\n" + FunctionComment;
  1576. }
  1577. if (!constructor) {
  1578. // Write out the function doc string. We only do this if it is
  1579. // not a constructor, since we don't have a place to put the
  1580. // constructor doc string.
  1581. out << "#ifndef NDEBUG\n";
  1582. out << "static char * " << func->_name << "_comment =\n";
  1583. output_quoted(out, 4, FunctionComment);
  1584. out << ";\n";
  1585. out << "#else\n";
  1586. out << "static char * " << func->_name << "_comment = NULL;\n";
  1587. out << "#endif\n";
  1588. }
  1589. out << "\n";
  1590. out1 << forward_decl.str();
  1591. out1 << out.str();
  1592. }
  1593. ////////////////////////////////////////////////////////
  1594. // Function : GetParnetDepth
  1595. //
  1596. // Support Function used to Sort the name based overrides.. For know must be complex to simple
  1597. ////////////////////////////////////////////////////////
  1598. int GetParnetDepth(CPPType *type)
  1599. {
  1600. int answer = 0;
  1601. // printf(" %s\n",type->get_local_name().c_str());
  1602. if (TypeManager::is_basic_string_char(type)) {
  1603. } else if (TypeManager::is_basic_string_wchar(type)) {
  1604. } else if (TypeManager::is_bool(type)) {
  1605. } else if (TypeManager::is_unsigned_longlong(type)) {
  1606. } else if (TypeManager::is_longlong(type)) {
  1607. } else if (TypeManager::is_integer(type)) {
  1608. } else if (TypeManager::is_float(type)) {
  1609. } else if (TypeManager::is_char_pointer(type)) {
  1610. } else if (TypeManager::is_pointer_to_PyObject(type)) {
  1611. } else if (TypeManager::is_pointer(type) ||TypeManager::is_reference(type) || TypeManager::is_struct(type) )
  1612. {
  1613. answer ++;
  1614. int deepest = 0;
  1615. TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(type)),false);
  1616. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  1617. const InterrogateType &itype = idb->get_type(type_index);
  1618. if(itype.is_class() ||itype.is_struct())
  1619. {
  1620. int num_derivations = itype.number_of_derivations();
  1621. for (int di = 0; di < num_derivations; di++)
  1622. {
  1623. TypeIndex d_type_Index = itype.get_derivation(di);
  1624. const InterrogateType &d_itype = idb->get_type(d_type_Index);
  1625. int this_one = GetParnetDepth(d_itype._cpptype);
  1626. if(this_one > deepest)
  1627. deepest = this_one;
  1628. }
  1629. }
  1630. answer += deepest;
  1631. // printf(" Class Name %s %d\n",itype.get_name().c_str(),answer);
  1632. }
  1633. // printf(" Class Name %s %d\n",itype.get_name().c_str(),answer);
  1634. return answer;
  1635. }
  1636. ////////////////////////////////////////////////////////
  1637. // The Core sort function for remap calling orders..
  1638. //////////////////////////////////////////////////////////
  1639. int RemapCompareLesss(FunctionRemap * in1 , FunctionRemap * in2)
  1640. {
  1641. if(in1->_parameters.size() != in2->_parameters.size())
  1642. return (in1->_parameters.size() > in2->_parameters.size());
  1643. int pcount = in1->_parameters.size();
  1644. for(int x = 0; x< pcount; x++)
  1645. {
  1646. CPPType *orig_type1 = in1->_parameters[x]._remap->get_orig_type();
  1647. CPPType *orig_type2 = in2->_parameters[x]._remap->get_orig_type();
  1648. int pd1 = GetParnetDepth(orig_type1);
  1649. int pd2 = GetParnetDepth(orig_type2);
  1650. if(pd1 != pd2)
  1651. return pd1> pd2;
  1652. }
  1653. // ok maybe something to do with return strength..
  1654. return false;
  1655. }
  1656. //////////////////////////////////////////////////////////
  1657. // Convience for the sort behavior..
  1658. ///////////////////////////////////////////////////////////
  1659. std::vector< FunctionRemap * > SortFunctionSet(std::set< FunctionRemap *> &remaps)
  1660. {
  1661. std::vector< FunctionRemap * > out;
  1662. for(std::set< FunctionRemap *>::iterator ii = remaps.begin(); ii!= remaps.end(); ii++)
  1663. out.push_back(*ii);
  1664. std::sort(out.begin(), out.end(), RemapCompareLesss);
  1665. return out;
  1666. }
  1667. ///////////////////////////////////////////////////////////
  1668. // Function : write_function_forset
  1669. //
  1670. // A set is defined as all rempas that have the same number of paramaters..
  1671. ///////////////////////////////////////////////////////////
  1672. void InterfaceMakerPythonNative::write_function_forset(ostream &out, InterfaceMaker::Function *func,
  1673. std::set< FunctionRemap *> &remapsin, string &expected_params, int indent_level,ostream &forward_decl, const std::string &functionname, bool is_inplace)
  1674. {
  1675. if(remapsin.size() > 1)
  1676. {
  1677. // printf("---------------------------- Start Sort ----- %s , %s\n",func->_name.c_str(),functionname.c_str());
  1678. std::vector<FunctionRemap *> remaps = SortFunctionSet(remapsin);
  1679. std::vector<FunctionRemap *>::iterator sii;
  1680. for(sii = remaps.begin(); sii != remaps.end(); sii ++)
  1681. {
  1682. FunctionRemap *remap = (*sii);
  1683. if(isRemapLegal(*remap))
  1684. {
  1685. indent(out,indent_level)<< "{ // -2 " ;
  1686. remap->write_orig_prototype(out, 0); out << "\n" ;
  1687. write_function_instance(out, func, remap,expected_params,indent_level+4,false,forward_decl, func->_name, is_inplace);
  1688. indent(out,indent_level+4)<< "PyErr_Clear(); \n";
  1689. indent(out,indent_level)<< "}\n\n";
  1690. }
  1691. }
  1692. }
  1693. else
  1694. {
  1695. std::set<FunctionRemap *>::iterator sii;
  1696. for(sii = remapsin.begin(); sii != remapsin.end(); sii ++)
  1697. {
  1698. FunctionRemap *remap = (*sii);
  1699. if(isRemapLegal(*remap))
  1700. {
  1701. indent(out,indent_level)<< "// 1-" ;remap->write_orig_prototype(out, 0); out << "\n" ;
  1702. // indent(out,indent_level)<< "do{\n";
  1703. write_function_instance(out, func, remap,expected_params,indent_level+4,true,forward_decl, func->_name, is_inplace);
  1704. // indent(out,indent_level)<< "}while(false);\n";
  1705. }
  1706. }
  1707. }
  1708. }
  1709. ////////////////////////////////////////////////////////////////////
  1710. // Function: InterfaceMakerPythonNative::write_function_instance
  1711. // Access: Private
  1712. // Description: Writes out the particular function that handles a
  1713. // single instance of an overloaded function.
  1714. ////////////////////////////////////////////////////////////////////
  1715. void InterfaceMakerPythonNative::write_function_instance(ostream &out, InterfaceMaker::Function *func1,
  1716. FunctionRemap *remap, string &expected_params, int indent_level, bool errors_fatal, ostream &ForwardDeclrs, const std::string &functionnamestr, bool is_inplace)
  1717. {
  1718. string format_specifiers;
  1719. std::string keyword_list;
  1720. string parameter_list;
  1721. string container;
  1722. vector_string pexprs;
  1723. string extra_convert;
  1724. string extra_param_check;
  1725. string extra_cleanup;
  1726. string pname_for_pyobject;
  1727. bool is_constructor = false;
  1728. if (remap->_type == FunctionRemap::T_constructor) {
  1729. is_constructor = true;
  1730. }
  1731. // Make one pass through the parameter list. We will output a
  1732. // one-line temporary variable definition for each parameter, while
  1733. // simultaneously building the ParseTuple() function call and also
  1734. // the parameter expression list for call_function().
  1735. expected_params += remap->_cppfunc->get_simple_name();
  1736. expected_params += "(";
  1737. int pn;
  1738. for (pn = 0; pn < (int)remap->_parameters.size(); pn++) {
  1739. if(pn > 0) {
  1740. expected_params += ", ";
  1741. }
  1742. CPPType *orig_type = remap->_parameters[pn]._remap->get_orig_type();
  1743. CPPType *type = remap->_parameters[pn]._remap->get_new_type();
  1744. string param_name = remap->get_parameter_name(pn);
  1745. // This is the string to convert our local variable to the
  1746. // appropriate C++ type. Normally this is just a cast.
  1747. string pexpr_string =
  1748. "(" + type->get_local_name(&parser) + ")" + param_name;
  1749. if (!remap->_has_this || pn != 0) {
  1750. keyword_list += "\""+remap->_parameters[pn]._name + "\", ";
  1751. }
  1752. if (remap->_parameters[pn]._remap->new_type_is_atomic_string()) {
  1753. if (TypeManager::is_char_pointer(orig_type)) {
  1754. indent(out,indent_level)<< "char *" << param_name;
  1755. format_specifiers += "s";
  1756. parameter_list += ", &" + param_name;
  1757. } else if (TypeManager::is_wstring(orig_type)) {
  1758. indent(out,indent_level) << "Py_UNICODE *" << param_name
  1759. << "_str; int " << param_name << "_len";
  1760. format_specifiers += "u#";
  1761. parameter_list += ", &" + param_name
  1762. + "_str, &" + param_name + "_len";
  1763. pexpr_string = "basic_string<wchar_t>((wchar_t *)" +
  1764. param_name + "_str, " +
  1765. param_name + "_len)";
  1766. } else {
  1767. indent(out,indent_level) << "char *" << param_name
  1768. << "_str; int " << param_name << "_len";
  1769. format_specifiers += "s#";
  1770. parameter_list += ", &" + param_name
  1771. + "_str, &" + param_name + "_len";
  1772. pexpr_string = "basic_string<char>(" +
  1773. param_name + "_str, " +
  1774. param_name + "_len)";
  1775. }
  1776. expected_params += "string";
  1777. } else if (TypeManager::is_bool(type)) {
  1778. indent(out,indent_level) << "PyObject *" << param_name;
  1779. format_specifiers += "O";
  1780. parameter_list += ", &" + param_name;
  1781. pexpr_string = "(PyObject_IsTrue(" + param_name + ")!=0)";
  1782. expected_params += "bool";
  1783. pname_for_pyobject += param_name;
  1784. } else if (TypeManager::is_unsigned_longlong(type)) {
  1785. indent(out,indent_level) << "PyObject *" << param_name;
  1786. format_specifiers += "O";
  1787. parameter_list += ", &" + param_name;
  1788. extra_convert += " PyObject *" + param_name + "_long = PyNumber_Long(" + param_name + ");";
  1789. extra_param_check += "|| (" + param_name + "_long == NULL)";
  1790. pexpr_string = "PyLong_AsUnsignedLongLong(" + param_name + "_long)";
  1791. extra_cleanup += " Py_XDECREF(" + param_name + "_long);";
  1792. expected_params += "unsigned long long";
  1793. pname_for_pyobject += param_name;
  1794. } else if (TypeManager::is_longlong(type)) {
  1795. indent(out,indent_level) << "PyObject *" << param_name;
  1796. format_specifiers += "O";
  1797. parameter_list += ", &" + param_name;
  1798. extra_convert += " PyObject *" + param_name + "_long = PyNumber_Long(" + param_name + ");";
  1799. extra_param_check += "|| (" + param_name + "_long == NULL)";
  1800. pexpr_string = "PyLong_AsLongLong(" + param_name + "_long)";
  1801. extra_cleanup += " Py_XDECREF(" + param_name + "_long);";
  1802. expected_params += "long long";
  1803. pname_for_pyobject += param_name;
  1804. } else if (TypeManager::is_unsigned_integer(type)) {
  1805. indent(out,indent_level) << "PyObject *" << param_name;
  1806. format_specifiers += "O";
  1807. parameter_list += ", &" + param_name;
  1808. extra_convert += " PyObject *" + param_name + "_uint = PyNumber_Long(" + param_name + ");";
  1809. extra_param_check += "|| (" + param_name + "_uint == NULL)";
  1810. pexpr_string = "PyLong_AsUnsignedLong(" + param_name + "_uint)";
  1811. extra_cleanup += " Py_XDECREF(" + param_name + "_uint);";
  1812. expected_params += "unsigned int";
  1813. pname_for_pyobject += param_name;
  1814. } else if (TypeManager::is_integer(type)) {
  1815. indent(out,indent_level) << "int " << param_name;
  1816. format_specifiers += "i";
  1817. parameter_list += ", &" + param_name;
  1818. expected_params += "int";
  1819. } else if (TypeManager::is_float(type)) {
  1820. indent(out,indent_level) << "double " << param_name;
  1821. format_specifiers += "d";
  1822. parameter_list += ", &" + param_name;
  1823. expected_params += "float ";
  1824. } else if (TypeManager::is_char_pointer(type)) {
  1825. indent(out,indent_level) << "char *" << param_name;
  1826. format_specifiers += "s";
  1827. parameter_list += ", &" + param_name;
  1828. expected_params += "string";
  1829. } else if (TypeManager::is_pointer_to_PyObject(type)) {
  1830. indent(out,indent_level) << "PyObject *" << param_name;
  1831. format_specifiers += "O";
  1832. parameter_list += ", &" + param_name;
  1833. pexpr_string = param_name;
  1834. pname_for_pyobject += param_name;
  1835. expected_params += "PyObject";
  1836. } else if (TypeManager::is_pointer(type)) {
  1837. expected_params += type->get_preferred_name();
  1838. if (!remap->_has_this || pn != 0) {
  1839. indent(out, indent_level)
  1840. << "PyObject *" << param_name;
  1841. format_specifiers += "O";
  1842. parameter_list += ", &" + param_name;
  1843. pname_for_pyobject += param_name;
  1844. TypeIndex p_type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(type)),false);
  1845. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  1846. const InterrogateType &p_itype = idb->get_type(p_type_index);
  1847. //make_safe_name(itype.get_scoped_name())
  1848. extra_convert += p_itype.get_scoped_name()+" *" + param_name + "_this = ("+p_itype.get_scoped_name()+" *)";
  1849. // need to a forward scope for this class..
  1850. if(!isExportThisRun(p_itype._cpptype)) {
  1851. _external_imports.insert(make_safe_name(p_itype.get_scoped_name()));
  1852. //ForwardDeclrs << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(p_itype.get_scoped_name()) << ";\n";
  1853. }
  1854. ostringstream str;
  1855. str << "DTOOL_Call_GetPointerThisClass(" << param_name
  1856. << ", &Dtool_" << make_safe_name(p_itype.get_scoped_name())
  1857. << ", " << pn << ", \""
  1858. << remap->_cppfunc->get_local_name(&parser)
  1859. << "\");\n";
  1860. extra_convert += str.str();
  1861. extra_param_check += "|| (" + param_name + "_this == NULL)";
  1862. pexpr_string = param_name + "_this";
  1863. }
  1864. } else {
  1865. // Ignore a parameter.
  1866. indent(out,indent_level) << "PyObject *" << param_name;
  1867. format_specifiers += "O";
  1868. parameter_list += ", &" + param_name;
  1869. expected_params += "any";
  1870. pname_for_pyobject += param_name;
  1871. }
  1872. if (remap->_parameters[pn]._has_name) {
  1873. expected_params += " " + remap->_parameters[pn]._name;
  1874. }
  1875. if (remap->_has_this && pn == 0) {
  1876. container = "local_this";
  1877. } else {
  1878. out << ";\n";
  1879. }
  1880. pexprs.push_back(pexpr_string);
  1881. }
  1882. expected_params += ")\n";
  1883. // If we got what claimed to be a unary operator, don't check for
  1884. // parameters, since we won't be getting any anyway.
  1885. if (!func1->_ifunc.is_unary_op()) {
  1886. std::string format_specifiers1 = format_specifiers + ":" + remap->_cppfunc->get_local_name(&parser);
  1887. indent(out,indent_level)
  1888. << "static char * key_word_list[] = {" << keyword_list << "NULL};\n";
  1889. if (remap->_parameters.size() == 1 ||
  1890. (remap->_has_this && remap->_parameters.size() == 2)) {
  1891. indent(out,indent_level)
  1892. << "// Special Case to Make operator work \n";
  1893. indent(out,indent_level)
  1894. << "if(PyTuple_Check(args) || (kwds != NULL && PyDict_Check(kwds)))\n";
  1895. indent(out,indent_level)
  1896. << " (PyArg_ParseTupleAndKeywords(args,kwds, \""
  1897. << format_specifiers1 << "\", key_word_list" << parameter_list
  1898. << "));\n";
  1899. indent(out,indent_level)
  1900. << "else\n";
  1901. indent(out,indent_level)
  1902. << " (PyArg_Parse(args, \"" << format_specifiers1 << "\""
  1903. << parameter_list << "));\n";
  1904. indent(out,indent_level)
  1905. << "if(!PyErr_Occurred())\n";
  1906. } else {
  1907. indent(out,indent_level)
  1908. << "if (PyArg_ParseTupleAndKeywords(args,kwds, \""
  1909. << format_specifiers1 << "\", key_word_list"
  1910. << parameter_list << "))\n";
  1911. }
  1912. }
  1913. indent(out,indent_level) << "{\n";
  1914. if (!extra_convert.empty()) {
  1915. indent(out,indent_level+4)
  1916. << extra_convert << "\n";
  1917. }
  1918. int extra_indent_level = indent_level+4;
  1919. if (!extra_param_check.empty()) {
  1920. indent(out,extra_indent_level)
  1921. << "if (!(" << extra_param_check.substr(3) << "))\n";
  1922. indent(out,extra_indent_level)
  1923. <<"{\n";
  1924. extra_indent_level+=4;
  1925. }
  1926. if (!remap->_void_return &&
  1927. remap->_return_type->new_type_is_atomic_string()) {
  1928. // Treat strings as a special case. We don't want to format the
  1929. // return expression.
  1930. string tt;
  1931. string return_expr = remap->call_function(out, extra_indent_level, false, container, pexprs);
  1932. CPPType *type = remap->_return_type->get_orig_type();
  1933. indent(out,extra_indent_level);
  1934. type->output_instance(out, "return_value", &parser);
  1935. // type->output_instance(tt, "return_value", &parser);
  1936. out << " = " << return_expr << ";\n";
  1937. if (track_interpreter) {
  1938. indent(out,extra_indent_level) << "in_interpreter = 1;\n";
  1939. }
  1940. if (!extra_cleanup.empty()) {
  1941. indent(out,extra_indent_level) << extra_cleanup << "\n";
  1942. }
  1943. return_expr = manage_return_value(out, 4, remap, "return_value");
  1944. do_assert_init(out, extra_indent_level,is_constructor);
  1945. pack_return_value(out, extra_indent_level, remap, return_expr,ForwardDeclrs,is_inplace);
  1946. } else {
  1947. string return_expr = remap->call_function(out, extra_indent_level, true, container, pexprs);
  1948. if (return_expr.empty()) {
  1949. if (track_interpreter) {
  1950. indent(out,extra_indent_level) << "in_interpreter = 1;\n";
  1951. }
  1952. if (!extra_cleanup.empty()) {
  1953. indent(out,extra_indent_level) << extra_cleanup << "\n";
  1954. }
  1955. do_assert_init(out, extra_indent_level,is_constructor);
  1956. indent(out,extra_indent_level) << "return Py_BuildValue(\"\");\n";
  1957. } else {
  1958. CPPType *type = remap->_return_type->get_temporary_type();
  1959. if(!is_inplace) {
  1960. indent(out,extra_indent_level);
  1961. type->output_instance(out, "return_value", &parser);
  1962. out << " = " << return_expr << ";\n";
  1963. }
  1964. if (track_interpreter) {
  1965. indent(out,extra_indent_level) << "in_interpreter = 1;\n";
  1966. }
  1967. if (!extra_cleanup.empty()) {
  1968. indent(out,extra_indent_level) << extra_cleanup << "\n";
  1969. }
  1970. return_expr = manage_return_value(out, extra_indent_level, remap, "return_value");
  1971. do_assert_init(out, extra_indent_level,is_constructor);
  1972. pack_return_value(out, extra_indent_level, remap, remap->_return_type->temporary_to_return(return_expr),ForwardDeclrs,is_inplace);
  1973. }
  1974. }
  1975. if (!extra_param_check.empty()) {
  1976. extra_indent_level-=4;
  1977. indent(out,extra_indent_level)<< "}\n";
  1978. }
  1979. indent(out,indent_level) << "}\n";
  1980. }
  1981. ////////////////////////////////////////////////////////////////////
  1982. // Function: InterfaceMakerPythonNative::pack_return_value
  1983. // Access: Private
  1984. // Description: Outputs a command to pack the indicated expression,
  1985. // of the return_type type, as a Python return value.
  1986. ////////////////////////////////////////////////////////////////////
  1987. void InterfaceMakerPythonNative::pack_return_value(ostream &out, int indent_level,
  1988. FunctionRemap *remap, string return_expr, ostream &ForwardDeclrs, bool is_inplace)
  1989. {
  1990. CPPType *orig_type = remap->_return_type->get_orig_type();
  1991. CPPType *type = remap->_return_type->get_new_type();
  1992. if (remap->_return_type->new_type_is_atomic_string()) {
  1993. if (TypeManager::is_char_pointer(orig_type)) {
  1994. indent(out, indent_level)
  1995. << "return PyString_FromString(" << return_expr << ");\n";
  1996. } else if (TypeManager::is_wstring(orig_type)) {
  1997. indent(out, indent_level)
  1998. << "return PyUnicode_FromWideChar("
  1999. << return_expr << ".data(), (int)" << return_expr << ".length());\n";
  2000. } else {
  2001. indent(out, indent_level)
  2002. << "return PyString_FromStringAndSize("
  2003. << return_expr << ".data(), (int)" << return_expr << ".length());\n";
  2004. }
  2005. } else if (TypeManager::is_unsigned_longlong(type)) {
  2006. indent(out, indent_level)
  2007. << "return PyLong_FromUnsignedLongLong(" << return_expr << ");\n";
  2008. } else if (TypeManager::is_longlong(type)) {
  2009. indent(out, indent_level)
  2010. << "return PyLong_FromLongLong(" << return_expr << ");\n";
  2011. } else if(TypeManager::is_unsigned_integer(type)){
  2012. indent(out, indent_level)
  2013. << "return PyLong_FromUnsignedLong(" << return_expr << ");\n";
  2014. }else if (TypeManager::is_integer(type)) {
  2015. indent(out, indent_level)
  2016. << "return PyInt_FromLong(" << return_expr << ");\n";
  2017. } else if (TypeManager::is_float(type)) {
  2018. indent(out, indent_level)
  2019. << "return PyFloat_FromDouble(" << return_expr << ");\n";
  2020. } else if (TypeManager::is_char_pointer(type)) {
  2021. indent(out, indent_level)
  2022. << "return PyString_FromString(" << return_expr << ");\n";
  2023. }
  2024. else if (TypeManager::is_pointer_to_PyObject(type))
  2025. {
  2026. indent(out, indent_level)
  2027. << "return "<< return_expr << ";\n";
  2028. }
  2029. else if (TypeManager::is_pointer(type))
  2030. {
  2031. if (TypeManager::is_struct(orig_type) || TypeManager::is_ref_to_anything(orig_type))
  2032. {
  2033. if( TypeManager::is_ref_to_anything(orig_type))
  2034. {
  2035. TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(type)),false);
  2036. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  2037. const InterrogateType &itype = idb->get_type(type_index);
  2038. std::string ows_memory_flag("true");
  2039. if(remap->_return_value_needs_management)
  2040. ows_memory_flag = "true";
  2041. else
  2042. ows_memory_flag = "false";
  2043. if(!isExportThisRun(itype._cpptype))
  2044. {
  2045. _external_imports.insert(make_safe_name(itype.get_scoped_name()));
  2046. //ForwardDeclrs << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
  2047. }
  2048. WriteReturnInstance(out,indent_level,return_expr,ows_memory_flag,itype.get_scoped_name(),itype._cpptype,is_inplace);
  2049. // indent(out, indent_level)
  2050. // <<"return DTool_CreatePyInstance((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(itype.get_scoped_name()) << ","<<ows_memory_flag<<");\n";
  2051. }
  2052. else
  2053. {
  2054. if (remap->_type == FunctionRemap::T_constructor )
  2055. {
  2056. // should only reach this in the INIT function a a Class .. IE the PY exists before the CPP object
  2057. // this is were we type to returned a class/struct.. ie CPP TYpe
  2058. TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(orig_type)),false);
  2059. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  2060. const InterrogateType &itype = idb->get_type(type_index);
  2061. indent(out, indent_level)
  2062. <<"return DTool_PyInit_Finalize(self, " << return_expr <<",&"<<CLASS_PREFEX << make_safe_name(itype.get_scoped_name()) << ",true);\n";
  2063. }
  2064. else
  2065. {
  2066. std::string ows_memory_flag("true");
  2067. if(remap->_return_value_needs_management)
  2068. ows_memory_flag = "true";
  2069. else
  2070. ows_memory_flag = "false";
  2071. if(remap->_manage_reference_count)
  2072. {
  2073. TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(type)),false);
  2074. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  2075. const InterrogateType &itype = idb->get_type(type_index);
  2076. if(!isExportThisRun(itype._cpptype))
  2077. {
  2078. _external_imports.insert(make_safe_name(itype.get_scoped_name()));
  2079. //ForwardDeclrs << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
  2080. }
  2081. // ForwardDeclrs << "extern \"C\" struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
  2082. WriteReturnInstance(out,indent_level,return_expr,ows_memory_flag,itype.get_scoped_name(),itype._cpptype,is_inplace);
  2083. //indent(out, indent_level)
  2084. // <<"return DTool_CreatePyInstance((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(itype.get_scoped_name()) << ","<<ows_memory_flag<<");\n";
  2085. }
  2086. else
  2087. {
  2088. TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(orig_type)),false);
  2089. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  2090. const InterrogateType &itype = idb->get_type(type_index);
  2091. if(!isExportThisRun(itype._cpptype))
  2092. {
  2093. _external_imports.insert(make_safe_name(itype.get_scoped_name()));
  2094. //ForwardDeclrs << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
  2095. }
  2096. // ForwardDeclrs << "extern \"C\" struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
  2097. //indent(out, indent_level)
  2098. // <<"return DTool_CreatePyInstance((void *)" << return_expr <<"," << CLASS_PREFEX << make_safe_name(itype.get_scoped_name()) << ","<<ows_memory_flag<<");\n";
  2099. WriteReturnInstance(out,indent_level,return_expr,ows_memory_flag,itype.get_scoped_name(),itype._cpptype,is_inplace);
  2100. }
  2101. }
  2102. }
  2103. }
  2104. else if( TypeManager::is_struct(orig_type->as_pointer_type()->_pointing_at) )
  2105. {
  2106. TypeIndex type_index = builder.get_type(TypeManager::unwrap(TypeManager::resolve_type(orig_type)),false);
  2107. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  2108. const InterrogateType &itype = idb->get_type(type_index);
  2109. std::string ows_memory_flag("true");
  2110. if(remap->_return_value_needs_management)
  2111. ows_memory_flag ="true";
  2112. else
  2113. ows_memory_flag = "false";
  2114. if(!isExportThisRun(itype._cpptype))
  2115. {
  2116. _external_imports.insert(make_safe_name(itype.get_scoped_name()));
  2117. //ForwardDeclrs << "IMPORT_THIS struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
  2118. }
  2119. // ForwardDeclrs << "extern \"C\" struct Dtool_PyTypedObject Dtool_" << make_safe_name(itype.get_scoped_name()) << ";\n";
  2120. WriteReturnInstance(out,indent_level,return_expr,ows_memory_flag,itype.get_scoped_name(),itype._cpptype,is_inplace);
  2121. //indent(out, indent_level)
  2122. // << "return DTool_CreatePyInstance((void *)" << return_expr <<","<<CLASS_PREFEX << make_safe_name(itype.get_scoped_name()) <<","<< ows_memory_flag<< ");\n";
  2123. }
  2124. else
  2125. {
  2126. indent(out, indent_level)<<" Shouln Never Reach This InterfaceMakerPythonNative::pack_return_value";
  2127. //<< "return PyInt_FromLong((int)" << return_expr << ");\n";
  2128. }
  2129. } else {
  2130. // Return None.
  2131. indent(out, indent_level)
  2132. << "return Py_BuildValue(\"\");\n";
  2133. }
  2134. }
  2135. ////////////////////////////////////////////////////////////////////
  2136. ////////////////////////////////////////////////////////////////////
  2137. // Function: InterfaceMakerPythonNative::record_object
  2138. // Access: Protected
  2139. // Description: Records the indicated type, which may be a struct
  2140. // type, along with all of its associated methods, if
  2141. // any.
  2142. ////////////////////////////////////////////////////////////////////
  2143. InterfaceMaker::Object *InterfaceMakerPythonNative::record_object(TypeIndex type_index)
  2144. {
  2145. if (type_index == 0) {
  2146. return (Object *)NULL;
  2147. }
  2148. Objects::iterator oi = _objects.find(type_index);
  2149. if (oi != _objects.end()) {
  2150. return (*oi).second;
  2151. }
  2152. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  2153. const InterrogateType &itype = idb->get_type(type_index);
  2154. if(!isCppTypeLegal(itype._cpptype))
  2155. {
  2156. return (Object *)NULL;
  2157. }
  2158. Object *object = new Object(itype);
  2159. bool inserted = _objects.insert(Objects::value_type(type_index, object)).second;
  2160. assert(inserted);
  2161. Function *function;
  2162. int num_constructors = itype.number_of_constructors();
  2163. for (int ci = 0; ci < num_constructors; ci++)
  2164. {
  2165. function = record_function(itype, itype.get_constructor(ci));
  2166. if(isFunctionLegal(function))
  2167. object->_constructors.push_back(function);
  2168. }
  2169. int num_methods = itype.number_of_methods();
  2170. int mi;
  2171. for (mi = 0; mi < num_methods; mi++)
  2172. {
  2173. function = record_function(itype, itype.get_method(mi));
  2174. if(isFunctionLegal(function))
  2175. object->_methods.push_back(function);
  2176. }
  2177. int num_casts = itype.number_of_casts();
  2178. for (mi = 0; mi < num_casts; mi++)
  2179. {
  2180. function = record_function(itype, itype.get_cast(mi));
  2181. if(isFunctionLegal(function))
  2182. object->_methods.push_back(function);
  2183. }
  2184. int num_derivations = itype.number_of_derivations();
  2185. for (int di = 0; di < num_derivations; di++)
  2186. {
  2187. TypeIndex d_type_Index = itype.get_derivation(di);
  2188. idb->get_type(d_type_Index);
  2189. if(!interrogate_type_is_unpublished(d_type_Index))
  2190. {
  2191. if (itype.derivation_has_upcast(di))
  2192. {
  2193. function = record_function(itype, itype.derivation_get_upcast(di));
  2194. if(isFunctionLegal(function))
  2195. object->_methods.push_back(function);
  2196. }
  2197. if (itype.derivation_has_downcast(di))
  2198. {
  2199. // Downcasts are methods of the base class, not the child class.
  2200. TypeIndex base_type_index = itype.get_derivation(di);
  2201. const InterrogateType &base_type = idb->get_type(base_type_index);
  2202. function = record_function(base_type, itype.derivation_get_downcast(di));
  2203. if(isFunctionLegal(function))
  2204. {
  2205. Object * pobject = record_object(base_type_index);
  2206. if(pobject != NULL)
  2207. pobject->_methods.push_back(function);
  2208. }
  2209. }
  2210. }
  2211. }
  2212. int num_elements = itype.number_of_elements();
  2213. for (int ei = 0; ei < num_elements; ei++)
  2214. {
  2215. ElementIndex element_index = itype.get_element(ei);
  2216. const InterrogateElement &ielement = idb->get_element(element_index);
  2217. if (ielement.has_getter())
  2218. {
  2219. FunctionIndex func_index = ielement.get_getter();
  2220. record_function(itype, func_index);
  2221. }
  2222. if (ielement.has_setter())
  2223. {
  2224. FunctionIndex func_index = ielement.get_setter();
  2225. record_function(itype, func_index);
  2226. }
  2227. }
  2228. int num_nested = itype.number_of_nested_types();
  2229. for (int ni = 0; ni < num_nested; ni++)
  2230. {
  2231. TypeIndex nested_index = itype.get_nested_type(ni);
  2232. record_object(nested_index);
  2233. }
  2234. return object;
  2235. }
  2236. ////////////////////////////////////////////////////////////////////
  2237. // Function: InterfaceMaker::generate_wrappers
  2238. // Access: Public, Virtual
  2239. // Description: Walks through the set of functions in the database
  2240. // and generates wrappers for each function, storing
  2241. // these in the database. No actual code should be
  2242. // output yet; this just updates the database with the
  2243. // wrapper information.
  2244. ////////////////////////////////////////////////////////////////////
  2245. void InterfaceMakerPythonNative::generate_wrappers()
  2246. {
  2247. inside_python_native = true;
  2248. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  2249. // We use a while loop rather than a simple for loop, because we
  2250. // might increase the number of types recursively during the
  2251. // traversal.
  2252. int ti = 0;
  2253. while (ti < idb->get_num_all_types())
  2254. {
  2255. TypeIndex type_index = idb->get_all_type(ti);
  2256. record_object(type_index);
  2257. ++ti;
  2258. }
  2259. int num_global_elements = idb->get_num_global_elements();
  2260. for (int gi = 0; gi < num_global_elements; ++gi) {
  2261. printf(" Global Type = %d",gi);
  2262. TypeIndex type_index = idb->get_global_element(gi);
  2263. record_object(type_index);
  2264. }
  2265. int num_functions = idb->get_num_global_functions();
  2266. for (int fi = 0; fi < num_functions; fi++)
  2267. {
  2268. FunctionIndex func_index = idb->get_global_function(fi);
  2269. record_function(dummy_type, func_index);
  2270. }
  2271. int num_manifests = idb->get_num_global_manifests();
  2272. for (int mi = 0; mi < num_manifests; mi++)
  2273. {
  2274. ManifestIndex manifest_index = idb->get_global_manifest(mi);
  2275. const InterrogateManifest &iman = idb->get_manifest(manifest_index);
  2276. if (iman.has_getter())
  2277. {
  2278. FunctionIndex func_index = iman.get_getter();
  2279. record_function(dummy_type, func_index);
  2280. }
  2281. }
  2282. int num_elements = idb->get_num_global_elements();
  2283. for (int ei = 0; ei < num_elements; ei++)
  2284. {
  2285. printf(" Element %d\n",ei);
  2286. ElementIndex element_index = idb->get_global_element(ei);
  2287. const InterrogateElement &ielement = idb->get_element(element_index);
  2288. if (ielement.has_getter())
  2289. {
  2290. FunctionIndex func_index = ielement.get_getter();
  2291. record_function(dummy_type, func_index);
  2292. }
  2293. if (ielement.has_setter())
  2294. {
  2295. FunctionIndex func_index = ielement.get_setter();
  2296. record_function(dummy_type, func_index);
  2297. }
  2298. }
  2299. inside_python_native = false;
  2300. }
  2301. //////////////////////////////////////////////
  2302. // Function :isCppTypeLegal
  2303. //
  2304. // is the cpp object supported by by the dtool_py interface..
  2305. //////////////////////////////////////////////
  2306. bool InterfaceMakerPythonNative::isCppTypeLegal(CPPType *in_ctype)
  2307. {
  2308. if(in_ctype == NULL)
  2309. return false;
  2310. if(builder.in_ignoretype(in_ctype->get_local_name(&parser)))
  2311. {
  2312. return false;
  2313. }
  2314. //bool answer = false;
  2315. CPPType *type = TypeManager::unwrap(TypeManager::resolve_type(in_ctype));
  2316. type = TypeManager::unwrap(type);
  2317. //CPPType *type = ctype;
  2318. if(TypeManager::is_basic_string_char(type))
  2319. {
  2320. return true;
  2321. }
  2322. else if(TypeManager::is_basic_string_wchar(type))
  2323. {
  2324. return true;
  2325. }
  2326. else if(TypeManager::is_simple(type))
  2327. {
  2328. return true;
  2329. }
  2330. else if(builder.in_forcetype(in_ctype->get_local_name(&parser)))
  2331. {
  2332. return true;
  2333. }
  2334. else if(TypeManager::IsExported(type) == true)
  2335. {
  2336. return true;
  2337. }
  2338. else if(TypeManager::is_pointer_to_PyObject(in_ctype) == true)
  2339. {
  2340. return true;
  2341. }
  2342. //if(answer == false)
  2343. // printf(" -------------------- Bad Type ?? %s \n",type->get_local_name().c_str());
  2344. return false;
  2345. }
  2346. //////////////////////////////////////////////
  2347. // Function :isExportThisRun
  2348. //
  2349. //////////////////////////////////////////////
  2350. bool InterfaceMakerPythonNative::isExportThisRun(CPPType *ctype)
  2351. {
  2352. CPPType *type = TypeManager::unwrap(ctype);
  2353. if(TypeManager::IsLocal(type))
  2354. return true;
  2355. if(builder.in_forcetype(type->get_local_name(&parser)))
  2356. return true;
  2357. return false;
  2358. }
  2359. //////////////////////////////////////////////
  2360. // Function : isExportThisRun
  2361. /////////////////////////////////////////////
  2362. bool InterfaceMakerPythonNative::isExportThisRun(Function *func)
  2363. {
  2364. if(func == NULL || !isFunctionLegal(func))
  2365. return false;
  2366. Function::Remaps::const_iterator ri;
  2367. for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri)
  2368. {
  2369. FunctionRemap *remap = (*ri);
  2370. return isExportThisRun(remap->_cpptype);
  2371. }
  2372. return false;
  2373. }
  2374. //////////////////////////////////////////////
  2375. // Function : isRemapLegal
  2376. //////////////////////////////////////////////
  2377. bool InterfaceMakerPythonNative::isRemapLegal( FunctionRemap &remap)
  2378. {
  2379. // return must be legal and managable..
  2380. if(!isCppTypeLegal(remap._return_type->get_orig_type()))
  2381. {
  2382. // printf(" isRemapLegal Return Is Bad %s\n",remap._return_type->get_orig_type()->get_fully_scoped_name().c_str());
  2383. return false;
  2384. }
  2385. // ouch .. bad things will happen here .. do not even try..
  2386. if(remap._ForcedVoidReturn)
  2387. return false;
  2388. // all params must be legal
  2389. for (int pn = 0; pn < (int)remap._parameters.size(); pn++)
  2390. {
  2391. CPPType *orig_type = remap._parameters[pn]._remap->get_orig_type();
  2392. if(!isCppTypeLegal(orig_type))
  2393. return false;
  2394. }
  2395. // ok all looks ok.
  2396. return true;
  2397. }
  2398. ////////////////////////////////////////////////////////////////////////
  2399. // Function : isFunctionLegal
  2400. ////////////////////////////////////////////////////////////////////////
  2401. bool InterfaceMakerPythonNative::isFunctionLegal( Function *func)
  2402. {
  2403. Function::Remaps::const_iterator ri;
  2404. for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri)
  2405. {
  2406. FunctionRemap *remap = (*ri);
  2407. if(isRemapLegal(*remap))
  2408. {
  2409. // printf(" Function Is Marked Legal %s\n",func->_name.c_str());
  2410. return true;
  2411. }
  2412. }
  2413. // printf(" Function Is Marked Illegal %s\n",func->_name.c_str());
  2414. return false;
  2415. }
  2416. ////////////////////////////////////////////////////////////////////////
  2417. // Function : isFunctionWithThis
  2418. //
  2419. // If any rempas have a this .. this function has a this..( of self) to python..
  2420. ////////////////////////////////////////////////////////////////////////
  2421. bool InterfaceMakerPythonNative::isFunctionWithThis( Function *func)
  2422. {
  2423. Function::Remaps::const_iterator ri;
  2424. for (ri = func->_remaps.begin(); ri != func->_remaps.end(); ++ri)
  2425. {
  2426. FunctionRemap *remap = (*ri);
  2427. if(remap->_has_this)
  2428. return true;
  2429. }
  2430. return false;
  2431. }
  2432. ////////////////////////////////////////////////////////////////////
  2433. // Function: InterfaceMakerPython::test_assert
  2434. // Access: Protected
  2435. // Description: Outputs code to check to see if an assertion has
  2436. // failed while the C++ code was executing, and report
  2437. // this failure back to Python.
  2438. ////////////////////////////////////////////////////////////////////
  2439. void InterfaceMakerPythonNative::do_assert_init(ostream &out, int indent_level, bool constructor) const {
  2440. if (watch_asserts) {
  2441. out << "#ifndef NDEBUG\n";
  2442. indent(out, indent_level)
  2443. << "Notify *notify = Notify::ptr();\n";
  2444. indent(out, indent_level)
  2445. << "if (notify->has_assert_failed()) {\n";
  2446. indent(out, indent_level + 2)
  2447. << "PyErr_SetString(PyExc_AssertionError, notify->get_assert_error_message().c_str());\n";
  2448. indent(out, indent_level + 2)
  2449. << "notify->clear_assert_failed();\n";
  2450. if(constructor)
  2451. indent(out, indent_level + 2) << "return -1;\n";
  2452. else
  2453. indent(out, indent_level + 2) << "return (PyObject *)NULL;\n";
  2454. indent(out, indent_level)
  2455. << "}\n";
  2456. indent(out, indent_level)
  2457. << "if (PyErr_Occurred()) {\n";
  2458. if(constructor)
  2459. indent(out, indent_level + 2) << "return -1;\n";
  2460. else
  2461. indent(out, indent_level + 2) << "return (PyObject *)NULL;\n";
  2462. indent(out, indent_level)
  2463. << "}\n";
  2464. out << "#endif\n";
  2465. }
  2466. }
  2467. ////////////////////////////////////////////////////////
  2468. // Function : IsRunTimeTyped
  2469. ///////////////////////////////////////////////////////
  2470. bool InterfaceMakerPythonNative::IsRunTimeTyped(const InterrogateType &itype)
  2471. {
  2472. TypeIndex ptype_id = itype.get_outer_class();
  2473. if(ptype_id > 0)
  2474. {
  2475. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  2476. InterrogateType ptype = idb->get_type(ptype_id);
  2477. return IsRunTimeTyped(ptype);
  2478. }
  2479. if(itype.get_name() == "TypedObject")
  2480. return true;
  2481. return false;
  2482. };
  2483. //////////////////////////////////////////////////////////
  2484. // Function : DoesInheritFromIsClass
  2485. //
  2486. // Helper function to check cpp class inharatience..
  2487. ///////////////////////////////////////////////////////////
  2488. bool InterfaceMakerPythonNative::DoesInheritFromIsClass( const CPPStructType * inclass, const std::string &name)
  2489. {
  2490. if(inclass == NULL)
  2491. return false;
  2492. CPPStructType::Derivation::const_iterator bi;
  2493. for (bi = inclass->_derivation.begin();
  2494. bi != inclass->_derivation.end();
  2495. ++bi)
  2496. {
  2497. const CPPStructType::Base &base = (*bi);
  2498. CPPStructType *base_type = TypeManager::resolve_type(base._base)->as_struct_type();
  2499. if(base_type != NULL)
  2500. {
  2501. std::string scoped_name = base_type->get_fully_scoped_name();
  2502. if(scoped_name == name)
  2503. return true;
  2504. if(DoesInheritFromIsClass(base_type,name) == true)
  2505. return true;
  2506. }
  2507. }
  2508. return false;
  2509. }
  2510. ////////////////////////////////////////////////////////////////////////////////////////////
  2511. // Function : HasAGetKeyFunction
  2512. //
  2513. // does the class have a supportable GetKey for hash usage..
  2514. //////////////////////////////////////////////////////////////////////////////////////////
  2515. bool InterfaceMakerPythonNative::HasAGetKeyFunction(const InterrogateType &itype_class)
  2516. {
  2517. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  2518. int num_methods = itype_class.number_of_methods();
  2519. int mi;
  2520. for (mi = 0; mi < num_methods; mi++)
  2521. {
  2522. FunctionIndex func_index = itype_class.get_method(mi);
  2523. const InterrogateFunction &ifunc = idb->get_function(func_index);
  2524. if(ifunc.get_name() == "get_key")
  2525. {
  2526. if (ifunc._instances != (InterrogateFunction::Instances *)NULL)
  2527. {
  2528. InterrogateFunction::Instances::const_iterator ii;
  2529. for (ii = ifunc._instances->begin();ii != ifunc._instances->end();++ii)
  2530. {
  2531. CPPInstance *cppinst = (*ii).second;
  2532. CPPFunctionType *cppfunc = cppinst->_type->as_function_type();
  2533. if(cppfunc != NULL)
  2534. {
  2535. if(cppfunc->_parameters != NULL && cppfunc->_return_type != NULL && TypeManager::is_integer(cppfunc->_return_type))
  2536. {
  2537. if(cppfunc->_parameters->_parameters.size() == 0)
  2538. {
  2539. return true;
  2540. }
  2541. }
  2542. }
  2543. }
  2544. }
  2545. }
  2546. }
  2547. return false;
  2548. };
  2549. ////////////////////////////////////////////////////////////////////
  2550. // Function: InterfaceMakerPythonNative::NeedsAStrFunction
  2551. // Access: Private
  2552. // Description: Returns -1 if the class does not define write() (and
  2553. // therefore cannot support a __str__ function).
  2554. //
  2555. // Returns 1 if the class defines write(ostream).
  2556. //
  2557. // Returns 2 if the class defines write(ostream, int).
  2558. ////////////////////////////////////////////////////////////////////
  2559. int InterfaceMakerPythonNative::
  2560. NeedsAStrFunction(const InterrogateType &itype_class) {
  2561. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  2562. int num_methods = itype_class.number_of_methods();
  2563. int mi;
  2564. for (mi = 0; mi < num_methods; ++mi) {
  2565. FunctionIndex func_index = itype_class.get_method(mi);
  2566. const InterrogateFunction &ifunc = idb->get_function(func_index);
  2567. if (ifunc.get_name() == "write") {
  2568. if (ifunc._instances != (InterrogateFunction::Instances *)NULL) {
  2569. InterrogateFunction::Instances::const_iterator ii;
  2570. for (ii = ifunc._instances->begin();
  2571. ii != ifunc._instances->end();
  2572. ++ii) {
  2573. CPPInstance *cppinst = (*ii).second;
  2574. CPPFunctionType *cppfunc = cppinst->_type->as_function_type();
  2575. if (cppfunc != NULL) {
  2576. if (cppfunc->_parameters != NULL &&
  2577. cppfunc->_return_type != NULL &&
  2578. TypeManager::is_void(cppfunc->_return_type)) {
  2579. if (cppfunc->_parameters->_parameters.size() == 1) {
  2580. CPPInstance *inst1 = cppfunc->_parameters->_parameters[0];
  2581. if (TypeManager::is_pointer_to_ostream(inst1->_type)) {
  2582. // write(ostream)
  2583. return 1;
  2584. }
  2585. }
  2586. if (cppfunc->_parameters->_parameters.size() == 2) {
  2587. CPPInstance *inst1 = cppfunc->_parameters->_parameters[0];
  2588. if (TypeManager::is_pointer_to_ostream(inst1->_type)) {
  2589. inst1 = cppfunc->_parameters->_parameters[1];
  2590. if (inst1->_initializer != NULL) {
  2591. // write(ostream, int = 0)
  2592. return 1;
  2593. }
  2594. if (TypeManager::is_integer(inst1->_type)) {
  2595. // write(ostream, int)
  2596. return 2;
  2597. }
  2598. }
  2599. }
  2600. }
  2601. }
  2602. }
  2603. }
  2604. }
  2605. }
  2606. return -1;
  2607. }
  2608. ////////////////////////////////////////////////////////////////////
  2609. // Function: InterfaceMakerPythonNative::NeedsAReprFunction
  2610. // Access: Private
  2611. // Description: Returns -1 if the class does not define output() or
  2612. // python_repr() (and therefore cannot support a
  2613. // __repr__ function).
  2614. //
  2615. // Returns 1 if the class defines python_repr(ostream, string).
  2616. //
  2617. // Returns 2 if the class defines output(ostream).
  2618. ////////////////////////////////////////////////////////////////////
  2619. int InterfaceMakerPythonNative::
  2620. NeedsAReprFunction(const InterrogateType &itype_class) {
  2621. InterrogateDatabase *idb = InterrogateDatabase::get_ptr();
  2622. int num_methods = itype_class.number_of_methods();
  2623. int mi;
  2624. for (mi = 0; mi < num_methods; ++mi) {
  2625. FunctionIndex func_index = itype_class.get_method(mi);
  2626. const InterrogateFunction &ifunc = idb->get_function(func_index);
  2627. if (ifunc.get_name() == "python_repr") {
  2628. if (ifunc._instances != (InterrogateFunction::Instances *)NULL) {
  2629. InterrogateFunction::Instances::const_iterator ii;
  2630. for (ii = ifunc._instances->begin();
  2631. ii != ifunc._instances->end();
  2632. ++ii) {
  2633. CPPInstance *cppinst = (*ii).second;
  2634. CPPFunctionType *cppfunc = cppinst->_type->as_function_type();
  2635. if (cppfunc != NULL) {
  2636. if (cppfunc->_parameters != NULL &&
  2637. cppfunc->_return_type != NULL &&
  2638. TypeManager::is_void(cppfunc->_return_type)) {
  2639. if (cppfunc->_parameters->_parameters.size() == 2) {
  2640. CPPInstance *inst1 = cppfunc->_parameters->_parameters[0];
  2641. if (TypeManager::is_pointer_to_ostream(inst1->_type)) {
  2642. inst1 = cppfunc->_parameters->_parameters[1];
  2643. if (TypeManager::is_string(inst1->_type) ||
  2644. TypeManager::is_char_pointer(inst1->_type)) {
  2645. // python_repr(ostream, string)
  2646. return 1;
  2647. }
  2648. }
  2649. }
  2650. }
  2651. }
  2652. }
  2653. }
  2654. }
  2655. }
  2656. for (mi = 0; mi < num_methods; ++mi) {
  2657. FunctionIndex func_index = itype_class.get_method(mi);
  2658. const InterrogateFunction &ifunc = idb->get_function(func_index);
  2659. if (ifunc.get_name() == "output") {
  2660. if (ifunc._instances != (InterrogateFunction::Instances *)NULL) {
  2661. InterrogateFunction::Instances::const_iterator ii;
  2662. for (ii = ifunc._instances->begin();
  2663. ii != ifunc._instances->end();
  2664. ++ii) {
  2665. CPPInstance *cppinst = (*ii).second;
  2666. CPPFunctionType *cppfunc = cppinst->_type->as_function_type();
  2667. if (cppfunc != NULL) {
  2668. if (cppfunc->_parameters != NULL &&
  2669. cppfunc->_return_type != NULL &&
  2670. TypeManager::is_void(cppfunc->_return_type)) {
  2671. if (cppfunc->_parameters->_parameters.size() == 1) {
  2672. CPPInstance *inst1 = cppfunc->_parameters->_parameters[0];
  2673. if (TypeManager::is_pointer_to_ostream(inst1->_type)) {
  2674. // output(ostream)
  2675. return 2;
  2676. }
  2677. }
  2678. if (cppfunc->_parameters->_parameters.size() >= 2) {
  2679. CPPInstance *inst1 = cppfunc->_parameters->_parameters[0];
  2680. if (TypeManager::is_pointer_to_ostream(inst1->_type)) {
  2681. inst1 = cppfunc->_parameters->_parameters[1];
  2682. if (inst1->_initializer != NULL) {
  2683. // output(ostream, foo = bar, ...)
  2684. return 2;
  2685. }
  2686. }
  2687. }
  2688. }
  2689. }
  2690. }
  2691. }
  2692. }
  2693. }
  2694. return -1;
  2695. }
  2696. ////////////////////////////////////////////////////////////////////
  2697. // Function: InterfaceMakerPythonNative::output_quoted
  2698. // Access: Private
  2699. // Description: Outputs the indicated string as a single quoted,
  2700. // multi-line string to the generated C++ source code.
  2701. // The output point is left on the last line of the
  2702. // string, following the trailing quotation mark.
  2703. ////////////////////////////////////////////////////////////////////
  2704. void InterfaceMakerPythonNative::
  2705. output_quoted(ostream &out, int indent_level, const std::string &str) {
  2706. indent(out, indent_level)
  2707. << '"';
  2708. std::string::const_iterator si;
  2709. for (si = str.begin(); si != str.end(); ++si) {
  2710. switch (*si) {
  2711. case '"':
  2712. case '\\':
  2713. out << '\\' << *si;
  2714. break;
  2715. case '\n':
  2716. out << "\\n\"\n";
  2717. indent(out, indent_level)
  2718. << '"';
  2719. break;
  2720. default:
  2721. if (!isprint(*si)) {
  2722. out << "\\" << oct << setw(3) << setfill('0') << (unsigned int)(*si)
  2723. << dec;
  2724. } else {
  2725. out << *si;
  2726. }
  2727. }
  2728. }
  2729. out << '"';
  2730. }