Nodes.h 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. /*
  2. * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
  3. * Copyright (C) 2001 Peter Kelly (pmk@post.com)
  4. * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All rights reserved.
  5. * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
  6. * Copyright (C) 2007 Maks Orlovich
  7. * Copyright (C) 2007 Eric Seidel <eric@webkit.org>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Library General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Library General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Library General Public License
  20. * along with this library; see the file COPYING.LIB. If not, write to
  21. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301, USA.
  23. *
  24. */
  25. #ifndef Nodes_h
  26. #define Nodes_h
  27. #include "Error.h"
  28. #include "JITCode.h"
  29. #include "Opcode.h"
  30. #include "ParserArena.h"
  31. #include "ParserTokens.h"
  32. #include "ResultType.h"
  33. #include "SourceCode.h"
  34. #include "SymbolTable.h"
  35. #include <wtf/MathExtras.h>
  36. namespace JSC {
  37. class ArgumentListNode;
  38. class BytecodeGenerator;
  39. class FunctionBodyNode;
  40. class Label;
  41. class PropertyListNode;
  42. class ReadModifyResolveNode;
  43. class RegisterID;
  44. class JSScope;
  45. class ScopeNode;
  46. enum Operator {
  47. OpEqual,
  48. OpPlusEq,
  49. OpMinusEq,
  50. OpMultEq,
  51. OpDivEq,
  52. OpPlusPlus,
  53. OpMinusMinus,
  54. OpAndEq,
  55. OpXOrEq,
  56. OpOrEq,
  57. OpModEq,
  58. OpLShift,
  59. OpRShift,
  60. OpURShift
  61. };
  62. enum LogicalOperator {
  63. OpLogicalAnd,
  64. OpLogicalOr
  65. };
  66. enum FallThroughMode {
  67. FallThroughMeansTrue = 0,
  68. FallThroughMeansFalse = 1
  69. };
  70. inline FallThroughMode invert(FallThroughMode fallThroughMode) { return static_cast<FallThroughMode>(!fallThroughMode); }
  71. typedef HashSet<RefPtr<StringImpl>, IdentifierRepHash> IdentifierSet;
  72. namespace DeclarationStacks {
  73. enum VarAttrs { IsConstant = 1, HasInitializer = 2 };
  74. typedef Vector<std::pair<const Identifier*, unsigned> > VarStack;
  75. typedef Vector<FunctionBodyNode*> FunctionStack;
  76. }
  77. struct SwitchInfo {
  78. enum SwitchType { SwitchNone, SwitchImmediate, SwitchCharacter, SwitchString };
  79. uint32_t bytecodeOffset;
  80. SwitchType switchType;
  81. };
  82. class ParserArenaFreeable {
  83. public:
  84. // ParserArenaFreeable objects are are freed when the arena is deleted.
  85. // Destructors are not called. Clients must not call delete on such objects.
  86. void* operator new(size_t, VM*);
  87. };
  88. class ParserArenaDeletable {
  89. public:
  90. virtual ~ParserArenaDeletable() { }
  91. // ParserArenaDeletable objects are deleted when the arena is deleted.
  92. // Clients must not call delete directly on such objects.
  93. void* operator new(size_t, VM*);
  94. };
  95. template <typename T>
  96. struct ParserArenaData : ParserArenaDeletable {
  97. T data;
  98. };
  99. class ParserArenaRefCounted : public RefCounted<ParserArenaRefCounted> {
  100. protected:
  101. ParserArenaRefCounted(VM*);
  102. public:
  103. virtual ~ParserArenaRefCounted()
  104. {
  105. ASSERT(deletionHasBegun());
  106. }
  107. };
  108. class Node : public ParserArenaFreeable {
  109. protected:
  110. Node(const JSTokenLocation&);
  111. public:
  112. virtual ~Node() { }
  113. int lineNo() const { return m_lineNumber; }
  114. int startOffset() const { return m_startOffset; }
  115. int lineStartOffset() const { return m_lineStartOffset; }
  116. protected:
  117. int m_lineNumber;
  118. int m_startOffset;
  119. int m_lineStartOffset;
  120. };
  121. class ExpressionNode : public Node {
  122. protected:
  123. ExpressionNode(const JSTokenLocation&, ResultType = ResultType::unknownType());
  124. public:
  125. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* destination = 0) = 0;
  126. virtual bool isNumber() const { return false; }
  127. virtual bool isString() const { return false; }
  128. virtual bool isNull() const { return false; }
  129. virtual bool isPure(BytecodeGenerator&) const { return false; }
  130. virtual bool isConstant() const { return false; }
  131. virtual bool isLocation() const { return false; }
  132. virtual bool isResolveNode() const { return false; }
  133. virtual bool isBracketAccessorNode() const { return false; }
  134. virtual bool isDotAccessorNode() const { return false; }
  135. virtual bool isFuncExprNode() const { return false; }
  136. virtual bool isCommaNode() const { return false; }
  137. virtual bool isSimpleArray() const { return false; }
  138. virtual bool isAdd() const { return false; }
  139. virtual bool isSubtract() const { return false; }
  140. virtual bool isBoolean() const { return false; }
  141. virtual void emitBytecodeInConditionContext(BytecodeGenerator&, Label*, Label*, FallThroughMode);
  142. virtual ExpressionNode* stripUnaryPlus() { return this; }
  143. ResultType resultDescriptor() const { return m_resultType; }
  144. private:
  145. ResultType m_resultType;
  146. };
  147. class StatementNode : public Node {
  148. protected:
  149. StatementNode(const JSTokenLocation&);
  150. public:
  151. virtual void emitBytecode(BytecodeGenerator&, RegisterID* destination = 0) = 0;
  152. void setLoc(unsigned firstLine, unsigned lastLine, int startOffset, int lineStartOffset);
  153. unsigned firstLine() const { return lineNo(); }
  154. unsigned lastLine() const { return m_lastLine; }
  155. virtual bool isEmptyStatement() const { return false; }
  156. virtual bool isReturnNode() const { return false; }
  157. virtual bool isExprStatement() const { return false; }
  158. virtual bool isBreak() const { return false; }
  159. virtual bool isContinue() const { return false; }
  160. virtual bool isBlock() const { return false; }
  161. private:
  162. int m_lastLine;
  163. };
  164. class ConstantNode : public ExpressionNode {
  165. public:
  166. ConstantNode(const JSTokenLocation&, ResultType);
  167. virtual bool isPure(BytecodeGenerator&) const { return true; }
  168. virtual bool isConstant() const { return true; }
  169. virtual JSValue jsValue(BytecodeGenerator&) const = 0;
  170. private:
  171. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  172. void emitBytecodeInConditionContext(BytecodeGenerator&, Label* trueTarget, Label* falseTarget, FallThroughMode);
  173. };
  174. class NullNode : public ConstantNode {
  175. public:
  176. NullNode(const JSTokenLocation&);
  177. private:
  178. virtual bool isNull() const { return true; }
  179. virtual JSValue jsValue(BytecodeGenerator&) const { return jsNull(); }
  180. };
  181. class BooleanNode : public ConstantNode {
  182. public:
  183. BooleanNode(const JSTokenLocation&, bool value);
  184. bool value() { return m_value; }
  185. private:
  186. virtual bool isBoolean() const { return true; }
  187. virtual JSValue jsValue(BytecodeGenerator&) const { return jsBoolean(m_value); }
  188. bool m_value;
  189. };
  190. class NumberNode : public ConstantNode {
  191. public:
  192. NumberNode(const JSTokenLocation&, double value);
  193. double value() { return m_value; }
  194. void setValue(double value) { m_value = value; }
  195. private:
  196. virtual bool isNumber() const { return true; }
  197. virtual JSValue jsValue(BytecodeGenerator&) const { return jsNumber(m_value); }
  198. double m_value;
  199. };
  200. class StringNode : public ConstantNode {
  201. public:
  202. StringNode(const JSTokenLocation&, const Identifier&);
  203. const Identifier& value() { return m_value; }
  204. private:
  205. virtual bool isString() const { return true; }
  206. virtual JSValue jsValue(BytecodeGenerator&) const;
  207. const Identifier& m_value;
  208. };
  209. class ThrowableExpressionData {
  210. public:
  211. ThrowableExpressionData()
  212. : m_divot(static_cast<uint32_t>(-1))
  213. , m_divotStartOffset(static_cast<uint16_t>(-1))
  214. , m_divotEndOffset(static_cast<uint16_t>(-1))
  215. , m_divotLine(static_cast<uint32_t>(-1))
  216. , m_divotLineStart(static_cast<uint32_t>(-1))
  217. {
  218. }
  219. ThrowableExpressionData(unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart)
  220. : m_divot(divot)
  221. , m_divotStartOffset(startOffset)
  222. , m_divotEndOffset(endOffset)
  223. , m_divotLine(divotLine)
  224. , m_divotLineStart(divotLineStart)
  225. {
  226. ASSERT(m_divot >= m_divotLineStart);
  227. }
  228. void setExceptionSourceCode(unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart)
  229. {
  230. ASSERT(divot >= divotLineStart);
  231. m_divot = divot;
  232. m_divotStartOffset = startOffset;
  233. m_divotEndOffset = endOffset;
  234. m_divotLine = divotLine;
  235. m_divotLineStart = divotLineStart;
  236. }
  237. uint32_t divot() const { return m_divot; }
  238. uint16_t divotStartOffset() const { return m_divotStartOffset; }
  239. uint16_t divotEndOffset() const { return m_divotEndOffset; }
  240. uint32_t divotLine() const { return m_divotLine; }
  241. uint32_t divotLineStart() const { return m_divotLineStart; }
  242. protected:
  243. RegisterID* emitThrowReferenceError(BytecodeGenerator&, const String& message);
  244. private:
  245. uint32_t m_divot;
  246. uint16_t m_divotStartOffset;
  247. uint16_t m_divotEndOffset;
  248. uint32_t m_divotLine;
  249. uint32_t m_divotLineStart;
  250. };
  251. class ThrowableSubExpressionData : public ThrowableExpressionData {
  252. public:
  253. ThrowableSubExpressionData()
  254. : m_subexpressionDivotOffset(0)
  255. , m_subexpressionEndOffset(0)
  256. , m_subexpressionLineOffset(0)
  257. , m_subexpressionLineStartOffset(0)
  258. {
  259. }
  260. ThrowableSubExpressionData(unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart)
  261. : ThrowableExpressionData(divot, startOffset, endOffset, divotLine, divotLineStart)
  262. , m_subexpressionDivotOffset(0)
  263. , m_subexpressionEndOffset(0)
  264. , m_subexpressionLineOffset(0)
  265. , m_subexpressionLineStartOffset(0)
  266. {
  267. }
  268. void setSubexpressionInfo(uint32_t subexpressionDivot, uint16_t subexpressionOffset, uint32_t subexpressionLine, uint32_t subexpressionLineStart)
  269. {
  270. ASSERT(subexpressionDivot <= divot());
  271. // Overflow means we can't do this safely, so just point at the primary divot,
  272. // divotLine, or divotLineStart.
  273. if ((divot() - subexpressionDivot) & ~0xFFFF)
  274. return;
  275. if ((divotLine() - subexpressionLine) & ~0xFFFF)
  276. return;
  277. if ((divotLineStart() - subexpressionLineStart) & ~0xFFFF)
  278. return;
  279. m_subexpressionDivotOffset = divot() - subexpressionDivot;
  280. m_subexpressionEndOffset = subexpressionOffset;
  281. m_subexpressionLineOffset = divotLine() - subexpressionLine;
  282. m_subexpressionLineStartOffset = divotLineStart() - subexpressionLineStart;
  283. }
  284. unsigned subexpressionDivot() { return divot() - m_subexpressionDivotOffset; }
  285. unsigned subexpressionStartOffset() { return divotStartOffset() - m_subexpressionDivotOffset; }
  286. unsigned subexpressionEndOffset() { return m_subexpressionEndOffset; }
  287. unsigned subexpressionLine() { return divotLine() - m_subexpressionLineOffset; }
  288. unsigned subexpressionLineStart() { return divotLineStart() - m_subexpressionLineStartOffset; }
  289. protected:
  290. uint16_t m_subexpressionDivotOffset;
  291. uint16_t m_subexpressionEndOffset;
  292. uint16_t m_subexpressionLineOffset;
  293. uint16_t m_subexpressionLineStartOffset;
  294. };
  295. class ThrowablePrefixedSubExpressionData : public ThrowableExpressionData {
  296. public:
  297. ThrowablePrefixedSubExpressionData()
  298. : m_subexpressionDivotOffset(0)
  299. , m_subexpressionStartOffset(0)
  300. , m_subexpressionLineOffset(0)
  301. , m_subexpressionLineStartOffset(0)
  302. {
  303. }
  304. ThrowablePrefixedSubExpressionData(unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart)
  305. : ThrowableExpressionData(divot, startOffset, endOffset, divotLine, divotLineStart)
  306. , m_subexpressionDivotOffset(0)
  307. , m_subexpressionStartOffset(0)
  308. , m_subexpressionLineOffset(0)
  309. , m_subexpressionLineStartOffset(0)
  310. {
  311. }
  312. void setSubexpressionInfo(uint32_t subexpressionDivot, uint16_t subexpressionOffset, uint32_t subexpressionLine, uint32_t subexpressionLineStart)
  313. {
  314. ASSERT(subexpressionDivot >= divot());
  315. // Overflow means we can't do this safely, so just point at the primary divot,
  316. // divotLine, or divotLineStart.
  317. if ((subexpressionDivot - divot()) & ~0xFFFF)
  318. return;
  319. if ((subexpressionLine - divotLine()) & ~0xFFFF)
  320. return;
  321. if ((subexpressionLineStart - divotLineStart()) & ~0xFFFF)
  322. return;
  323. m_subexpressionDivotOffset = subexpressionDivot - divot();
  324. m_subexpressionStartOffset = subexpressionOffset;
  325. m_subexpressionLineOffset = subexpressionLine - divotLine();
  326. m_subexpressionLineStartOffset = subexpressionLineStart - divotLineStart();
  327. }
  328. unsigned subexpressionDivot() { return divot() + m_subexpressionDivotOffset; }
  329. unsigned subexpressionStartOffset() { return m_subexpressionStartOffset; }
  330. unsigned subexpressionEndOffset() { return divotEndOffset() + m_subexpressionDivotOffset; }
  331. unsigned subexpressionLine() { return divotLine() + m_subexpressionLineOffset; }
  332. unsigned subexpressionLineStart() { return divotLineStart() + m_subexpressionLineStartOffset; }
  333. protected:
  334. uint16_t m_subexpressionDivotOffset;
  335. uint16_t m_subexpressionStartOffset;
  336. uint16_t m_subexpressionLineOffset;
  337. uint16_t m_subexpressionLineStartOffset;
  338. };
  339. class RegExpNode : public ExpressionNode, public ThrowableExpressionData {
  340. public:
  341. RegExpNode(const JSTokenLocation&, const Identifier& pattern, const Identifier& flags);
  342. private:
  343. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  344. const Identifier& m_pattern;
  345. const Identifier& m_flags;
  346. };
  347. class ThisNode : public ExpressionNode {
  348. public:
  349. ThisNode(const JSTokenLocation&);
  350. private:
  351. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  352. };
  353. class ResolveNode : public ExpressionNode {
  354. public:
  355. ResolveNode(const JSTokenLocation&, const Identifier&, unsigned startOffset, unsigned divotLine, unsigned divotLineStart);
  356. const Identifier& identifier() const { return m_ident; }
  357. private:
  358. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  359. virtual bool isPure(BytecodeGenerator&) const ;
  360. virtual bool isLocation() const { return true; }
  361. virtual bool isResolveNode() const { return true; }
  362. const Identifier& m_ident;
  363. uint32_t m_startOffset;
  364. uint32_t m_divotLine;
  365. uint32_t m_divotLineStart;
  366. };
  367. class ElementNode : public ParserArenaFreeable {
  368. public:
  369. ElementNode(int elision, ExpressionNode*);
  370. ElementNode(ElementNode*, int elision, ExpressionNode*);
  371. int elision() const { return m_elision; }
  372. ExpressionNode* value() { return m_node; }
  373. ElementNode* next() { return m_next; }
  374. private:
  375. ElementNode* m_next;
  376. int m_elision;
  377. ExpressionNode* m_node;
  378. };
  379. class ArrayNode : public ExpressionNode {
  380. public:
  381. ArrayNode(const JSTokenLocation&, int elision);
  382. ArrayNode(const JSTokenLocation&, ElementNode*);
  383. ArrayNode(const JSTokenLocation&, int elision, ElementNode*);
  384. ArgumentListNode* toArgumentList(VM*, int, int) const;
  385. private:
  386. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  387. virtual bool isSimpleArray() const ;
  388. ElementNode* m_element;
  389. int m_elision;
  390. bool m_optional;
  391. };
  392. class PropertyNode : public ParserArenaFreeable {
  393. public:
  394. enum Type { Constant = 1, Getter = 2, Setter = 4 };
  395. PropertyNode(VM*, const Identifier&, ExpressionNode*, Type);
  396. PropertyNode(VM*, double, ExpressionNode*, Type);
  397. const Identifier& name() const { return m_name; }
  398. Type type() const { return m_type; }
  399. private:
  400. friend class PropertyListNode;
  401. const Identifier& m_name;
  402. ExpressionNode* m_assign;
  403. Type m_type;
  404. };
  405. class PropertyListNode : public ExpressionNode {
  406. public:
  407. PropertyListNode(const JSTokenLocation&, PropertyNode*);
  408. PropertyListNode(const JSTokenLocation&, PropertyNode*, PropertyListNode*);
  409. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  410. private:
  411. PropertyNode* m_node;
  412. PropertyListNode* m_next;
  413. };
  414. class ObjectLiteralNode : public ExpressionNode {
  415. public:
  416. ObjectLiteralNode(const JSTokenLocation&);
  417. ObjectLiteralNode(const JSTokenLocation&, PropertyListNode*);
  418. private:
  419. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  420. PropertyListNode* m_list;
  421. };
  422. class BracketAccessorNode : public ExpressionNode, public ThrowableExpressionData {
  423. public:
  424. BracketAccessorNode(const JSTokenLocation&, ExpressionNode* base, ExpressionNode* subscript, bool subscriptHasAssignments);
  425. ExpressionNode* base() const { return m_base; }
  426. ExpressionNode* subscript() const { return m_subscript; }
  427. bool subscriptHasAssignments() const { return m_subscriptHasAssignments; }
  428. private:
  429. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  430. virtual bool isLocation() const { return true; }
  431. virtual bool isBracketAccessorNode() const { return true; }
  432. ExpressionNode* m_base;
  433. ExpressionNode* m_subscript;
  434. bool m_subscriptHasAssignments;
  435. };
  436. class DotAccessorNode : public ExpressionNode, public ThrowableExpressionData {
  437. public:
  438. DotAccessorNode(const JSTokenLocation&, ExpressionNode* base, const Identifier&);
  439. ExpressionNode* base() const { return m_base; }
  440. const Identifier& identifier() const { return m_ident; }
  441. private:
  442. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  443. virtual bool isLocation() const { return true; }
  444. virtual bool isDotAccessorNode() const { return true; }
  445. ExpressionNode* m_base;
  446. const Identifier& m_ident;
  447. };
  448. class ArgumentListNode : public ExpressionNode {
  449. public:
  450. ArgumentListNode(const JSTokenLocation&, ExpressionNode*);
  451. ArgumentListNode(const JSTokenLocation&, ArgumentListNode*, ExpressionNode*);
  452. ArgumentListNode* m_next;
  453. ExpressionNode* m_expr;
  454. private:
  455. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  456. };
  457. class ArgumentsNode : public ParserArenaFreeable {
  458. public:
  459. ArgumentsNode();
  460. ArgumentsNode(ArgumentListNode*);
  461. ArgumentListNode* m_listNode;
  462. };
  463. class NewExprNode : public ExpressionNode, public ThrowableExpressionData {
  464. public:
  465. NewExprNode(const JSTokenLocation&, ExpressionNode*);
  466. NewExprNode(const JSTokenLocation&, ExpressionNode*, ArgumentsNode*);
  467. private:
  468. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  469. ExpressionNode* m_expr;
  470. ArgumentsNode* m_args;
  471. };
  472. class EvalFunctionCallNode : public ExpressionNode, public ThrowableExpressionData {
  473. public:
  474. EvalFunctionCallNode(const JSTokenLocation&, ArgumentsNode*, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  475. private:
  476. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  477. ArgumentsNode* m_args;
  478. };
  479. class FunctionCallValueNode : public ExpressionNode, public ThrowableExpressionData {
  480. public:
  481. FunctionCallValueNode(const JSTokenLocation&, ExpressionNode*, ArgumentsNode*, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  482. private:
  483. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  484. ExpressionNode* m_expr;
  485. ArgumentsNode* m_args;
  486. };
  487. class FunctionCallResolveNode : public ExpressionNode, public ThrowableExpressionData {
  488. public:
  489. FunctionCallResolveNode(const JSTokenLocation&, const Identifier&, ArgumentsNode*, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  490. private:
  491. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  492. const Identifier& m_ident;
  493. ArgumentsNode* m_args;
  494. };
  495. class FunctionCallBracketNode : public ExpressionNode, public ThrowableSubExpressionData {
  496. public:
  497. FunctionCallBracketNode(const JSTokenLocation&, ExpressionNode* base, ExpressionNode* subscript, ArgumentsNode*, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  498. private:
  499. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  500. ExpressionNode* m_base;
  501. ExpressionNode* m_subscript;
  502. ArgumentsNode* m_args;
  503. };
  504. class FunctionCallDotNode : public ExpressionNode, public ThrowableSubExpressionData {
  505. public:
  506. FunctionCallDotNode(const JSTokenLocation&, ExpressionNode* base, const Identifier&, ArgumentsNode*, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  507. private:
  508. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  509. protected:
  510. ExpressionNode* m_base;
  511. const Identifier& m_ident;
  512. ArgumentsNode* m_args;
  513. };
  514. class CallFunctionCallDotNode : public FunctionCallDotNode {
  515. public:
  516. CallFunctionCallDotNode(const JSTokenLocation&, ExpressionNode* base, const Identifier&, ArgumentsNode*, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  517. private:
  518. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  519. };
  520. class ApplyFunctionCallDotNode : public FunctionCallDotNode {
  521. public:
  522. ApplyFunctionCallDotNode(const JSTokenLocation&, ExpressionNode* base, const Identifier&, ArgumentsNode*, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  523. private:
  524. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  525. };
  526. class DeleteResolveNode : public ExpressionNode, public ThrowableExpressionData {
  527. public:
  528. DeleteResolveNode(const JSTokenLocation&, const Identifier&, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  529. private:
  530. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  531. const Identifier& m_ident;
  532. };
  533. class DeleteBracketNode : public ExpressionNode, public ThrowableExpressionData {
  534. public:
  535. DeleteBracketNode(const JSTokenLocation&, ExpressionNode* base, ExpressionNode* subscript, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  536. private:
  537. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  538. ExpressionNode* m_base;
  539. ExpressionNode* m_subscript;
  540. };
  541. class DeleteDotNode : public ExpressionNode, public ThrowableExpressionData {
  542. public:
  543. DeleteDotNode(const JSTokenLocation&, ExpressionNode* base, const Identifier&, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  544. private:
  545. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  546. ExpressionNode* m_base;
  547. const Identifier& m_ident;
  548. };
  549. class DeleteValueNode : public ExpressionNode {
  550. public:
  551. DeleteValueNode(const JSTokenLocation&, ExpressionNode*);
  552. private:
  553. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  554. ExpressionNode* m_expr;
  555. };
  556. class VoidNode : public ExpressionNode {
  557. public:
  558. VoidNode(const JSTokenLocation&, ExpressionNode*);
  559. private:
  560. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  561. ExpressionNode* m_expr;
  562. };
  563. class TypeOfResolveNode : public ExpressionNode {
  564. public:
  565. TypeOfResolveNode(const JSTokenLocation&, const Identifier&);
  566. const Identifier& identifier() const { return m_ident; }
  567. private:
  568. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  569. const Identifier& m_ident;
  570. };
  571. class TypeOfValueNode : public ExpressionNode {
  572. public:
  573. TypeOfValueNode(const JSTokenLocation&, ExpressionNode*);
  574. private:
  575. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  576. ExpressionNode* m_expr;
  577. };
  578. class PrefixNode : public ExpressionNode, public ThrowablePrefixedSubExpressionData {
  579. public:
  580. PrefixNode(const JSTokenLocation&, ExpressionNode*, Operator, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  581. protected:
  582. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  583. virtual RegisterID* emitResolve(BytecodeGenerator&, RegisterID* = 0);
  584. virtual RegisterID* emitBracket(BytecodeGenerator&, RegisterID* = 0);
  585. virtual RegisterID* emitDot(BytecodeGenerator&, RegisterID* = 0);
  586. ExpressionNode* m_expr;
  587. Operator m_operator;
  588. };
  589. class PostfixNode : public PrefixNode {
  590. public:
  591. PostfixNode(const JSTokenLocation&, ExpressionNode*, Operator, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  592. private:
  593. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  594. virtual RegisterID* emitResolve(BytecodeGenerator&, RegisterID* = 0);
  595. virtual RegisterID* emitBracket(BytecodeGenerator&, RegisterID* = 0);
  596. virtual RegisterID* emitDot(BytecodeGenerator&, RegisterID* = 0);
  597. };
  598. class UnaryOpNode : public ExpressionNode {
  599. public:
  600. UnaryOpNode(const JSTokenLocation&, ResultType, ExpressionNode*, OpcodeID);
  601. protected:
  602. ExpressionNode* expr() { return m_expr; }
  603. const ExpressionNode* expr() const { return m_expr; }
  604. private:
  605. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  606. OpcodeID opcodeID() const { return m_opcodeID; }
  607. ExpressionNode* m_expr;
  608. OpcodeID m_opcodeID;
  609. };
  610. class UnaryPlusNode : public UnaryOpNode {
  611. public:
  612. UnaryPlusNode(const JSTokenLocation&, ExpressionNode*);
  613. private:
  614. virtual ExpressionNode* stripUnaryPlus() { return expr(); }
  615. };
  616. class NegateNode : public UnaryOpNode {
  617. public:
  618. NegateNode(const JSTokenLocation&, ExpressionNode*);
  619. };
  620. class BitwiseNotNode : public ExpressionNode {
  621. public:
  622. BitwiseNotNode(const JSTokenLocation&, ExpressionNode*);
  623. protected:
  624. ExpressionNode* expr() { return m_expr; }
  625. const ExpressionNode* expr() const { return m_expr; }
  626. private:
  627. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  628. ExpressionNode* m_expr;
  629. };
  630. class LogicalNotNode : public UnaryOpNode {
  631. public:
  632. LogicalNotNode(const JSTokenLocation&, ExpressionNode*);
  633. private:
  634. void emitBytecodeInConditionContext(BytecodeGenerator&, Label* trueTarget, Label* falseTarget, FallThroughMode);
  635. };
  636. class BinaryOpNode : public ExpressionNode {
  637. public:
  638. BinaryOpNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, OpcodeID, bool rightHasAssignments);
  639. BinaryOpNode(const JSTokenLocation&, ResultType, ExpressionNode* expr1, ExpressionNode* expr2, OpcodeID, bool rightHasAssignments);
  640. RegisterID* emitStrcat(BytecodeGenerator& generator, RegisterID* destination, RegisterID* lhs = 0, ReadModifyResolveNode* emitExpressionInfoForMe = 0);
  641. void emitBytecodeInConditionContext(BytecodeGenerator&, Label* trueTarget, Label* falseTarget, FallThroughMode);
  642. ExpressionNode* lhs() { return m_expr1; };
  643. ExpressionNode* rhs() { return m_expr2; };
  644. private:
  645. void tryFoldToBranch(BytecodeGenerator&, TriState& branchCondition, ExpressionNode*& branchExpression);
  646. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  647. protected:
  648. OpcodeID opcodeID() const { return m_opcodeID; }
  649. protected:
  650. ExpressionNode* m_expr1;
  651. ExpressionNode* m_expr2;
  652. private:
  653. OpcodeID m_opcodeID;
  654. protected:
  655. bool m_rightHasAssignments;
  656. };
  657. class MultNode : public BinaryOpNode {
  658. public:
  659. MultNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  660. };
  661. class DivNode : public BinaryOpNode {
  662. public:
  663. DivNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  664. };
  665. class ModNode : public BinaryOpNode {
  666. public:
  667. ModNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  668. };
  669. class AddNode : public BinaryOpNode {
  670. public:
  671. AddNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  672. virtual bool isAdd() const { return true; }
  673. };
  674. class SubNode : public BinaryOpNode {
  675. public:
  676. SubNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  677. virtual bool isSubtract() const { return true; }
  678. };
  679. class LeftShiftNode : public BinaryOpNode {
  680. public:
  681. LeftShiftNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  682. };
  683. class RightShiftNode : public BinaryOpNode {
  684. public:
  685. RightShiftNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  686. };
  687. class UnsignedRightShiftNode : public BinaryOpNode {
  688. public:
  689. UnsignedRightShiftNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  690. };
  691. class LessNode : public BinaryOpNode {
  692. public:
  693. LessNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  694. };
  695. class GreaterNode : public BinaryOpNode {
  696. public:
  697. GreaterNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  698. };
  699. class LessEqNode : public BinaryOpNode {
  700. public:
  701. LessEqNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  702. };
  703. class GreaterEqNode : public BinaryOpNode {
  704. public:
  705. GreaterEqNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  706. };
  707. class ThrowableBinaryOpNode : public BinaryOpNode, public ThrowableExpressionData {
  708. public:
  709. ThrowableBinaryOpNode(const JSTokenLocation&, ResultType, ExpressionNode* expr1, ExpressionNode* expr2, OpcodeID, bool rightHasAssignments);
  710. ThrowableBinaryOpNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, OpcodeID, bool rightHasAssignments);
  711. private:
  712. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  713. };
  714. class InstanceOfNode : public ThrowableBinaryOpNode {
  715. public:
  716. InstanceOfNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  717. private:
  718. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  719. };
  720. class InNode : public ThrowableBinaryOpNode {
  721. public:
  722. InNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  723. };
  724. class EqualNode : public BinaryOpNode {
  725. public:
  726. EqualNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  727. private:
  728. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  729. };
  730. class NotEqualNode : public BinaryOpNode {
  731. public:
  732. NotEqualNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  733. };
  734. class StrictEqualNode : public BinaryOpNode {
  735. public:
  736. StrictEqualNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  737. private:
  738. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  739. };
  740. class NotStrictEqualNode : public BinaryOpNode {
  741. public:
  742. NotStrictEqualNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  743. };
  744. class BitAndNode : public BinaryOpNode {
  745. public:
  746. BitAndNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  747. };
  748. class BitOrNode : public BinaryOpNode {
  749. public:
  750. BitOrNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  751. };
  752. class BitXOrNode : public BinaryOpNode {
  753. public:
  754. BitXOrNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, bool rightHasAssignments);
  755. };
  756. // m_expr1 && m_expr2, m_expr1 || m_expr2
  757. class LogicalOpNode : public ExpressionNode {
  758. public:
  759. LogicalOpNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, LogicalOperator);
  760. private:
  761. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  762. void emitBytecodeInConditionContext(BytecodeGenerator&, Label* trueTarget, Label* falseTarget, FallThroughMode);
  763. ExpressionNode* m_expr1;
  764. ExpressionNode* m_expr2;
  765. LogicalOperator m_operator;
  766. };
  767. // The ternary operator, "m_logical ? m_expr1 : m_expr2"
  768. class ConditionalNode : public ExpressionNode {
  769. public:
  770. ConditionalNode(const JSTokenLocation&, ExpressionNode* logical, ExpressionNode* expr1, ExpressionNode* expr2);
  771. private:
  772. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  773. ExpressionNode* m_logical;
  774. ExpressionNode* m_expr1;
  775. ExpressionNode* m_expr2;
  776. };
  777. class ReadModifyResolveNode : public ExpressionNode, public ThrowableExpressionData {
  778. public:
  779. ReadModifyResolveNode(const JSTokenLocation&, const Identifier&, Operator, ExpressionNode* right, bool rightHasAssignments, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned line, unsigned lineStart);
  780. private:
  781. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  782. const Identifier& m_ident;
  783. ExpressionNode* m_right;
  784. Operator m_operator;
  785. bool m_rightHasAssignments;
  786. };
  787. class AssignResolveNode : public ExpressionNode, public ThrowableExpressionData {
  788. public:
  789. AssignResolveNode(const JSTokenLocation&, const Identifier&, ExpressionNode* right);
  790. private:
  791. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  792. const Identifier& m_ident;
  793. ExpressionNode* m_right;
  794. };
  795. class ReadModifyBracketNode : public ExpressionNode, public ThrowableSubExpressionData {
  796. public:
  797. ReadModifyBracketNode(const JSTokenLocation&, ExpressionNode* base, ExpressionNode* subscript, Operator, ExpressionNode* right, bool subscriptHasAssignments, bool rightHasAssignments, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  798. private:
  799. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  800. ExpressionNode* m_base;
  801. ExpressionNode* m_subscript;
  802. ExpressionNode* m_right;
  803. Operator m_operator : 30;
  804. bool m_subscriptHasAssignments : 1;
  805. bool m_rightHasAssignments : 1;
  806. };
  807. class AssignBracketNode : public ExpressionNode, public ThrowableExpressionData {
  808. public:
  809. AssignBracketNode(const JSTokenLocation&, ExpressionNode* base, ExpressionNode* subscript, ExpressionNode* right, bool subscriptHasAssignments, bool rightHasAssignments, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  810. private:
  811. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  812. ExpressionNode* m_base;
  813. ExpressionNode* m_subscript;
  814. ExpressionNode* m_right;
  815. bool m_subscriptHasAssignments : 1;
  816. bool m_rightHasAssignments : 1;
  817. };
  818. class AssignDotNode : public ExpressionNode, public ThrowableExpressionData {
  819. public:
  820. AssignDotNode(const JSTokenLocation&, ExpressionNode* base, const Identifier&, ExpressionNode* right, bool rightHasAssignments, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  821. private:
  822. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  823. ExpressionNode* m_base;
  824. const Identifier& m_ident;
  825. ExpressionNode* m_right;
  826. bool m_rightHasAssignments;
  827. };
  828. class ReadModifyDotNode : public ExpressionNode, public ThrowableSubExpressionData {
  829. public:
  830. ReadModifyDotNode(const JSTokenLocation&, ExpressionNode* base, const Identifier&, Operator, ExpressionNode* right, bool rightHasAssignments, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  831. private:
  832. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  833. ExpressionNode* m_base;
  834. const Identifier& m_ident;
  835. ExpressionNode* m_right;
  836. Operator m_operator : 31;
  837. bool m_rightHasAssignments : 1;
  838. };
  839. class AssignErrorNode : public ExpressionNode, public ThrowableExpressionData {
  840. public:
  841. AssignErrorNode(const JSTokenLocation&, unsigned divot, unsigned startOffset, unsigned endOffset, unsigned divotLine, unsigned divotLineStart);
  842. private:
  843. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  844. };
  845. typedef Vector<ExpressionNode*, 8> ExpressionVector;
  846. class CommaNode : public ExpressionNode, public ParserArenaDeletable {
  847. public:
  848. CommaNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2);
  849. using ParserArenaDeletable::operator new;
  850. void append(ExpressionNode* expr) { m_expressions.append(expr); }
  851. private:
  852. virtual bool isCommaNode() const { return true; }
  853. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  854. ExpressionVector m_expressions;
  855. };
  856. class ConstDeclNode : public ExpressionNode {
  857. public:
  858. ConstDeclNode(const JSTokenLocation&, const Identifier&, ExpressionNode*);
  859. bool hasInitializer() const { return m_init; }
  860. const Identifier& ident() { return m_ident; }
  861. private:
  862. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  863. virtual RegisterID* emitCodeSingle(BytecodeGenerator&);
  864. const Identifier& m_ident;
  865. public:
  866. ConstDeclNode* m_next;
  867. private:
  868. ExpressionNode* m_init;
  869. };
  870. class ConstStatementNode : public StatementNode {
  871. public:
  872. ConstStatementNode(const JSTokenLocation&, ConstDeclNode* next);
  873. private:
  874. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  875. ConstDeclNode* m_next;
  876. };
  877. class SourceElements : public ParserArenaDeletable {
  878. public:
  879. SourceElements();
  880. void append(StatementNode*);
  881. StatementNode* singleStatement() const;
  882. StatementNode* lastStatement() const;
  883. void emitBytecode(BytecodeGenerator&, RegisterID* destination);
  884. private:
  885. Vector<StatementNode*> m_statements;
  886. };
  887. class BlockNode : public StatementNode {
  888. public:
  889. BlockNode(const JSTokenLocation&, SourceElements* = 0);
  890. StatementNode* singleStatement() const;
  891. StatementNode* lastStatement() const;
  892. private:
  893. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  894. virtual bool isBlock() const { return true; }
  895. SourceElements* m_statements;
  896. };
  897. class EmptyStatementNode : public StatementNode {
  898. public:
  899. EmptyStatementNode(const JSTokenLocation&);
  900. private:
  901. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  902. virtual bool isEmptyStatement() const { return true; }
  903. };
  904. class DebuggerStatementNode : public StatementNode {
  905. public:
  906. DebuggerStatementNode(const JSTokenLocation&);
  907. private:
  908. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  909. };
  910. class ExprStatementNode : public StatementNode {
  911. public:
  912. ExprStatementNode(const JSTokenLocation&, ExpressionNode*);
  913. ExpressionNode* expr() const { return m_expr; }
  914. private:
  915. virtual bool isExprStatement() const { return true; }
  916. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  917. ExpressionNode* m_expr;
  918. };
  919. class VarStatementNode : public StatementNode {
  920. public:
  921. VarStatementNode(const JSTokenLocation&, ExpressionNode*);
  922. private:
  923. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  924. ExpressionNode* m_expr;
  925. };
  926. class IfElseNode : public StatementNode {
  927. public:
  928. IfElseNode(const JSTokenLocation&, ExpressionNode* condition, StatementNode* ifBlock, StatementNode* elseBlock);
  929. private:
  930. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  931. bool tryFoldBreakAndContinue(BytecodeGenerator&, StatementNode* ifBlock,
  932. Label*& trueTarget, FallThroughMode&);
  933. ExpressionNode* m_condition;
  934. StatementNode* m_ifBlock;
  935. StatementNode* m_elseBlock;
  936. };
  937. class DoWhileNode : public StatementNode {
  938. public:
  939. DoWhileNode(const JSTokenLocation&, StatementNode*, ExpressionNode*);
  940. private:
  941. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  942. StatementNode* m_statement;
  943. ExpressionNode* m_expr;
  944. };
  945. class WhileNode : public StatementNode {
  946. public:
  947. WhileNode(const JSTokenLocation&, ExpressionNode*, StatementNode*);
  948. private:
  949. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  950. ExpressionNode* m_expr;
  951. StatementNode* m_statement;
  952. };
  953. class ForNode : public StatementNode {
  954. public:
  955. ForNode(const JSTokenLocation&, ExpressionNode* expr1, ExpressionNode* expr2, ExpressionNode* expr3, StatementNode*);
  956. private:
  957. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  958. ExpressionNode* m_expr1;
  959. ExpressionNode* m_expr2;
  960. ExpressionNode* m_expr3;
  961. StatementNode* m_statement;
  962. };
  963. class ForInNode : public StatementNode, public ThrowableExpressionData {
  964. public:
  965. ForInNode(const JSTokenLocation&, ExpressionNode*, ExpressionNode*, StatementNode*);
  966. ForInNode(VM*, const JSTokenLocation&, const Identifier&, ExpressionNode*, ExpressionNode*, StatementNode*, unsigned divot, int startOffset, int endOffset, unsigned divotLine, unsigned divotLineStart);
  967. private:
  968. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  969. ExpressionNode* m_init;
  970. ExpressionNode* m_lexpr;
  971. ExpressionNode* m_expr;
  972. StatementNode* m_statement;
  973. };
  974. class ContinueNode : public StatementNode, public ThrowableExpressionData {
  975. public:
  976. ContinueNode(VM*, const JSTokenLocation&);
  977. ContinueNode(const JSTokenLocation&, const Identifier&);
  978. Label* trivialTarget(BytecodeGenerator&);
  979. private:
  980. virtual bool isContinue() const { return true; }
  981. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  982. const Identifier& m_ident;
  983. };
  984. class BreakNode : public StatementNode, public ThrowableExpressionData {
  985. public:
  986. BreakNode(VM*, const JSTokenLocation&);
  987. BreakNode(const JSTokenLocation&, const Identifier&);
  988. Label* trivialTarget(BytecodeGenerator&);
  989. private:
  990. virtual bool isBreak() const { return true; }
  991. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  992. const Identifier& m_ident;
  993. };
  994. class ReturnNode : public StatementNode, public ThrowableExpressionData {
  995. public:
  996. ReturnNode(const JSTokenLocation&, ExpressionNode* value);
  997. ExpressionNode* value() { return m_value; }
  998. private:
  999. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  1000. virtual bool isReturnNode() const { return true; }
  1001. ExpressionNode* m_value;
  1002. };
  1003. class WithNode : public StatementNode {
  1004. public:
  1005. WithNode(const JSTokenLocation&, ExpressionNode*, StatementNode*, uint32_t divot, unsigned divotLine, unsigned divotLineStart, uint32_t expressionLength);
  1006. private:
  1007. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  1008. ExpressionNode* m_expr;
  1009. StatementNode* m_statement;
  1010. uint32_t m_divot;
  1011. uint32_t m_divotLine;
  1012. uint32_t m_divotLineStart;
  1013. uint32_t m_expressionLength;
  1014. };
  1015. class LabelNode : public StatementNode, public ThrowableExpressionData {
  1016. public:
  1017. LabelNode(const JSTokenLocation&, const Identifier& name, StatementNode*);
  1018. private:
  1019. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  1020. const Identifier& m_name;
  1021. StatementNode* m_statement;
  1022. };
  1023. class ThrowNode : public StatementNode, public ThrowableExpressionData {
  1024. public:
  1025. ThrowNode(const JSTokenLocation&, ExpressionNode*);
  1026. private:
  1027. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  1028. ExpressionNode* m_expr;
  1029. };
  1030. class TryNode : public StatementNode {
  1031. public:
  1032. TryNode(const JSTokenLocation&, StatementNode* tryBlock, const Identifier& exceptionIdent, StatementNode* catchBlock, StatementNode* finallyBlock);
  1033. private:
  1034. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  1035. StatementNode* m_tryBlock;
  1036. const Identifier& m_exceptionIdent;
  1037. StatementNode* m_catchBlock;
  1038. StatementNode* m_finallyBlock;
  1039. };
  1040. class ParameterNode : public ParserArenaFreeable {
  1041. public:
  1042. ParameterNode(const Identifier&);
  1043. ParameterNode(ParameterNode*, const Identifier&);
  1044. const Identifier& ident() const { return m_ident; }
  1045. ParameterNode* nextParam() const { return m_next; }
  1046. private:
  1047. const Identifier& m_ident;
  1048. ParameterNode* m_next;
  1049. };
  1050. class ScopeNode : public StatementNode, public ParserArenaRefCounted {
  1051. public:
  1052. typedef DeclarationStacks::VarStack VarStack;
  1053. typedef DeclarationStacks::FunctionStack FunctionStack;
  1054. ScopeNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, bool inStrictContext);
  1055. ScopeNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, const SourceCode&, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, CodeFeatures, int numConstants);
  1056. using ParserArenaRefCounted::operator new;
  1057. void destroyData()
  1058. {
  1059. m_arena.reset();
  1060. m_varStack.clear();
  1061. m_functionStack.clear();
  1062. m_statements = 0;
  1063. m_capturedVariables.clear();
  1064. }
  1065. const SourceCode& source() const { return m_source; }
  1066. const String& sourceURL() const { return m_source.provider()->url(); }
  1067. intptr_t sourceID() const { return m_source.providerID(); }
  1068. int startLine() const { return m_startLineNumber; }
  1069. int startStartOffset() const { return m_startStartOffset; }
  1070. int startLineStartOffset() const { return m_startLineStartOffset; }
  1071. void setFeatures(CodeFeatures features) { m_features = features; }
  1072. CodeFeatures features() { return m_features; }
  1073. bool usesEval() const { return m_features & EvalFeature; }
  1074. bool usesArguments() const { return (m_features & ArgumentsFeature) && !(m_features & ShadowsArgumentsFeature); }
  1075. bool isStrictMode() const { return m_features & StrictModeFeature; }
  1076. void setUsesArguments() { m_features |= ArgumentsFeature; }
  1077. bool usesThis() const { return m_features & ThisFeature; }
  1078. bool needsActivationForMoreThanVariables() const { return m_features & (EvalFeature | WithFeature | CatchFeature); }
  1079. bool needsActivation() const { return (hasCapturedVariables()) || (m_features & (EvalFeature | WithFeature | CatchFeature)); }
  1080. bool hasCapturedVariables() const { return !!m_capturedVariables.size(); }
  1081. size_t capturedVariableCount() const { return m_capturedVariables.size(); }
  1082. bool captures(const Identifier& ident) { return m_capturedVariables.contains(ident.impl()); }
  1083. VarStack& varStack() { return m_varStack; }
  1084. FunctionStack& functionStack() { return m_functionStack; }
  1085. int neededConstants()
  1086. {
  1087. // We may need 2 more constants than the count given by the parser,
  1088. // because of the various uses of jsUndefined() and jsNull().
  1089. return m_numConstants + 2;
  1090. }
  1091. StatementNode* singleStatement() const;
  1092. void emitStatementsBytecode(BytecodeGenerator&, RegisterID* destination);
  1093. protected:
  1094. void setSource(const SourceCode& source) { m_source = source; }
  1095. ParserArena m_arena;
  1096. int m_startLineNumber;
  1097. unsigned m_startStartOffset;
  1098. unsigned m_startLineStartOffset;
  1099. private:
  1100. CodeFeatures m_features;
  1101. SourceCode m_source;
  1102. VarStack m_varStack;
  1103. FunctionStack m_functionStack;
  1104. int m_numConstants;
  1105. SourceElements* m_statements;
  1106. IdentifierSet m_capturedVariables;
  1107. };
  1108. class ProgramNode : public ScopeNode {
  1109. public:
  1110. static const bool isFunctionNode = false;
  1111. static PassRefPtr<ProgramNode> create(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants);
  1112. unsigned startColumn() { return m_startColumn; }
  1113. static const bool scopeIsFunction = false;
  1114. private:
  1115. ProgramNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants);
  1116. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  1117. unsigned m_startColumn;
  1118. };
  1119. class EvalNode : public ScopeNode {
  1120. public:
  1121. static const bool isFunctionNode = false;
  1122. static PassRefPtr<EvalNode> create(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants);
  1123. unsigned startColumn() { return 1; }
  1124. static const bool scopeIsFunction = false;
  1125. private:
  1126. EvalNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants);
  1127. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  1128. };
  1129. class FunctionParameters : public RefCounted<FunctionParameters> {
  1130. WTF_MAKE_FAST_ALLOCATED;
  1131. public:
  1132. static PassRefPtr<FunctionParameters> create(ParameterNode*);
  1133. ~FunctionParameters();
  1134. unsigned size() const { return m_size; }
  1135. const Identifier& at(unsigned index) const { ASSERT(index < m_size); return identifiers()[index]; }
  1136. private:
  1137. FunctionParameters(ParameterNode*, unsigned size);
  1138. Identifier* identifiers() { return reinterpret_cast<Identifier*>(&m_storage); }
  1139. const Identifier* identifiers() const { return reinterpret_cast<const Identifier*>(&m_storage); }
  1140. unsigned m_size;
  1141. void* m_storage;
  1142. };
  1143. class FunctionBodyNode : public ScopeNode {
  1144. public:
  1145. static const bool isFunctionNode = true;
  1146. static FunctionBodyNode* create(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, bool isStrictMode);
  1147. static PassRefPtr<FunctionBodyNode> create(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants);
  1148. FunctionParameters* parameters() const { return m_parameters.get(); }
  1149. size_t parameterCount() const { return m_parameters->size(); }
  1150. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  1151. void finishParsing(const SourceCode&, ParameterNode*, const Identifier&, FunctionNameIsInScopeToggle);
  1152. void finishParsing(PassRefPtr<FunctionParameters>, const Identifier&, FunctionNameIsInScopeToggle);
  1153. const Identifier& ident() { return m_ident; }
  1154. void setInferredName(const Identifier& inferredName) { ASSERT(!inferredName.isNull()); m_inferredName = inferredName; }
  1155. const Identifier& inferredName() { return m_inferredName.isEmpty() ? m_ident : m_inferredName; }
  1156. bool functionNameIsInScope() { return m_functionNameIsInScopeToggle == FunctionNameIsInScope; }
  1157. FunctionNameIsInScopeToggle functionNameIsInScopeToggle() { return m_functionNameIsInScopeToggle; }
  1158. void setFunctionStart(int functionStart) { m_functionStart = functionStart; }
  1159. int functionStart() const { return m_functionStart; }
  1160. unsigned startColumn() const { return m_startColumn; }
  1161. static const bool scopeIsFunction = true;
  1162. private:
  1163. FunctionBodyNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, bool inStrictContext);
  1164. FunctionBodyNode(VM*, const JSTokenLocation& start, const JSTokenLocation& end, unsigned startColumn, SourceElements*, VarStack*, FunctionStack*, IdentifierSet&, const SourceCode&, CodeFeatures, int numConstants);
  1165. Identifier m_ident;
  1166. Identifier m_inferredName;
  1167. FunctionNameIsInScopeToggle m_functionNameIsInScopeToggle;
  1168. RefPtr<FunctionParameters> m_parameters;
  1169. int m_functionStart;
  1170. unsigned m_startColumn;
  1171. };
  1172. class FuncExprNode : public ExpressionNode {
  1173. public:
  1174. FuncExprNode(const JSTokenLocation&, const Identifier&, FunctionBodyNode*, const SourceCode&, ParameterNode* = 0);
  1175. FunctionBodyNode* body() { return m_body; }
  1176. private:
  1177. virtual RegisterID* emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  1178. virtual bool isFuncExprNode() const { return true; }
  1179. FunctionBodyNode* m_body;
  1180. };
  1181. class FuncDeclNode : public StatementNode {
  1182. public:
  1183. FuncDeclNode(const JSTokenLocation&, const Identifier&, FunctionBodyNode*, const SourceCode&, ParameterNode* = 0);
  1184. FunctionBodyNode* body() { return m_body; }
  1185. private:
  1186. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  1187. FunctionBodyNode* m_body;
  1188. };
  1189. class CaseClauseNode : public ParserArenaFreeable {
  1190. public:
  1191. CaseClauseNode(ExpressionNode*, SourceElements* = 0);
  1192. ExpressionNode* expr() const { return m_expr; }
  1193. void emitBytecode(BytecodeGenerator&, RegisterID* destination);
  1194. private:
  1195. ExpressionNode* m_expr;
  1196. SourceElements* m_statements;
  1197. };
  1198. class ClauseListNode : public ParserArenaFreeable {
  1199. public:
  1200. ClauseListNode(CaseClauseNode*);
  1201. ClauseListNode(ClauseListNode*, CaseClauseNode*);
  1202. CaseClauseNode* getClause() const { return m_clause; }
  1203. ClauseListNode* getNext() const { return m_next; }
  1204. private:
  1205. CaseClauseNode* m_clause;
  1206. ClauseListNode* m_next;
  1207. };
  1208. class CaseBlockNode : public ParserArenaFreeable {
  1209. public:
  1210. CaseBlockNode(ClauseListNode* list1, CaseClauseNode* defaultClause, ClauseListNode* list2);
  1211. void emitBytecodeForBlock(BytecodeGenerator&, RegisterID* input, RegisterID* destination);
  1212. private:
  1213. SwitchInfo::SwitchType tryTableSwitch(Vector<ExpressionNode*, 8>& literalVector, int32_t& min_num, int32_t& max_num);
  1214. static const size_t s_tableSwitchMinimum = 10;
  1215. ClauseListNode* m_list1;
  1216. CaseClauseNode* m_defaultClause;
  1217. ClauseListNode* m_list2;
  1218. };
  1219. class SwitchNode : public StatementNode {
  1220. public:
  1221. SwitchNode(const JSTokenLocation&, ExpressionNode*, CaseBlockNode*);
  1222. private:
  1223. virtual void emitBytecode(BytecodeGenerator&, RegisterID* = 0);
  1224. ExpressionNode* m_expr;
  1225. CaseBlockNode* m_block;
  1226. };
  1227. struct ElementList {
  1228. ElementNode* head;
  1229. ElementNode* tail;
  1230. };
  1231. struct PropertyList {
  1232. PropertyListNode* head;
  1233. PropertyListNode* tail;
  1234. };
  1235. struct ArgumentList {
  1236. ArgumentListNode* head;
  1237. ArgumentListNode* tail;
  1238. };
  1239. struct ConstDeclList {
  1240. ConstDeclNode* head;
  1241. ConstDeclNode* tail;
  1242. };
  1243. struct ParameterList {
  1244. ParameterNode* head;
  1245. ParameterNode* tail;
  1246. };
  1247. struct ClauseList {
  1248. ClauseListNode* head;
  1249. ClauseListNode* tail;
  1250. };
  1251. } // namespace JSC
  1252. #endif // Nodes_h