Skip to content

Commit ad53e64

Browse files
committed
Parser Refactor: Use different ParseNode kind for knopStr and knopName and enforce child node type for some operations
1 parent 2f37655 commit ad53e64

File tree

12 files changed

+356
-326
lines changed

12 files changed

+356
-326
lines changed

lib/Parser/Hash.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ void Ident::TrySetIsUsedInLdElem(ParseNode * pnode)
210210
{
211211
if (pnode && pnode->nop == knopStr)
212212
{
213-
pnode->AsParseNodePid()->pid->SetIsUsedInLdElem(true);
213+
pnode->AsParseNodeStr()->pid->SetIsUsedInLdElem(true);
214214
}
215215
}
216216

lib/Parser/Parse.cpp

Lines changed: 58 additions & 103 deletions
Large diffs are not rendered by default.

lib/Parser/Parse.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ class Parser
129129
void SetIsInParsingArgList(bool set) { m_isInParsingArgList = set; }
130130

131131
bool GetHasDestructuringPattern() const { return m_hasDestructuringPattern; }
132-
void SetHasDestructuringPattern(bool set) { m_hasDestructuringPattern = set; }
133-
134-
static IdentPtr PidFromNode(ParseNodePtr pnode);
132+
void SetHasDestructuringPattern(bool set) { m_hasDestructuringPattern = set; }
135133

136134
ParseNode* CopyPnode(ParseNode* pnode);
137135

@@ -268,9 +266,9 @@ class Parser
268266
ParseNodeVar * CreateDeclNode(OpCode nop, IdentPtr pid, SymbolType symbolType, bool errorOnRedecl = true);
269267

270268
ParseNodeInt * CreateIntNode(int32 lw);
271-
ParseNodePid * CreateStrNode(IdentPtr pid);
272-
ParseNodePid * CreateNameNode(IdentPtr pid);
273-
ParseNodePid * CreateNameNode(IdentPtr pid, PidRefStack * ref, charcount_t ichMin, charcount_t ichLim);
269+
ParseNodeStr * CreateStrNode(IdentPtr pid);
270+
ParseNodeName * CreateNameNode(IdentPtr pid);
271+
ParseNodeName * CreateNameNode(IdentPtr pid, PidRefStack * ref, charcount_t ichMin, charcount_t ichLim);
274272
ParseNodeSpecialName * CreateSpecialNameNode(IdentPtr pid, PidRefStack * ref, charcount_t ichMin, charcount_t ichLim);
275273
ParseNodeSuperReference * CreateSuperReferenceNode(OpCode nop, ParseNodeSpecialName * pnode1, ParseNodePtr pnode2);
276274
ParseNodeProg * CreateProgNode(bool isModuleSource, ULONG lineNumber);

lib/Parser/ParseTreeComparer.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,16 +172,18 @@ namespace Js
172172
switch (left->nop)
173173
{
174174
case knopName:
175+
return ComputeDistance(left->AsParseNodeName()->pid, right->AsParseNodeName()->pid);
176+
175177
case knopStr:
176-
return ComputeDistance(left->AsParseNodePid()->pid, right->AsParseNodePid()->pid);
178+
return ComputeDistance(left->AsParseNodeStr()->pid, right->AsParseNodeStr()->pid);
177179

178180
case knopInt:
179181
return left->AsParseNodeInt()->lw == right->AsParseNodeInt()->lw ? ExactMatchDistance : 1.0;
180182

181183
case knopFlt:
182184
return left->AsParseNodeFloat()->dbl == right->AsParseNodeFloat()->dbl ? ExactMatchDistance : 1.0;
183185

184-
case knopRegExp: //TODO: AsParseNodePid()->regexPattern
186+
case knopRegExp: //TODO: AsParseNodeRegExp()->regexPattern
185187
break;
186188
}
187189

