astAlloc.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. ret->str = (char *) consoleAlloc(dStrlen(str) + 1);
  205. ret->tag = tag;
  206. ret->doc = doc;
  207. dStrcpy(ret->str, str);
  208. return ret;
  209. }
  210. ConstantNode *ConstantNode::alloc( S32 lineNumber, StringTableEntry value )
  211. {
  212. ConstantNode *ret = (ConstantNode *) consoleAlloc(sizeof(ConstantNode));
  213. constructInPlace(ret);
  214. ret->dbgLineNumber = lineNumber;
  215. ret->value = value;
  216. return ret;
  217. }
  218. AssignExprNode *AssignExprNode::alloc( S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex, ExprNode *expr )
  219. {
  220. AssignExprNode *ret = (AssignExprNode *) consoleAlloc(sizeof(AssignExprNode));
  221. constructInPlace(ret);
  222. ret->dbgLineNumber = lineNumber;
  223. ret->varName = varName;
  224. ret->expr = expr;
  225. ret->arrayIndex = arrayIndex;
  226. return ret;
  227. }
  228. AssignOpExprNode *AssignOpExprNode::alloc( S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex, ExprNode *expr, S32 op )
  229. {
  230. AssignOpExprNode *ret = (AssignOpExprNode *) consoleAlloc(sizeof(AssignOpExprNode));
  231. constructInPlace(ret);
  232. ret->dbgLineNumber = lineNumber;
  233. ret->varName = varName;
  234. ret->expr = expr;
  235. ret->arrayIndex = arrayIndex;
  236. ret->op = op;
  237. return ret;
  238. }
  239. TTagSetStmtNode *TTagSetStmtNode::alloc( S32 lineNumber, StringTableEntry tag, ExprNode *valueExpr, ExprNode *stringExpr )
  240. {
  241. TTagSetStmtNode *ret = (TTagSetStmtNode *) consoleAlloc(sizeof(TTagSetStmtNode));
  242. constructInPlace(ret);
  243. ret->dbgLineNumber = lineNumber;
  244. ret->tag = tag;
  245. ret->valueExpr = valueExpr;
  246. ret->stringExpr = stringExpr;
  247. return ret;
  248. }
  249. TTagDerefNode *TTagDerefNode::alloc( S32 lineNumber, ExprNode *expr )
  250. {
  251. TTagDerefNode *ret = (TTagDerefNode *) consoleAlloc(sizeof(TTagDerefNode));
  252. constructInPlace(ret);
  253. ret->dbgLineNumber = lineNumber;
  254. ret->expr = expr;
  255. return ret;
  256. }
  257. TTagExprNode *TTagExprNode::alloc( S32 lineNumber, StringTableEntry tag )
  258. {
  259. TTagExprNode *ret = (TTagExprNode *) consoleAlloc(sizeof(TTagExprNode));
  260. constructInPlace(ret);
  261. ret->dbgLineNumber = lineNumber;
  262. ret->tag = tag;
  263. return ret;
  264. }
  265. FuncCallExprNode *FuncCallExprNode::alloc( S32 lineNumber, StringTableEntry funcName, StringTableEntry nameSpace, ExprNode *args, bool dot )
  266. {
  267. FuncCallExprNode *ret = (FuncCallExprNode *) consoleAlloc(sizeof(FuncCallExprNode));
  268. constructInPlace(ret);
  269. ret->dbgLineNumber = lineNumber;
  270. ret->funcName = funcName;
  271. ret->nameSpace = nameSpace;
  272. ret->args = args;
  273. if(dot)
  274. ret->callType = MethodCall;
  275. else
  276. {
  277. if(nameSpace && !dStricmp(nameSpace, "Parent"))
  278. ret->callType = ParentCall;
  279. else
  280. ret->callType = FunctionCall;
  281. }
  282. return ret;
  283. }
  284. AssertCallExprNode *AssertCallExprNode::alloc( S32 lineNumber, ExprNode *testExpr, const char *message )
  285. {
  286. #ifdef TORQUE_ENABLE_SCRIPTASSERTS
  287. AssertCallExprNode *ret = (AssertCallExprNode *) consoleAlloc(sizeof(FuncCallExprNode));
  288. constructInPlace(ret);
  289. ret->dbgLineNumber = lineNumber;
  290. ret->testExpr = testExpr;
  291. ret->message = message ? message : "TorqueScript assert!";
  292. return ret;
  293. #else
  294. return NULL;
  295. #endif
  296. }
  297. SlotAccessNode *SlotAccessNode::alloc( S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName )
  298. {
  299. SlotAccessNode *ret = (SlotAccessNode *) consoleAlloc(sizeof(SlotAccessNode));
  300. constructInPlace(ret);
  301. ret->dbgLineNumber = lineNumber;
  302. ret->objectExpr = objectExpr;
  303. ret->arrayExpr = arrayExpr;
  304. ret->slotName = slotName;
  305. return ret;
  306. }
  307. InternalSlotAccessNode *InternalSlotAccessNode::alloc( S32 lineNumber, ExprNode *objectExpr, ExprNode *slotExpr, bool recurse )
  308. {
  309. InternalSlotAccessNode *ret = (InternalSlotAccessNode *) consoleAlloc(sizeof(InternalSlotAccessNode));
  310. constructInPlace(ret);
  311. ret->dbgLineNumber = lineNumber;
  312. ret->objectExpr = objectExpr;
  313. ret->slotExpr = slotExpr;
  314. ret->recurse = recurse;
  315. return ret;
  316. }
  317. SlotAssignNode *SlotAssignNode::alloc( S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName, ExprNode *valueExpr, U32 typeID /* = -1 */ )
  318. {
  319. SlotAssignNode *ret = (SlotAssignNode *) consoleAlloc(sizeof(SlotAssignNode));
  320. constructInPlace(ret);
  321. ret->dbgLineNumber = lineNumber;
  322. ret->objectExpr = objectExpr;
  323. ret->arrayExpr = arrayExpr;
  324. ret->slotName = slotName;
  325. ret->valueExpr = valueExpr;
  326. ret->typeID = typeID;
  327. return ret;
  328. }
  329. SlotAssignOpNode *SlotAssignOpNode::alloc( S32 lineNumber, ExprNode *objectExpr, StringTableEntry slotName, ExprNode *arrayExpr, S32 op, ExprNode *valueExpr )
  330. {
  331. SlotAssignOpNode *ret = (SlotAssignOpNode *) consoleAlloc(sizeof(SlotAssignOpNode));
  332. constructInPlace(ret);
  333. ret->dbgLineNumber = lineNumber;
  334. ret->objectExpr = objectExpr;
  335. ret->arrayExpr = arrayExpr;
  336. ret->slotName = slotName;
  337. ret->op = op;
  338. ret->valueExpr = valueExpr;
  339. return ret;
  340. }
  341. ObjectDeclNode *ObjectDeclNode::alloc( S32 lineNumber, ExprNode *classNameExpr, ExprNode *objectNameExpr, ExprNode *argList, StringTableEntry parentObject, SlotAssignNode *slotDecls, ObjectDeclNode *subObjects, bool isDatablock, bool classNameInternal, bool isSingleton )
  342. {
  343. ObjectDeclNode *ret = (ObjectDeclNode *) consoleAlloc(sizeof(ObjectDeclNode));
  344. constructInPlace(ret);
  345. ret->dbgLineNumber = lineNumber;
  346. ret->classNameExpr = classNameExpr;
  347. ret->objectNameExpr = objectNameExpr;
  348. ret->argList = argList;
  349. ret->slotDecls = slotDecls;
  350. ret->subObjects = subObjects;
  351. ret->isDatablock = isDatablock;
  352. ret->isClassNameInternal = classNameInternal;
  353. ret->isSingleton = isSingleton;
  354. ret->failOffset = 0;
  355. if(parentObject)
  356. ret->parentObject = parentObject;
  357. else
  358. ret->parentObject = StringTable->EmptyString();
  359. return ret;
  360. }
  361. FunctionDeclStmtNode *FunctionDeclStmtNode::alloc( S32 lineNumber, StringTableEntry fnName, StringTableEntry nameSpace, VarNode *args, StmtNode *stmts )
  362. {
  363. FunctionDeclStmtNode *ret = (FunctionDeclStmtNode *) consoleAlloc(sizeof(FunctionDeclStmtNode));
  364. constructInPlace(ret);
  365. ret->dbgLineNumber = lineNumber;
  366. ret->fnName = fnName;
  367. ret->args = args;
  368. ret->stmts = stmts;
  369. ret->nameSpace = nameSpace;
  370. ret->package = NULL;
  371. return ret;
  372. }