Skip to content

Commit a73606e

Browse files
committed
Convert parse node to class hierarchy
Fix an issue with near null AV in ParseFncFunc when we are validating the function formals with destructuring without passing in a ParseNode for the function. Fix some benign type confusion from the assert where two kind of node have same field at the same location (the sym between ParseNodeVar and ParseNodePid) and between pnodeTarget/pnode1 in ParseNodeCall and ParseNodeBin Fix simple this property assignment statement only constructor identification because of type confusion of ParseNodeVar and ParseNodePid in Parse::ParseExpr
1 parent 86c3d09 commit a73606e

27 files changed

+3698
-3380
lines changed

lib/Parser/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_library (Chakra.Parser OBJECT
1010
OctoquadIdentifier.cpp
1111
Parse.cpp
1212
ParserPch.cpp
13+
ptree.cpp
1314
RegexCompileTime.cpp
1415
RegexParser.cpp
1516
RegexPattern.cpp

lib/Parser/Chakra.Parser.vcxproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<PrecompiledHeader>Create</PrecompiledHeader>
6565
</ClCompile>
6666
<ClCompile Include="$(MSBuildThisFileDirectory)TextbookBoyerMoore.cpp" />
67+
<ClCompile Include="$(MSBuildThisFileDirectory)ptree.cpp" />
6768
<None Include="HashFunc.cpp" />
6869
</ItemGroup>
6970
<ItemGroup>
@@ -148,4 +149,4 @@
148149
</ItemGroup>
149150
<Import Project="$(BuildConfigPropsPath)Chakra.Build.targets" Condition="exists('$(BuildConfigPropsPath)Chakra.Build.targets')" />
150151
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
151-
</Project>
152+
</Project>

lib/Parser/FormalsUtil.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
template <class Fn, bool mapRest>
77
void MapFormalsImpl(ParseNode *pnodeFunc, Fn fn)
88
{
9-
for (ParseNode *pnode = pnodeFunc->sxFnc.pnodeParams; pnode != nullptr; pnode = pnode->GetFormalNext())
9+
for (ParseNode *pnode = pnodeFunc->AsParseNodeFnc()->pnodeParams; pnode != nullptr; pnode = pnode->GetFormalNext())
1010
{
1111
fn(pnode);
1212
}
13-
if (mapRest && pnodeFunc->sxFnc.pnodeRest != nullptr)
13+
if (mapRest && pnodeFunc->AsParseNodeFnc()->pnodeRest != nullptr)
1414
{
15-
fn(pnodeFunc->sxFnc.pnodeRest);
15+
fn(pnodeFunc->AsParseNodeFnc()->pnodeRest);
1616
}
1717
}
1818

@@ -31,11 +31,11 @@ void MapFormals(ParseNode *pnodeFunc, Fn fn)
3131
template <class Fn>
3232
void MapFormalsFromPattern(ParseNode *pnodeFunc, Fn fn)
3333
{
34-
for (ParseNode *pnode = pnodeFunc->sxFnc.pnodeParams; pnode != nullptr; pnode = pnode->GetFormalNext())
34+
for (ParseNode *pnode = pnodeFunc->AsParseNodeFnc()->pnodeParams; pnode != nullptr; pnode = pnode->GetFormalNext())
3535
{
3636
if (pnode->nop == knopParamPattern)
3737
{
38-
Parser::MapBindIdentifier(pnode->sxParamPattern.pnode1, fn);
38+
Parser::MapBindIdentifier(pnode->AsParseNodeParamPattern()->pnode1, fn);
3939
}
4040
}
4141
}

lib/Parser/Hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ struct Ident
176176
{
177177
if (pnode && pnode->nop == knopStr)
178178
{
179-
pnode->sxPid.pid->SetIsUsedInLdElem(true);
179+
pnode->AsParseNodePid()->pid->SetIsUsedInLdElem(true);
180180
}
181181
}
182182

lib/Parser/Parse.cpp

Lines changed: 1211 additions & 1210 deletions
Large diffs are not rendered by default.

lib/Parser/Parse.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ class Parser
280280
ParseNodePtr CreateNameNode(IdentPtr pid)
281281
{
282282
ParseNodePtr pnode = CreateNode(knopName);
283-
pnode->sxPid.pid = pid;
284-
pnode->sxPid.sym=NULL;
285-
pnode->sxPid.symRef=NULL;
283+
pnode->AsParseNodePid()->pid = pid;
284+
pnode->AsParseNodePid()->sym=NULL;
285+
pnode->AsParseNodePid()->symRef=NULL;
286286
return pnode;
287287
}
288288
ParseNodePtr CreateSpecialNameNode(IdentPtr pid)
@@ -291,12 +291,12 @@ class Parser
291291

292292
// knopSpecialName is unused in the code
293293
pnode->nop = knopName;
294-
pnode->sxPid.pid = pid;
295-
pnode->sxPid.sym = NULL;
296-
pnode->sxPid.symRef = NULL;
294+
pnode->AsParseNodePid()->pid = pid;
295+
pnode->AsParseNodePid()->sym = NULL;
296+
pnode->AsParseNodePid()->symRef = NULL;
297297
pnode->isSpecialName = true;
298-
pnode->sxSpecialName.isThis = false;
299-
pnode->sxSpecialName.isSuper = false;
298+
pnode->AsParseNodeSpecialName()->isThis = false;
299+
pnode->AsParseNodeSpecialName()->isSuper = false;
300300
return pnode;
301301
}
302302
ParseNodePtr CreateBlockNode(PnodeBlockType blockType = PnodeBlockType::Regular)
@@ -509,8 +509,8 @@ class Parser
509509
{
510510
if (buildAST)
511511
{
512-
pnode->sxStmt.grfnop = 0;
513-
pnode->sxStmt.pnodeOuter = (NULL == m_pstmtCur) ? NULL : m_pstmtCur->pnodeStmt;
512+
pnode->AsParseNodeStmt()->grfnop = 0;
513+
pnode->AsParseNodeStmt()->pnodeOuter = (NULL == m_pstmtCur) ? NULL : m_pstmtCur->pnodeStmt;
514514

515515
pStmt->pnodeStmt = pnode;
516516
}
@@ -538,12 +538,12 @@ class Parser
538538
if (this->GetCurrentFunctionNode())
539539
{
540540
ParseNodePtr pnodeFunc = GetCurrentFunctionNode();
541-
pnodeFunc->sxFnc.SetCallsEval(true);
541+
pnodeFunc->AsParseNodeFnc()->SetCallsEval(true);
542542
}
543543
ParseNode *pnodeBlock = GetCurrentBlock();
544544
if (pnodeBlock != NULL)
545545
{
546-
pnodeBlock->sxBlock.SetCallsEval(true);
546+
pnodeBlock->AsParseNodeBlock()->SetCallsEval(true);
547547
PushDynamicBlock();
548548
}
549549
}
@@ -613,10 +613,10 @@ class Parser
613613
{
614614
if ((*current)->nop == knopList)
615615
{
616-
handler(&(*current)->sxBin.pnode1);
616+
handler(&(*current)->AsParseNodeBin()->pnode1);
617617

618618
// Advance to the next node
619-
current = &(*current)->sxBin.pnode2;
619+
current = &(*current)->AsParseNodeBin()->pnode2;
620620
}
621621
else
622622
{
@@ -642,11 +642,11 @@ class Parser
642642
ParseNodePtr bindIdentNode = elementNode;
643643
if (bindIdentNode->nop == knopAsg)
644644
{
645-
bindIdentNode = bindIdentNode->sxBin.pnode1;
645+
bindIdentNode = bindIdentNode->AsParseNodeBin()->pnode1;
646646
}
647647
else if (bindIdentNode->nop == knopEllipsis)
648648
{
649-
bindIdentNode = bindIdentNode->sxUni.pnode1;
649+
bindIdentNode = bindIdentNode->AsParseNodeUni()->pnode1;
650650
}
651651

652652
if (bindIdentNode->IsPattern())
@@ -668,21 +668,21 @@ class Parser
668668
{
669669
if (patternNode->nop == knopAsg)
670670
{
671-
patternNode = patternNode->sxBin.pnode1;
671+
patternNode = patternNode->AsParseNodeBin()->pnode1;
672672
}
673673

674674
Assert(patternNode->IsPattern());
675675
if (patternNode->nop == knopArrayPattern)
676676
{
677-
ForEachItemInList(patternNode->sxArrLit.pnode1, [&](ParseNodePtr item) {
677+
ForEachItemInList(patternNode->AsParseNodeArrLit()->pnode1, [&](ParseNodePtr item) {
678678
MapBindIdentifierFromElement(item, handler);
679679
});
680680
}
681681
else
682682
{
683-
ForEachItemInList(patternNode->sxUni.pnode1, [&](ParseNodePtr item) {
683+
ForEachItemInList(patternNode->AsParseNodeUni()->pnode1, [&](ParseNodePtr item) {
684684
Assert(item->nop == knopObjectPatternMember);
685-
MapBindIdentifierFromElement(item->sxBin.pnode2, handler);
685+
MapBindIdentifierFromElement(item->AsParseNodeBin()->pnode2, handler);
686686
});
687687
}
688688
}
@@ -906,7 +906,7 @@ class Parser
906906
BOOL NodeIsSuperName(ParseNodePtr pnode);
907907
BOOL IsJSONValid(ParseNodePtr pnodeExpr)
908908
{
909-
OpCode jnop = (knopNeg == pnodeExpr->nop) ? pnodeExpr->sxUni.pnode1->nop : pnodeExpr->nop;
909+
OpCode jnop = (knopNeg == pnodeExpr->nop) ? pnodeExpr->AsParseNodeUni()->pnode1->nop : pnodeExpr->nop;
910910
if (knopNeg == pnodeExpr->nop)
911911
{
912912
return (knopInt == jnop || knopFlt == jnop);

lib/Parser/ParseTreeComparer.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ namespace Js
173173
{
174174
case knopName:
175175
case knopStr:
176-
return ComputeDistance(left->sxPid.pid, right->sxPid.pid);
176+
return ComputeDistance(left->AsParseNodePid()->pid, right->AsParseNodePid()->pid);
177177

178178
case knopInt:
179-
return left->sxInt.lw == right->sxInt.lw ? ExactMatchDistance : 1.0;
179+
return left->AsParseNodeInt()->lw == right->AsParseNodeInt()->lw ? ExactMatchDistance : 1.0;
180180

181181
case knopFlt:
182-
return left->sxFlt.dbl == right->sxFlt.dbl ? ExactMatchDistance : 1.0;
182+
return left->AsParseNodeFloat()->dbl == right->AsParseNodeFloat()->dbl ? ExactMatchDistance : 1.0;
183183

184-
case knopRegExp: //TODO: sxPid.regexPattern
184+
case knopRegExp: //TODO: AsParseNodePid()->regexPattern
185185
break;
186186
}
187187

@@ -381,7 +381,7 @@ namespace Js
381381
static bool IsToken(ParseNode* pnode)
382382
{
383383
// TODO: We may use a new flag fnopToken
384-
return (ParseNode::Grfnop(pnode->nop) & fnopLeaf)
384+
return (pnode->Grfnop() & fnopLeaf)
385385
&& pnode->nop != knopFncDecl
386386
&& pnode->nop != knopClassDecl;
387387
}
@@ -407,16 +407,16 @@ namespace Js
407407
{
408408
case knopName:
409409
case knopStr:
410-
return AreEquivalent(left->sxPid.pid, right->sxPid.pid);
410+
return AreEquivalent(left->AsParseNodePid()->pid, right->AsParseNodePid()->pid);
411411

412412
case knopInt:
413-
return left->sxInt.lw == right->sxInt.lw;
413+
return left->AsParseNodeInt()->lw == right->AsParseNodeInt()->lw;
414414

415415
case knopFlt:
416-
return left->sxFlt.dbl == right->sxFlt.dbl;
416+
return left->AsParseNodeFloat()->dbl == right->AsParseNodeFloat()->dbl;
417417

418418
case knopRegExp:
419-
//TODO: sxPid.regexPattern
419+
//TODO: AsParseNodePid()->regexPattern
420420
break;
421421
}
422422

@@ -434,10 +434,10 @@ namespace Js
434434
switch (left->nop)
435435
{
436436
case knopVarDecl:
437-
return AreEquivalent(left->sxVar.pid, right->sxVar.pid);
437+
return AreEquivalent(left->AsParseNodeVar()->pid, right->AsParseNodeVar()->pid);
438438

439439
case knopFncDecl:
440-
return AreEquivalent(left->sxFnc.pid, right->sxFnc.pid);
440+
return AreEquivalent(left->AsParseNodeFnc()->pid, right->AsParseNodeFnc()->pid);
441441

442442
//TODO: other nodes with data
443443
}

lib/Parser/ParserCommon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ enum ErrorTypeEnum
3434
#endif
3535
};
3636

37-
struct ParseNode;
37+
class ParseNode;
3838
typedef ParseNode *ParseNodePtr;
3939

4040
struct Ident;

0 commit comments

Comments
 (0)