astAlloc.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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/compiler.h"
  25. #include "console/consoleInternal.h"
  26. using namespace Compiler;
  27. /// @file
  28. ///
  29. /// TorqueScript AST node allocators.
  30. ///
  31. /// These static methods exist to allocate new AST node for the compiler. They
  32. /// all allocate memory from the consoleAllocator for efficiency, and often take
  33. /// arguments relating to the state of the nodes. They are called from gram.y
  34. /// (really gram.c) as the lexer analyzes the script code.
  35. //------------------------------------------------------------
  36. BreakStmtNode *BreakStmtNode::alloc(S32 lineNumber)
  37. {
  38. BreakStmtNode *ret = (BreakStmtNode *)consoleAlloc(sizeof(BreakStmtNode));
  39. constructInPlace(ret);
  40. ret->dbgLineNumber = lineNumber;
  41. return ret;
  42. }
  43. ContinueStmtNode *ContinueStmtNode::alloc(S32 lineNumber)
  44. {
  45. ContinueStmtNode *ret = (ContinueStmtNode *)consoleAlloc(sizeof(ContinueStmtNode));
  46. constructInPlace(ret);
  47. ret->dbgLineNumber = lineNumber;
  48. return ret;
  49. }
  50. ReturnStmtNode *ReturnStmtNode::alloc(S32 lineNumber, ExprNode *expr)
  51. {
  52. ReturnStmtNode *ret = (ReturnStmtNode *)consoleAlloc(sizeof(ReturnStmtNode));
  53. constructInPlace(ret);
  54. ret->expr = expr;
  55. ret->dbgLineNumber = lineNumber;
  56. return ret;
  57. }
  58. IfStmtNode *IfStmtNode::alloc(S32 lineNumber, ExprNode *testExpr, StmtNode *ifBlock, StmtNode *elseBlock, bool propagate)
  59. {
  60. IfStmtNode *ret = (IfStmtNode *)consoleAlloc(sizeof(IfStmtNode));
  61. constructInPlace(ret);
  62. ret->dbgLineNumber = lineNumber;
  63. ret->testExpr = testExpr;
  64. ret->ifBlock = ifBlock;
  65. ret->elseBlock = elseBlock;
  66. ret->propagate = propagate;
  67. return ret;
  68. }
  69. LoopStmtNode *LoopStmtNode::alloc(S32 lineNumber, ExprNode *initExpr, ExprNode *testExpr, ExprNode *endLoopExpr, StmtNode *loopBlock, bool isDoLoop)
  70. {
  71. LoopStmtNode *ret = (LoopStmtNode *)consoleAlloc(sizeof(LoopStmtNode));
  72. constructInPlace(ret);
  73. ret->dbgLineNumber = lineNumber;
  74. ret->testExpr = testExpr;
  75. ret->initExpr = initExpr;
  76. ret->endLoopExpr = endLoopExpr;
  77. ret->loopBlock = loopBlock;
  78. ret->isDoLoop = isDoLoop;
  79. // Deal with setting some dummy constant nodes if we weren't provided with
  80. // info... This allows us to play nice with missing parts of for(;;) for
  81. // instance.
  82. if (!ret->testExpr) ret->testExpr = IntNode::alloc(lineNumber, 1);
  83. return ret;
  84. }
  85. IterStmtNode* IterStmtNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode* containerExpr, StmtNode* body, bool isStringIter)
  86. {
  87. IterStmtNode* ret = (IterStmtNode*)consoleAlloc(sizeof(IterStmtNode));
  88. constructInPlace(ret);
  89. ret->dbgLineNumber = lineNumber;
  90. ret->varName = varName;
  91. ret->containerExpr = containerExpr;
  92. ret->body = body;
  93. ret->isStringIter = isStringIter;
  94. return ret;
  95. }
  96. FloatBinaryExprNode *FloatBinaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *left, ExprNode *right)
  97. {
  98. FloatBinaryExprNode *ret = (FloatBinaryExprNode *)consoleAlloc(sizeof(FloatBinaryExprNode));
  99. constructInPlace(ret);
  100. ret->dbgLineNumber = lineNumber;
  101. ret->op = op;
  102. ret->left = left;
  103. ret->right = right;
  104. return ret;
  105. }
  106. IntBinaryExprNode *IntBinaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *left, ExprNode *right)
  107. {
  108. IntBinaryExprNode *ret = (IntBinaryExprNode *)consoleAlloc(sizeof(IntBinaryExprNode));
  109. constructInPlace(ret);
  110. ret->dbgLineNumber = lineNumber;
  111. ret->op = op;
  112. ret->left = left;
  113. ret->right = right;
  114. return ret;
  115. }
  116. StreqExprNode *StreqExprNode::alloc(S32 lineNumber, ExprNode *left, ExprNode *right, bool eq)
  117. {
  118. StreqExprNode *ret = (StreqExprNode *)consoleAlloc(sizeof(StreqExprNode));
  119. constructInPlace(ret);
  120. ret->dbgLineNumber = lineNumber;
  121. ret->left = left;
  122. ret->right = right;
  123. ret->eq = eq;
  124. return ret;
  125. }
  126. StrcatExprNode *StrcatExprNode::alloc(S32 lineNumber, ExprNode *left, ExprNode *right, S32 appendChar)
  127. {
  128. StrcatExprNode *ret = (StrcatExprNode *)consoleAlloc(sizeof(StrcatExprNode));
  129. constructInPlace(ret);
  130. ret->dbgLineNumber = lineNumber;
  131. ret->left = left;
  132. ret->right = right;
  133. ret->appendChar = appendChar;
  134. return ret;
  135. }
  136. CommaCatExprNode *CommaCatExprNode::alloc(S32 lineNumber, ExprNode *left, ExprNode *right)
  137. {
  138. CommaCatExprNode *ret = (CommaCatExprNode *)consoleAlloc(sizeof(CommaCatExprNode));
  139. constructInPlace(ret);
  140. ret->dbgLineNumber = lineNumber;
  141. ret->left = left;
  142. ret->right = right;
  143. return ret;
  144. }
  145. IntUnaryExprNode *IntUnaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *expr)
  146. {
  147. IntUnaryExprNode *ret = (IntUnaryExprNode *)consoleAlloc(sizeof(IntUnaryExprNode));
  148. constructInPlace(ret);
  149. ret->dbgLineNumber = lineNumber;
  150. ret->op = op;
  151. ret->expr = expr;
  152. return ret;
  153. }
  154. FloatUnaryExprNode *FloatUnaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *expr)
  155. {
  156. FloatUnaryExprNode *ret = (FloatUnaryExprNode *)consoleAlloc(sizeof(FloatUnaryExprNode));
  157. constructInPlace(ret);
  158. ret->dbgLineNumber = lineNumber;
  159. ret->op = op;
  160. ret->expr = expr;
  161. return ret;
  162. }
  163. VarNode *VarNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex)
  164. {
  165. VarNode *ret = (VarNode *)consoleAlloc(sizeof(VarNode));
  166. constructInPlace(ret);
  167. ret->dbgLineNumber = lineNumber;
  168. ret->varName = varName;
  169. ret->arrayIndex = arrayIndex;
  170. return ret;
  171. }
  172. IntNode *IntNode::alloc(S32 lineNumber, S32 value)
  173. {
  174. IntNode *ret = (IntNode *)consoleAlloc(sizeof(IntNode));
  175. constructInPlace(ret);
  176. ret->dbgLineNumber = lineNumber;
  177. ret->value = value;
  178. return ret;
  179. }
  180. ConditionalExprNode *ConditionalExprNode::alloc(S32 lineNumber, ExprNode *testExpr, ExprNode *trueExpr, ExprNode *falseExpr)
  181. {
  182. ConditionalExprNode *ret = (ConditionalExprNode *)consoleAlloc(sizeof(ConditionalExprNode));
  183. constructInPlace(ret);
  184. ret->dbgLineNumber = lineNumber;
  185. ret->testExpr = testExpr;
  186. ret->trueExpr = trueExpr;
  187. ret->falseExpr = falseExpr;
  188. ret->integer = false;
  189. return ret;
  190. }
  191. FloatNode *FloatNode::alloc(S32 lineNumber, F64 value)
  192. {
  193. FloatNode *ret = (FloatNode *)consoleAlloc(sizeof(FloatNode));
  194. constructInPlace(ret);
  195. ret->dbgLineNumber = lineNumber;
  196. ret->value = value;
  197. return ret;
  198. }
  199. StrConstNode *StrConstNode::alloc(S32 lineNumber, char *str, bool tag, bool doc)
  200. {
  201. StrConstNode *ret = (StrConstNode *)consoleAlloc(sizeof(StrConstNode));
  202. constructInPlace(ret);
  203. ret->dbgLineNumber = lineNumber;
  204. dsize_t retStrLen = dStrlen(str) + 1;
  205. ret->str = (char *)consoleAlloc(retStrLen);
  206. ret->tag = tag;
  207. ret->doc = doc;
  208. dStrcpy(ret->str, str, retStrLen);
  209. return ret;
  210. }
  211. ConstantNode *ConstantNode::alloc(S32 lineNumber, StringTableEntry value)
  212. {
  213. ConstantNode *ret = (ConstantNode *)consoleAlloc(sizeof(ConstantNode));
  214. constructInPlace(ret);
  215. ret->dbgLineNumber = lineNumber;
  216. ret->value = value;
  217. return ret;
  218. }
  219. AssignExprNode *AssignExprNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex, ExprNode *expr)
  220. {
  221. AssignExprNode *ret = (AssignExprNode *)consoleAlloc(sizeof(AssignExprNode));
  222. constructInPlace(ret);
  223. ret->dbgLineNumber = lineNumber;
  224. ret->varName = varName;
  225. ret->expr = expr;
  226. ret->arrayIndex = arrayIndex;
  227. return ret;
  228. }
  229. AssignOpExprNode *AssignOpExprNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex, ExprNode *expr, S32 op)
  230. {
  231. AssignOpExprNode *ret = (AssignOpExprNode *)consoleAlloc(sizeof(AssignOpExprNode));
  232. constructInPlace(ret);
  233. ret->dbgLineNumber = lineNumber;
  234. ret->varName = varName;
  235. ret->expr = expr;
  236. ret->arrayIndex = arrayIndex;
  237. ret->op = op;
  238. return ret;
  239. }
  240. TTagSetStmtNode *TTagSetStmtNode::alloc(S32 lineNumber, StringTableEntry tag, ExprNode *valueExpr, ExprNode *stringExpr)
  241. {
  242. TTagSetStmtNode *ret = (TTagSetStmtNode *)consoleAlloc(sizeof(TTagSetStmtNode));
  243. constructInPlace(ret);
  244. ret->dbgLineNumber = lineNumber;
  245. ret->tag = tag;
  246. ret->valueExpr = valueExpr;
  247. ret->stringExpr = stringExpr;
  248. return ret;
  249. }
  250. TTagDerefNode *TTagDerefNode::alloc(S32 lineNumber, ExprNode *expr)
  251. {
  252. TTagDerefNode *ret = (TTagDerefNode *)consoleAlloc(sizeof(TTagDerefNode));
  253. constructInPlace(ret);
  254. ret->dbgLineNumber = lineNumber;
  255. ret->expr = expr;
  256. return ret;
  257. }
  258. TTagExprNode *TTagExprNode::alloc(S32 lineNumber, StringTableEntry tag)
  259. {
  260. TTagExprNode *ret = (TTagExprNode *)consoleAlloc(sizeof(TTagExprNode));
  261. constructInPlace(ret);
  262. ret->dbgLineNumber = lineNumber;
  263. ret->tag = tag;
  264. return ret;
  265. }
  266. FuncCallExprNode *FuncCallExprNode::alloc(S32 lineNumber, StringTableEntry funcName, StringTableEntry nameSpace, ExprNode *args, bool dot)
  267. {
  268. FuncCallExprNode *ret = (FuncCallExprNode *)consoleAlloc(sizeof(FuncCallExprNode));
  269. constructInPlace(ret);
  270. ret->dbgLineNumber = lineNumber;
  271. ret->funcName = funcName;
  272. ret->nameSpace = nameSpace;
  273. ret->args = args;
  274. if (dot)
  275. ret->callType = MethodCall;
  276. else
  277. {
  278. if (nameSpace && !dStricmp(nameSpace, "Parent"))
  279. ret->callType = ParentCall;
  280. else
  281. ret->callType = FunctionCall;
  282. }
  283. return ret;
  284. }
  285. FuncPointerCallExprNode *FuncPointerCallExprNode::alloc(S32 lineNumber, ExprNode *funcPointer, ExprNode *args)
  286. {
  287. FuncPointerCallExprNode *ret = (FuncPointerCallExprNode *)consoleAlloc(sizeof(FuncPointerCallExprNode));
  288. constructInPlace(ret);
  289. ret->dbgLineNumber = lineNumber;
  290. ret->funcPointer = funcPointer;
  291. ret->args = args;
  292. return ret;
  293. }
  294. AssertCallExprNode *AssertCallExprNode::alloc(S32 lineNumber, ExprNode *testExpr, const char *message)
  295. {
  296. #ifdef TORQUE_ENABLE_SCRIPTASSERTS
  297. AssertCallExprNode *ret = (AssertCallExprNode *)consoleAlloc(sizeof(FuncCallExprNode));
  298. constructInPlace(ret);
  299. ret->dbgLineNumber = lineNumber;
  300. ret->testExpr = testExpr;
  301. ret->message = message ? message : "TorqueScript assert!";
  302. return ret;
  303. #else
  304. return NULL;
  305. #endif
  306. }
  307. SlotAccessNode *SlotAccessNode::alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName)
  308. {
  309. SlotAccessNode *ret = (SlotAccessNode *)consoleAlloc(sizeof(SlotAccessNode));
  310. constructInPlace(ret);
  311. ret->dbgLineNumber = lineNumber;
  312. ret->objectExpr = objectExpr;
  313. ret->arrayExpr = arrayExpr;
  314. ret->slotName = slotName;
  315. return ret;
  316. }
  317. InternalSlotAccessNode *InternalSlotAccessNode::alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *slotExpr, bool recurse)
  318. {
  319. InternalSlotAccessNode *ret = (InternalSlotAccessNode *)consoleAlloc(sizeof(InternalSlotAccessNode));
  320. constructInPlace(ret);
  321. ret->dbgLineNumber = lineNumber;
  322. ret->objectExpr = objectExpr;
  323. ret->slotExpr = slotExpr;
  324. ret->recurse = recurse;
  325. return ret;
  326. }
  327. SlotAssignNode *SlotAssignNode::alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName, ExprNode *valueExpr, U32 typeID /* = -1 */)
  328. {
  329. SlotAssignNode *ret = (SlotAssignNode *)consoleAlloc(sizeof(SlotAssignNode));
  330. constructInPlace(ret);
  331. ret->dbgLineNumber = lineNumber;
  332. ret->objectExpr = objectExpr;
  333. ret->arrayExpr = arrayExpr;
  334. ret->slotName = slotName;
  335. ret->valueExpr = valueExpr;
  336. ret->typeID = typeID;
  337. return ret;
  338. }
  339. SlotAssignOpNode *SlotAssignOpNode::alloc(S32 lineNumber, ExprNode *objectExpr, StringTableEntry slotName, ExprNode *arrayExpr, S32 op, ExprNode *valueExpr)
  340. {
  341. SlotAssignOpNode *ret = (SlotAssignOpNode *)consoleAlloc(sizeof(SlotAssignOpNode));
  342. constructInPlace(ret);
  343. ret->dbgLineNumber = lineNumber;
  344. ret->objectExpr = objectExpr;
  345. ret->arrayExpr = arrayExpr;
  346. ret->slotName = slotName;
  347. ret->op = op;
  348. ret->valueExpr = valueExpr;
  349. return ret;
  350. }
  351. ObjectDeclNode *ObjectDeclNode::alloc(S32 lineNumber, ExprNode *classNameExpr, ExprNode *objectNameExpr, ExprNode *argList, StringTableEntry parentObject, SlotAssignNode *slotDecls, ObjectDeclNode *subObjects, bool isDatablock, bool classNameInternal, bool isSingleton)
  352. {
  353. ObjectDeclNode *ret = (ObjectDeclNode *)consoleAlloc(sizeof(ObjectDeclNode));
  354. constructInPlace(ret);
  355. ret->dbgLineNumber = lineNumber;
  356. ret->classNameExpr = classNameExpr;
  357. ret->objectNameExpr = objectNameExpr;
  358. ret->argList = argList;
  359. ret->slotDecls = slotDecls;
  360. ret->subObjects = subObjects;
  361. ret->isDatablock = isDatablock;
  362. ret->isClassNameInternal = classNameInternal;
  363. ret->isSingleton = isSingleton;
  364. ret->failOffset = 0;
  365. if (parentObject)
  366. ret->parentObject = parentObject;
  367. else
  368. ret->parentObject = StringTable->EmptyString();
  369. return ret;
  370. }
  371. FunctionDeclStmtNode *FunctionDeclStmtNode::alloc(S32 lineNumber, StringTableEntry fnName, StringTableEntry nameSpace, VarNode *args, StmtNode *stmts)
  372. {
  373. FunctionDeclStmtNode *ret = (FunctionDeclStmtNode *)consoleAlloc(sizeof(FunctionDeclStmtNode));
  374. constructInPlace(ret);
  375. ret->dbgLineNumber = lineNumber;
  376. ret->fnName = fnName;
  377. ret->args = args;
  378. ret->stmts = stmts;
  379. ret->nameSpace = nameSpace;
  380. ret->package = NULL;
  381. return ret;
  382. }