consoleDoc.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/console.h"
  24. #include "console/ast.h"
  25. #include "core/tAlgorithm.h"
  26. #include "core/strings/findMatch.h"
  27. #include "console/consoleInternal.h"
  28. #include "console/consoleObject.h"
  29. #include "core/stream/fileStream.h"
  30. #include "console/compiler.h"
  31. #include "core/frameAllocator.h"
  32. #include "console/engineAPI.h"
  33. //--- Information pertaining to this page... ------------------
  34. /// @file
  35. ///
  36. /// For specifics on using the consoleDoc functionality, see @ref console_autodoc
  37. ConsoleFunctionGroupBegin(ConsoleDoc, "Console self-documentation functions. These output psuedo C++ suitable for feeeding through Doxygen or another auto documentation tool.");
  38. DefineConsoleFunction( dumpConsoleClasses, void, (bool dumpScript, bool dumpEngine), ( true, true ),
  39. "@brief Dumps all declared console classes to the console.\n\n"
  40. "@param dumpScript Optional parameter specifying whether or not classes defined in script should be dumped.\n"
  41. "@param dumpEngine Optional parameter specifying whether or not classes defined in the engine should be dumped.\n"
  42. "@ingroup Logging")
  43. {
  44. Namespace::dumpClasses( dumpScript, dumpEngine );
  45. }
  46. DefineConsoleFunction(dumpConsoleFunctions, void, ( bool dumpScript, bool dumpEngine ), ( true, true ),
  47. "@brief Dumps all declared console functions to the console.\n"
  48. "@param dumpScript Optional parameter specifying whether or not functions defined in script should be dumped.\n"
  49. "@param dumpEngine Optional parameter specitying whether or not functions defined in the engine should be dumped.\n"
  50. "@ingroup Logging")
  51. {
  52. Namespace::dumpFunctions( dumpScript, dumpEngine );
  53. }
  54. ConsoleFunctionGroupEnd(ConsoleDoc);
  55. /// Helper table to convert type ids to human readable names.
  56. const char *typeNames[] =
  57. {
  58. "Script",
  59. "string",
  60. "int",
  61. "float",
  62. "void",
  63. "bool",
  64. "",
  65. "",
  66. "unknown_overload"
  67. };
  68. void printClassHeader(const char* usage, const char * className, const char * superClassName, const bool stub)
  69. {
  70. if(stub)
  71. {
  72. Con::printf("/// Stub class");
  73. Con::printf("/// ");
  74. Con::printf("/// @note This is a stub class to ensure a proper class hierarchy. No ");
  75. Con::printf("/// information was available for this class.");
  76. }
  77. if((usage != NULL) && strlen(usage))
  78. {
  79. // Copy Usage Document
  80. S32 usageLen = dStrlen( usage );
  81. FrameTemp<char> usageStr( usageLen );
  82. dStrcpy( usageStr, usage );
  83. // Print Header
  84. Con::printf( "/*!" );
  85. // Print line by line, skipping the @field lines.
  86. //
  87. // fetch first line end
  88. char *newLine = dStrchr( usageStr, '\n' );
  89. char *usagePtr = usageStr;
  90. do
  91. {
  92. // Copy of one line
  93. static char lineStr[2048] = {0};
  94. // Keyword will hold the last keyword (word following '@' or '\') encountered.
  95. static char keyword[8] = {0};
  96. S32 lineLen = 0;
  97. // If not the last line, increment pointer
  98. if( newLine != NULL )
  99. {
  100. *newLine = '\0';
  101. newLine ++;
  102. }
  103. // Copy line and update usagePtr
  104. dStrcpy( lineStr, usagePtr );
  105. usagePtr = (newLine != NULL ) ? newLine : usagePtr;
  106. lineLen = dStrlen( lineStr );
  107. // Get the keyword. This is the first word after an '@' or '\'.
  108. const char* tempkw = dStrchr( lineStr, '@' );
  109. if( !tempkw )
  110. tempkw = dStrchr( lineStr, '\\' );
  111. // If we found a new keyword, set it, otherwise, keep using the
  112. // most recently found.
  113. if( tempkw )
  114. {
  115. dStrncpy( keyword, tempkw + 1, 5 );
  116. keyword[5] = '\0';
  117. }
  118. // Print all fields that aren't associated with the 'field' keyword.
  119. if( dStrcmp( keyword, "field" ) )
  120. Con::printf( "%s", lineStr ); // print lineStr as an unformatted string (otherwise '%' characters in the string could cause problems)
  121. // Fetch next line ending
  122. newLine = dStrchr( usagePtr, '\n' );
  123. } while( newLine != NULL );
  124. // DocBlock Footer
  125. Con::printf( " */" );
  126. }
  127. // Print out appropriate class header
  128. if(superClassName)
  129. Con::printf("class %s : public %s {", className, superClassName ? superClassName : "");
  130. else if(!className)
  131. Con::printf("namespace Global {");
  132. else
  133. Con::printf("class %s {", className);
  134. if(className)
  135. Con::printf(" public:");
  136. }
  137. void printClassMethod(const bool isVirtual, const char *retType, const char *methodName, const char* args, const char*usage)
  138. {
  139. if(usage && usage[0] != ';' && usage[0] != 0)
  140. Con::printf(" /*! %s */", usage);
  141. Con::printf(" %s%s %s(%s) {}", isVirtual ? "virtual " : "", retType, methodName, args);
  142. }
  143. void printGroupStart(const char * aName, const char * aDocs)
  144. {
  145. Con::printf("");
  146. Con::printf(" /*! @name %s", aName);
  147. if(aDocs)
  148. {
  149. Con::printf(" ");
  150. Con::printf(" %s", aDocs);
  151. }
  152. Con::printf(" @{ */");
  153. // Add a blank comment in order to make sure groups are parsed properly.
  154. Con::printf(" /*! */");
  155. }
  156. void printClassMember(const bool isDeprec, const char * aType, const char * aName, const char * aDocs)
  157. {
  158. Con::printf(" /*!");
  159. if(aDocs)
  160. {
  161. Con::printf(" %s", aDocs);
  162. Con::printf(" ");
  163. }
  164. if(isDeprec)
  165. Con::printf(" @deprecated This member is deprecated, which means that its value is always undefined.");
  166. Con::printf(" */");
  167. Con::printf(" %s %s;", isDeprec ? "deprecated" : aType, aName);
  168. }
  169. void printGroupEnd()
  170. {
  171. Con::printf(" /// @}");
  172. Con::printf("");
  173. }
  174. void printClassFooter()
  175. {
  176. Con::printf("};");
  177. Con::printf("");
  178. }
  179. void Namespace::printNamespaceEntries(Namespace * g, bool dumpScript, bool dumpEngine )
  180. {
  181. static bool inGroup = false;
  182. // Go through all the entries.
  183. // Iterate through the methods of the namespace...
  184. for(Entry *ewalk = g->mEntryList; ewalk; ewalk = ewalk->mNext)
  185. {
  186. S32 eType = ewalk->mType;
  187. const char * funcName = ewalk->mFunctionName;
  188. if( ( eType == Entry::ConsoleFunctionType ) && !dumpScript )
  189. continue;
  190. if( ( eType != Entry::ConsoleFunctionType ) && !dumpEngine )
  191. continue;
  192. // If it's a function
  193. if( eType >= Entry::ConsoleFunctionType )
  194. {
  195. printClassMethod(true, typeNames[eType], funcName, ewalk->getArgumentsString().c_str(),
  196. ewalk->getDocString().c_str());
  197. }
  198. else if(ewalk->mType == Entry::GroupMarker)
  199. {
  200. if(!inGroup)
  201. printGroupStart(ewalk->cb.mGroupName, ewalk->mUsage);
  202. else
  203. printGroupEnd();
  204. inGroup = !inGroup;
  205. }
  206. else if(ewalk->mType == Entry::ScriptCallbackType)
  207. {
  208. // It's a script callback - emit some sort of appropriate info.
  209. Con::printf(" /*! %s */", ewalk->getDocString().c_str());
  210. Con::printf(" %s;", ewalk->getPrototypeString().c_str());
  211. Con::printf("");
  212. }
  213. else if(ewalk->mFunctionOffset) // If it's a builtin function...
  214. {
  215. String args = ewalk->mCode->getFunctionArgs(ewalk->mFunctionOffset);
  216. printClassMethod(false, typeNames[ewalk->mType], ewalk->mFunctionName, args, "");
  217. }
  218. else
  219. {
  220. Con::printf(" // got an unknown thing?? %d", ewalk->mType );
  221. }
  222. }
  223. }
  224. void Namespace::dumpClasses( bool dumpScript, bool dumpEngine )
  225. {
  226. VectorPtr<Namespace*> vec;
  227. trashCache();
  228. vec.reserve( 1024 );
  229. // We use mHashSequence to mark if we have traversed...
  230. // so mark all as zero to start.
  231. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  232. walk->mHashSequence = 0;
  233. for(Namespace *walk = mNamespaceList; walk; walk = walk->mNext)
  234. {
  235. VectorPtr<Namespace*> stack;
  236. stack.reserve( 1024 );
  237. // Get all the parents of this namespace... (and mark them as we go)
  238. Namespace *parentWalk = walk;
  239. while(parentWalk)
  240. {
  241. if(parentWalk->mHashSequence != 0)
  242. break;
  243. if(parentWalk->mPackage == 0)
  244. {
  245. parentWalk->mHashSequence = 1; // Mark as traversed.
  246. stack.push_back(parentWalk);
  247. }
  248. parentWalk = parentWalk->mParent;
  249. }
  250. // Load stack into our results vector.
  251. while(stack.size())
  252. {
  253. vec.push_back(stack[stack.size() - 1]);
  254. stack.pop_back();
  255. }
  256. }
  257. // Go through previously discovered classes
  258. U32 i;
  259. for(i = 0; i < vec.size(); i++)
  260. {
  261. const char *className = vec[i]->mName;
  262. const char *superClassName = vec[i]->mParent ? vec[i]->mParent->mName : NULL;
  263. // Skip the global namespace, that gets dealt with in dumpFunctions
  264. if(!className) continue;
  265. // If we're just dumping script functions, then we don't want to dump
  266. // a class that only contains script functions. So, we iterate over all
  267. // the functions.
  268. if( !dumpScript )
  269. {
  270. bool found = false;
  271. for(Entry *ewalk = vec[i]->mEntryList; ewalk; ewalk = ewalk->mNext)
  272. {
  273. if( ewalk->mType != Entry::ConsoleFunctionType )
  274. {
  275. found = true;
  276. break;
  277. }
  278. }
  279. // If we don't have engine functions and the namespace name
  280. // doesn't match the class name... then its a script class.
  281. if ( !found && !vec[i]->isClass() )
  282. continue;
  283. }
  284. // And we do the same for engine functions.
  285. if( !dumpEngine )
  286. {
  287. bool found = false;
  288. for(Entry *ewalk = vec[i]->mEntryList; ewalk; ewalk = ewalk->mNext)
  289. {
  290. if( ewalk->mType == Entry::ConsoleFunctionType )
  291. {
  292. found = true;
  293. break;
  294. }
  295. }
  296. if( !found )
  297. continue;
  298. }
  299. // If we hit a class with no members and no classRep, do clever filtering.
  300. if(vec[i]->mEntryList == NULL && vec[i]->mClassRep == NULL)
  301. {
  302. // Print out a short stub so we get a proper class hierarchy.
  303. if(superClassName) { // Filter hack; we don't want non-inheriting classes...
  304. printClassHeader( NULL, className,superClassName, true);
  305. printClassFooter();
  306. }
  307. continue;
  308. }
  309. // Print the header for the class..
  310. printClassHeader(vec[i]->mUsage, className, superClassName, false);
  311. // Deal with entries.
  312. printNamespaceEntries(vec[i], dumpScript, dumpEngine);
  313. // Deal with the classRep (to get members)...
  314. AbstractClassRep *rep = vec[i]->mClassRep;
  315. AbstractClassRep::FieldList emptyList;
  316. AbstractClassRep::FieldList *parentList = &emptyList;
  317. AbstractClassRep::FieldList *fieldList = &emptyList;
  318. // Since all fields are defined in the engine, if we're not dumping
  319. // engine stuff, than we shouldn't dump the fields.
  320. if(dumpEngine && rep)
  321. {
  322. // Get information about the parent's fields...
  323. AbstractClassRep *parentRep = vec[i]->mParent ? vec[i]->mParent->mClassRep : NULL;
  324. if(parentRep)
  325. parentList = &(parentRep->mFieldList);
  326. // Get information about our fields
  327. fieldList = &(rep->mFieldList);
  328. // Go through all our fields...
  329. for(U32 j = 0; j < fieldList->size(); j++)
  330. {
  331. switch((*fieldList)[j].type)
  332. {
  333. case AbstractClassRep::StartArrayFieldType:
  334. case AbstractClassRep::EndArrayFieldType:
  335. break;
  336. case AbstractClassRep::StartGroupFieldType:
  337. printGroupStart((*fieldList)[j].pGroupname, (*fieldList)[j].pFieldDocs);
  338. break;
  339. case AbstractClassRep::EndGroupFieldType:
  340. printGroupEnd();
  341. break;
  342. default:
  343. case AbstractClassRep::DeprecatedFieldType:
  344. {
  345. // Skip over fields that are already defined in
  346. // our parent class.
  347. if ( parentRep && parentRep->findField( (*fieldList)[j].pFieldname ) )
  348. continue;
  349. bool isDeprecated = ((*fieldList)[j].type == AbstractClassRep::DeprecatedFieldType);
  350. if(isDeprecated)
  351. {
  352. printClassMember(
  353. true,
  354. "<deprecated>",
  355. (*fieldList)[j].pFieldname,
  356. (*fieldList)[j].pFieldDocs
  357. );
  358. }
  359. else
  360. {
  361. ConsoleBaseType *cbt = ConsoleBaseType::getType((*fieldList)[j].type);
  362. printClassMember(
  363. false,
  364. cbt ? cbt->getTypeClassName() : "<unknown>",
  365. (*fieldList)[j].pFieldname,
  366. (*fieldList)[j].pFieldDocs
  367. );
  368. }
  369. }
  370. }
  371. }
  372. }
  373. if( dumpScript )
  374. {
  375. // Print out fields defined in script docs for this namespace.
  376. // These fields are specified by the 'field' keyword in the usage
  377. // string.
  378. // The field type and name.
  379. char fieldName[256];
  380. char fieldDoc[1024];
  381. // Usage string iterator.
  382. const char* field = vec[i]->mUsage;
  383. while( field )
  384. {
  385. // Find the first field keyword.
  386. const char* tempField = dStrstr( field, "@field" );
  387. if( !tempField )
  388. tempField = dStrstr( field, "\\field" );
  389. field = tempField;
  390. if( !field )
  391. break;
  392. // Move to the field name.
  393. field += 7;
  394. // Copy the field type and name. These should both be followed by a
  395. // space so only in this case will we actually store it.
  396. S32 spaceCount = 0;
  397. S32 index = 0;
  398. bool valid = false;
  399. while( field && ( *field != '\n' ) )
  400. {
  401. if( index >= 255 )
  402. break;
  403. if( *field == ' ' )
  404. spaceCount++;
  405. if( spaceCount == 2 )
  406. {
  407. valid = true;
  408. break;
  409. }
  410. fieldName[index++] = *field;
  411. field++;
  412. }
  413. if( !valid )
  414. continue;
  415. fieldName[index] = '\0';
  416. // Now copy from field to the next keyword.
  417. const char* nextKeyword = dStrchr( field, '@' );
  418. if( !nextKeyword )
  419. nextKeyword = dStrchr( field, '\\' );
  420. // Grab the length of the doc string.
  421. S32 docLen = dStrlen( field );
  422. if( nextKeyword )
  423. docLen = nextKeyword - field;
  424. // Make sure it will fit in the buffer.
  425. if( docLen > 1023 )
  426. docLen = 1023;
  427. // Copy.
  428. dStrncpy( fieldDoc, field, docLen );
  429. fieldDoc[docLen] = '\0';
  430. field += docLen;
  431. // Print
  432. Con::printf( " /*!" );
  433. Con::printf( " %s", fieldDoc );
  434. Con::printf( " */" );
  435. Con::printf( " %s;", fieldName );
  436. }
  437. }
  438. // Close the class/namespace.
  439. printClassFooter();
  440. }
  441. }
  442. void Namespace::dumpFunctions( bool dumpScript, bool dumpEngine )
  443. {
  444. // Get the global namespace.
  445. Namespace* g = find(NULL); //->mParent;
  446. printClassHeader(NULL, NULL,NULL, false);
  447. while(g)
  448. {
  449. printNamespaceEntries(g, dumpScript, dumpEngine );
  450. g = g->mParent;
  451. }
  452. printClassFooter();
  453. }