idl_scan.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "code_generator.h"
  17. string CodeGenerator::makeName()
  18. {
  19. int iHigh = uiNameIndex/26;
  20. int iLow = uiNameIndex%26;
  21. uiNameIndex++;
  22. ostringstream s;
  23. s << "_" << TC_Common::upper(IDL_NAMESPACE_STR) << "_MODULE_";
  24. if (iHigh != 0)
  25. {
  26. s << string(1, (char)(iHigh + 65)) << string(1, (char)(iLow + 65));
  27. }
  28. else
  29. {
  30. s << string(1, (char)(iLow + 65));
  31. }
  32. s << "_";
  33. return s.str();
  34. }
  35. bool CodeGenerator::isDependent(const string& sNamespace, const string& sName) const
  36. {
  37. return _depMembers.find(sNamespace + "::" + sName) != _depMembers.end();
  38. }
  39. string CodeGenerator::findName(const string& sNamespace, const string& sName, const bool &bBase)
  40. {
  41. #ifdef DUMP_FIND_NAME
  42. cout << "FINDNAME BEGIN:" << sNamespace << "|" << sName << endl;
  43. #endif
  44. for (map<string, ImportFile>::iterator it = _mapFiles.begin(); it != _mapFiles.end(); it++)
  45. {
  46. #ifdef DUMP_FIND_NAME
  47. for (map<string, ImportFileType>::iterator demo = it->second.mapVars.begin(); demo != it->second.mapVars.end(); demo++)
  48. {
  49. cout << "FINDNAME:" << it->second.sModule << "|" << demo->first << "|" << demo->second.sNamespace << "|" << demo->second.sName << endl;
  50. }
  51. #endif
  52. map<string, ImportFileType>::iterator inIter = it->second.mapVars.find(sNamespace + "::" + sName);
  53. if (inIter == it->second.mapVars.end())
  54. {
  55. continue;
  56. }
  57. #ifdef DUMP_FIND_NAME
  58. cout << "DEPMEMBER:" << it->first << "|" << inIter->second.sNamespace << "::" << inIter->second.sName << endl;
  59. #endif
  60. _depMembers.insert(inIter->second.sNamespace + "::" + inIter->second.sName);
  61. string prefix;
  62. if (bBase && it->second.sModule.empty()) {
  63. prefix = "base.";
  64. } else if (!it->second.sModule.empty()) {
  65. prefix = it->second.sModule + ".";
  66. }
  67. switch (inIter->second.iType)
  68. {
  69. case ImportFileType::EN_ENUM : // [[fallthrough]]
  70. case ImportFileType::EN_STRUCT :
  71. {
  72. return prefix + inIter->second.sNamespace + "." + inIter->second.sName;
  73. }
  74. case ImportFileType::EN_ENUM_VALUE :
  75. {
  76. return prefix + inIter->second.sNamespace + "." + inIter->second.sTypeName + "." + inIter->second.sName;
  77. }
  78. default :
  79. {
  80. return it->second.sModule;
  81. }
  82. }
  83. }
  84. return "";
  85. }
  86. void CodeGenerator::scan(const string& sFile, bool bNotPrefix)
  87. {
  88. if (_mapFiles.find(sFile) != _mapFiles.end())
  89. {
  90. return ;
  91. }
  92. string sIdlFile = getRealFileInfo(sFile);
  93. g_parse->parse(sIdlFile);
  94. vector<ContextPtr> contexts = g_parse->getContexts();
  95. for(size_t i = 0; i < contexts.size(); i++)
  96. {
  97. if (sIdlFile == contexts[i]->getFileName())
  98. {
  99. ImportFile item;
  100. item.sFile = "./" + TC_File::excludeFileExt(TC_File::extractFileName(sFile)) + IDL_TYPE + ".js";
  101. item.sModule = bNotPrefix?"":makeName();
  102. vector<NamespacePtr> namespaces = contexts[i]->getNamespaces();
  103. for (size_t ii = 0; ii < namespaces.size(); ii++)
  104. {
  105. string sNamespace = namespaces[ii]->getId();
  106. vector<StructPtr> & ss = namespaces[ii]->getAllStructPtr();
  107. for (size_t iii = 0; iii < ss.size(); iii++)
  108. {
  109. vector<string> vecNames = TC_Common::sepstr<string>(ss[iii]->getSid(), "::");
  110. ImportFileType temp;
  111. temp.iType = ImportFileType::EN_STRUCT;
  112. temp.sNamespace = sNamespace;
  113. temp.sName = vecNames[1];
  114. item.mapVars.insert(make_pair(temp.sNamespace + "::" + temp.sName, temp));
  115. }
  116. vector<EnumPtr> & es = namespaces[ii]->getAllEnumPtr();
  117. for (size_t iii = 0; iii < es.size(); iii++)
  118. {
  119. vector<string> vecNames = TC_Common::sepstr<string>(es[iii]->getSid(), "::");
  120. ImportFileType temp;
  121. temp.iType = ImportFileType::EN_ENUM;
  122. temp.sNamespace = sNamespace;
  123. temp.sName = vecNames[1];
  124. item.mapVars.insert(make_pair(temp.sNamespace + "::" + temp.sName, temp));
  125. vector<TypeIdPtr> & eMember = es[iii]->getAllMemberPtr();
  126. for (size_t iiii = 0; iiii < eMember.size(); iiii++)
  127. {
  128. ImportFileType temp;
  129. temp.iType = ImportFileType::EN_ENUM_VALUE;
  130. temp.sNamespace = sNamespace;
  131. temp.sTypeName = vecNames[1];
  132. temp.sName = eMember[iiii]->getId();
  133. #ifdef DUMP_FIND_NAME
  134. cout << "GET::::" << temp.sNamespace << "|" << temp.sName << endl;
  135. #endif
  136. item.mapVars.insert(make_pair(temp.sNamespace + "::" + temp.sName, temp));
  137. }
  138. }
  139. }
  140. _mapFiles.insert(make_pair(sFile, item));
  141. vector<string> vecFiles = contexts[i]->getIncludes();
  142. for (size_t ii = 0; ii < vecFiles.size(); ii++)
  143. {
  144. string sFileName = TC_File::extractFilePath(vecFiles[ii]) + TC_File::excludeFileExt(TC_File::extractFileName(vecFiles[ii])) + "." + TC_Common::lower(IDL_TYPE);
  145. scan(sFileName, false);
  146. }
  147. }
  148. }
  149. }