Generator.cpp 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  1. #include "Generator.h"
  2. Generator* Generator::__instance = NULL;
  3. // Warning flags.
  4. static bool __printTemplateWarning = false;
  5. static bool __printVarargWarning = false;
  6. static bool __printOperatorWarning = false;
  7. // Utility functions (local to this file).
  8. static string trim(const string& str);
  9. static string stripTypeQualifiers(const string& typeStr, FunctionBinding::Param::Kind& kind, int& levelsOfIndirection);
  10. static inline bool isWantedFileNormal(const string& s);
  11. static inline bool isNamespaceFile(const string& s);
  12. static inline bool isGeneratedBindingFile(const string& s);
  13. static bool getFileList(string directory, vector<string>& files, bool (*isWantedFile)(const string& s));
  14. static bool isReservedKeyword(string name);
  15. Generator* Generator::getInstance()
  16. {
  17. if (!__instance)
  18. __instance = new Generator();
  19. return __instance;
  20. }
  21. void Generator::releaseInstance()
  22. {
  23. SAFE_DELETE(__instance);
  24. }
  25. string Generator::getUniqueName(string name)
  26. {
  27. // Calculate the unique name for the given identifier (class, struct, enum, etc).
  28. string uniquename = name;
  29. size_t i = uniquename.find("::");
  30. while (i != uniquename.npos)
  31. {
  32. uniquename.replace(i, 2, SCOPE_REPLACEMENT);
  33. i = uniquename.find("::");
  34. }
  35. return uniquename;
  36. }
  37. string Generator::getUniqueNameFromRef(string refId)
  38. {
  39. return getUniqueName(getIdentifier(refId));
  40. }
  41. vector<string> Generator::getScopePath(string classname, string ns)
  42. {
  43. size_t i = classname.find("::");
  44. vector<string> scopePath;
  45. while (i != classname.npos)
  46. {
  47. string scope = classname.substr(0, i);
  48. if (scope != ns)
  49. scopePath.push_back(scope);
  50. classname = classname.substr(i + 2);
  51. i = classname.find("::");
  52. }
  53. return scopePath;
  54. }
  55. string Generator::getIdentifier(string refId)
  56. {
  57. if (_refIds.find(refId) != _refIds.end())
  58. return _refIds[refId];
  59. return refId;
  60. }
  61. string Generator::getClassNameAndNamespace(string classname, string* ns)
  62. {
  63. size_t index = classname.find("::");
  64. if (index != classname.npos)
  65. {
  66. string classNS = classname.substr(0, index);
  67. if (_namespaces.find(classNS) != _namespaces.end())
  68. {
  69. if (ns)
  70. *ns = classNS;
  71. string tmp = classname.substr(index + 2);
  72. if (_namespaces[classNS].find(tmp) != _namespaces[classNS].end())
  73. return classname.substr(index + 2);
  74. else
  75. {
  76. size_t tmpIndex = tmp.find("::");
  77. if (tmpIndex != tmp.npos)
  78. {
  79. tmp = tmp.substr(0, tmpIndex);
  80. if (_namespaces[classNS].find(tmp) != _namespaces[classNS].end())
  81. {
  82. _namespaces[classNS].insert(classname.substr(index + 2));
  83. return classname.substr(index + 2);
  84. }
  85. }
  86. }
  87. }
  88. }
  89. return classname;
  90. }
  91. void Generator::setIdentifier(string refId, string id)
  92. {
  93. _refIds[refId] = id;
  94. }
  95. bool Generator::isDerived(const ClassBinding& c, string classname)
  96. {
  97. for (unsigned int i = 0; i < c.derived.size(); i++)
  98. {
  99. // If the derived class is not in the ref ID table, then it
  100. // is a hidden (protected, private, etc.) class, so don't consider it.
  101. if (_refIds.find(c.derived[i]) != _refIds.end())
  102. {
  103. const string& derivedClassName = getIdentifier(c.derived[i]);
  104. if (derivedClassName == classname ||
  105. (_classes.find(derivedClassName) != _classes.end() &&
  106. isDerived(_classes[derivedClassName], classname)))
  107. {
  108. return true;
  109. }
  110. }
  111. }
  112. return false;
  113. }
  114. bool Generator::isRef(string classname)
  115. {
  116. // Check if the class is derived from Ref.
  117. for (unsigned int i = 0; i < _topLevelBaseClasses.size(); i++)
  118. {
  119. if (_topLevelBaseClasses[i] == REF_CLASS_NAME)
  120. {
  121. return isDerived(_classes[_topLevelBaseClasses[i]], classname);
  122. }
  123. }
  124. return classname == REF_CLASS_NAME;
  125. }
  126. string Generator::getCompoundName(XMLElement* node)
  127. {
  128. // Get the name of the namespace, class, struct, or file that we are processing.
  129. XMLElement* nameElement = node->FirstChildElement("compoundname");
  130. if (!nameElement)
  131. {
  132. GP_ERROR("Missing 'compoundname' child node of 'compounddef' node for XML document '%s'.", _file);
  133. return "";
  134. }
  135. const char* text = nameElement->GetText();
  136. if (!text)
  137. {
  138. GP_ERROR("The 'compoundname' node is empty for XML document '%s'.", _file);
  139. return "";
  140. }
  141. return string(text);
  142. }
  143. void Generator::run(string inDir, string outDir, string* bindingNS)
  144. {
  145. // Set the output directory.
  146. _outDir = outDir;
  147. // Get a list of all .cpp and .h files in the output directory so
  148. // we can delete them before generating new bindings.
  149. vector<string> oldBindingsFiles;
  150. getFileList(outDir, oldBindingsFiles, isGeneratedBindingFile);
  151. // Get a list of the Doxygen XML files that specify a namespace.
  152. // Note: we must do this before adding the normal files so that
  153. // when we process the files sequentially, we process the namespaces
  154. // before the normal files (so that namespaces can be removed
  155. // properly from class/struct names, etc.)
  156. vector<string> files;
  157. getFileList(inDir, files, isNamespaceFile);
  158. // Add all of the normal Doxygen XML files we want to use to generate Lua bindings.
  159. if (!getFileList(inDir, files, isWantedFileNormal) || files.empty())
  160. {
  161. GP_ERROR("Failed to get a valid list of files to generate bindings from using directory '%s'.", inDir.c_str());
  162. return;
  163. }
  164. // Go through each file and build the data needed to generate the bindings.
  165. for (vector<string>::iterator iter = files.begin(); iter != files.end(); iter++)
  166. {
  167. tinyxml2::XMLDocument doc;
  168. _file = iter->c_str();
  169. doc.LoadFile(iter->c_str());
  170. int ret = doc.ErrorID();
  171. if (ret != 0)
  172. {
  173. GP_ERROR("Failed to load XML document '%s' properly with error ID %d.", iter->c_str(), ret);
  174. continue;
  175. }
  176. XMLElement* rootNode = doc.FirstChildElement("doxygen");
  177. if (!rootNode)
  178. {
  179. GP_ERROR("Missing root 'doxygen' node for XML document '%s'.", iter->c_str());
  180. continue;
  181. }
  182. XMLElement* node = rootNode->FirstChildElement("compounddef");
  183. if (!node)
  184. {
  185. GP_ERROR("Missing 'compounddef' node for XML document '%s'.", iter->c_str());
  186. continue;
  187. }
  188. const char* kind = node->Attribute("kind");
  189. if (kind && strcmp(kind, "namespace") == 0)
  190. {
  191. string name = getCompoundName(node);
  192. cout << "Parsing namespace " << name << "...\n";
  193. getNamespace(node, name);
  194. }
  195. else if ( (kind && (strcmp(kind, "class") == 0 || strcmp(kind, "struct") == 0) ) && strcmp(node->Attribute("prot"), "public") == 0)
  196. {
  197. string name = getCompoundName(node);
  198. cout << "Parsing class/struct " << name << "...\n";
  199. getClass(node, name);
  200. }
  201. else if (kind && strcmp(kind, "file") == 0)
  202. {
  203. string name = getCompoundName(node);
  204. cout << "Parsing file " << name << "...\n";
  205. if (name.find(".h") != name.npos)
  206. {
  207. getFunctions(node);
  208. getEnums(node, name);
  209. }
  210. else if (name.find(".cpp") != name.npos)
  211. {
  212. getIncludes(node, name);
  213. }
  214. }
  215. }
  216. // Resolve all inheritance.
  217. resolveInheritance();
  218. // Resolve all unrecognized parameter and return value types.
  219. resolveTypes();
  220. // Resolve all inherited include files.
  221. resolveInheritedIncludes();
  222. // Generate the script bindings.
  223. generateBindings(bindingNS);
  224. // Print out all warnings (unsupported types, function name-Lua keyword clashes, etc.)
  225. if (__warnings.size() > 0)
  226. {
  227. cout << "\nWarnings:\n";
  228. for (set<string>::iterator iter = __warnings.begin(); iter!= __warnings.end(); iter++)
  229. {
  230. GP_WARN("%s", iter->c_str());
  231. }
  232. }
  233. // Print warnings for the use of unsupported C++ features.
  234. if (__printTemplateWarning)
  235. GP_WARN("Detected the use of template parameters; this feature of C++ is not supported.");
  236. if (__printVarargWarning)
  237. GP_WARN("Detected the use of variable argument lists; this feature of C++ is not supported.");
  238. if (__printOperatorWarning)
  239. GP_WARN("Detected the use of operator overloading; this feature of C++ is not supported.");
  240. // Delete files that are no longer needed and print warnings for them
  241. for (unsigned int i = 0; i < oldBindingsFiles.size(); i++)
  242. {
  243. if (std::find(generatedFiles.begin(), generatedFiles.end(), oldBindingsFiles[i]) == generatedFiles.end())
  244. {
  245. GP_WARN("Deleting unused file: %s", oldBindingsFiles[i].c_str());
  246. remove(oldBindingsFiles[i].c_str());
  247. }
  248. }
  249. }
  250. Generator::Generator()
  251. {
  252. }
  253. Generator::~Generator()
  254. {
  255. }
  256. void Generator::getFunctions(XMLElement* fileNode, string ns)
  257. {
  258. // Process all public, non-static functions and variables that are defined in this file.
  259. XMLElement* node = NULL;
  260. const char* kind = NULL;
  261. const char* isStatic = NULL;
  262. for (node = fileNode->FirstChildElement("sectiondef"); node != NULL; node = node->NextSiblingElement("sectiondef"))
  263. {
  264. kind = node->Attribute("kind");
  265. if (kind && strcmp(kind, "func") == 0)
  266. {
  267. XMLElement* e = node->FirstChildElement("memberdef");
  268. while (e)
  269. {
  270. isStatic = e->Attribute("static");
  271. if (e->FirstChildElement("templateparamlist"))
  272. {
  273. __printTemplateWarning = true;
  274. }
  275. else if (getScriptFlag(e) != "ignore" &&
  276. (!isStatic || strcmp(isStatic, "yes") != 0))
  277. {
  278. FunctionBinding b;
  279. b.type = FunctionBinding::GLOBAL_FUNCTION;
  280. b.name = getName(e);
  281. if (!isReservedKeyword(b.name))
  282. {
  283. // Ignore operators.
  284. if (b.name.find("operator") != 0)
  285. {
  286. b.returnParam = getParam(e);
  287. getParams(e, b);
  288. getCreateFlag(e, b);
  289. _functions[b.getFunctionName()].push_back(b);
  290. // Add the function to the set of namespace members if it is part of a namespace.
  291. if (ns.size() > 0)
  292. _namespaces[ns].insert(b.name);
  293. }
  294. else
  295. {
  296. __printOperatorWarning = true;
  297. }
  298. }
  299. else
  300. {
  301. __warnings.insert(string("Function name '") + b.name + string("' is a reserved Lua keyword; binding '") + b.getFunctionName() + string("' was not generated."));
  302. }
  303. }
  304. e = e->NextSiblingElement("memberdef");
  305. }
  306. }
  307. else if (kind && strcmp(kind, "var") == 0)
  308. {
  309. XMLElement* e = node->FirstChildElement("memberdef");
  310. while (e)
  311. {
  312. isStatic = e->Attribute("static");
  313. if (getScriptFlag(e) != "ignore" &&
  314. (!isStatic || strcmp(isStatic, "yes") != 0))
  315. {
  316. FunctionBinding b;
  317. if (getIsConstType(e))
  318. b.type = FunctionBinding::GLOBAL_CONSTANT;
  319. else
  320. b.type = FunctionBinding::GLOBAL_VARIABLE;
  321. b.name = getName(e);
  322. if (!isReservedKeyword(b.name))
  323. {
  324. b.returnParam = getParam(e, true);
  325. _functions[b.getFunctionName()].push_back(b);
  326. // Add the variable to the set of namespace members if it is part of a namespace.
  327. if (ns.size() > 0)
  328. _namespaces[ns].insert(b.name);
  329. }
  330. else
  331. {
  332. __warnings.insert(string("Function name '") + b.name + string("' is a reserved Lua keyword; binding '") + b.getFunctionName() + string("' was not generated."));
  333. }
  334. }
  335. e = e->NextSiblingElement("memberdef");
  336. }
  337. }
  338. }
  339. }
  340. void Generator::getNamespace(XMLElement* nsNode, const string& name)
  341. {
  342. // Skip the 'std' namespace.
  343. if (name == "std")
  344. return;
  345. // Create the set of all classes that are a part of this namespace.
  346. XMLElement* e = nsNode->FirstChildElement("innerclass");
  347. while (e)
  348. {
  349. string classname = e->GetText();
  350. size_t i = classname.find("::");
  351. if (i != classname.npos)
  352. classname = classname.substr(i + 2);
  353. _namespaces[name].insert(classname);
  354. e = e->NextSiblingElement("innerclass");
  355. }
  356. // Parse all functions that are directly in this namespace.
  357. getFunctions(nsNode, name);
  358. // Parse all enums that are directly in this namespace.
  359. getEnums(nsNode, name);
  360. // Parse all typedefs.
  361. getTypedefs(nsNode, name);
  362. }
  363. void Generator::getClass(XMLElement* classNode, const string& name)
  364. {
  365. // Get the ref id for the class.
  366. string refId = classNode->Attribute("id");
  367. // Create the class binding object that we will store the function bindings in (name -> binding).
  368. ClassBinding classBinding(name, refId);
  369. // Store the mapping between the ref id and the class's fully qualified name.
  370. Generator::getInstance()->setIdentifier(refId, classBinding.classname);
  371. // Check if we should ignore this class.
  372. if (getScriptFlag(classNode) == "ignore")
  373. return;
  374. // Get the include header for the original class declaration.
  375. XMLElement* includeElement = classNode->FirstChildElement("includes");
  376. if (includeElement)
  377. {
  378. const char* text = includeElement->GetText();
  379. if (!text)
  380. {
  381. GP_ERROR("The 'includes' node is empty (a class must have an include file) for XML document '%s'.", _file);
  382. return;
  383. }
  384. classBinding.include = text;
  385. }
  386. else
  387. {
  388. // See if we are an inner class. If so, attempt to get the include from the root parent class.
  389. size_t index = classBinding.classname.find("::");
  390. if (index != classBinding.classname.npos)
  391. {
  392. string parentClassName = classBinding.classname.substr(0, index);
  393. if (_classes.find(parentClassName) != _classes.end())
  394. classBinding.include = _classes[parentClassName].include;
  395. }
  396. else
  397. {
  398. // Attempt to guess the name of the header.
  399. classBinding.include = classBinding.classname + ".h";
  400. }
  401. }
  402. // Track whether the class has any pure virtual functions.
  403. bool hasPureVirtual = false;
  404. // Process all public members that are defined in this class.
  405. XMLElement* node = NULL;
  406. const char* kind = NULL;
  407. for (node = classNode->FirstChildElement("sectiondef"); node != NULL; node = node->NextSiblingElement("sectiondef"))
  408. {
  409. kind = node->Attribute("kind");
  410. if (!kind)
  411. {
  412. GP_ERROR("Section definition is missing required 'kind' attribute in XML document '%s'.", _file);
  413. return;
  414. }
  415. // Process public static functions.
  416. if (strcmp(kind, "public-static-func") == 0)
  417. {
  418. XMLElement* e = node->FirstChildElement("memberdef");
  419. while (e)
  420. {
  421. if (e->FirstChildElement("templateparamlist"))
  422. {
  423. __printTemplateWarning = true;
  424. }
  425. else if (getScriptFlag(e) != "ignore")
  426. {
  427. FunctionBinding b(classBinding.classname, classBinding.uniquename);
  428. b.type = FunctionBinding::STATIC_FUNCTION;
  429. b.name = getName(e);
  430. if (!isReservedKeyword(b.name))
  431. {
  432. b.returnParam = getParam(e, false, b.classname);
  433. getParams(e, b);
  434. getCreateFlag(e, b);
  435. classBinding.bindings[b.getFunctionName()].push_back(b);
  436. }
  437. else
  438. {
  439. __warnings.insert(string("Function name '") + b.name + string("' is a reserved Lua keyword; binding '") + b.getFunctionName() + string("' was not generated."));
  440. }
  441. }
  442. e = e->NextSiblingElement("memberdef");
  443. }
  444. }
  445. // Process public member functions.
  446. else if (strcmp(kind, "public-func") == 0)
  447. {
  448. XMLElement* e = node->FirstChildElement("memberdef");
  449. while (e)
  450. {
  451. if (e->FirstChildElement("templateparamlist"))
  452. {
  453. __printTemplateWarning = true;
  454. }
  455. else if (getScriptFlag(e) != "ignore")
  456. {
  457. if (strcmp(e->Attribute("virt"), "pure-virtual") == 0)
  458. hasPureVirtual = true;
  459. FunctionBinding b(classBinding.classname, classBinding.uniquename);
  460. b.type = FunctionBinding::MEMBER_FUNCTION;
  461. b.name = getName(e);
  462. if (!isReservedKeyword(b.name))
  463. {
  464. // Ignore operators.
  465. if (b.name.find("operator") != 0)
  466. {
  467. b.returnParam = getParam(e, false, b.classname);
  468. if (b.returnParam.type == FunctionBinding::Param::TYPE_CONSTRUCTOR)
  469. {
  470. b.returnParam.info = refId;
  471. b.own = true;
  472. }
  473. getParams(e, b);
  474. getCreateFlag(e, b);
  475. classBinding.bindings[b.getFunctionName()].push_back(b);
  476. }
  477. else
  478. {
  479. __printOperatorWarning = true;
  480. }
  481. }
  482. else
  483. {
  484. __warnings.insert(string("Function name '") + b.name + string("' is a reserved Lua keyword; binding '") + b.getFunctionName() + string("' was not generated."));
  485. }
  486. }
  487. e = e->NextSiblingElement("memberdef");
  488. }
  489. }
  490. // Process public static variables.
  491. else if (strcmp(kind, "public-static-attrib") == 0)
  492. {
  493. XMLElement* e = node->FirstChildElement("memberdef");
  494. while (e)
  495. {
  496. if (getScriptFlag(e) != "ignore")
  497. {
  498. FunctionBinding b(classBinding.classname, classBinding.uniquename);
  499. if (getIsConstType(e))
  500. b.type = FunctionBinding::STATIC_CONSTANT;
  501. else
  502. b.type = FunctionBinding::STATIC_VARIABLE;
  503. b.name = getName(e);
  504. if (!isReservedKeyword(b.name))
  505. {
  506. b.returnParam = getParam(e, true, b.classname);
  507. classBinding.bindings[b.getFunctionName()].push_back(b);
  508. }
  509. else
  510. {
  511. __warnings.insert(string("Function name '") + b.name + string("' is a reserved Lua keyword; binding '") + b.getFunctionName() + string("' was not generated."));
  512. }
  513. }
  514. e = e->NextSiblingElement("memberdef");
  515. }
  516. }
  517. // Process public member variables.
  518. else if (strcmp(kind, "public-attrib") == 0)
  519. {
  520. XMLElement* e = node->FirstChildElement("memberdef");
  521. while (e)
  522. {
  523. if (e->FirstChildElement("templateparamlist"))
  524. {
  525. __printTemplateWarning = true;
  526. }
  527. else if (getScriptFlag(e) != "ignore")
  528. {
  529. FunctionBinding b(classBinding.classname, classBinding.uniquename);
  530. if (getIsConstType(e))
  531. b.type = FunctionBinding::MEMBER_CONSTANT;
  532. else
  533. b.type = FunctionBinding::MEMBER_VARIABLE;
  534. b.name = getName(e);
  535. if (!isReservedKeyword(b.name))
  536. {
  537. b.returnParam = getParam(e, true, b.classname);
  538. classBinding.bindings[b.getFunctionName()].push_back(b);
  539. }
  540. else
  541. {
  542. __warnings.insert(string("Function name '") + b.name + string("' is a reserved Lua keyword; binding '") + b.getFunctionName() + string("' was not generated."));
  543. }
  544. }
  545. e = e->NextSiblingElement("memberdef");
  546. }
  547. }
  548. else if (strcmp(kind, "protected-func") == 0 ||
  549. strcmp(kind, "private-func") == 0)
  550. {
  551. // Check for inaccessible constructor/destructor
  552. // and create a list of all other inaccessible functions
  553. // (we do this so that classes that override a public function
  554. // and change its scope to protected or private work properly
  555. // from Lua-i.e. no binding is generated for such functions).
  556. XMLElement* e = node->FirstChildElement("memberdef");
  557. while (e)
  558. {
  559. if (strcmp(e->Attribute("virt"), "pure-virtual") == 0)
  560. hasPureVirtual = true;
  561. FunctionBinding::Param t = getParam(e);
  562. if (t.type == FunctionBinding::Param::TYPE_CONSTRUCTOR)
  563. {
  564. classBinding.inaccessibleConstructor = true;
  565. }
  566. else if (t.type == FunctionBinding::Param::TYPE_DESTRUCTOR)
  567. {
  568. classBinding.inaccessibleDestructor = true;
  569. }
  570. else
  571. {
  572. FunctionBinding b(classBinding.classname, classBinding.uniquename);
  573. b.type = FunctionBinding::MEMBER_FUNCTION;
  574. b.name = getName(e);
  575. if (!isReservedKeyword(b.name))
  576. {
  577. // Ignore operators.
  578. if (b.name.find("operator") != 0)
  579. {
  580. b.returnParam = getParam(e, false, b.classname);
  581. if (b.returnParam.type == FunctionBinding::Param::TYPE_CONSTRUCTOR)
  582. {
  583. b.returnParam.info = refId;
  584. b.own = true;
  585. }
  586. getParams(e, b);
  587. getCreateFlag(e, b);
  588. classBinding.hidden[b.getFunctionName()].push_back(b);
  589. }
  590. else
  591. {
  592. __printOperatorWarning = true;
  593. }
  594. }
  595. }
  596. e = e->NextSiblingElement("memberdef");
  597. }
  598. }
  599. else if (strcmp(kind, "protected-static-func") == 0 ||
  600. strcmp(kind, "private-static-func") == 0)
  601. {
  602. // Create a list of all other inaccessible functions
  603. // (we do this so that classes that override a public function
  604. // and change its scope to protected or private work properly
  605. // from Lua-i.e. no binding is generated for such functions).
  606. XMLElement* e = node->FirstChildElement("memberdef");
  607. while (e)
  608. {
  609. if (e->FirstChildElement("templateparamlist"))
  610. {
  611. __printTemplateWarning = true;
  612. }
  613. else if (getScriptFlag(e) != "ignore")
  614. {
  615. FunctionBinding b(classBinding.classname, classBinding.uniquename);
  616. b.type = FunctionBinding::STATIC_FUNCTION;
  617. b.name = getName(e);
  618. if (!isReservedKeyword(b.name))
  619. {
  620. b.returnParam = getParam(e, false, b.classname);
  621. getParams(e, b);
  622. getCreateFlag(e, b);
  623. classBinding.hidden[b.getFunctionName()].push_back(b);
  624. }
  625. }
  626. e = e->NextSiblingElement("memberdef");
  627. }
  628. }
  629. // Process public enums and typedefs.
  630. else if (strcmp(kind, "public-type") == 0)
  631. {
  632. XMLElement* e = node->FirstChildElement("memberdef");
  633. while (e)
  634. {
  635. if (getScriptFlag(e) != "ignore")
  636. {
  637. if (strcmp(e->Attribute("kind"), "enum") == 0)
  638. {
  639. getEnum(e, classBinding.classname, classBinding.ns, classBinding.include);
  640. }
  641. else if (strcmp(e->Attribute("kind"), "typedef") == 0)
  642. {
  643. getTypedef(e, classBinding.classname, classBinding.ns);
  644. }
  645. }
  646. e = e->NextSiblingElement("memberdef");
  647. }
  648. }
  649. else
  650. {
  651. // Ignore other section definitions.
  652. }
  653. }
  654. // If the class has a pure-virtual method, we mark the constructor as inaccessible
  655. // and remove any constructor bindings that are in the list.
  656. if (hasPureVirtual)
  657. {
  658. classBinding.inaccessibleConstructor = true;
  659. map<string, vector<FunctionBinding> >::iterator iter = classBinding.bindings.begin();
  660. for (; iter != classBinding.bindings.end(); iter++)
  661. {
  662. if (iter->second[0].returnParam.type == FunctionBinding::Param::TYPE_CONSTRUCTOR)
  663. {
  664. classBinding.bindings.erase(iter);
  665. break;
  666. }
  667. }
  668. }
  669. // Store the class's derived class(es)' ref id(s) if it has any.
  670. node = classNode->FirstChildElement("derivedcompoundref");
  671. while (node)
  672. {
  673. classBinding.derived.push_back(node->Attribute("refid"));
  674. node = node->NextSiblingElement("derivedcompoundref");
  675. }
  676. // If the class has no base classes and it has derived classes, add it to the list of top-level classes.
  677. if (!classNode->FirstChildElement("basecompoundref") && classBinding.derived.size() > 0)
  678. _topLevelBaseClasses.push_back(classBinding.classname);
  679. // Insert the class binding into the global table.
  680. _classes[classBinding.classname] = classBinding;
  681. }
  682. void Generator::getEnums(XMLElement* fileNode, string ns)
  683. {
  684. XMLElement* node = NULL;
  685. const char* kind = NULL;
  686. for (node = fileNode->FirstChildElement("sectiondef"); node != NULL; node = node->NextSiblingElement("sectiondef"))
  687. {
  688. kind = node->Attribute("kind");
  689. if (!kind)
  690. {
  691. GP_ERROR("Section definition is missing required 'kind' attribute in XML document '%s'.", _file);
  692. return;
  693. }
  694. // Process the enums.
  695. if (strcmp(kind, "enum") == 0)
  696. {
  697. XMLElement* e = node->FirstChildElement("memberdef");
  698. while (e)
  699. {
  700. if (getScriptFlag(e) != "ignore" && strcmp(e->Attribute("kind"), "enum") == 0)
  701. {
  702. getEnum(e, "", ns);
  703. }
  704. e = e->NextSiblingElement("memberdef");
  705. }
  706. }
  707. }
  708. }
  709. void Generator::getEnum(XMLElement* e, string classname, string ns, string include)
  710. {
  711. // Get the enumeration's name.
  712. string enumname = classname;
  713. if (enumname.size() > 0)
  714. enumname += string("::");
  715. enumname += getName(e);
  716. // Get the ref id for the enum.
  717. string refId = e->Attribute("id");
  718. // Store the ref id to name mapping.
  719. Generator::getInstance()->setIdentifier(refId, enumname);
  720. // Add the enum to the enum list if it's not there
  721. // (we implicitly create it when we set its scope path).
  722. if (_enums.find(enumname) == _enums.end())
  723. _enums[enumname].scopePath = Generator::getScopePath(enumname, ns);
  724. // Set the include file for the enum.
  725. if (include.size() > 0)
  726. _enums[enumname].include = include;
  727. else
  728. {
  729. XMLElement* location = e->FirstChildElement("location");
  730. if (location)
  731. _enums[enumname].include = location->Attribute("file");
  732. }
  733. for (e = e->FirstChildElement("enumvalue"); e; e = e->NextSiblingElement("enumvalue"))
  734. {
  735. _enums[enumname].values.push_back(getName(e));
  736. }
  737. // Add the enum to the set of namespace members if it is part of a namespace.
  738. if (ns.size() > 0)
  739. _namespaces[ns].insert(enumname);
  740. }
  741. void Generator::getTypedefs(XMLElement* fileNode, string ns)
  742. {
  743. XMLElement* node = NULL;
  744. const char* kind = NULL;
  745. for (node = fileNode->FirstChildElement("sectiondef"); node != NULL; node = node->NextSiblingElement("sectiondef"))
  746. {
  747. kind = node->Attribute("kind");
  748. if (!kind)
  749. {
  750. GP_ERROR("Section definition is missing required 'kind' attribute in XML document '%s'.", _file);
  751. return;
  752. }
  753. // Process the enums.
  754. if (strcmp(kind, "typedef") == 0)
  755. {
  756. XMLElement* e = node->FirstChildElement("memberdef");
  757. while (e)
  758. {
  759. if (getScriptFlag(e) != "ignore" && strcmp(e->Attribute("kind"), "typedef") == 0)
  760. {
  761. getTypedef(e, "", ns);
  762. }
  763. e = e->NextSiblingElement("memberdef");
  764. }
  765. }
  766. }
  767. }
  768. void Generator::getTypedef(XMLElement* e, string classname, string ns)
  769. {
  770. TypedefBinding t;
  771. // Get the typedef's name.
  772. string tname = classname;
  773. if (tname.size() > 0)
  774. tname += string("::");
  775. tname += getName(e);
  776. // Get the typedef's id and store the mapping from ref id to name.
  777. string refId = e->Attribute("id");
  778. setIdentifier(refId, tname);
  779. FunctionBinding::Param p = getParam(e);
  780. if (p.info.size() > 0)
  781. t.refId = p.info;
  782. _typedefs[tname] = t;
  783. // Add the typedef to the set of namespace members if it is part of a namespace.
  784. if (ns.size() > 0)
  785. _namespaces[ns].insert(tname);
  786. }
  787. static inline bool getScriptFlagHelper(XMLElement* e, string& flag)
  788. {
  789. XMLElement* x = e->FirstChildElement("xmlonly");
  790. if (x)
  791. {
  792. flag = x->GetText();
  793. return true;
  794. }
  795. x = e->FirstChild() ? e->FirstChild()->ToElement() : NULL;
  796. while (x)
  797. {
  798. if (getScriptFlagHelper(x, flag))
  799. return true;
  800. x = x->NextSibling() ? x->NextSibling()->ToElement() : NULL;
  801. }
  802. return false;
  803. }
  804. string Generator::getScriptFlag(XMLElement* e)
  805. {
  806. string flag;
  807. XMLElement* n = e->FirstChildElement("detaileddescription");
  808. if (n)
  809. getScriptFlagHelper(n, flag);
  810. return flag;
  811. }
  812. void Generator::getCreateFlag(XMLElement* e, FunctionBinding& b)
  813. {
  814. if (getScriptFlag(e) == "create")
  815. b.own = true;
  816. }
  817. string Generator::getName(XMLElement* e)
  818. {
  819. // Get the name for the binding.
  820. XMLElement* nameElement = e->FirstChildElement("name");
  821. if (!nameElement)
  822. {
  823. GP_ERROR("Missing 'name' child node in XML document '%s'.", _file);
  824. return string();
  825. }
  826. return nameElement->GetText();
  827. }
  828. bool Generator::getIsConstType(XMLElement* e)
  829. {
  830. XMLElement* typeElement = e->FirstChildElement("type");
  831. if (!typeElement)
  832. {
  833. GP_ERROR("Missing 'type' node in XML document '%s'.", _file);
  834. return false;
  835. }
  836. XMLNode* textNode = typeElement->FirstChild();
  837. if (textNode)
  838. {
  839. XMLText* text = textNode->ToText();
  840. if (text)
  841. {
  842. string type = text->Value();
  843. return type.find("const") != type.npos;
  844. }
  845. }
  846. return false;
  847. }
  848. FunctionBinding::Param Generator::getParam(XMLElement* e, bool isVariable, string classname)
  849. {
  850. // Get the type for the current element (either the return type or a parameter type).
  851. XMLElement* typeElement = e->FirstChildElement("type");
  852. if (!typeElement)
  853. {
  854. GP_ERROR("Missing 'type' node in XML document '%s'.", _file);
  855. }
  856. FunctionBinding::Param p;
  857. XMLNode* textNode = typeElement->FirstChild();
  858. if (!textNode)
  859. {
  860. // Get the name of the current element to see if it is a destructor.
  861. string name = getName(e);
  862. if (name.find("~") == 0)
  863. {
  864. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_DESTRUCTOR);
  865. }
  866. else
  867. {
  868. // Assume it's a constructor.
  869. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_CONSTRUCTOR, FunctionBinding::Param::KIND_POINTER, name);
  870. }
  871. }
  872. else
  873. {
  874. // Get the type string without const or reference qualifiers (and trim whitespace).
  875. string refId = "";
  876. string typeStr = "";
  877. int levelsOfIndirection = 0;
  878. FunctionBinding::Param::Kind kind;
  879. {
  880. // Attempt to process the type as reference (i.e. class, struct, enum, typedef, etc.) type.
  881. XMLNode* node = textNode;
  882. while (node)
  883. {
  884. if (strcmp(node->Value(), "ref") == 0)
  885. {
  886. refId = node->ToElement()->Attribute("refid");
  887. typeStr += node->ToElement()->GetText();
  888. }
  889. else if (node->ToText())
  890. {
  891. typeStr += node->ToText()->Value();
  892. }
  893. node = node->NextSibling();
  894. }
  895. typeStr = stripTypeQualifiers(typeStr, kind, levelsOfIndirection);
  896. }
  897. if (typeStr == "void")
  898. {
  899. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_VOID, kind);
  900. }
  901. else if (typeStr == "bool")
  902. {
  903. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_BOOL, kind);
  904. }
  905. else if (typeStr == "char" && kind != FunctionBinding::Param::KIND_POINTER)
  906. {
  907. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_CHAR, kind);
  908. }
  909. else if (typeStr == "short")
  910. {
  911. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_SHORT, kind);
  912. }
  913. else if (typeStr == "int")
  914. {
  915. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_INT, kind);
  916. }
  917. else if (typeStr == "long")
  918. {
  919. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_LONG, kind);
  920. }
  921. else if (typeStr == "unsigned char")
  922. {
  923. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_UCHAR, kind);
  924. }
  925. else if (typeStr == "unsigned short")
  926. {
  927. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_USHORT, kind);
  928. }
  929. else if (typeStr == "unsigned int")
  930. {
  931. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_UINT, kind);
  932. }
  933. else if (typeStr == "unsigned long")
  934. {
  935. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_ULONG, kind);
  936. }
  937. else if (typeStr == "float")
  938. {
  939. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_FLOAT, kind);
  940. }
  941. else if (typeStr == "double")
  942. {
  943. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_DOUBLE, kind);
  944. }
  945. else if (typeStr == "string" || typeStr == "std::string")
  946. {
  947. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_STRING, kind, "string");
  948. }
  949. else if (typeStr == "char" && kind == FunctionBinding::Param::KIND_POINTER)
  950. {
  951. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_STRING, kind, "char*");
  952. }
  953. else if (_enums.find(typeStr) != _enums.end() ||
  954. (classname.size() > 0 && _enums.find(classname + string("::") + typeStr) != _enums.end()))
  955. {
  956. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_ENUM, kind, refId);
  957. }
  958. else if (typeStr == "...")
  959. {
  960. __printVarargWarning = true;
  961. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_VARARGS);
  962. }
  963. else
  964. {
  965. p = FunctionBinding::Param(FunctionBinding::Param::TYPE_UNRECOGNIZED, kind, (refId.size() > 0) ? refId : typeStr);
  966. }
  967. p.levelsOfIndirection = levelsOfIndirection;
  968. // Check if the type is a pointer declared with square brackets (i.e. float x[4]).
  969. XMLElement* arrayElement = NULL;
  970. if ((arrayElement = e->FirstChildElement("array")) != NULL ||
  971. (isVariable && (arrayElement = e->FirstChildElement("argsstring")) != NULL))
  972. {
  973. const char* text = arrayElement->GetText();
  974. string arrayString = (text ? text : "");
  975. string::size_type i = arrayString.find("[");
  976. string::size_type k = arrayString.find("]");
  977. if (i != arrayString.npos && k != arrayString.npos)
  978. {
  979. p.kind = FunctionBinding::Param::KIND_POINTER;
  980. p.levelsOfIndirection = 1;
  981. if (i != k - 1)
  982. p.info = arrayString.substr(i + 1, k - (i + 1));
  983. }
  984. }
  985. }
  986. // Get the default value for the parameter if it has one.
  987. XMLElement* defaultValueElement = e->FirstChildElement("defval");
  988. if (defaultValueElement)
  989. {
  990. p.hasDefaultValue = true;
  991. }
  992. return p;
  993. }
  994. void Generator::getParams(XMLElement* e, FunctionBinding& b)
  995. {
  996. XMLElement* param = e->FirstChildElement("param");
  997. while (param)
  998. {
  999. FunctionBinding::Param p = getParam(param, false, b.classname);
  1000. if (p.type != FunctionBinding::Param::TYPE_VARARGS)
  1001. b.paramTypes.push_back(p);
  1002. param = param->NextSiblingElement("param");
  1003. }
  1004. }
  1005. void Generator::resolveMembers(const ClassBinding& c)
  1006. {
  1007. vector<ClassBinding*> derived;
  1008. // Go through the derived classes' bindings and update them.
  1009. ClassBinding* cb = NULL;
  1010. map<string, vector<FunctionBinding> >::const_iterator iter;
  1011. for (unsigned int i = 0; i < c.derived.size(); i++)
  1012. {
  1013. // If the class is not in the map of classes, then
  1014. // it was marked to be ignored, so skip over it.
  1015. string derivedClassName = getIdentifier(c.derived[i]);
  1016. if (_classes.find(derivedClassName) == _classes.end())
  1017. continue;
  1018. cb = &_classes[derivedClassName];
  1019. // Go through this class' bindings and add them to the current
  1020. // derived class if they are not already there (excluding constructors and destructors).
  1021. for (iter = c.bindings.begin(); iter != c.bindings.end(); iter++)
  1022. {
  1023. FunctionBinding b(cb->classname, cb->uniquename);
  1024. b.type = iter->second[0].type;
  1025. b.name = iter->second[0].name;
  1026. map<string, vector<FunctionBinding> >::iterator findIter = cb->bindings.find(b.getFunctionName());
  1027. map<string, vector<FunctionBinding> >::iterator hiddenIter = cb->hidden.find(b.getFunctionName());
  1028. if (findIter == cb->bindings.end() && hiddenIter == cb->hidden.end())
  1029. {
  1030. for (unsigned int i = 0, iCount = iter->second.size(); i < iCount; i++)
  1031. {
  1032. if (iter->second[i].returnParam.type != FunctionBinding::Param::TYPE_CONSTRUCTOR &&
  1033. iter->second[i].returnParam.type != FunctionBinding::Param::TYPE_DESTRUCTOR)
  1034. {
  1035. // Copy the binding, but set its function name to
  1036. // empty so that it is regenerated correctly.
  1037. b = iter->second[i];
  1038. b.functionName = "";
  1039. b.classname = cb->classname;
  1040. b.uniquename = cb->uniquename;
  1041. cb->bindings[b.getFunctionName()].push_back(b);
  1042. }
  1043. }
  1044. }
  1045. else
  1046. {
  1047. bool addBinding;
  1048. for (unsigned int i = 0, iCount = iter->second.size(); i < iCount; i++)
  1049. {
  1050. string name = iter->second[i].name;
  1051. addBinding = true;
  1052. if (findIter != cb->bindings.end())
  1053. {
  1054. for (unsigned int j = 0, jCount = findIter->second.size(); j < jCount; j++)
  1055. {
  1056. if (FunctionBinding::signaturesMatch(iter->second[i], findIter->second[j]))
  1057. {
  1058. addBinding = false;
  1059. break;
  1060. }
  1061. }
  1062. // To call the base function, we have to qualify the call since
  1063. // the derived class has a function with the same name and different parameters.
  1064. if (addBinding)
  1065. name = iter->second[i].classname + string("::") + iter->second[i].name;
  1066. }
  1067. if (hiddenIter != cb->hidden.end())
  1068. {
  1069. for (unsigned int j = 0, jCount = hiddenIter->second.size(); j < jCount; j++)
  1070. {
  1071. if (FunctionBinding::signaturesMatch(iter->second[i], hiddenIter->second[j]))
  1072. {
  1073. addBinding = false;
  1074. break;
  1075. }
  1076. }
  1077. }
  1078. if (addBinding)
  1079. {
  1080. FunctionBinding b = iter->second[i];
  1081. b.name = name;
  1082. b.functionName = findIter->first;
  1083. b.classname = cb->classname;
  1084. b.uniquename = getUniqueName(cb->classname);
  1085. if (findIter != cb->bindings.end())
  1086. findIter->second.push_back(b);
  1087. else
  1088. cb->bindings[b.getFunctionName()].push_back(b);
  1089. }
  1090. }
  1091. }
  1092. }
  1093. derived.push_back(cb);
  1094. }
  1095. // Go through the derived classes' bindings and resolve the members for their derived classes.
  1096. for (unsigned int i = 0; i < derived.size(); i++)
  1097. {
  1098. resolveMembers(*derived[i]);
  1099. }
  1100. }
  1101. void Generator::resolveInheritance()
  1102. {
  1103. // Go through the class inheritance tree and update each class's bindings list
  1104. // to include the bindings from the base class that haven't been overridden.
  1105. for (unsigned int i = 0; i < _topLevelBaseClasses.size(); i++)
  1106. {
  1107. resolveMembers(_classes[_topLevelBaseClasses[i]]);
  1108. }
  1109. }
  1110. void Generator::resolveIncludes(const ClassBinding& c)
  1111. {
  1112. vector<ClassBinding*> derived;
  1113. // Go through the derived classes' bindings and update them.
  1114. ClassBinding* cb = NULL;
  1115. map<string, vector<FunctionBinding> >::const_iterator iter;
  1116. for (unsigned int i = 0; i < c.derived.size(); i++)
  1117. {
  1118. // If the class is not in the map of classes, then
  1119. // it was marked to be ignored, so skip over it.
  1120. string derivedClassName = getIdentifier(c.derived[i]);
  1121. if (_classes.find(derivedClassName) == _classes.end())
  1122. continue;
  1123. cb = &_classes[derivedClassName];
  1124. // Add all include files (uniquely) from the base class to the derived class.
  1125. map<string, set<string> >::iterator baseIncludes = _includes.find(c.include);
  1126. if (baseIncludes != _includes.end() && baseIncludes->second.size() > 0)
  1127. {
  1128. set<string>::iterator iter = baseIncludes->second.begin();
  1129. set<string>& derivedIncludes = _includes[cb->include];
  1130. for (; iter != baseIncludes->second.end(); iter++)
  1131. {
  1132. derivedIncludes.insert(*iter);
  1133. }
  1134. }
  1135. derived.push_back(cb);
  1136. }
  1137. // Go through the derived classes' bindings and resolve the members for their derived classes.
  1138. for (unsigned int i = 0; i < derived.size(); i++)
  1139. {
  1140. resolveIncludes(*derived[i]);
  1141. }
  1142. }
  1143. void Generator::resolveInheritedIncludes()
  1144. {
  1145. // Go through the class inheritance tree and update each class's
  1146. // list of includes to include the includes from the base class
  1147. for (unsigned int i = 0; i < _topLevelBaseClasses.size(); i++)
  1148. {
  1149. resolveIncludes(_classes[_topLevelBaseClasses[i]]);
  1150. }
  1151. }
  1152. void Generator::resolveType(FunctionBinding::Param* param, string functionName, string header)
  1153. {
  1154. string name = getIdentifier(param->info);
  1155. if (param->type == FunctionBinding::Param::TYPE_UNRECOGNIZED)
  1156. {
  1157. map<string, TypedefBinding>::iterator typedefIter = _typedefs.find(name);
  1158. if (typedefIter != _typedefs.end() && typedefIter->second.refId.size() > 0)
  1159. {
  1160. param->type = FunctionBinding::Param::TYPE_OBJECT;
  1161. param->info = typedefIter->second.refId;
  1162. }
  1163. else
  1164. {
  1165. map<string, ClassBinding>::iterator classIter = _classes.find(name);
  1166. if (classIter != _classes.end())
  1167. {
  1168. param->type = FunctionBinding::Param::TYPE_OBJECT;
  1169. }
  1170. else
  1171. {
  1172. map<string, EnumBinding>::iterator enumIter = _enums.find(name);
  1173. if (enumIter != _enums.end())
  1174. {
  1175. param->type = FunctionBinding::Param::TYPE_ENUM;
  1176. }
  1177. else
  1178. {
  1179. __warnings.insert(string("Unrecognized C++ type: ") + functionName + string(" -- ") + (name.size() > 0 ? name : param->info));
  1180. }
  1181. }
  1182. }
  1183. }
  1184. // Ensure that the header for the Lua enum conversion
  1185. // functions is included in the file containing the
  1186. // generated binding that this param/return value is part of.
  1187. if (param->type == FunctionBinding::Param::TYPE_ENUM)
  1188. {
  1189. string enumHeader = string("lua_") + getUniqueName(name) + string(".h");
  1190. if (_includes.find(header) != _includes.end())
  1191. {
  1192. set<string>& includes = _includes[header];
  1193. if (includes.find(enumHeader) == includes.end())
  1194. includes.insert(enumHeader);
  1195. }
  1196. else
  1197. {
  1198. _includes[header].insert(enumHeader);
  1199. }
  1200. }
  1201. }
  1202. void Generator::resolveTypes()
  1203. {
  1204. // Go through all member functions and attempt to resolve unrecognized types.
  1205. for (map<string, ClassBinding>::iterator iter = _classes.begin(); iter != _classes.end(); iter++)
  1206. {
  1207. map<string, vector<FunctionBinding> >::iterator functionNameIter = iter->second.bindings.begin();
  1208. for (; functionNameIter != iter->second.bindings.end(); functionNameIter++)
  1209. {
  1210. vector<FunctionBinding>::iterator functionIter = functionNameIter->second.begin();
  1211. for (; functionIter != functionNameIter->second.end(); functionIter++)
  1212. {
  1213. resolveType(&functionIter->returnParam, iter->first + string("::") + functionIter->name, iter->second.include);
  1214. vector<FunctionBinding::Param>::iterator paramIter = functionIter->paramTypes.begin();
  1215. for (; paramIter != functionIter->paramTypes.end(); paramIter++)
  1216. {
  1217. resolveType(&(*paramIter), iter->first + string("::") + functionIter->name, iter->second.include);
  1218. }
  1219. }
  1220. }
  1221. }
  1222. // Go through all non-member functions and attempt to resolve unrecognized types.
  1223. string globalHeader = string("lua_") + string(LUA_GLOBAL_FILENAME) + string(".h");
  1224. for (map<string, vector<FunctionBinding> >::iterator iter = _functions.begin(); iter != _functions.end(); iter++)
  1225. {
  1226. vector<FunctionBinding>::iterator functionIter = iter->second.begin();
  1227. for (; functionIter != iter->second.end(); functionIter++)
  1228. {
  1229. resolveType(&functionIter->returnParam, functionIter->name, globalHeader);
  1230. vector<FunctionBinding::Param>::iterator paramIter = functionIter->paramTypes.begin();
  1231. for (; paramIter != functionIter->paramTypes.end(); paramIter++)
  1232. {
  1233. resolveType(&(*paramIter), functionIter->name, globalHeader);
  1234. }
  1235. }
  1236. }
  1237. }
  1238. void Generator::generateBindings(string* bindingNS)
  1239. {
  1240. bool generatingGameplay = false;
  1241. if (bindingNS && (*bindingNS == "gameplay"))
  1242. generatingGameplay = true;
  1243. string luaAllHStr = _outDir + string(LUA_ALL_BINDINGS_FILENAME) + string(".h");
  1244. ostringstream luaAllH;
  1245. string includeGuard = string(LUA_ALL_BINDINGS_FILENAME) + string("_H_");
  1246. transform(includeGuard.begin(), includeGuard.end(), includeGuard.begin(), ::toupper);
  1247. luaAllH << "#ifndef " << includeGuard << "\n";
  1248. luaAllH << "#define " << includeGuard << "\n\n";
  1249. string luaAllCppStr = _outDir + string(LUA_ALL_BINDINGS_FILENAME) + string(".cpp");
  1250. ostringstream luaAllCpp;
  1251. luaAllCpp << "#include \"Base.h\"\n";
  1252. luaAllCpp << "#include \"" << string(LUA_ALL_BINDINGS_FILENAME) << ".h\"\n\n";
  1253. if (bindingNS)
  1254. {
  1255. luaAllCpp << "namespace " << *bindingNS << "\n";
  1256. luaAllCpp << "{\n\n";
  1257. }
  1258. luaAllCpp << "void lua_RegisterAllBindings()\n";
  1259. luaAllCpp << "{\n";
  1260. // Write out all the class bindings.
  1261. if (_classes.size() > 0)
  1262. {
  1263. map<string, ClassBinding>::iterator iter = _classes.begin();
  1264. for (; iter != _classes.end(); iter++)
  1265. {
  1266. if (generatingGameplay || (!generatingGameplay && _namespaces["gameplay"].find(iter->second.classname) == _namespaces["gameplay"].end()))
  1267. {
  1268. cout << "Generating bindings for '" << iter->first << "'...\n";
  1269. iter->second.write(_outDir, _includes[iter->second.include], bindingNS);
  1270. luaAllH << "#include \"lua_" << iter->second.uniquename << ".h\"\n";
  1271. luaAllCpp << " luaRegister_" << iter->second.uniquename << "();\n";
  1272. }
  1273. }
  1274. }
  1275. // Go through all the classes and if they have any derived classes, add them to the list of base classes.
  1276. vector<string> baseClasses;
  1277. for (map<string, ClassBinding>::iterator iter = _classes.begin(); iter != _classes.end(); iter++)
  1278. {
  1279. if (iter->second.derived.size() > 0)
  1280. baseClasses.push_back(iter->first);
  1281. }
  1282. // Write out all the enum files.
  1283. if (_enums.size() > 0)
  1284. {
  1285. // Write out the enum conversion function declarations.
  1286. for (map<string, EnumBinding>::iterator iter = _enums.begin(); iter != _enums.end(); iter++)
  1287. {
  1288. if (generatingGameplay || (!generatingGameplay && _namespaces["gameplay"].find(iter->first) == _namespaces["gameplay"].end()))
  1289. {
  1290. cout << "Generating bindings for '" << iter->first << "'...\n";
  1291. // Header.
  1292. string enumHStr = _outDir + string("lua_") + getUniqueName(iter->first) + string(".h");
  1293. ostringstream enumH;
  1294. includeGuard = string("lua_") + getUniqueName(iter->first) + string("_H_");
  1295. transform(includeGuard.begin(), includeGuard.end(), includeGuard.begin(), ::toupper);
  1296. enumH << "#ifndef " << includeGuard << "\n";
  1297. enumH << "#define " << includeGuard << "\n\n";
  1298. enumH << "#include \"" << iter->second.include << "\"\n\n";
  1299. if (bindingNS)
  1300. {
  1301. enumH << "namespace " << *bindingNS << "\n";
  1302. enumH << "{\n\n";
  1303. }
  1304. enumH << "// Lua bindings for enum conversion functions for " << iter->first << ".\n";
  1305. enumH << iter->first << " lua_enumFromString_" << getUniqueName(iter->first) << "(const char* s);\n";
  1306. enumH << "const char* lua_stringFromEnum_" << getUniqueName(iter->first) << "(" << iter->first << " e);\n\n";
  1307. if (bindingNS)
  1308. {
  1309. enumH << "}\n\n";
  1310. }
  1311. enumH << "#endif\n";
  1312. writeFile(enumHStr, enumH.str());
  1313. // Implementation.
  1314. string enumCppStr = _outDir + string("lua_") + getUniqueName(iter->first) + string(".cpp");
  1315. ostringstream enumCpp(enumCppStr.c_str());
  1316. enumCpp << "#include \"Base.h\"\n";
  1317. enumCpp << "#include \"lua_" << getUniqueName(iter->first) << ".h\"\n\n";
  1318. if (bindingNS)
  1319. {
  1320. enumCpp << "namespace " << *bindingNS << "\n";
  1321. enumCpp << "{\n\n";
  1322. }
  1323. enumCpp << "static const char* enumStringEmpty = \"\";\n\n";
  1324. // Build the scope string if applicable.
  1325. string scope;
  1326. if (iter->second.scopePath.size() > 0)
  1327. {
  1328. for (unsigned int i = 0; i < iter->second.scopePath.size(); i++)
  1329. {
  1330. scope += iter->second.scopePath[i] + string("::");
  1331. }
  1332. }
  1333. // Write out the string constants that correspond to the enumeration values.
  1334. for (unsigned int i = 0; i < iter->second.values.size(); i++)
  1335. {
  1336. enumCpp << "static const char* luaEnumString_" << getUniqueName(iter->first) << "_";
  1337. enumCpp << iter->second.values[i] << " = \"" << iter->second.values[i] << "\";\n";
  1338. }
  1339. enumCpp << "\n";
  1340. enumCpp << iter->first << " lua_enumFromString_" << getUniqueName(iter->first) << "(const char* s)\n";
  1341. enumCpp << "{\n";
  1342. for (unsigned int i = 0; i < iter->second.values.size(); i++)
  1343. {
  1344. enumCpp << " ";
  1345. enumCpp << "if (strcmp(s, luaEnumString_" << getUniqueName(iter->first) << "_" << iter->second.values[i] << ") == 0)\n";
  1346. enumCpp << " return ";
  1347. if (scope.size() > 0)
  1348. enumCpp << scope;
  1349. enumCpp << iter->second.values[i] << ";\n";
  1350. if (i == iter->second.values.size() - 1)
  1351. {
  1352. //enumCpp << " GP_ERROR(\"Invalid enumeration value '%s' for enumeration " << iter->first << ".\", s);\n";
  1353. enumCpp << " return ";
  1354. if (scope.size() > 0)
  1355. enumCpp << scope;
  1356. enumCpp << iter->second.values[0] << ";\n";
  1357. }
  1358. }
  1359. enumCpp << "}\n\n";
  1360. enumCpp << "const char* lua_stringFromEnum_" << getUniqueName(iter->first) << "(" << iter->first << " e)\n";
  1361. enumCpp << "{\n";
  1362. // Write out the enum-to-string conversion code.
  1363. for (unsigned int i = 0; i < iter->second.values.size(); i++)
  1364. {
  1365. enumCpp << " ";
  1366. enumCpp << "if (e == ";
  1367. if (scope.size() > 0)
  1368. enumCpp << scope;
  1369. enumCpp << iter->second.values[i] << ")\n";
  1370. enumCpp << " return luaEnumString_" << getUniqueName(iter->first) << "_" << iter->second.values[i] << ";\n";
  1371. if (i == iter->second.values.size() - 1)
  1372. {
  1373. //enumCpp << " GP_ERROR(\"Invalid enumeration value '%d' for enumeration " << iter->first << ".\", e);\n";
  1374. enumCpp << " return enumStringEmpty;\n";
  1375. }
  1376. }
  1377. enumCpp << "}\n\n";
  1378. if (bindingNS)
  1379. {
  1380. enumCpp << "}\n\n";
  1381. }
  1382. writeFile(enumCppStr, enumCpp.str());
  1383. }
  1384. }
  1385. }
  1386. // Write out the global bindings file.
  1387. cout << "Generating global bindings...\n";
  1388. if (baseClasses.size() > 0 || _functions.size() > 0 || _enums.size() > 0)
  1389. {
  1390. // Calculate if there are global function bindings to write out.
  1391. bool generateGlobalFunctionBindings = false;
  1392. if (_functions.size() > 0)
  1393. {
  1394. for (map<string, vector<FunctionBinding> >::iterator iter = _functions.begin(); iter != _functions.end(); iter++)
  1395. {
  1396. if (generatingGameplay || (!generatingGameplay && _namespaces["gameplay"].find(iter->second[0].name) == _namespaces["gameplay"].end()))
  1397. {
  1398. generateGlobalFunctionBindings = true;
  1399. break;
  1400. }
  1401. }
  1402. }
  1403. // Calculate if there are enum bindings to write out.
  1404. bool generateEnumBindings = false;
  1405. if (_enums.size() > 0)
  1406. {
  1407. for (map<string, EnumBinding>::iterator iter = _enums.begin(); iter != _enums.end(); iter++)
  1408. {
  1409. if (generatingGameplay || (!generatingGameplay && _namespaces["gameplay"].find(iter->first) == _namespaces["gameplay"].end()))
  1410. {
  1411. generateEnumBindings = true;
  1412. break;
  1413. }
  1414. }
  1415. }
  1416. // Write out the header file.
  1417. {
  1418. string path = _outDir + string(LUA_GLOBAL_FILENAME) + string(".h");
  1419. ostringstream global;
  1420. includeGuard = string(LUA_GLOBAL_FILENAME) + string("_H_");
  1421. transform(includeGuard.begin(), includeGuard.end(), includeGuard.begin(), ::toupper);
  1422. global << "#ifndef " << includeGuard << "\n";
  1423. global << "#define " << includeGuard << "\n\n";
  1424. // Write out the needed includes for the global enumeration function.
  1425. for (map<string, EnumBinding>::iterator iter = _enums.begin(); iter != _enums.end(); iter++)
  1426. {
  1427. if (generatingGameplay || (!generatingGameplay && _namespaces["gameplay"].find(iter->first) == _namespaces["gameplay"].end()))
  1428. global << "#include \"lua_" << getUniqueName(iter->first) << ".h\"\n";
  1429. }
  1430. global << "\n";
  1431. luaAllH << "#include \"" << string(LUA_GLOBAL_FILENAME) << ".h\"\n";
  1432. luaAllCpp << " luaRegister_" << LUA_GLOBAL_FILENAME << "();\n";
  1433. if (bindingNS)
  1434. {
  1435. global << "namespace " << *bindingNS << "\n";
  1436. global << "{\n\n";
  1437. }
  1438. if (generateGlobalFunctionBindings)
  1439. {
  1440. global << "// Lua bindings for global functions.\n";
  1441. // Write out the binding function declarations.
  1442. for (map<string, vector<FunctionBinding> >::iterator iter = _functions.begin(); iter != _functions.end(); iter++)
  1443. {
  1444. if (generatingGameplay || (!generatingGameplay && _namespaces["gameplay"].find(iter->second[0].name) == _namespaces["gameplay"].end()))
  1445. global << "int " << iter->second[0].getFunctionName() << "(lua_State* state);\n";
  1446. }
  1447. global << "\n";
  1448. }
  1449. if (generateEnumBindings)
  1450. {
  1451. global << "// Global enum to string conversion function (used to pass enums to Lua from C++).\n";
  1452. global << "const char* lua_stringFromEnumGlobal(std::string& enumname, unsigned int value);\n\n";
  1453. }
  1454. // Write out the signature of the function used to register the global functions with Lua.
  1455. global << "void luaRegister_" << LUA_GLOBAL_FILENAME << "();\n\n";
  1456. if (bindingNS)
  1457. global << "}\n\n";
  1458. global << "#endif\n";
  1459. writeFile(path, global.str());
  1460. }
  1461. // Write out the implementation.
  1462. {
  1463. string path = _outDir + string(LUA_GLOBAL_FILENAME) + string(".cpp");
  1464. ostringstream global;
  1465. global << "#include \"ScriptController.h\"\n";
  1466. global << "#include \"" << LUA_GLOBAL_FILENAME << ".h\"\n";
  1467. map<string, set<string> >::iterator iter = _includes.find(string(LUA_GLOBAL_FILENAME) + string(".h"));
  1468. if (iter != _includes.end())
  1469. {
  1470. set<string>::iterator includeIter = iter->second.begin();
  1471. for (; includeIter != iter->second.end(); includeIter++)
  1472. {
  1473. global << "#include \"" << *includeIter << "\"\n";
  1474. }
  1475. }
  1476. global << "\n";
  1477. if (bindingNS)
  1478. {
  1479. global << "namespace " << *bindingNS << "\n";
  1480. global << "{\n\n";
  1481. }
  1482. // Write out the function used to register all global bindings with Lua.
  1483. global << "void luaRegister_" << LUA_GLOBAL_FILENAME << "()\n";
  1484. global << "{\n";
  1485. if (generateGlobalFunctionBindings)
  1486. {
  1487. // Bind the non-member functions.
  1488. for (map<string, vector<FunctionBinding> >::iterator iter = _functions.begin(); iter != _functions.end(); iter++)
  1489. {
  1490. if (generatingGameplay || (!generatingGameplay && _namespaces["gameplay"].find(iter->second[0].name) == _namespaces["gameplay"].end()))
  1491. global << " gameplay::ScriptUtil::registerFunction(\"" << iter->second[0].name << "\", " << iter->second[0].getFunctionName() << ");\n";
  1492. }
  1493. }
  1494. // Generate the hierarchy map.
  1495. if (baseClasses.size() > 0)
  1496. {
  1497. for (unsigned int i = 0, count = baseClasses.size(); i < count; i++)
  1498. {
  1499. set<string> derived;
  1500. getAllDerived(derived, baseClasses[i]);
  1501. for (set<string>::iterator iter = derived.begin(); iter != derived.end(); iter++)
  1502. {
  1503. if (generatingGameplay || (!generatingGameplay && _namespaces["gameplay"].find(*iter) == _namespaces["gameplay"].end()))
  1504. global << " gameplay::ScriptUtil::setGlobalHierarchyPair(\"" << baseClasses[i] << "\", \"" << *iter << "\");\n";
  1505. }
  1506. }
  1507. }
  1508. // Register all enums.
  1509. if (generateEnumBindings)
  1510. {
  1511. global << " gameplay::ScriptUtil::addStringFromEnumConversionFunction(&";
  1512. if (bindingNS)
  1513. global << *bindingNS << "::";
  1514. global << "lua_stringFromEnumGlobal);\n";
  1515. for (map<string, EnumBinding>::iterator iter = _enums.begin(); iter != _enums.end(); iter++)
  1516. {
  1517. if (generatingGameplay || (!generatingGameplay && _namespaces["gameplay"].find(iter->first) == _namespaces["gameplay"].end()))
  1518. {
  1519. global << "\n // Register enumeration " << iter->first << ".\n";
  1520. global << " {\n";
  1521. global << " std::vector<std::string> scopePath;\n";
  1522. for (unsigned int i = 0; i < iter->second.scopePath.size(); i++)
  1523. {
  1524. global << " scopePath.push_back(\"" << iter->second.scopePath[i] << "\");\n";
  1525. }
  1526. vector<string>::iterator enumIter = iter->second.values.begin();
  1527. for (; enumIter != iter->second.values.end(); enumIter++)
  1528. {
  1529. global << " gameplay::ScriptUtil::registerConstantString(\"" << *enumIter << "\", \"" << *enumIter << "\", scopePath);\n";
  1530. }
  1531. global << " }\n";
  1532. }
  1533. }
  1534. }
  1535. global << "}\n\n";
  1536. // Write out the binding functions.
  1537. if (generateGlobalFunctionBindings)
  1538. {
  1539. for (map<string, vector<FunctionBinding> >::iterator iter = _functions.begin(); iter != _functions.end(); iter++)
  1540. {
  1541. if (generatingGameplay || (!generatingGameplay && _namespaces["gameplay"].find(iter->second[0].name) == _namespaces["gameplay"].end()))
  1542. FunctionBinding::write(global, iter->second);
  1543. }
  1544. }
  1545. // Write out the global enum conversion function (used to pass enums from C++ to Lua).
  1546. if (generateEnumBindings)
  1547. {
  1548. global << "static const char* enumStringEmpty = \"\";\n\n";
  1549. global << "const char* lua_stringFromEnumGlobal(std::string& enumname, unsigned int value)\n";
  1550. global << "{\n";
  1551. for (map<string, EnumBinding>::iterator iter = _enums.begin(); iter != _enums.end(); iter++)
  1552. {
  1553. if (generatingGameplay || (!generatingGameplay && _namespaces["gameplay"].find(iter->first) == _namespaces["gameplay"].end()))
  1554. {
  1555. global << " if (enumname == \"";
  1556. global << iter->first << "\")\n";
  1557. global << " return lua_stringFromEnum_" << getUniqueName(iter->first) << "((" << iter->first << ")value);\n";
  1558. }
  1559. }
  1560. global << "\n";
  1561. global << " GP_ERROR(\"Unrecognized enumeration type '%s'.\", enumname.c_str());\n";
  1562. global << " return enumStringEmpty;\n";
  1563. global << "}\n\n";
  1564. }
  1565. if (bindingNS)
  1566. global << "}\n";
  1567. writeFile(path, global.str());
  1568. }
  1569. }
  1570. luaAllCpp << "}\n\n";
  1571. if (bindingNS)
  1572. luaAllCpp << "}\n\n";
  1573. writeFile(luaAllCppStr, luaAllCpp.str());
  1574. if (bindingNS)
  1575. {
  1576. luaAllH << "\nnamespace " << *bindingNS << "\n";
  1577. luaAllH << "{\n\n";
  1578. }
  1579. luaAllH << "void lua_RegisterAllBindings();\n\n";
  1580. if (bindingNS)
  1581. luaAllH<< "}\n\n";
  1582. luaAllH << "#endif\n";
  1583. writeFile(luaAllHStr, luaAllH.str());
  1584. }
  1585. void Generator::getAllDerived(set<string>& derived, string classname)
  1586. {
  1587. for (unsigned int i = 0, count = _classes[classname].derived.size(); i < count; i++)
  1588. {
  1589. // If the derived class is not in the ref ID table, then it
  1590. // is a hidden (protected, private, etc.) class, so don't include it.
  1591. if (_refIds.find(_classes[classname].derived[i]) != _refIds.end())
  1592. {
  1593. string derivedClassName = getIdentifier(_classes[classname].derived[i]);
  1594. derived.insert(derivedClassName);
  1595. getAllDerived(derived, derivedClassName);
  1596. }
  1597. }
  1598. }
  1599. void Generator::getIncludes(XMLElement* e, string filename)
  1600. {
  1601. filename.replace(filename.find(".cpp"), 4, ".h");
  1602. for (e = e->FirstChildElement("includes"); e; e = e->NextSiblingElement("includes"))
  1603. {
  1604. if (e->Attribute("refid"))
  1605. _includes[filename].insert(e->GetText());
  1606. }
  1607. }
  1608. // ----------------------------------------------------------------------------
  1609. // Utility functions
  1610. static string trim(const string& str)
  1611. {
  1612. string s = str;
  1613. while (isspace(s[0]))
  1614. {
  1615. s.erase(s.begin());
  1616. }
  1617. while (isspace(s[s.size() - 1]))
  1618. {
  1619. s.erase(s.begin() + s.size() - 1);
  1620. }
  1621. return s;
  1622. }
  1623. static string stripTypeQualifiers(const string& typeStr, FunctionBinding::Param::Kind& kind, int& levelsOfIndirection)
  1624. {
  1625. levelsOfIndirection = 0;
  1626. string type = typeStr;
  1627. kind = FunctionBinding::Param::KIND_VALUE;
  1628. // Check if the type is a reference.
  1629. string::size_type i = type.find("&");
  1630. if (i != type.npos)
  1631. {
  1632. kind = FunctionBinding::Param::KIND_REFERENCE;
  1633. type.erase(type.begin() + i);
  1634. }
  1635. // Check if the type is a pointer.
  1636. while ((i = type.find("*")) != std::string::npos)
  1637. {
  1638. kind = FunctionBinding::Param::KIND_POINTER;
  1639. type.erase(type.begin() + i);
  1640. ++levelsOfIndirection;
  1641. int j = 0;
  1642. }
  1643. // Ignore const qualifiers.
  1644. i = type.find("const ");
  1645. if (i != type.npos)
  1646. {
  1647. type.erase(type.begin() + i, type.begin() + i + 6);
  1648. }
  1649. return trim(type);
  1650. }
  1651. static inline bool isWantedFileNormal(const string& s)
  1652. {
  1653. if (s.find(".xml") == s.size() - 4)
  1654. {
  1655. if (s.find("class") == 0 || s.find("struct") == 0 ||
  1656. (s.find("_") == 0 && (s.find("h.xml") != s.npos || s.find("cpp.xml") != s.npos)))
  1657. return true;
  1658. }
  1659. return false;
  1660. }
  1661. static inline bool isNamespaceFile(const string& s)
  1662. {
  1663. if (s.find(".xml") == s.size() - 4)
  1664. {
  1665. if (s.find("namespace") == 0)
  1666. return true;
  1667. }
  1668. return false;
  1669. }
  1670. static inline bool isGeneratedBindingFile(const string& s)
  1671. {
  1672. return ( (s.find(".cpp") == s.size() - 4) || (s.find(".h") == s.size() - 2) );
  1673. }
  1674. static bool getFileList(string directory, vector<string>& files, bool (*isWantedFile)(const string& s))
  1675. {
  1676. #ifdef WIN32
  1677. string path = directory + string("/*");
  1678. // Convert char to wchar.
  1679. basic_string<TCHAR> wPath;
  1680. wPath.assign(path.begin(), path.end());
  1681. WIN32_FIND_DATA FindFileData;
  1682. HANDLE hFind = FindFirstFile(wPath.c_str(), &FindFileData);
  1683. if (hFind == INVALID_HANDLE_VALUE)
  1684. {
  1685. return false;
  1686. }
  1687. do
  1688. {
  1689. // Add to the list if this is not a directory and it passes the test function.
  1690. if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
  1691. {
  1692. // Convert wchar to char.
  1693. basic_string<TCHAR> wfilename(FindFileData.cFileName);
  1694. string filename;
  1695. filename.assign(wfilename.begin(), wfilename.end());
  1696. if (isWantedFile(filename))
  1697. {
  1698. filename = string(directory) + filename;
  1699. files.push_back(filename);
  1700. }
  1701. }
  1702. } while (FindNextFile(hFind, &FindFileData) != 0);
  1703. FindClose(hFind);
  1704. return true;
  1705. #else
  1706. string path = directory + string("/.");
  1707. struct dirent* dp;
  1708. DIR* dir = opendir(path.c_str());
  1709. if (!dir)
  1710. {
  1711. return false;
  1712. }
  1713. while ((dp = readdir(dir)) != NULL)
  1714. {
  1715. string filepath(path);
  1716. filepath.append("/");
  1717. filepath.append(dp->d_name);
  1718. struct stat buf;
  1719. if (!stat(filepath.c_str(), &buf))
  1720. {
  1721. // Add to the list if this is not a directory and it passes the test function.
  1722. if (!S_ISDIR(buf.st_mode))
  1723. {
  1724. string filename = dp->d_name;
  1725. if (isWantedFile(filename))
  1726. {
  1727. filename = string(directory) + filename;
  1728. files.push_back(filename);
  1729. }
  1730. }
  1731. }
  1732. }
  1733. closedir(dir);
  1734. return true;
  1735. #endif
  1736. }
  1737. bool isReservedKeyword(string name)
  1738. {
  1739. static set<string> keywords;
  1740. if (keywords.size() == 0)
  1741. {
  1742. keywords.insert("and");
  1743. keywords.insert("break");
  1744. keywords.insert("do");
  1745. keywords.insert("else");
  1746. keywords.insert("elseif");
  1747. keywords.insert("end");
  1748. keywords.insert("false");
  1749. keywords.insert("for");
  1750. keywords.insert("function");
  1751. keywords.insert("if");
  1752. keywords.insert("in");
  1753. keywords.insert("local");
  1754. keywords.insert("nil");
  1755. keywords.insert("not");
  1756. keywords.insert("or");
  1757. keywords.insert("repeat");
  1758. keywords.insert("return");
  1759. keywords.insert("then");
  1760. keywords.insert("true");
  1761. keywords.insert("until");
  1762. keywords.insert("while");
  1763. };
  1764. return keywords.find(name) != keywords.end();
  1765. }
  1766. // ----------------------------------------------------------------------------