@@ -406,8 +408,10 @@ namespace Js
406408
switch (left->nop)
407409
{
408410
case knopName:
411+
return AreEquivalent(left->AsParseNodeName()->pid, right->AsParseNodeName()->pid);
412+
409413
case knopStr:
410-
return AreEquivalent(left->AsParseNodePid()->pid, right->AsParseNodePid()->pid);
414+
return AreEquivalent(left->AsParseNodeStr()->pid, right->AsParseNodeStr()->pid);
411415

412416
case knopInt:
413417
return left->AsParseNodeInt()->lw == right->AsParseNodeInt()->lw;

lib/Parser/pnodewalk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ class ParseNodeWalker : public WalkerPolicy
367367

368368
ResultType WalkCatch(ParseNodeCatch *pnode, Context context)
369369
{
370-
ResultType result = WalkFirstChild(pnode->pnodeParam, context);
370+
ResultType result = WalkFirstChild(pnode->GetParam(), context);
371371
if (ContinueWalk(result))
372372
{
373373
result = WalkNode(pnode, context);

lib/Parser/ptlist.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ PTNODE(knopNone , "<none>" , Nop , None , fnopNone
2121
/***************************************************************************
2222
Leaf nodes.
2323
***************************************************************************/
24-
PTNODE(knopName , "name" , Nop , Pid , fnopLeaf|fnopAllowDefer, "NameExpr" )
24+
PTNODE(knopName , "name" , Nop , Name , fnopLeaf|fnopAllowDefer, "NameExpr" )
2525
PTNODE(knopInt , "int const" , Nop , Int , fnopLeaf|fnopConst , "NumberLit" )
2626
PTNODE(knopImport , "import" , Nop , None , fnopLeaf , "ImportExpr" )
2727
PTNODE(knopFlt , "flt const" , Nop , Float , fnopLeaf|fnopConst , "NumberLit" )
28-
PTNODE(knopStr , "str const" , Nop , Pid , fnopLeaf|fnopConst , "StringLit" )
28+
PTNODE(knopStr , "str const" , Nop , Str , fnopLeaf|fnopConst , "StringLit" )
2929
PTNODE(knopRegExp , "reg expr" , Nop , RegExp , fnopLeaf|fnopConst , "RegExprLit" )
3030
PTNODE(knopNull , "null" , Nop , None , fnopLeaf , "NullLit" )
3131
PTNODE(knopFalse , "false" , Nop , None , fnopLeaf , "FalseLit" )

lib/Parser/ptree.cpp

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,21 @@ ParseNodeVar * ParseNode::AsParseNodeVar()
5959
return reinterpret_cast<ParseNodeVar *>(this);
6060
}
6161

62-
ParseNodePid * ParseNode::AsParseNodePid()
62+
ParseNodeStr * ParseNode::AsParseNodeStr()
6363
{
64-
Assert(this->nop == knopName || this->nop == knopStr);
65-
return reinterpret_cast<ParseNodePid *>(this);
64+
Assert(this->nop == knopStr);
65+
return reinterpret_cast<ParseNodeStr *>(this);
66+
}
67+
68+
ParseNodeName * ParseNode::AsParseNodeName()
69+
{
70+
Assert(this->nop == knopName);
71+
return reinterpret_cast<ParseNodeName *>(this);
6672
}
6773

6874
ParseNodeSpecialName * ParseNode::AsParseNodeSpecialName()
6975
{
70-
Assert(this->nop == knopName && this->AsParseNodePid()->IsSpecialName());
76+
Assert(this->nop == knopName && this->AsParseNodeName()->IsSpecialName());
7177
return reinterpret_cast<ParseNodeSpecialName *>(this);
7278
}
7379

@@ -245,9 +251,13 @@ ParseNodeModule * ParseNode::AsParseNodeModule()
245251

246252
IdentPtr ParseNode::name()
247253
{
248-
if (this->nop == knopName || this->nop == knopStr)
254+
if (this->nop == knopStr)
249255
{
250-
return this->AsParseNodePid()->pid;
256+
return this->AsParseNodeStr()->pid;
257+
}
258+
else if (this->nop == knopName)
259+
{
260+
return this->AsParseNodeName()->pid;
251261
}
252262
else if (this->nop == knopVarDecl || this->nop == knopConstDecl)
253263
{
@@ -274,7 +284,7 @@ ParseNodePtr ParseNode::GetFormalNext()
274284

275285
bool ParseNode::IsUserIdentifier()
276286
{
277-
return this->nop == knopName && !this->AsParseNodePid()->IsSpecialName();
287+
return this->nop == knopName && !this->AsParseNodeName()->IsSpecialName();
278288
}
279289

280290
ParseNodeUni::ParseNodeUni(OpCode nop, charcount_t ichMin, charcount_t ichLim, ParseNode * pnode1)
@@ -286,6 +296,13 @@ ParseNodeUni::ParseNodeUni(OpCode nop, charcount_t ichMin, charcount_t ichLim, P
286296
ParseNodeBin::ParseNodeBin(OpCode nop, charcount_t ichMin, charcount_t ichLim, ParseNode * pnode1, ParseNode * pnode2)
287297
: ParseNode(nop, ichMin, ichLim)
288298
{
299+
// Member name is either a string or a computed name
300+
Assert((nop != knopMember && nop != knopMemberShort && nop != knopObjectPatternMember && nop != knopGetMember && nop != knopSetMember)
301+
|| (pnode1->nop == knopStr || pnode1->nop == knopComputedName));
302+
303+
// Dot's rhs has to be a name;
304+
Assert(nop != knopDot || pnode2->nop == knopName);
305+
289306
this->pnode1 = pnode1;
290307
this->pnode2 = pnode2;
291308

@@ -326,21 +343,45 @@ ParseNodeRegExp::ParseNodeRegExp(OpCode nop, charcount_t ichMin, charcount_t ich
326343
this->regexPatternIndex = 0;
327344
}
328345

329-
ParseNodePid::ParseNodePid(OpCode nop, charcount_t ichMin, charcount_t ichLim, IdentPtr pid)
330-
: ParseNode(nop, ichMin, ichLim)
346+
ParseNodeStr::ParseNodeStr(charcount_t ichMin, charcount_t ichLim, IdentPtr name)
347+
: ParseNode(knopStr, ichMin, ichLim), pid(name)
331348
{
332-
this->pid = pid;
333-
this->sym = nullptr;
349+
}
350+
351+
ParseNodeName::ParseNodeName(charcount_t ichMin, charcount_t ichLim, IdentPtr name)
352+
: ParseNode(knopName, ichMin, ichLim), pid(name)
353+
{
354+
this->sym = nullptr;
334355
this->symRef = nullptr;
335356
this->isSpecialName = false;
336357
}
337358

359+
void ParseNodeName::SetSymRef(PidRefStack * ref)
360+
{
361+
Assert(this->symRef == nullptr);
362+
this->symRef = ref->GetSymRef();
363+
}
364+
365+
Js::PropertyId ParseNodeName::PropertyIdFromNameNode() const
366+
{
367+
Js::PropertyId propertyId;
368+
Symbol *sym = this->sym;
369+
if (sym)
370+
{
371+
propertyId = sym->GetPosition();
372+
}
373+
else
374+
{
375+
propertyId = this->pid->GetPropertyId();
376+
}
377+
return propertyId;
378+
}
379+
338380
ParseNodeVar::ParseNodeVar(OpCode nop, charcount_t ichMin, charcount_t ichLim, IdentPtr name)
339-
: ParseNode(nop, ichMin, ichLim)
381+
: ParseNode(nop, ichMin, ichLim), pid(name)
340382
{
341383
Assert(nop == knopVarDecl || nop == knopConstDecl || nop == knopLetDecl || nop == knopTemp);
342384

343-
this->pid = name;
344385
this->pnodeInit = nullptr;
345386
this->pnodeNext = nullptr;
346387
this->sym = nullptr;
@@ -545,7 +586,7 @@ ParseNodeFinally::ParseNodeFinally(OpCode nop, charcount_t ichMin, charcount_t i
545586
}
546587

547588
ParseNodeSpecialName::ParseNodeSpecialName(charcount_t ichMin, charcount_t ichLim, IdentPtr pid)
548-
: ParseNodePid(knopName, ichMin, ichLim, pid)
589+
: ParseNodeName(ichMin, ichLim, pid)
549590
{
550591
this->SetIsSpecialName();
551592
this->isThis = false;

lib/Parser/ptree.h

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ class ParseNodeTri;
7474
class ParseNodeInt;
7575
class ParseNodeFloat;
7676
class ParseNodeRegExp;
77-
class ParseNodePid;
77+
class ParseNodeStr;
78+
class ParseNodeName;
7879
class ParseNodeVar;
7980
class ParseNodeCall;
8081
class ParseNodeSuperCall;
@@ -120,7 +121,8 @@ class ParseNode
120121
ParseNodeFloat * AsParseNodeFloat();
121122
ParseNodeRegExp * AsParseNodeRegExp();
122123
ParseNodeVar * AsParseNodeVar();
123-
ParseNodePid * AsParseNodePid();
124+
ParseNodeStr * AsParseNodeStr();
125+
ParseNodeName * AsParseNodeName();
124126

125127
ParseNodeSpecialName * AsParseNodeSpecialName();
126128
ParseNodeExportDefault * AsParseNodeExportDefault();
@@ -323,24 +325,39 @@ class ParseNodeRegExp : public ParseNode
323325
};
324326

325327
// identifier or string
328+
329+
class ParseNodeStr : public ParseNode
330+
{
331+
public:
332+
ParseNodeStr(charcount_t ichMin, charcount_t ichLim, IdentPtr pid);
333+
334+
IdentPtr const pid;
335+
336+
DISABLE_SELF_CAST(ParseNodeStr);
337+
338+
};
339+
326340
class Symbol;
327341
struct PidRefStack;
328-
class ParseNodePid : public ParseNode
342+
class ParseNodeName : public ParseNode
329343
{
330344
public:
331-
ParseNodePid(OpCode nop, charcount_t ichMin, charcount_t ichLim, IdentPtr pid);
345+
ParseNodeName(charcount_t ichMin, charcount_t ichLim, IdentPtr pid);
332346

333-
IdentPtr pid;
347+
IdentPtr const pid;
348+
private:
334349
Symbol **symRef;
350+
public:
335351
Symbol *sym;
336352

337353
void SetSymRef(PidRefStack *ref);
354+
void ClearSymRef() { symRef = nullptr; }
338355
Symbol **GetSymRef() const { return symRef; }
339356
Js::PropertyId PropertyIdFromNameNode() const;
340357

341358
bool IsSpecialName() { return isSpecialName; }
342359

343-
DISABLE_SELF_CAST(ParseNodePid);
360+
DISABLE_SELF_CAST(ParseNodeName);
344361

345362
protected:
346363
void SetIsSpecialName() { isSpecialName = true; }
@@ -356,7 +373,7 @@ class ParseNodeVar : public ParseNode
356373
ParseNodeVar(OpCode nop, charcount_t ichMin, charcount_t ichLim, IdentPtr name);
357374

358375
ParseNodePtr pnodeNext;
359-
IdentPtr pid;
376+
IdentPtr const pid;
360377
Symbol *sym;
361378
Symbol **symRef;
362379
ParseNodePtr pnodeInit;
@@ -962,8 +979,15 @@ class ParseNodeCatch : public ParseNodeStmt
962979
public:
963980
ParseNodeCatch(OpCode nop, charcount_t ichMin, charcount_t ichLim);
964981

982+
ParseNodePtr GetParam() { return pnodeParam; }
983+
void SetParam(ParseNodeName * pnode) { pnodeParam = pnode; }
984+
void SetParam(ParseNodeParamPattern * pnode) { pnodeParam = pnode; }
985+
965986
ParseNodePtr pnodeNext;
966-
ParseNodePtr pnodeParam;
987+
988+
private:
989+
ParseNode * pnodeParam; // Name or ParamPattern
990+
public:
967991
ParseNodePtr pnodeBody;
968992
ParseNodePtr pnodeScopes;
969993
Scope *scope;
@@ -983,7 +1007,7 @@ class ParseNodeFinally : public ParseNodeStmt
9831007
};
9841008

9851009
// special name like 'this'
986-
class ParseNodeSpecialName : public ParseNodePid
1010+
class ParseNodeSpecialName : public ParseNodeName
9871011
{
9881012
public:
9891013
ParseNodeSpecialName(charcount_t ichMin, charcount_t ichLim, IdentPtr pid);

0 commit comments

Comments
 (0)