astAlloc.cpp
Engine/source/console/astAlloc.cpp
TorqueScript AST node allocators.
Detailed Description
TorqueScript AST node allocators.
These static methods exist to allocate new AST node for the compiler. They all allocate memory from the consoleAllocator for efficiency, and often take arguments relating to the state of the nodes. They are called from gram.y (really gram.c) as the lexer analyzes the script code.
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "platform/platform.h" 25#include "console/console.h" 26#include "console/compiler.h" 27#include "console/consoleInternal.h" 28 29using namespace Compiler; 30 31/// @file 32/// 33/// TorqueScript AST node allocators. 34/// 35/// These static methods exist to allocate new AST node for the compiler. They 36/// all allocate memory from the consoleAllocator for efficiency, and often take 37/// arguments relating to the state of the nodes. They are called from gram.y 38/// (really gram.c) as the lexer analyzes the script code. 39 40//------------------------------------------------------------ 41 42BreakStmtNode *BreakStmtNode::alloc(S32 lineNumber) 43{ 44 BreakStmtNode *ret = (BreakStmtNode *)consoleAlloc(sizeof(BreakStmtNode)); 45 constructInPlace(ret); 46 ret->dbgLineNumber = lineNumber; 47 return ret; 48} 49 50ContinueStmtNode *ContinueStmtNode::alloc(S32 lineNumber) 51{ 52 ContinueStmtNode *ret = (ContinueStmtNode *)consoleAlloc(sizeof(ContinueStmtNode)); 53 constructInPlace(ret); 54 ret->dbgLineNumber = lineNumber; 55 return ret; 56} 57 58ReturnStmtNode *ReturnStmtNode::alloc(S32 lineNumber, ExprNode *expr) 59{ 60 ReturnStmtNode *ret = (ReturnStmtNode *)consoleAlloc(sizeof(ReturnStmtNode)); 61 constructInPlace(ret); 62 ret->expr = expr; 63 ret->dbgLineNumber = lineNumber; 64 65 return ret; 66} 67 68IfStmtNode *IfStmtNode::alloc(S32 lineNumber, ExprNode *testExpr, StmtNode *ifBlock, StmtNode *elseBlock, bool propagate) 69{ 70 IfStmtNode *ret = (IfStmtNode *)consoleAlloc(sizeof(IfStmtNode)); 71 constructInPlace(ret); 72 ret->dbgLineNumber = lineNumber; 73 74 ret->testExpr = testExpr; 75 ret->ifBlock = ifBlock; 76 ret->elseBlock = elseBlock; 77 ret->propagate = propagate; 78 79 return ret; 80} 81 82LoopStmtNode *LoopStmtNode::alloc(S32 lineNumber, ExprNode *initExpr, ExprNode *testExpr, ExprNode *endLoopExpr, StmtNode *loopBlock, bool isDoLoop) 83{ 84 LoopStmtNode *ret = (LoopStmtNode *)consoleAlloc(sizeof(LoopStmtNode)); 85 constructInPlace(ret); 86 ret->dbgLineNumber = lineNumber; 87 ret->testExpr = testExpr; 88 ret->initExpr = initExpr; 89 ret->endLoopExpr = endLoopExpr; 90 ret->loopBlock = loopBlock; 91 ret->isDoLoop = isDoLoop; 92 93 // Deal with setting some dummy constant nodes if we weren't provided with 94 // info... This allows us to play nice with missing parts of for(;;) for 95 // instance. 96 if (!ret->testExpr) ret->testExpr = IntNode::alloc(lineNumber, 1); 97 98 return ret; 99} 100 101IterStmtNode* IterStmtNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode* containerExpr, StmtNode* body, bool isStringIter) 102{ 103 IterStmtNode* ret = (IterStmtNode*)consoleAlloc(sizeof(IterStmtNode)); 104 constructInPlace(ret); 105 106 ret->dbgLineNumber = lineNumber; 107 ret->varName = varName; 108 ret->containerExpr = containerExpr; 109 ret->body = body; 110 ret->isStringIter = isStringIter; 111 112 return ret; 113} 114 115FloatBinaryExprNode *FloatBinaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *left, ExprNode *right) 116{ 117 FloatBinaryExprNode *ret = (FloatBinaryExprNode *)consoleAlloc(sizeof(FloatBinaryExprNode)); 118 constructInPlace(ret); 119 ret->dbgLineNumber = lineNumber; 120 121 ret->op = op; 122 ret->left = left; 123 ret->right = right; 124 125 return ret; 126} 127 128IntBinaryExprNode *IntBinaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *left, ExprNode *right) 129{ 130 IntBinaryExprNode *ret = (IntBinaryExprNode *)consoleAlloc(sizeof(IntBinaryExprNode)); 131 constructInPlace(ret); 132 ret->dbgLineNumber = lineNumber; 133 134 ret->op = op; 135 ret->left = left; 136 ret->right = right; 137 138 return ret; 139} 140 141StreqExprNode *StreqExprNode::alloc(S32 lineNumber, ExprNode *left, ExprNode *right, bool eq) 142{ 143 StreqExprNode *ret = (StreqExprNode *)consoleAlloc(sizeof(StreqExprNode)); 144 constructInPlace(ret); 145 ret->dbgLineNumber = lineNumber; 146 ret->left = left; 147 ret->right = right; 148 ret->eq = eq; 149 150 return ret; 151} 152 153StrcatExprNode *StrcatExprNode::alloc(S32 lineNumber, ExprNode *left, ExprNode *right, S32 appendChar) 154{ 155 StrcatExprNode *ret = (StrcatExprNode *)consoleAlloc(sizeof(StrcatExprNode)); 156 constructInPlace(ret); 157 ret->dbgLineNumber = lineNumber; 158 ret->left = left; 159 ret->right = right; 160 ret->appendChar = appendChar; 161 162 return ret; 163} 164 165CommaCatExprNode *CommaCatExprNode::alloc(S32 lineNumber, ExprNode *left, ExprNode *right) 166{ 167 CommaCatExprNode *ret = (CommaCatExprNode *)consoleAlloc(sizeof(CommaCatExprNode)); 168 constructInPlace(ret); 169 ret->dbgLineNumber = lineNumber; 170 ret->left = left; 171 ret->right = right; 172 173 return ret; 174} 175 176IntUnaryExprNode *IntUnaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *expr) 177{ 178 IntUnaryExprNode *ret = (IntUnaryExprNode *)consoleAlloc(sizeof(IntUnaryExprNode)); 179 constructInPlace(ret); 180 ret->dbgLineNumber = lineNumber; 181 ret->op = op; 182 ret->expr = expr; 183 return ret; 184} 185 186FloatUnaryExprNode *FloatUnaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *expr) 187{ 188 FloatUnaryExprNode *ret = (FloatUnaryExprNode *)consoleAlloc(sizeof(FloatUnaryExprNode)); 189 constructInPlace(ret); 190 ret->dbgLineNumber = lineNumber; 191 ret->op = op; 192 ret->expr = expr; 193 return ret; 194} 195 196VarNode *VarNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex) 197{ 198 VarNode *ret = (VarNode *)consoleAlloc(sizeof(VarNode)); 199 constructInPlace(ret); 200 ret->dbgLineNumber = lineNumber; 201 ret->varName = varName; 202 ret->arrayIndex = arrayIndex; 203 return ret; 204} 205 206IntNode *IntNode::alloc(S32 lineNumber, S32 value) 207{ 208 IntNode *ret = (IntNode *)consoleAlloc(sizeof(IntNode)); 209 constructInPlace(ret); 210 ret->dbgLineNumber = lineNumber; 211 ret->value = value; 212 return ret; 213} 214 215ConditionalExprNode *ConditionalExprNode::alloc(S32 lineNumber, ExprNode *testExpr, ExprNode *trueExpr, ExprNode *falseExpr) 216{ 217 ConditionalExprNode *ret = (ConditionalExprNode *)consoleAlloc(sizeof(ConditionalExprNode)); 218 constructInPlace(ret); 219 ret->dbgLineNumber = lineNumber; 220 ret->testExpr = testExpr; 221 ret->trueExpr = trueExpr; 222 ret->falseExpr = falseExpr; 223 ret->integer = false; 224 return ret; 225} 226 227FloatNode *FloatNode::alloc(S32 lineNumber, F64 value) 228{ 229 FloatNode *ret = (FloatNode *)consoleAlloc(sizeof(FloatNode)); 230 constructInPlace(ret); 231 232 ret->dbgLineNumber = lineNumber; 233 ret->value = value; 234 return ret; 235} 236 237StrConstNode *StrConstNode::alloc(S32 lineNumber, char *str, bool tag, bool doc) 238{ 239 StrConstNode *ret = (StrConstNode *)consoleAlloc(sizeof(StrConstNode)); 240 constructInPlace(ret); 241 ret->dbgLineNumber = lineNumber; 242 dsize_t retStrLen = dStrlen(str) + 1; 243 ret->str = (char *)consoleAlloc(retStrLen); 244 ret->tag = tag; 245 ret->doc = doc; 246 dStrcpy(ret->str, str, retStrLen); 247 248 return ret; 249} 250 251ConstantNode *ConstantNode::alloc(S32 lineNumber, StringTableEntry value) 252{ 253 ConstantNode *ret = (ConstantNode *)consoleAlloc(sizeof(ConstantNode)); 254 constructInPlace(ret); 255 ret->dbgLineNumber = lineNumber; 256 ret->value = value; 257 return ret; 258} 259 260AssignExprNode *AssignExprNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex, ExprNode *expr) 261{ 262 AssignExprNode *ret = (AssignExprNode *)consoleAlloc(sizeof(AssignExprNode)); 263 constructInPlace(ret); 264 ret->dbgLineNumber = lineNumber; 265 ret->varName = varName; 266 ret->expr = expr; 267 ret->arrayIndex = arrayIndex; 268 269 return ret; 270} 271 272AssignOpExprNode *AssignOpExprNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex, ExprNode *expr, S32 op) 273{ 274 AssignOpExprNode *ret = (AssignOpExprNode *)consoleAlloc(sizeof(AssignOpExprNode)); 275 constructInPlace(ret); 276 ret->dbgLineNumber = lineNumber; 277 ret->varName = varName; 278 ret->expr = expr; 279 ret->arrayIndex = arrayIndex; 280 ret->op = op; 281 return ret; 282} 283 284TTagSetStmtNode *TTagSetStmtNode::alloc(S32 lineNumber, StringTableEntry tag, ExprNode *valueExpr, ExprNode *stringExpr) 285{ 286 TTagSetStmtNode *ret = (TTagSetStmtNode *)consoleAlloc(sizeof(TTagSetStmtNode)); 287 constructInPlace(ret); 288 ret->dbgLineNumber = lineNumber; 289 ret->tag = tag; 290 ret->valueExpr = valueExpr; 291 ret->stringExpr = stringExpr; 292 return ret; 293} 294 295TTagDerefNode *TTagDerefNode::alloc(S32 lineNumber, ExprNode *expr) 296{ 297 TTagDerefNode *ret = (TTagDerefNode *)consoleAlloc(sizeof(TTagDerefNode)); 298 constructInPlace(ret); 299 ret->dbgLineNumber = lineNumber; 300 ret->expr = expr; 301 return ret; 302} 303 304TTagExprNode *TTagExprNode::alloc(S32 lineNumber, StringTableEntry tag) 305{ 306 TTagExprNode *ret = (TTagExprNode *)consoleAlloc(sizeof(TTagExprNode)); 307 constructInPlace(ret); 308 ret->dbgLineNumber = lineNumber; 309 ret->tag = tag; 310 return ret; 311} 312 313FuncCallExprNode *FuncCallExprNode::alloc(S32 lineNumber, StringTableEntry funcName, StringTableEntry nameSpace, ExprNode *args, bool dot) 314{ 315 FuncCallExprNode *ret = (FuncCallExprNode *)consoleAlloc(sizeof(FuncCallExprNode)); 316 constructInPlace(ret); 317 ret->dbgLineNumber = lineNumber; 318 ret->funcName = funcName; 319 ret->nameSpace = nameSpace; 320 ret->args = args; 321 if (dot) 322 ret->callType = MethodCall; 323 else 324 { 325 if (nameSpace && !dStricmp(nameSpace, "Parent")) 326 ret->callType = ParentCall; 327 else 328 ret->callType = FunctionCall; 329 } 330 return ret; 331} 332 333FuncPointerCallExprNode *FuncPointerCallExprNode::alloc(S32 lineNumber, ExprNode *funcPointer, ExprNode *args) 334{ 335 FuncPointerCallExprNode *ret = (FuncPointerCallExprNode *)consoleAlloc(sizeof(FuncPointerCallExprNode)); 336 constructInPlace(ret); 337 ret->dbgLineNumber = lineNumber; 338 ret->funcPointer = funcPointer; 339 ret->args = args; 340 return ret; 341} 342 343AssertCallExprNode *AssertCallExprNode::alloc(S32 lineNumber, ExprNode *testExpr, const char *message) 344{ 345#ifdef TORQUE_ENABLE_SCRIPTASSERTS 346 347 AssertCallExprNode *ret = (AssertCallExprNode *)consoleAlloc(sizeof(FuncCallExprNode)); 348 constructInPlace(ret); 349 ret->dbgLineNumber = lineNumber; 350 ret->testExpr = testExpr; 351 ret->message = message ? message : "TorqueScript assert!"; 352 return ret; 353 354#else 355 356 return NULL; 357 358#endif 359} 360 361SlotAccessNode *SlotAccessNode::alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName) 362{ 363 SlotAccessNode *ret = (SlotAccessNode *)consoleAlloc(sizeof(SlotAccessNode)); 364 constructInPlace(ret); 365 ret->dbgLineNumber = lineNumber; 366 ret->objectExpr = objectExpr; 367 ret->arrayExpr = arrayExpr; 368 ret->slotName = slotName; 369 return ret; 370} 371 372InternalSlotAccessNode *InternalSlotAccessNode::alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *slotExpr, bool recurse) 373{ 374 InternalSlotAccessNode *ret = (InternalSlotAccessNode *)consoleAlloc(sizeof(InternalSlotAccessNode)); 375 constructInPlace(ret); 376 ret->dbgLineNumber = lineNumber; 377 ret->objectExpr = objectExpr; 378 ret->slotExpr = slotExpr; 379 ret->recurse = recurse; 380 return ret; 381} 382 383SlotAssignNode *SlotAssignNode::alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName, ExprNode *valueExpr, U32 typeID /* = -1 */) 384{ 385 SlotAssignNode *ret = (SlotAssignNode *)consoleAlloc(sizeof(SlotAssignNode)); 386 constructInPlace(ret); 387 ret->dbgLineNumber = lineNumber; 388 ret->objectExpr = objectExpr; 389 ret->arrayExpr = arrayExpr; 390 ret->slotName = slotName; 391 ret->valueExpr = valueExpr; 392 ret->typeID = typeID; 393 return ret; 394} 395 396SlotAssignOpNode *SlotAssignOpNode::alloc(S32 lineNumber, ExprNode *objectExpr, StringTableEntry slotName, ExprNode *arrayExpr, S32 op, ExprNode *valueExpr) 397{ 398 SlotAssignOpNode *ret = (SlotAssignOpNode *)consoleAlloc(sizeof(SlotAssignOpNode)); 399 constructInPlace(ret); 400 ret->dbgLineNumber = lineNumber; 401 ret->objectExpr = objectExpr; 402 ret->arrayExpr = arrayExpr; 403 ret->slotName = slotName; 404 ret->op = op; 405 ret->valueExpr = valueExpr; 406 return ret; 407} 408 409ObjectDeclNode *ObjectDeclNode::alloc(S32 lineNumber, ExprNode *classNameExpr, ExprNode *objectNameExpr, ExprNode *argList, StringTableEntry parentObject, SlotAssignNode *slotDecls, ObjectDeclNode *subObjects, bool isDatablock, bool classNameInternal, bool isSingleton) 410{ 411 ObjectDeclNode *ret = (ObjectDeclNode *)consoleAlloc(sizeof(ObjectDeclNode)); 412 constructInPlace(ret); 413 ret->dbgLineNumber = lineNumber; 414 ret->classNameExpr = classNameExpr; 415 ret->objectNameExpr = objectNameExpr; 416 ret->argList = argList; 417 ret->slotDecls = slotDecls; 418 ret->subObjects = subObjects; 419 ret->isDatablock = isDatablock; 420 ret->isClassNameInternal = classNameInternal; 421 ret->isSingleton = isSingleton; 422 ret->failOffset = 0; 423 if (parentObject) 424 ret->parentObject = parentObject; 425 else 426 ret->parentObject = StringTable->EmptyString(); 427 return ret; 428} 429 430FunctionDeclStmtNode *FunctionDeclStmtNode::alloc(S32 lineNumber, StringTableEntry fnName, StringTableEntry nameSpace, VarNode *args, StmtNode *stmts) 431{ 432 FunctionDeclStmtNode *ret = (FunctionDeclStmtNode *)consoleAlloc(sizeof(FunctionDeclStmtNode)); 433 constructInPlace(ret); 434 ret->dbgLineNumber = lineNumber; 435 ret->fnName = fnName; 436 ret->args = args; 437 ret->stmts = stmts; 438 ret->nameSpace = nameSpace; 439 ret->package = NULL; 440 return ret; 441} 442