tars2php.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 "tars2php.h"
  17. #include "util/tc_md5.h"
  18. #include "util/tc_file.h"
  19. #include "util/tc_common.h"
  20. #include <string>
  21. #define TAB g_parse->getTab()
  22. #define INC_TAB g_parse->incTab()
  23. #define DEL_TAB g_parse->delTab()
  24. //////////////////////////////////////////////////////////////////////////////////
  25. //
  26. Tars2Php::Tars2Php(): m_bCheckDefault(false)
  27. {
  28. }
  29. string Tars2Php::writeTo(const TypeIdPtr &pPtr) const
  30. {
  31. ostringstream s;
  32. s << TAB << "$this->" << pPtr->getId() << "->write($_out," << pPtr->getTag() << ");" << endl;
  33. return s.str();
  34. }
  35. string Tars2Php::readFrom(const TypeIdPtr &pPtr, bool bIsRequire) const
  36. {
  37. ostringstream s;
  38. s<<TAB<<"$this->"<<pPtr->getId()<<"->read($_in ";
  39. s << ", " << pPtr->getTag() << ", " << ((pPtr->isRequire() && bIsRequire)?"true":"false") << ");" << endl;
  40. return s.str();
  41. }
  42. int Tars2Php::getSuffix(const TypeIdPtr &pPtr) const
  43. {
  44. BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr->getTypePtr());
  45. if(bPtr && bPtr->kind() == Builtin::KindString && bPtr->isArray())
  46. {
  47. return bPtr->getSize();
  48. }
  49. VectorPtr vPtr = VectorPtr::dynamicCast(pPtr->getTypePtr());
  50. if(vPtr && vPtr->isArray())
  51. {
  52. return vPtr->getSize();
  53. }
  54. return -1;
  55. }
  56. string Tars2Php::toStrSuffix(const TypeIdPtr &pPtr) const
  57. {
  58. ostringstream s;
  59. int i = getSuffix(pPtr);
  60. if(i >= 0)
  61. {
  62. s << "[" << i << "]";
  63. }
  64. return s.str();
  65. }
  66. /*******************************BuiltinPtr********************************/
  67. string Tars2Php::tostr(const TypePtr &pPtr) const
  68. {
  69. BuiltinPtr bPtr = BuiltinPtr::dynamicCast(pPtr);
  70. if(bPtr) return tostrBuiltin(bPtr);
  71. VectorPtr vPtr = VectorPtr::dynamicCast(pPtr);
  72. if(vPtr) return tostrVector(vPtr);
  73. MapPtr mPtr = MapPtr::dynamicCast(pPtr);
  74. if(mPtr) return tostrMap(mPtr);
  75. StructPtr sPtr = StructPtr::dynamicCast(pPtr);
  76. if(sPtr) return tostrStruct(sPtr);
  77. EnumPtr ePtr = EnumPtr::dynamicCast(pPtr);
  78. if(ePtr) return tostrEnum(ePtr);
  79. if(!pPtr) return "void";
  80. assert(false);
  81. return "";
  82. }
  83. string Tars2Php::tostrBuiltin(const BuiltinPtr &pPtr) const
  84. {
  85. string s;
  86. switch(pPtr->kind())
  87. {
  88. case Builtin::KindBool:
  89. s = "c_char";
  90. break;
  91. case Builtin::KindByte:
  92. s = (pPtr->isUnsigned()?"c_short":"c_char");
  93. break;
  94. case Builtin::KindShort:
  95. s = (pPtr->isUnsigned()?"c_short":"c_short");
  96. break;
  97. case Builtin::KindInt:
  98. s = (pPtr->isUnsigned()?"c_int":"c_int");
  99. break;
  100. case Builtin::KindLong:
  101. s = "c_int64";
  102. break;
  103. case Builtin::KindFloat:
  104. s = "c_float";
  105. break;
  106. case Builtin::KindDouble:
  107. s = "c_double";
  108. break;
  109. case Builtin::KindString:
  110. s = "c_string";
  111. break;
  112. case Builtin::KindVector:
  113. s = "c_vector";
  114. break;
  115. case Builtin::KindMap:
  116. s = "c_map";
  117. break;
  118. default:
  119. assert(false);
  120. break;
  121. }
  122. return s;
  123. }
  124. /*******************************VectorPtr********************************/
  125. string Tars2Php::tostrVector(const VectorPtr &pPtr) const
  126. {
  127. string s = string("c_vector (new ") + tostr(pPtr->getTypePtr()) + string(")");
  128. return s;
  129. }
  130. /*******************************MapPtr********************************/
  131. string Tars2Php::tostrMap(const MapPtr &pPtr) const
  132. {
  133. string s = string("c_map (new ") + tostr(pPtr->getLeftTypePtr()) + ",new " + tostr(pPtr->getRightTypePtr()) + string(")");
  134. return s;
  135. }
  136. /*******************************StructPtr********************************/
  137. string Tars2Php::tostrStruct(const StructPtr &pPtr) const
  138. {
  139. return pPtr->getId();
  140. }
  141. /////////////////////////////////////////////////////////////////////
  142. string Tars2Php::tostrEnum(const EnumPtr &pPtr) const
  143. {
  144. return "c_int";//pPtr->getId();
  145. }
  146. ///////////////////////////////////////////////////////////////////////
  147. string Tars2Php::generatePHP(const StructPtr &pPtr, const string& namespaceId) const
  148. {
  149. ostringstream s;
  150. vector<TypeIdPtr>& member = pPtr->getAllMemberPtr();
  151. s << TAB << "class " << pPtr->getId() << " extends c_struct" << endl;
  152. s << TAB << "{" << endl;
  153. INC_TAB;
  154. for(size_t k = 0;k < member.size();k++)
  155. {
  156. s<< TAB << "public $" <<member[k]->getId()<<";"<<endl;
  157. }
  158. s<<endl;
  159. s << TAB << "public function __clone()" << endl;
  160. s << TAB << "{" << endl;
  161. INC_TAB;
  162. for(size_t k = 0;k < member.size();k++)
  163. {
  164. s<< TAB << "$this->" <<member[k]->getId()<<" = clone $this->"<<member[k]->getId()<<";"<<endl;
  165. }
  166. DEL_TAB;
  167. s << TAB << "}" << endl;
  168. s <<endl;
  169. s << TAB << "public function __construct()" << endl;
  170. s << TAB << "{" << endl;
  171. INC_TAB;
  172. for(size_t k = 0;k < member.size();k++)
  173. {
  174. s<< TAB << "$this->" <<member[k]->getId()<<" = new "<< tostr(member[k]->getTypePtr())<<";"<<endl;
  175. }
  176. DEL_TAB;
  177. s << TAB << "}" << endl;
  178. s <<endl;
  179. ////////////////////////////////////////////////////////////
  180. s << TAB << "public function get_class_name()" << endl;
  181. s << TAB << "{" << endl;
  182. INC_TAB;
  183. s << TAB << "return " << "\"" << namespaceId << "." << pPtr->getId() << "\"" << ";" << endl;
  184. DEL_TAB;
  185. s << TAB << "}" << endl;
  186. s<<endl;
  187. //write begin
  188. s << TAB << "public function write(&$_out,$tag)" << endl;
  189. s << TAB << "{" << endl;
  190. INC_TAB;
  191. s << TAB << "tars_header::_pack_header($_out,'c_struct_begin',$tag);" << endl;
  192. for(size_t j = 0; j < member.size(); j++)
  193. {
  194. s << writeTo(member[j]);
  195. }
  196. s << TAB << "tars_header::_pack_header($_out,'c_struct_end',0);" << endl;
  197. DEL_TAB;
  198. s << TAB << "}" << endl;
  199. //write end
  200. ///read begin
  201. s << TAB << "public function read(&$_in,$tag,$isRequire = true)" << endl;
  202. s << TAB << "{" << endl;
  203. INC_TAB;
  204. s << TAB << "tars_header::_check_struct($_in,$type,$tag,$isRequire);"<<endl;
  205. s << TAB << "tars_header::_unpack_header($_in,$type,$this_tag);"<<endl;
  206. for(size_t j = 0; j < member.size(); j++)
  207. {
  208. s << readFrom(member[j]);
  209. }
  210. s<< TAB << "$this->_skip_struct($_in);" << endl;
  211. DEL_TAB;
  212. s << TAB << "}" << endl;
  213. //read end
  214. DEL_TAB;
  215. s << TAB << "}" << endl;
  216. return s.str();
  217. }
  218. /******************************NamespacePtr***************************************/
  219. string Tars2Php::generatePHP(const NamespacePtr &pPtr) const
  220. {
  221. ostringstream s;
  222. vector<StructPtr> &ss = pPtr->getAllStructPtr();
  223. for(size_t i = 0; i < ss.size(); i++)
  224. {
  225. s << generatePHP(ss[i], pPtr->getId()) << endl;
  226. }
  227. return s.str();
  228. }
  229. void Tars2Php::generatePHP(const ContextPtr &pPtr) const
  230. {
  231. string n = tars::TC_File::excludeFileExt(tars::TC_File::extractFileName(pPtr->getFileName()));
  232. string fileH = m_sBaseDir + FILE_SEP + n + "_wup.php";
  233. string define = "<?php";
  234. ostringstream s;
  235. s << define << endl;
  236. s << endl;
  237. s << g_parse->printHeaderRemark();
  238. s << endl;
  239. s << "require_once('tars.php');" <<endl;
  240. s <<endl;
  241. vector<NamespacePtr> namespaces = pPtr->getNamespaces();
  242. for(size_t i = 0; i < namespaces.size(); i++)
  243. {
  244. s << generatePHP(namespaces[i]) << endl;
  245. }
  246. s << "?>"<<endl;
  247. tars::TC_File::makeDirRecursive(m_sBaseDir);
  248. tars::TC_File::save2file(fileH, s.str());
  249. }
  250. void Tars2Php::generatePHP_Pdu(const ContextPtr &pPtr) const
  251. {
  252. string n = tars::TC_File::excludeFileExt(tars::TC_File::extractFileName(pPtr->getFileName()));
  253. string fileH = m_sBaseDir + FILE_SEP + n + "_pdu.php";
  254. string define = "<?php";
  255. ostringstream s;
  256. s << define << endl;
  257. s << endl;
  258. s << g_parse->printHeaderRemark();
  259. s << endl;
  260. s << "require_once('tars.php');" <<endl;
  261. vector<NamespacePtr> namespaces = pPtr->getNamespaces();
  262. for(size_t i = 0; i < namespaces.size(); i++)
  263. {
  264. s << generatePHP(namespaces[i]) << endl;
  265. }
  266. s << "?>";
  267. tars::TC_File::makeDirRecursive(m_sBaseDir);
  268. tars::TC_File::save2file(fileH, s.str());
  269. }
  270. void Tars2Php::createFile(const string &file, const vector<string> &vsCoder)
  271. {
  272. std::vector<ContextPtr> contexts = g_parse->getContexts();
  273. for(size_t i = 0; i < contexts.size(); i++)
  274. {
  275. if(file == contexts[i]->getFileName())
  276. {
  277. generatePHP(contexts[i]);
  278. }
  279. }
  280. }