element.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 "element.h"
  17. #include <iostream>
  18. #include <cassert>
  19. #include "parse.h"
  20. #include "util/tc_file.h"
  21. #include "util/tc_common.h"
  22. /*************************************************************************************************/
  23. const char* Builtin::builtinTable[] =
  24. {
  25. "void",
  26. "bool",
  27. "byte",
  28. "short",
  29. "int",
  30. "long",
  31. "float",
  32. "double",
  33. "string",
  34. "vector",
  35. "map"
  36. };
  37. Builtin::Builtin(Kind kind,bool isUnsigned = false) : _kind(kind),_isUnsigned(isUnsigned)
  38. {
  39. }
  40. Builtin::Kind Builtin::kind() const
  41. {
  42. return _kind;
  43. }
  44. bool Builtin::isSimple() const
  45. {
  46. switch(_kind)
  47. {
  48. case Builtin::KindBool:
  49. case Builtin::KindByte:
  50. case Builtin::KindShort:
  51. case Builtin::KindInt:
  52. case Builtin::KindLong:
  53. case Builtin::KindFloat:
  54. case Builtin::KindDouble:
  55. return true;
  56. default:
  57. return false;
  58. }
  59. return true;
  60. }
  61. string Builtin::def() const
  62. {
  63. switch(_kind)
  64. {
  65. case Builtin::KindBool:
  66. return "true";
  67. case Builtin::KindByte:
  68. case Builtin::KindShort:
  69. case Builtin::KindInt:
  70. case Builtin::KindLong:
  71. case Builtin::KindFloat:
  72. case Builtin::KindDouble:
  73. return "0";
  74. case Builtin::KindString:
  75. return "";
  76. default:
  77. assert(false);
  78. }
  79. return "";
  80. }
  81. /*************************************************************************************************/
  82. Vector::Vector(const TypePtr& ptr) : _ptr(ptr)
  83. {
  84. }
  85. /*************************************************************************************************/
  86. Map::Map(const TypePtr& pleft, const TypePtr& pright): _pleft(pleft), _pright(pright)
  87. {
  88. }
  89. /*************************************************************************************************/
  90. TypeId::TypeId(const TypePtr& ptr, const string&id)
  91. : _ptr(ptr)
  92. , _id(id)
  93. , _bRequire(true)
  94. , _tag(0)
  95. , _bHasDefault(false)
  96. , _size(0)
  97. , _array(false)
  98. {
  99. BuiltinPtr bPtr = BuiltinPtr::dynamicCast(_ptr);
  100. if(bPtr)
  101. {
  102. _bHasDefault = true;
  103. _default = bPtr->def();
  104. }
  105. }
  106. void TypeId::setRequire(int tag)
  107. {
  108. _bRequire = true;
  109. _tag = tag;
  110. }
  111. void TypeId::setDefault(const string &def)
  112. {
  113. BuiltinPtr bPtr = BuiltinPtr::dynamicCast(_ptr);
  114. EnumPtr ePtr = EnumPtr::dynamicCast(_ptr);
  115. assert(bPtr || ePtr);
  116. _bHasDefault = true;
  117. _default = def;
  118. }
  119. void TypeId::disableDefault()
  120. {
  121. BuiltinPtr bPtr = BuiltinPtr::dynamicCast(_ptr);
  122. assert(bPtr);
  123. _bHasDefault = false;
  124. }
  125. void TypeId::setOptional(int tag)
  126. {
  127. _bRequire = false;
  128. _tag = tag;
  129. }
  130. /*************************************************************************************************/
  131. NamespacePtr Container::createNamespace(const string &id)
  132. {
  133. NamespacePtr np = NamespacePtr::dynamicCast(this);
  134. if(np)
  135. {
  136. g_parse->error("namespace can't be nested!");
  137. }
  138. _ns.push_back(new Namespace(id));
  139. g_parse->currentContextPtr()->addNamespacePtr(_ns.back());
  140. return _ns.back();
  141. }
  142. /*************************************************************************************************/
  143. ParamDeclPtr Operation::createParamDecl(const TypeIdPtr &typeIdPtr, bool v, bool k)
  144. {
  145. _itag++;
  146. if(_itag > 255)
  147. {
  148. g_parse->error("operation '" + _id + "' can't have more than 255 paramters!");
  149. }
  150. if(typeIdPtr->getId() == getId())
  151. {
  152. g_parse->error("paramters name '" + _id + "' conflicts with operation name!");
  153. }
  154. typeIdPtr->setRequire(_itag);
  155. _ps.push_back(new ParamDecl(typeIdPtr, v, k));
  156. return _ps.back();
  157. }
  158. /*************************************************************************************************/
  159. OperationPtr Interface::createOperation(const string &id, const TypePtr &typePtr)
  160. {
  161. for(size_t i = 0; i < _ops.size(); i++)
  162. {
  163. if(_ops[i]->getId() == id)
  164. {
  165. g_parse->error("operation '" + id + "' exists in interface '" + _id + "'");
  166. return NULL;
  167. }
  168. }
  169. _ops.push_back(new Operation(id, typePtr));
  170. return _ops.back();
  171. }
  172. /*************************************************************************************************/
  173. InterfacePtr Namespace::createInterface(const string &id)
  174. {
  175. for(size_t i = 0; i < _is.size(); i++)
  176. {
  177. if(_is[i]->getId() == id)
  178. {
  179. g_parse->error("interface '" + id + "' exists in namespace '" + _id + "'");
  180. return NULL;
  181. }
  182. }
  183. if(id == _id)
  184. {
  185. g_parse->error("interface '" + id + "' must not be same with namespace '" + _id + "'");
  186. return NULL;
  187. }
  188. _is.push_back(new Interface(id));
  189. return _is.back();
  190. }
  191. StructPtr Namespace::createStruct(const string& id)
  192. {
  193. g_parse->checkConflict(_id + "::" + id);
  194. _ss.push_back(new Struct(id, _id + "::" + id));
  195. g_parse->addStructPtr(_ss.back());
  196. return _ss.back();
  197. }
  198. EnumPtr Namespace::createEnum(const string &id)
  199. {
  200. g_parse->checkConflict(_id + "::" + id);
  201. _es.push_back(new Enum(id, _id + "::" + id));
  202. g_parse->addEnumPtr(_es.back());
  203. return _es.back();
  204. }
  205. ConstPtr Namespace::createConst(TypeIdPtr &pPtr, ConstGrammarPtr &cPtr)
  206. {
  207. for(size_t i = 0; i < _cs.size(); i++)
  208. {
  209. if(_cs[i]->getTypeIdPtr()->getId() == pPtr->getId())
  210. {
  211. g_parse->error("const type '" + pPtr->getId() + "' exists in namespace '" + _id + "'");
  212. return NULL;
  213. }
  214. }
  215. g_parse->checkConstValue(pPtr, cPtr->t);
  216. _cs.push_back(new Const(pPtr, cPtr));
  217. return _cs.back();
  218. }
  219. /*************************************************************************************************/
  220. void Struct::addTypeId(const TypeIdPtr &typeIdPtr)
  221. {
  222. StructPtr sp = StructPtr::dynamicCast(typeIdPtr->getTypePtr());
  223. if(sp)
  224. {
  225. if(sp->getSid() == getSid())
  226. {
  227. g_parse->error("struct can't take self as member data");
  228. }
  229. }
  230. for(size_t i = 0; i < _members.size(); i++)
  231. {
  232. if(_members[i]->getId() == typeIdPtr->getId())
  233. {
  234. g_parse->error("data member '" + typeIdPtr->getId() + "' duplicate definition");
  235. }
  236. if(_members[i]->getTag() == typeIdPtr->getTag())
  237. {
  238. g_parse->error("data member '" + typeIdPtr->getId() + "' has equal tag with '" + _members[i]->getId() + "'");
  239. }
  240. if(_members[i]->getTag() > typeIdPtr->getTag())
  241. {
  242. _members.insert(_members.begin() + i, typeIdPtr);
  243. return;
  244. }
  245. }
  246. _members.push_back(typeIdPtr);
  247. }
  248. void Struct::addKey(const string &member)
  249. {
  250. size_t i;
  251. for(i = 0; i < _members.size(); i++)
  252. {
  253. if(_members[i]->getId() == member)
  254. {
  255. break;
  256. }
  257. }
  258. if(i == _members.size())
  259. {
  260. g_parse->error("key member '" + member + "' is not struct member");
  261. }
  262. _key.push_back(member);
  263. }
  264. /***********************************************************************************/
  265. void Enum::addMember(const TypeIdPtr &typeIdPtr)
  266. {
  267. for(size_t i = 0; i < _members.size(); i++)
  268. {
  269. if(_members[i]->getId() == typeIdPtr->getId())
  270. {
  271. g_parse->error("enum member '" + typeIdPtr->getId() + "' duplicate definition");
  272. }
  273. }
  274. _members.push_back(typeIdPtr);
  275. }
  276. /***********************************************************************************/
  277. void Context::addInclude(const string &incl)
  278. {
  279. if(incl == _filename)
  280. {
  281. g_parse->error("can't include self");
  282. }
  283. _includes.push_back(tars::TC_File::excludeFileExt(incl) + ".h");
  284. }