interfaceMakerPythonNative.cxx 109 KB

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