Torque3D/Engine/source/console/astAlloc.cpp

440 lines
14 KiB
C++
Raw Normal View History

2012-09-19 15:15:01 +00:00
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#include "platform/platform.h"
#include "console/console.h"
#include "console/compiler.h"
#include "console/consoleInternal.h"
using namespace Compiler;
/// @file
///
/// 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.
//------------------------------------------------------------
2017-11-06 04:33:32 +00:00
BreakStmtNode *BreakStmtNode::alloc(S32 lineNumber)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
BreakStmtNode *ret = (BreakStmtNode *)consoleAlloc(sizeof(BreakStmtNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
return ret;
}
2017-11-06 04:33:32 +00:00
ContinueStmtNode *ContinueStmtNode::alloc(S32 lineNumber)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
ContinueStmtNode *ret = (ContinueStmtNode *)consoleAlloc(sizeof(ContinueStmtNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
return ret;
}
2017-11-06 04:33:32 +00:00
ReturnStmtNode *ReturnStmtNode::alloc(S32 lineNumber, ExprNode *expr)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
ReturnStmtNode *ret = (ReturnStmtNode *)consoleAlloc(sizeof(ReturnStmtNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->expr = expr;
ret->dbgLineNumber = lineNumber;
return ret;
}
2017-11-06 04:33:32 +00:00
IfStmtNode *IfStmtNode::alloc(S32 lineNumber, ExprNode *testExpr, StmtNode *ifBlock, StmtNode *elseBlock, bool propagate)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
IfStmtNode *ret = (IfStmtNode *)consoleAlloc(sizeof(IfStmtNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->testExpr = testExpr;
ret->ifBlock = ifBlock;
ret->elseBlock = elseBlock;
ret->propagate = propagate;
return ret;
}
2017-11-06 04:33:32 +00:00
LoopStmtNode *LoopStmtNode::alloc(S32 lineNumber, ExprNode *initExpr, ExprNode *testExpr, ExprNode *endLoopExpr, StmtNode *loopBlock, bool isDoLoop)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
LoopStmtNode *ret = (LoopStmtNode *)consoleAlloc(sizeof(LoopStmtNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->testExpr = testExpr;
ret->initExpr = initExpr;
ret->endLoopExpr = endLoopExpr;
ret->loopBlock = loopBlock;
ret->isDoLoop = isDoLoop;
// Deal with setting some dummy constant nodes if we weren't provided with
// info... This allows us to play nice with missing parts of for(;;) for
// instance.
2017-11-06 04:33:32 +00:00
if (!ret->testExpr) ret->testExpr = IntNode::alloc(lineNumber, 1);
2012-09-19 15:15:01 +00:00
return ret;
}
2017-11-06 04:33:32 +00:00
IterStmtNode* IterStmtNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode* containerExpr, StmtNode* body, bool isStringIter)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
IterStmtNode* ret = (IterStmtNode*)consoleAlloc(sizeof(IterStmtNode));
constructInPlace(ret);
2012-09-19 15:15:01 +00:00
ret->dbgLineNumber = lineNumber;
ret->varName = varName;
ret->containerExpr = containerExpr;
ret->body = body;
ret->isStringIter = isStringIter;
2017-11-06 04:33:32 +00:00
2012-09-19 15:15:01 +00:00
return ret;
}
2017-11-06 04:33:32 +00:00
FloatBinaryExprNode *FloatBinaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *left, ExprNode *right)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
FloatBinaryExprNode *ret = (FloatBinaryExprNode *)consoleAlloc(sizeof(FloatBinaryExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->op = op;
ret->left = left;
ret->right = right;
return ret;
}
2017-11-06 04:33:32 +00:00
IntBinaryExprNode *IntBinaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *left, ExprNode *right)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
IntBinaryExprNode *ret = (IntBinaryExprNode *)consoleAlloc(sizeof(IntBinaryExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->op = op;
ret->left = left;
ret->right = right;
return ret;
}
2017-11-06 04:33:32 +00:00
StreqExprNode *StreqExprNode::alloc(S32 lineNumber, ExprNode *left, ExprNode *right, bool eq)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
StreqExprNode *ret = (StreqExprNode *)consoleAlloc(sizeof(StreqExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->left = left;
ret->right = right;
ret->eq = eq;
return ret;
}
2017-11-06 04:33:32 +00:00
StrcatExprNode *StrcatExprNode::alloc(S32 lineNumber, ExprNode *left, ExprNode *right, S32 appendChar)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
StrcatExprNode *ret = (StrcatExprNode *)consoleAlloc(sizeof(StrcatExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->left = left;
ret->right = right;
ret->appendChar = appendChar;
return ret;
}
2017-11-06 04:33:32 +00:00
CommaCatExprNode *CommaCatExprNode::alloc(S32 lineNumber, ExprNode *left, ExprNode *right)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
CommaCatExprNode *ret = (CommaCatExprNode *)consoleAlloc(sizeof(CommaCatExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->left = left;
ret->right = right;
return ret;
}
2017-11-06 04:33:32 +00:00
IntUnaryExprNode *IntUnaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *expr)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
IntUnaryExprNode *ret = (IntUnaryExprNode *)consoleAlloc(sizeof(IntUnaryExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->op = op;
ret->expr = expr;
return ret;
}
2017-11-06 04:33:32 +00:00
FloatUnaryExprNode *FloatUnaryExprNode::alloc(S32 lineNumber, S32 op, ExprNode *expr)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
FloatUnaryExprNode *ret = (FloatUnaryExprNode *)consoleAlloc(sizeof(FloatUnaryExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->op = op;
ret->expr = expr;
return ret;
}
2017-11-06 04:33:32 +00:00
VarNode *VarNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
VarNode *ret = (VarNode *)consoleAlloc(sizeof(VarNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->varName = varName;
ret->arrayIndex = arrayIndex;
return ret;
}
2017-11-06 04:33:32 +00:00
IntNode *IntNode::alloc(S32 lineNumber, S32 value)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
IntNode *ret = (IntNode *)consoleAlloc(sizeof(IntNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->value = value;
return ret;
}
2017-11-06 04:33:32 +00:00
ConditionalExprNode *ConditionalExprNode::alloc(S32 lineNumber, ExprNode *testExpr, ExprNode *trueExpr, ExprNode *falseExpr)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
ConditionalExprNode *ret = (ConditionalExprNode *)consoleAlloc(sizeof(ConditionalExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->testExpr = testExpr;
ret->trueExpr = trueExpr;
ret->falseExpr = falseExpr;
ret->integer = false;
return ret;
}
2017-11-06 04:33:32 +00:00
FloatNode *FloatNode::alloc(S32 lineNumber, F64 value)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
FloatNode *ret = (FloatNode *)consoleAlloc(sizeof(FloatNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
2017-11-06 04:33:32 +00:00
2012-09-19 15:15:01 +00:00
ret->dbgLineNumber = lineNumber;
ret->value = value;
return ret;
}
2017-11-06 04:33:32 +00:00
StrConstNode *StrConstNode::alloc(S32 lineNumber, char *str, bool tag, bool doc)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
StrConstNode *ret = (StrConstNode *)consoleAlloc(sizeof(StrConstNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
2017-11-06 04:33:32 +00:00
ret->str = (char *)consoleAlloc(dStrlen(str) + 1);
2012-09-19 15:15:01 +00:00
ret->tag = tag;
ret->doc = doc;
dStrcpy(ret->str, str);
return ret;
}
2017-11-06 04:33:32 +00:00
ConstantNode *ConstantNode::alloc(S32 lineNumber, StringTableEntry value)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
ConstantNode *ret = (ConstantNode *)consoleAlloc(sizeof(ConstantNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->value = value;
return ret;
}
2017-11-06 04:33:32 +00:00
AssignExprNode *AssignExprNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex, ExprNode *expr)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
AssignExprNode *ret = (AssignExprNode *)consoleAlloc(sizeof(AssignExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->varName = varName;
ret->expr = expr;
ret->arrayIndex = arrayIndex;
return ret;
}
2017-11-06 04:33:32 +00:00
AssignOpExprNode *AssignOpExprNode::alloc(S32 lineNumber, StringTableEntry varName, ExprNode *arrayIndex, ExprNode *expr, S32 op)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
AssignOpExprNode *ret = (AssignOpExprNode *)consoleAlloc(sizeof(AssignOpExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->varName = varName;
ret->expr = expr;
ret->arrayIndex = arrayIndex;
ret->op = op;
return ret;
}
2017-11-06 04:33:32 +00:00
TTagSetStmtNode *TTagSetStmtNode::alloc(S32 lineNumber, StringTableEntry tag, ExprNode *valueExpr, ExprNode *stringExpr)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
TTagSetStmtNode *ret = (TTagSetStmtNode *)consoleAlloc(sizeof(TTagSetStmtNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->tag = tag;
ret->valueExpr = valueExpr;
ret->stringExpr = stringExpr;
return ret;
}
2017-11-06 04:33:32 +00:00
TTagDerefNode *TTagDerefNode::alloc(S32 lineNumber, ExprNode *expr)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
TTagDerefNode *ret = (TTagDerefNode *)consoleAlloc(sizeof(TTagDerefNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->expr = expr;
return ret;
}
2017-11-06 04:33:32 +00:00
TTagExprNode *TTagExprNode::alloc(S32 lineNumber, StringTableEntry tag)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
TTagExprNode *ret = (TTagExprNode *)consoleAlloc(sizeof(TTagExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->tag = tag;
return ret;
}
2017-11-06 04:33:32 +00:00
FuncCallExprNode *FuncCallExprNode::alloc(S32 lineNumber, StringTableEntry funcName, StringTableEntry nameSpace, ExprNode *args, bool dot)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
FuncCallExprNode *ret = (FuncCallExprNode *)consoleAlloc(sizeof(FuncCallExprNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->funcName = funcName;
ret->nameSpace = nameSpace;
ret->args = args;
2017-11-06 04:33:32 +00:00
if (dot)
2012-09-19 15:15:01 +00:00
ret->callType = MethodCall;
else
{
2017-11-06 04:33:32 +00:00
if (nameSpace && !dStricmp(nameSpace, "Parent"))
2012-09-19 15:15:01 +00:00
ret->callType = ParentCall;
else
ret->callType = FunctionCall;
}
return ret;
}
2017-11-06 04:33:32 +00:00
FuncPointerCallExprNode *FuncPointerCallExprNode::alloc(S32 lineNumber, ExprNode *funcPointer, ExprNode *args)
{
FuncPointerCallExprNode *ret = (FuncPointerCallExprNode *)consoleAlloc(sizeof(FuncPointerCallExprNode));
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->funcPointer = funcPointer;
ret->args = args;
return ret;
}
AssertCallExprNode *AssertCallExprNode::alloc(S32 lineNumber, ExprNode *testExpr, const char *message)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
#ifdef TORQUE_ENABLE_SCRIPTASSERTS
AssertCallExprNode *ret = (AssertCallExprNode *)consoleAlloc(sizeof(FuncCallExprNode));
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->testExpr = testExpr;
ret->message = message ? message : "TorqueScript assert!";
return ret;
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
#else
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
return NULL;
2012-09-19 15:15:01 +00:00
2017-11-06 04:33:32 +00:00
#endif
2012-09-19 15:15:01 +00:00
}
2017-11-06 04:33:32 +00:00
SlotAccessNode *SlotAccessNode::alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
SlotAccessNode *ret = (SlotAccessNode *)consoleAlloc(sizeof(SlotAccessNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->objectExpr = objectExpr;
ret->arrayExpr = arrayExpr;
ret->slotName = slotName;
return ret;
}
2017-11-06 04:33:32 +00:00
InternalSlotAccessNode *InternalSlotAccessNode::alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *slotExpr, bool recurse)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
InternalSlotAccessNode *ret = (InternalSlotAccessNode *)consoleAlloc(sizeof(InternalSlotAccessNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->objectExpr = objectExpr;
ret->slotExpr = slotExpr;
ret->recurse = recurse;
return ret;
}
2017-11-06 04:33:32 +00:00
SlotAssignNode *SlotAssignNode::alloc(S32 lineNumber, ExprNode *objectExpr, ExprNode *arrayExpr, StringTableEntry slotName, ExprNode *valueExpr, U32 typeID /* = -1 */)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
SlotAssignNode *ret = (SlotAssignNode *)consoleAlloc(sizeof(SlotAssignNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->objectExpr = objectExpr;
ret->arrayExpr = arrayExpr;
ret->slotName = slotName;
ret->valueExpr = valueExpr;
ret->typeID = typeID;
return ret;
}
2017-11-06 04:33:32 +00:00
SlotAssignOpNode *SlotAssignOpNode::alloc(S32 lineNumber, ExprNode *objectExpr, StringTableEntry slotName, ExprNode *arrayExpr, S32 op, ExprNode *valueExpr)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
SlotAssignOpNode *ret = (SlotAssignOpNode *)consoleAlloc(sizeof(SlotAssignOpNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->objectExpr = objectExpr;
ret->arrayExpr = arrayExpr;
ret->slotName = slotName;
ret->op = op;
ret->valueExpr = valueExpr;
return ret;
}
2017-11-06 04:33:32 +00:00
ObjectDeclNode *ObjectDeclNode::alloc(S32 lineNumber, ExprNode *classNameExpr, ExprNode *objectNameExpr, ExprNode *argList, StringTableEntry parentObject, SlotAssignNode *slotDecls, ObjectDeclNode *subObjects, bool isDatablock, bool classNameInternal, bool isSingleton)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
ObjectDeclNode *ret = (ObjectDeclNode *)consoleAlloc(sizeof(ObjectDeclNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->classNameExpr = classNameExpr;
ret->objectNameExpr = objectNameExpr;
ret->argList = argList;
ret->slotDecls = slotDecls;
ret->subObjects = subObjects;
ret->isDatablock = isDatablock;
ret->isClassNameInternal = classNameInternal;
ret->isSingleton = isSingleton;
ret->failOffset = 0;
2017-11-06 04:33:32 +00:00
if (parentObject)
2012-09-19 15:15:01 +00:00
ret->parentObject = parentObject;
else
ret->parentObject = StringTable->EmptyString();
2012-09-19 15:15:01 +00:00
return ret;
}
2017-11-06 04:33:32 +00:00
FunctionDeclStmtNode *FunctionDeclStmtNode::alloc(S32 lineNumber, StringTableEntry fnName, StringTableEntry nameSpace, VarNode *args, StmtNode *stmts)
2012-09-19 15:15:01 +00:00
{
2017-11-06 04:33:32 +00:00
FunctionDeclStmtNode *ret = (FunctionDeclStmtNode *)consoleAlloc(sizeof(FunctionDeclStmtNode));
2012-09-19 15:15:01 +00:00
constructInPlace(ret);
ret->dbgLineNumber = lineNumber;
ret->fnName = fnName;
ret->args = args;
ret->stmts = stmts;
ret->nameSpace = nameSpace;
ret->package = NULL;
return ret;
}