astAlloc.cc 11 KB

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