iv-phonic 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/include/iv/ast.h CHANGED
@@ -3,6 +3,7 @@
3
3
  #include <vector>
4
4
  #include <tr1/unordered_map>
5
5
  #include <tr1/tuple>
6
+ #include <tr1/type_traits>
6
7
  #include "uchar.h"
7
8
  #include "noncopyable.h"
8
9
  #include "utils.h"
@@ -11,8 +12,8 @@
11
12
  #include "token.h"
12
13
  #include "ast-fwd.h"
13
14
  #include "ast-visitor.h"
14
- #include "source.h"
15
15
  #include "location.h"
16
+ #include "static_assert.h"
16
17
  #include "ustringpiece.h"
17
18
 
18
19
  namespace iv {
@@ -44,14 +45,23 @@ namespace ast {
44
45
  enum NodeType {
45
46
  #define V(type)\
46
47
  k##type,
47
- AST_DERIVED_NODE_LIST(V)
48
+ AST_NODE_LIST(V)
48
49
  #undef V
49
50
  kNodeCount
50
51
  };
51
52
 
53
+ template<typename Factory, NodeType type>
54
+ class Inherit {
55
+ };
56
+
57
+ #define INHERIT(type)\
58
+ template<typename Factory>\
59
+ class type##Base\
60
+ : public Inherit<Factory, k##type> {\
61
+ }
62
+
52
63
  template<typename Factory>
53
- class Scope : public SpaceObject,
54
- private Noncopyable<Scope<Factory> >::type {
64
+ class Scope : private Noncopyable<Scope<Factory> >::type {
55
65
  public:
56
66
  typedef std::pair<Identifier<Factory>*, bool> Variable;
57
67
  typedef typename SpaceVector<Factory, Variable>::type Variables;
@@ -90,8 +100,14 @@ class Scope : public SpaceObject,
90
100
  };
91
101
 
92
102
  template<typename Factory>
93
- class AstNode : public SpaceObject,
94
- private Noncopyable<AstNode<Factory> >::type {
103
+ class Inherit<Factory, kAstNode>
104
+ : public SpaceObject,
105
+ private Noncopyable<Inherit<Factory, kAstNode> >::type {
106
+ };
107
+ INHERIT(AstNode);
108
+
109
+ template<typename Factory>
110
+ class AstNode : public AstNodeBase<Factory> {
95
111
  public:
96
112
  virtual ~AstNode() = 0;
97
113
 
@@ -129,44 +145,93 @@ inline AstNode<Factory>::~AstNode() { }
129
145
 
130
146
  // Expression
131
147
  template<typename Factory>
132
- class Expression : public AstNode<Factory> {
148
+ class Inherit<Factory, kExpression>
149
+ : public AstNode<Factory> {
150
+ };
151
+ INHERIT(Expression);
152
+
153
+ template<typename Factory>
154
+ class Expression : public ExpressionBase<Factory> {
133
155
  public:
134
156
  inline virtual bool IsValidLeftHandSide() const { return false; }
135
157
  DECLARE_NODE_TYPE(Expression)
136
158
  };
137
159
 
160
+ // Literal
161
+ template<typename Factory>
162
+ class Inherit<Factory, kLiteral>
163
+ : public Expression<Factory> {
164
+ };
165
+ INHERIT(Literal);
166
+
138
167
  template<typename Factory>
139
- class Literal : public Expression<Factory> {
168
+ class Literal : public LiteralBase<Factory> {
140
169
  public:
141
170
  DECLARE_NODE_TYPE(Literal)
142
171
  };
143
172
 
173
+
174
+ // Statement
144
175
  template<typename Factory>
145
- class Statement : public AstNode<Factory> {
176
+ class Inherit<Factory, kStatement>
177
+ : public AstNode<Factory> {
178
+ };
179
+ INHERIT(Statement);
180
+
181
+ template<typename Factory>
182
+ class Statement : public StatementBase<Factory> {
146
183
  public:
147
184
  DECLARE_NODE_TYPE(Statement)
148
185
  };
149
186
 
187
+ // BreakableStatement
188
+ template<typename Factory>
189
+ class Inherit<Factory, kBreakableStatement>
190
+ : public Statement<Factory> {
191
+ };
192
+ INHERIT(BreakableStatement);
193
+
150
194
  template<typename Factory>
151
- class BreakableStatement : public Statement<Factory> {
195
+ class BreakableStatement : public BreakableStatementBase<Factory> {
152
196
  public:
153
197
  DECLARE_NODE_TYPE(BreakableStatement)
154
198
  };
155
199
 
200
+ // NamedOnlyBreakableStatement
201
+ template<typename Factory>
202
+ class Inherit<Factory, kNamedOnlyBreakableStatement>
203
+ : public BreakableStatement<Factory> {
204
+ };
205
+ INHERIT(NamedOnlyBreakableStatement);
206
+
156
207
  template<typename Factory>
157
- class NamedOnlyBreakableStatement : public BreakableStatement<Factory> {
208
+ class NamedOnlyBreakableStatement : public NamedOnlyBreakableStatementBase<Factory> {
158
209
  public:
159
210
  DECLARE_NODE_TYPE(NamedOnlyBreakableStatement)
160
211
  };
161
212
 
213
+ // AnonymousBreakableStatement
162
214
  template<typename Factory>
163
- class AnonymousBreakableStatement : public BreakableStatement<Factory> {
215
+ class Inherit<Factory, kAnonymousBreakableStatement>
216
+ : public BreakableStatement<Factory> {
217
+ };
218
+ INHERIT(AnonymousBreakableStatement);
219
+
220
+ template<typename Factory>
221
+ class AnonymousBreakableStatement : public AnonymousBreakableStatementBase<Factory> {
164
222
  public:
165
223
  DECLARE_NODE_TYPE(AnonymousBreakableStatement)
166
224
  };
167
225
 
226
+ // Block
168
227
  template<typename Factory>
169
- class Block : public NamedOnlyBreakableStatement<Factory> {
228
+ class Inherit<Factory, kBlock>
229
+ : public NamedOnlyBreakableStatement<Factory> {
230
+ };
231
+ INHERIT(Block);
232
+
233
+ template<typename Factory>
234
+ class Block : public BlockBase<Factory> {
170
235
  public:
171
236
  typedef typename SpaceVector<Factory, Statement<Factory>*>::type Statements;
172
237
  explicit Block(Factory* factory)
@@ -184,8 +249,15 @@ class Block : public NamedOnlyBreakableStatement<Factory> {
184
249
  Statements body_;
185
250
  };
186
251
 
252
+ // FunctionStatement
187
253
  template<typename Factory>
188
- class FunctionStatement : public Statement<Factory> {
254
+ class Inherit<Factory, kFunctionStatement>
255
+ : public Statement<Factory> {
256
+ };
257
+ INHERIT(FunctionStatement);
258
+
259
+ template<typename Factory>
260
+ class FunctionStatement : public FunctionStatementBase<Factory> {
189
261
  public:
190
262
  explicit FunctionStatement(FunctionLiteral<Factory>* func)
191
263
  : function_(func) {
@@ -198,8 +270,36 @@ class FunctionStatement : public Statement<Factory> {
198
270
  FunctionLiteral<Factory>* function_;
199
271
  };
200
272
 
273
+ // FunctionDeclaration
274
+ template<typename Factory>
275
+ class Inherit<Factory, kFunctionDeclaration>
276
+ : public Statement<Factory> {
277
+ };
278
+ INHERIT(FunctionDeclaration);
279
+
280
+ template<typename Factory>
281
+ class FunctionDeclaration : public FunctionDeclarationBase<Factory> {
282
+ public:
283
+ explicit FunctionDeclaration(FunctionLiteral<Factory>* func)
284
+ : function_(func) {
285
+ }
286
+ inline FunctionLiteral<Factory>* function() const {
287
+ return function_;
288
+ }
289
+ DECLARE_DERIVED_NODE_TYPE(FunctionDeclaration)
290
+ private:
291
+ FunctionLiteral<Factory>* function_;
292
+ };
293
+
294
+ // VariableStatement
201
295
  template<typename Factory>
202
- class VariableStatement : public Statement<Factory> {
296
+ class Inherit<Factory, kVariableStatement>
297
+ : public Statement<Factory> {
298
+ };
299
+ INHERIT(VariableStatement);
300
+
301
+ template<typename Factory>
302
+ class VariableStatement : public VariableStatementBase<Factory> {
203
303
  public:
204
304
  typedef typename SpaceVector<
205
305
  Factory,
@@ -224,9 +324,16 @@ class VariableStatement : public Statement<Factory> {
224
324
  Declarations decls_;
225
325
  };
226
326
 
327
+ // Declaration
328
+ template<typename Factory>
329
+ class Inherit<Factory, kDeclaration>
330
+ : public SpaceObject,
331
+ private Noncopyable<Inherit<Factory, kDeclaration> >::type {
332
+ };
333
+ INHERIT(Declaration);
334
+
227
335
  template<typename Factory>
228
- class Declaration : public SpaceObject,
229
- private Noncopyable<Declaration<Factory> >::type {
336
+ class Declaration : public DeclarationBase<Factory> {
230
337
  public:
231
338
  Declaration(Identifier<Factory>* name, Expression<Factory>* expr)
232
339
  : name_(name),
@@ -243,14 +350,28 @@ class Declaration : public SpaceObject,
243
350
  Expression<Factory>* expr_;
244
351
  };
245
352
 
353
+ // EmptyStatement
354
+ template<typename Factory>
355
+ class Inherit<Factory, kEmptyStatement>
356
+ : public Statement<Factory> {
357
+ };
358
+ INHERIT(EmptyStatement);
359
+
246
360
  template<typename Factory>
247
- class EmptyStatement : public Statement<Factory> {
361
+ class EmptyStatement : public EmptyStatementBase<Factory> {
248
362
  public:
249
363
  DECLARE_DERIVED_NODE_TYPE(EmptyStatement)
250
364
  };
251
365
 
366
+ // IfStatement
367
+ template<typename Factory>
368
+ class Inherit<Factory, kIfStatement>
369
+ : public Statement<Factory> {
370
+ };
371
+ INHERIT(IfStatement);
372
+
252
373
  template<typename Factory>
253
- class IfStatement : public Statement<Factory> {
374
+ class IfStatement : public IfStatementBase<Factory> {
254
375
  public:
255
376
  IfStatement(Expression<Factory>* cond,
256
377
  Statement<Factory>* then,
@@ -270,86 +391,130 @@ class IfStatement : public Statement<Factory> {
270
391
  Statement<Factory>* else_;
271
392
  };
272
393
 
394
+ // IterationStatement
273
395
  template<typename Factory>
274
- class IterationStatement : public AnonymousBreakableStatement<Factory> {
396
+ class Inherit<Factory, kIterationStatement>
397
+ : public AnonymousBreakableStatement<Factory> {
398
+ };
399
+ INHERIT(IterationStatement);
400
+
401
+ template<typename Factory>
402
+ class IterationStatement : public IterationStatementBase<Factory> {
275
403
  public:
276
- explicit IterationStatement(Statement<Factory>* stmt)
277
- : body_(stmt) {
278
- }
279
- inline Statement<Factory>* body() const { return body_; }
280
404
  DECLARE_NODE_TYPE(IterationStatement)
281
- private:
282
- Statement<Factory>* body_;
283
405
  };
284
406
 
407
+ // DoWhileStatement
285
408
  template<typename Factory>
286
- class DoWhileStatement : public IterationStatement<Factory> {
409
+ class Inherit<Factory, kDoWhileStatement>
410
+ : public IterationStatement<Factory> {
411
+ };
412
+ INHERIT(DoWhileStatement);
413
+
414
+ template<typename Factory>
415
+ class DoWhileStatement : public DoWhileStatementBase<Factory> {
287
416
  public:
288
417
  DoWhileStatement(Statement<Factory>* body, Expression<Factory>* cond)
289
- : IterationStatement<Factory>(body),
418
+ : body_(body),
290
419
  cond_(cond) {
291
420
  }
421
+ inline Statement<Factory>* body() const { return body_; }
292
422
  inline Expression<Factory>* cond() const { return cond_; }
293
423
  DECLARE_DERIVED_NODE_TYPE(DoWhileStatement)
294
424
  private:
425
+ Statement<Factory>* body_;
295
426
  Expression<Factory>* cond_;
296
427
  };
297
428
 
429
+ // WhileStatement
430
+ template<typename Factory>
431
+ class Inherit<Factory, kWhileStatement>
432
+ : public IterationStatement<Factory> {
433
+ };
434
+ INHERIT(WhileStatement);
435
+
298
436
  template<typename Factory>
299
- class WhileStatement : public IterationStatement<Factory> {
437
+ class WhileStatement : public WhileStatementBase<Factory> {
300
438
  public:
301
439
  WhileStatement(Statement<Factory>* body, Expression<Factory>* cond)
302
- : IterationStatement<Factory>(body),
440
+ : body_(body),
303
441
  cond_(cond) {
304
442
  }
443
+ inline Statement<Factory>* body() const { return body_; }
305
444
  inline Expression<Factory>* cond() const { return cond_; }
306
445
  DECLARE_DERIVED_NODE_TYPE(WhileStatement)
307
446
  private:
447
+ Statement<Factory>* body_;
308
448
  Expression<Factory>* cond_;
309
449
  };
310
450
 
451
+ // ForStatement
311
452
  template<typename Factory>
312
- class ForStatement : public IterationStatement<Factory> {
453
+ class Inherit<Factory, kForStatement>
454
+ : public IterationStatement<Factory> {
455
+ };
456
+ INHERIT(ForStatement);
457
+
458
+ template<typename Factory>
459
+ class ForStatement : public ForStatementBase<Factory> {
313
460
  public:
314
461
  ForStatement(Statement<Factory>* body,
315
462
  Statement<Factory>* init,
316
463
  Expression<Factory>* cond,
317
464
  Statement<Factory>* next)
318
- : IterationStatement<Factory>(body),
465
+ : body_(body),
319
466
  init_(init),
320
467
  cond_(cond),
321
468
  next_(next) {
322
469
  }
470
+ inline Statement<Factory>* body() const { return body_; }
323
471
  inline Statement<Factory>* init() const { return init_; }
324
472
  inline Expression<Factory>* cond() const { return cond_; }
325
473
  inline Statement<Factory>* next() const { return next_; }
326
474
  DECLARE_DERIVED_NODE_TYPE(ForStatement)
327
475
  private:
476
+ Statement<Factory>* body_;
328
477
  Statement<Factory>* init_;
329
478
  Expression<Factory>* cond_;
330
479
  Statement<Factory>* next_;
331
480
  };
332
481
 
482
+ // ForInStatement
483
+ template<typename Factory>
484
+ class Inherit<Factory, kForInStatement>
485
+ : public IterationStatement<Factory> {
486
+ };
487
+ INHERIT(ForInStatement);
488
+
333
489
  template<typename Factory>
334
- class ForInStatement : public IterationStatement<Factory> {
490
+ class ForInStatement : public ForInStatementBase<Factory> {
335
491
  public:
336
492
  ForInStatement(Statement<Factory>* body,
337
493
  Statement<Factory>* each,
338
494
  Expression<Factory>* enumerable)
339
- : IterationStatement<Factory>(body),
495
+ : body_(body),
340
496
  each_(each),
341
497
  enumerable_(enumerable) {
342
498
  }
499
+ inline Statement<Factory>* body() const { return body_; }
343
500
  inline Statement<Factory>* each() const { return each_; }
344
501
  inline Expression<Factory>* enumerable() const { return enumerable_; }
345
502
  DECLARE_DERIVED_NODE_TYPE(ForInStatement)
346
503
  private:
504
+ Statement<Factory>* body_;
347
505
  Statement<Factory>* each_;
348
506
  Expression<Factory>* enumerable_;
349
507
  };
350
508
 
509
+ // ContinueStatement
510
+ template<typename Factory>
511
+ class Inherit<Factory, kContinueStatement>
512
+ : public Statement<Factory> {
513
+ };
514
+ INHERIT(ContinueStatement);
515
+
351
516
  template<typename Factory>
352
- class ContinueStatement : public Statement<Factory> {
517
+ class ContinueStatement : public ContinueStatementBase<Factory> {
353
518
  public:
354
519
  ContinueStatement(Identifier<Factory>* label,
355
520
  IterationStatement<Factory>** target)
@@ -368,19 +533,35 @@ class ContinueStatement : public Statement<Factory> {
368
533
  IterationStatement<Factory>** target_;
369
534
  };
370
535
 
536
+ // BreakStatement
371
537
  template<typename Factory>
372
- class BreakStatement : public Statement<Factory> {
538
+ class Inherit<Factory, kBreakStatement>
539
+ : public Statement<Factory> {
540
+ };
541
+ INHERIT(BreakStatement);
542
+
543
+ template<typename Factory>
544
+ class BreakStatement : public BreakStatementBase<Factory> {
373
545
  public:
374
546
  BreakStatement(Identifier<Factory>* label,
375
547
  BreakableStatement<Factory>** target)
376
548
  : label_(label),
377
549
  target_(target) {
378
- assert(target_);
550
+ // example >
551
+ // do {
552
+ // test: break test;
553
+ // } while (0);
554
+ // if above example, target is NULL
555
+ assert(target_ || label_);
379
556
  }
380
557
  inline Identifier<Factory>* label() const { return label_; }
381
558
  inline BreakableStatement<Factory>* target() const {
382
- assert(target_ && *target_);
383
- return *target_;
559
+ if (target_) {
560
+ assert(*target_);
561
+ return *target_;
562
+ } else {
563
+ return NULL;
564
+ }
384
565
  }
385
566
  DECLARE_DERIVED_NODE_TYPE(BreakStatement)
386
567
  private:
@@ -388,8 +569,15 @@ class BreakStatement : public Statement<Factory> {
388
569
  BreakableStatement<Factory>** target_;
389
570
  };
390
571
 
572
+ // ReturnStatement
391
573
  template<typename Factory>
392
- class ReturnStatement : public Statement<Factory> {
574
+ class Inherit<Factory, kReturnStatement>
575
+ : public Statement<Factory> {
576
+ };
577
+ INHERIT(ReturnStatement);
578
+
579
+ template<typename Factory>
580
+ class ReturnStatement : public ReturnStatementBase<Factory> {
393
581
  public:
394
582
  explicit ReturnStatement(Expression<Factory>* expr)
395
583
  : expr_(expr) {
@@ -400,8 +588,15 @@ class ReturnStatement : public Statement<Factory> {
400
588
  Expression<Factory>* expr_;
401
589
  };
402
590
 
591
+ // WithStatement
592
+ template<typename Factory>
593
+ class Inherit<Factory, kWithStatement>
594
+ : public Statement<Factory> {
595
+ };
596
+ INHERIT(WithStatement);
597
+
403
598
  template<typename Factory>
404
- class WithStatement : public Statement<Factory> {
599
+ class WithStatement : public WithStatementBase<Factory> {
405
600
  public:
406
601
  WithStatement(Expression<Factory>* context,
407
602
  Statement<Factory>* body)
@@ -416,8 +611,15 @@ class WithStatement : public Statement<Factory> {
416
611
  Statement<Factory>* body_;
417
612
  };
418
613
 
614
+ // LabelledStatement
419
615
  template<typename Factory>
420
- class LabelledStatement : public Statement<Factory> {
616
+ class Inherit<Factory, kLabelledStatement>
617
+ : public Statement<Factory> {
618
+ };
619
+ INHERIT(LabelledStatement);
620
+
621
+ template<typename Factory>
622
+ class LabelledStatement : public LabelledStatementBase<Factory> {
421
623
  public:
422
624
  LabelledStatement(Expression<Factory>* expr, Statement<Factory>* body)
423
625
  : body_(body) {
@@ -431,9 +633,16 @@ class LabelledStatement : public Statement<Factory> {
431
633
  Statement<Factory>* body_;
432
634
  };
433
635
 
636
+ // CaseClause
637
+ template<typename Factory>
638
+ class Inherit<Factory, kCaseClause>
639
+ : public SpaceObject,
640
+ private Noncopyable<Inherit<Factory, kCaseClause> >::type {
641
+ };
642
+ INHERIT(CaseClause);
643
+
434
644
  template<typename Factory>
435
- class CaseClause : public SpaceObject,
436
- private Noncopyable<CaseClause<Factory> >::type {
645
+ class CaseClause : public CaseClauseBase<Factory> {
437
646
  public:
438
647
  typedef typename SpaceVector<Factory, Statement<Factory>*>::type Statements;
439
648
  explicit CaseClause(bool is_default,
@@ -460,8 +669,15 @@ class CaseClause : public SpaceObject,
460
669
  bool default_;
461
670
  };
462
671
 
672
+ // SwitchStatement
673
+ template<typename Factory>
674
+ class Inherit<Factory, kSwitchStatement>
675
+ : public AnonymousBreakableStatement<Factory> {
676
+ };
677
+ INHERIT(SwitchStatement);
678
+
463
679
  template<typename Factory>
464
- class SwitchStatement : public AnonymousBreakableStatement<Factory> {
680
+ class SwitchStatement : public SwitchStatementBase<Factory> {
465
681
  public:
466
682
  typedef typename SpaceVector<Factory, CaseClause<Factory>*>::type CaseClauses;
467
683
  SwitchStatement(Expression<Factory>* expr,
@@ -481,8 +697,15 @@ class SwitchStatement : public AnonymousBreakableStatement<Factory> {
481
697
  CaseClauses clauses_;
482
698
  };
483
699
 
700
+ // ThrowStatement
701
+ template<typename Factory>
702
+ class Inherit<Factory, kThrowStatement>
703
+ : public Statement<Factory> {
704
+ };
705
+ INHERIT(ThrowStatement);
706
+
484
707
  template<typename Factory>
485
- class ThrowStatement : public Statement<Factory> {
708
+ class ThrowStatement : public ThrowStatementBase<Factory> {
486
709
  public:
487
710
  explicit ThrowStatement(Expression<Factory>* expr)
488
711
  : expr_(expr) {
@@ -493,8 +716,15 @@ class ThrowStatement : public Statement<Factory> {
493
716
  Expression<Factory>* expr_;
494
717
  };
495
718
 
719
+ // TryStatement
496
720
  template<typename Factory>
497
- class TryStatement : public Statement<Factory> {
721
+ class Inherit<Factory, kTryStatement>
722
+ : public Statement<Factory> {
723
+ };
724
+ INHERIT(TryStatement);
725
+
726
+ template<typename Factory>
727
+ class TryStatement : public TryStatementBase<Factory> {
498
728
  public:
499
729
  explicit TryStatement(Block<Factory>* try_block,
500
730
  Identifier<Factory>* catch_name,
@@ -517,13 +747,27 @@ class TryStatement : public Statement<Factory> {
517
747
  Block<Factory>* finally_block_;
518
748
  };
519
749
 
750
+ // DebuggerStatement
751
+ template<typename Factory>
752
+ class Inherit<Factory, kDebuggerStatement>
753
+ : public Statement<Factory> {
754
+ };
755
+ INHERIT(DebuggerStatement);
756
+
520
757
  template<typename Factory>
521
- class DebuggerStatement : public Statement<Factory> {
758
+ class DebuggerStatement : public DebuggerStatementBase<Factory> {
522
759
  DECLARE_DERIVED_NODE_TYPE(DebuggerStatement)
523
760
  };
524
761
 
762
+ // ExpressionStatement
525
763
  template<typename Factory>
526
- class ExpressionStatement : public Statement<Factory> {
764
+ class Inherit<Factory, kExpressionStatement>
765
+ : public Statement<Factory> {
766
+ };
767
+ INHERIT(ExpressionStatement);
768
+
769
+ template<typename Factory>
770
+ class ExpressionStatement : public ExpressionStatementBase<Factory> {
527
771
  public:
528
772
  explicit ExpressionStatement(Expression<Factory>* expr) : expr_(expr) { }
529
773
  inline Expression<Factory>* expr() const { return expr_; }
@@ -532,8 +776,15 @@ class ExpressionStatement : public Statement<Factory> {
532
776
  Expression<Factory>* expr_;
533
777
  };
534
778
 
779
+ // Assignment
780
+ template<typename Factory>
781
+ class Inherit<Factory, kAssignment>
782
+ : public Expression<Factory> {
783
+ };
784
+ INHERIT(Assignment);
785
+
535
786
  template<typename Factory>
536
- class Assignment : public Expression<Factory> {
787
+ class Assignment : public AssignmentBase<Factory> {
537
788
  public:
538
789
  Assignment(Token::Type op,
539
790
  Expression<Factory>* left, Expression<Factory>* right)
@@ -551,8 +802,15 @@ class Assignment : public Expression<Factory> {
551
802
  Expression<Factory>* right_;
552
803
  };
553
804
 
805
+ // BinaryOperation
806
+ template<typename Factory>
807
+ class Inherit<Factory, kBinaryOperation>
808
+ : public Expression<Factory> {
809
+ };
810
+ INHERIT(BinaryOperation);
811
+
554
812
  template<typename Factory>
555
- class BinaryOperation : public Expression<Factory> {
813
+ class BinaryOperation : public BinaryOperationBase<Factory> {
556
814
  public:
557
815
  BinaryOperation(Token::Type op,
558
816
  Expression<Factory>* left, Expression<Factory>* right)
@@ -570,8 +828,15 @@ class BinaryOperation : public Expression<Factory> {
570
828
  Expression<Factory>* right_;
571
829
  };
572
830
 
831
+ // ConditionalExpression
573
832
  template<typename Factory>
574
- class ConditionalExpression : public Expression<Factory> {
833
+ class Inherit<Factory, kConditionalExpression>
834
+ : public Expression<Factory> {
835
+ };
836
+ INHERIT(ConditionalExpression);
837
+
838
+ template<typename Factory>
839
+ class ConditionalExpression : public ConditionalExpressionBase<Factory> {
575
840
  public:
576
841
  ConditionalExpression(Expression<Factory>* cond,
577
842
  Expression<Factory>* left,
@@ -588,8 +853,15 @@ class ConditionalExpression : public Expression<Factory> {
588
853
  Expression<Factory>* right_;
589
854
  };
590
855
 
856
+ // UnaryOperation
591
857
  template<typename Factory>
592
- class UnaryOperation : public Expression<Factory> {
858
+ class Inherit<Factory, kUnaryOperation>
859
+ : public Expression<Factory> {
860
+ };
861
+ INHERIT(UnaryOperation);
862
+
863
+ template<typename Factory>
864
+ class UnaryOperation : public UnaryOperationBase<Factory> {
593
865
  public:
594
866
  UnaryOperation(Token::Type op, Expression<Factory>* expr)
595
867
  : op_(op),
@@ -603,8 +875,15 @@ class UnaryOperation : public Expression<Factory> {
603
875
  Expression<Factory>* expr_;
604
876
  };
605
877
 
878
+ // PostfixExpression
879
+ template<typename Factory>
880
+ class Inherit<Factory, kPostfixExpression>
881
+ : public Expression<Factory> {
882
+ };
883
+ INHERIT(PostfixExpression);
884
+
606
885
  template<typename Factory>
607
- class PostfixExpression : public Expression<Factory> {
886
+ class PostfixExpression : public PostfixExpressionBase<Factory> {
608
887
  public:
609
888
  PostfixExpression(Token::Type op, Expression<Factory>* expr)
610
889
  : op_(op),
@@ -618,36 +897,66 @@ class PostfixExpression : public Expression<Factory> {
618
897
  Expression<Factory>* expr_;
619
898
  };
620
899
 
900
+ // StringLiteral
621
901
  template<typename Factory>
622
- class StringLiteral : public Literal<Factory> {
902
+ class Inherit<Factory, kStringLiteral>
903
+ : public Literal<Factory> {
904
+ };
905
+ INHERIT(StringLiteral);
906
+
907
+ template<typename Factory>
908
+ class StringLiteral : public StringLiteralBase<Factory> {
623
909
  public:
624
910
  typedef typename SpaceUString<Factory>::type value_type;
625
911
  StringLiteral(const std::vector<uc16>& buffer,
626
912
  Factory* factory)
627
- : value_(buffer.data(),
628
- buffer.size(),
629
- typename value_type::allocator_type(factory)) {
913
+ {
914
+ InitializeStringLiteral(buffer, factory);
630
915
  }
631
916
 
632
917
  inline const value_type& value() const {
633
918
  return value_;
634
919
  }
635
920
  DECLARE_DERIVED_NODE_TYPE(StringLiteral)
921
+ protected:
922
+ StringLiteral() { }
923
+ void InitializeStringLiteral(const std::vector<uc16>& buffer,
924
+ Factory* factory) {
925
+ value_ = value_type(
926
+ buffer.data(),
927
+ buffer.size(),
928
+ typename value_type::allocator_type(factory));
929
+ }
636
930
  private:
637
- const value_type value_;
931
+ value_type value_;
638
932
  };
639
933
 
934
+ // Directivable
640
935
  template<typename Factory>
641
- class Directivable : public StringLiteral<Factory> {
936
+ class Inherit<Factory, kDirectivable>
937
+ : public StringLiteral<Factory> {
938
+ };
939
+ INHERIT(Directivable);
940
+
941
+ template<typename Factory>
942
+ class Directivable : public DirectivableBase<Factory> {
642
943
  public:
643
944
  explicit Directivable(const std::vector<uc16>& buffer,
644
- Factory* factory)
645
- : StringLiteral<Factory>(buffer, factory) { }
945
+ Factory* factory) {
946
+ InitializeStringLiteral(buffer, factory);
947
+ }
646
948
  DECLARE_NODE_TYPE(Directivable)
647
949
  };
648
950
 
951
+ // NumberLiteral
649
952
  template<typename Factory>
650
- class NumberLiteral : public Literal<Factory> {
953
+ class Inherit<Factory, kNumberLiteral>
954
+ : public Literal<Factory> {
955
+ };
956
+ INHERIT(NumberLiteral);
957
+
958
+ template<typename Factory>
959
+ class NumberLiteral : public NumberLiteralBase<Factory> {
651
960
  public:
652
961
  explicit NumberLiteral(const double & val)
653
962
  : value_(val) {
@@ -658,8 +967,15 @@ class NumberLiteral : public Literal<Factory> {
658
967
  double value_;
659
968
  };
660
969
 
970
+ // Identifier
661
971
  template<typename Factory>
662
- class Identifier : public Literal<Factory> {
972
+ class Inherit<Factory, kIdentifier>
973
+ : public Literal<Factory> {
974
+ };
975
+ INHERIT(Identifier);
976
+
977
+ template<typename Factory>
978
+ class Identifier : public IdentifierBase<Factory> {
663
979
  public:
664
980
  typedef typename SpaceUString<Factory>::type value_type;
665
981
  template<typename Range>
@@ -712,38 +1028,80 @@ class IdentifierKey {
712
1028
  value_type* ident_;
713
1029
  };
714
1030
 
1031
+ // ThisLiteral
1032
+ template<typename Factory>
1033
+ class Inherit<Factory, kThisLiteral>
1034
+ : public Literal<Factory> {
1035
+ };
1036
+ INHERIT(ThisLiteral);
1037
+
715
1038
  template<typename Factory>
716
- class ThisLiteral : public Literal<Factory> {
1039
+ class ThisLiteral : public ThisLiteralBase<Factory> {
717
1040
  public:
718
1041
  DECLARE_DERIVED_NODE_TYPE(ThisLiteral)
719
1042
  };
720
1043
 
1044
+ // NullLiteral
721
1045
  template<typename Factory>
722
- class NullLiteral : public Literal<Factory> {
1046
+ class Inherit<Factory, kNullLiteral>
1047
+ : public Literal<Factory> {
1048
+ };
1049
+ INHERIT(NullLiteral);
1050
+
1051
+ template<typename Factory>
1052
+ class NullLiteral : public NullLiteralBase<Factory> {
723
1053
  public:
724
1054
  DECLARE_DERIVED_NODE_TYPE(NullLiteral)
725
1055
  };
726
1056
 
1057
+ // TrueLiteral
1058
+ template<typename Factory>
1059
+ class Inherit<Factory, kTrueLiteral>
1060
+ : public Literal<Factory> {
1061
+ };
1062
+ INHERIT(TrueLiteral);
1063
+
727
1064
  template<typename Factory>
728
- class TrueLiteral : public Literal<Factory> {
1065
+ class TrueLiteral : public TrueLiteralBase<Factory> {
729
1066
  public:
730
1067
  DECLARE_DERIVED_NODE_TYPE(TrueLiteral)
731
1068
  };
732
1069
 
1070
+ // FalseLiteral
1071
+ template<typename Factory>
1072
+ class Inherit<Factory, kFalseLiteral>
1073
+ : public Literal<Factory> {
1074
+ };
1075
+ INHERIT(FalseLiteral);
1076
+
733
1077
  template<typename Factory>
734
- class FalseLiteral : public Literal<Factory> {
1078
+ class FalseLiteral : public FalseLiteralBase<Factory> {
735
1079
  public:
736
1080
  DECLARE_DERIVED_NODE_TYPE(FalseLiteral)
737
1081
  };
738
1082
 
1083
+ // Undefined
1084
+ template<typename Factory>
1085
+ class Inherit<Factory, kUndefined>
1086
+ : public Literal<Factory> {
1087
+ };
1088
+ INHERIT(Undefined);
1089
+
739
1090
  template<typename Factory>
740
- class Undefined : public Literal<Factory> {
1091
+ class Undefined : public UndefinedBase<Factory> {
741
1092
  public:
742
1093
  DECLARE_DERIVED_NODE_TYPE(Undefined)
743
1094
  };
744
1095
 
1096
+ // RegExpLiteral
745
1097
  template<typename Factory>
746
- class RegExpLiteral : public Literal<Factory> {
1098
+ class Inherit<Factory, kRegExpLiteral>
1099
+ : public Literal<Factory> {
1100
+ };
1101
+ INHERIT(RegExpLiteral);
1102
+
1103
+ template<typename Factory>
1104
+ class RegExpLiteral : public RegExpLiteralBase<Factory> {
747
1105
  public:
748
1106
  typedef typename SpaceUString<Factory>::type value_type;
749
1107
 
@@ -763,8 +1121,15 @@ class RegExpLiteral : public Literal<Factory> {
763
1121
  value_type flags_;
764
1122
  };
765
1123
 
1124
+ // ArrayLiteral
766
1125
  template<typename Factory>
767
- class ArrayLiteral : public Literal<Factory> {
1126
+ class Inherit<Factory, kArrayLiteral>
1127
+ : public Literal<Factory> {
1128
+ };
1129
+ INHERIT(ArrayLiteral);
1130
+
1131
+ template<typename Factory>
1132
+ class ArrayLiteral : public ArrayLiteralBase<Factory> {
768
1133
  public:
769
1134
  typedef typename SpaceVector<Factory, Expression<Factory>*>::type Expressions;
770
1135
 
@@ -782,8 +1147,15 @@ class ArrayLiteral : public Literal<Factory> {
782
1147
  Expressions items_;
783
1148
  };
784
1149
 
1150
+ // ObjectLiteral
1151
+ template<typename Factory>
1152
+ class Inherit<Factory, kObjectLiteral>
1153
+ : public Literal<Factory> {
1154
+ };
1155
+ INHERIT(ObjectLiteral);
1156
+
785
1157
  template<typename Factory>
786
- class ObjectLiteral : public Literal<Factory> {
1158
+ class ObjectLiteral : public ObjectLiteralBase<Factory> {
787
1159
  public:
788
1160
  enum PropertyDescriptorType {
789
1161
  DATA = 1,
@@ -820,8 +1192,15 @@ class ObjectLiteral : public Literal<Factory> {
820
1192
  Properties properties_;
821
1193
  };
822
1194
 
1195
+ // FunctionLiteral
823
1196
  template<typename Factory>
824
- class FunctionLiteral : public Literal<Factory> {
1197
+ class Inherit<Factory, kFunctionLiteral>
1198
+ : public Literal<Factory> {
1199
+ };
1200
+ INHERIT(FunctionLiteral);
1201
+
1202
+ template<typename Factory>
1203
+ class FunctionLiteral : public FunctionLiteralBase<Factory> {
825
1204
  public:
826
1205
  typedef typename SpaceVector<Factory, Statement<Factory>*>::type Statements;
827
1206
  typedef typename SpaceVector<Factory, Identifier<Factory>*>::type Identifiers;
@@ -871,13 +1250,6 @@ class FunctionLiteral : public Literal<Factory> {
871
1250
  inline void set_strict(bool strict) {
872
1251
  strict_ = strict;
873
1252
  }
874
- inline void SubStringSource(BasicSource* src) {
875
- source_ = src->SubString(start_position_,
876
- end_position_ - start_position_ + 1);
877
- }
878
- inline UStringPiece GetSource() const {
879
- return source_;
880
- }
881
1253
  FunctionLiteral(DeclType type, Factory* factory)
882
1254
  : name_(NULL),
883
1255
  type_(type),
@@ -904,29 +1276,42 @@ class FunctionLiteral : public Literal<Factory> {
904
1276
  bool strict_;
905
1277
  std::size_t start_position_;
906
1278
  std::size_t end_position_;
907
- UStringPiece source_;
908
1279
  };
909
1280
 
1281
+ // PropertyAccess
910
1282
  template<typename Factory>
911
- class PropertyAccess : public Expression<Factory> {
1283
+ class Inherit<Factory, kPropertyAccess>
1284
+ : public Expression<Factory> {
1285
+ };
1286
+ INHERIT(PropertyAccess);
1287
+
1288
+ template<typename Factory>
1289
+ class PropertyAccess : public PropertyAccessBase<Factory> {
912
1290
  public:
913
1291
  inline bool IsValidLeftHandSide() const { return true; }
914
1292
  inline Expression<Factory>* target() const { return target_; }
915
1293
  DECLARE_NODE_TYPE(PropertyAccess)
916
1294
  protected:
917
- explicit PropertyAccess(Expression<Factory>* obj)
918
- : target_(obj) {
1295
+ void InitializePropertyAccess(Expression<Factory>* obj) {
1296
+ target_ = obj;
919
1297
  }
920
1298
  Expression<Factory>* target_;
921
1299
  };
922
1300
 
1301
+ // IdentifierAccess
923
1302
  template<typename Factory>
924
- class IdentifierAccess : public PropertyAccess<Factory> {
1303
+ class Inherit<Factory, kIdentifierAccess>
1304
+ : public PropertyAccess<Factory> {
1305
+ };
1306
+ INHERIT(IdentifierAccess);
1307
+
1308
+ template<typename Factory>
1309
+ class IdentifierAccess : public IdentifierAccessBase<Factory> {
925
1310
  public:
926
1311
  IdentifierAccess(Expression<Factory>* obj,
927
1312
  Identifier<Factory>* key)
928
- : PropertyAccess<Factory>(obj),
929
- key_(key) {
1313
+ : key_(key) {
1314
+ InitializePropertyAccess(obj);
930
1315
  }
931
1316
  inline Identifier<Factory>* key() const { return key_; }
932
1317
  DECLARE_DERIVED_NODE_TYPE(IdentifierAccess)
@@ -934,13 +1319,20 @@ class IdentifierAccess : public PropertyAccess<Factory> {
934
1319
  Identifier<Factory>* key_;
935
1320
  };
936
1321
 
1322
+ // IndexAccess
1323
+ template<typename Factory>
1324
+ class Inherit<Factory, kIndexAccess>
1325
+ : public PropertyAccess<Factory> {
1326
+ };
1327
+ INHERIT(IndexAccess);
1328
+
937
1329
  template<typename Factory>
938
- class IndexAccess : public PropertyAccess<Factory> {
1330
+ class IndexAccess : public IndexAccessBase<Factory> {
939
1331
  public:
940
1332
  IndexAccess(Expression<Factory>* obj,
941
1333
  Expression<Factory>* key)
942
- : PropertyAccess<Factory>(obj),
943
- key_(key) {
1334
+ : key_(key) {
1335
+ InitializePropertyAccess(obj);
944
1336
  }
945
1337
  inline Expression<Factory>* key() const { return key_; }
946
1338
  DECLARE_DERIVED_NODE_TYPE(IndexAccess)
@@ -948,43 +1340,68 @@ class IndexAccess : public PropertyAccess<Factory> {
948
1340
  Expression<Factory>* key_;
949
1341
  };
950
1342
 
1343
+ // Call
951
1344
  template<typename Factory>
952
- class Call : public Expression<Factory> {
1345
+ class Inherit<Factory, kCall>
1346
+ : public Expression<Factory> {
1347
+ };
1348
+ INHERIT(Call);
1349
+
1350
+ template<typename Factory>
1351
+ class Call : public CallBase<Factory> {
953
1352
  public:
954
- typedef typename SpaceVector<Factory, Expression<Factory>*>::type Expressions;
955
1353
  inline bool IsValidLeftHandSide() const { return true; }
956
- Call(Expression<Factory>* target,
957
- Factory* factory)
1354
+ DECLARE_NODE_TYPE(Call)
1355
+ };
1356
+
1357
+ // FunctionCall
1358
+ template<typename Factory>
1359
+ class Inherit<Factory, kFunctionCall>
1360
+ : public Call<Factory> {
1361
+ };
1362
+ INHERIT(FunctionCall);
1363
+
1364
+ template<typename Factory>
1365
+ class FunctionCall : public FunctionCallBase<Factory> {
1366
+ public:
1367
+ typedef typename SpaceVector<Factory, Expression<Factory>*>::type Expressions;
1368
+ FunctionCall(Expression<Factory>* target,
1369
+ Factory* factory)
958
1370
  : target_(target),
959
1371
  args_(typename Expressions::allocator_type(factory)) {
960
1372
  }
961
1373
  void AddArgument(Expression<Factory>* expr) { args_.push_back(expr); }
962
1374
  inline Expression<Factory>* target() const { return target_; }
963
1375
  inline const Expressions& args() const { return args_; }
964
- DECLARE_NODE_TYPE(Call)
965
- protected:
1376
+ DECLARE_DERIVED_NODE_TYPE(FunctionCall)
1377
+ private:
966
1378
  Expression<Factory>* target_;
967
1379
  Expressions args_;
968
1380
  };
969
1381
 
1382
+ // ConstructorCall
970
1383
  template<typename Factory>
971
- class FunctionCall : public Call<Factory> {
972
- public:
973
- FunctionCall(Expression<Factory>* target,
974
- Factory* factory)
975
- : Call<Factory>(target, factory) {
976
- }
977
- DECLARE_DERIVED_NODE_TYPE(FunctionCall)
1384
+ class Inherit<Factory, kConstructorCall>
1385
+ : public Call<Factory> {
978
1386
  };
1387
+ INHERIT(ConstructorCall);
979
1388
 
980
1389
  template<typename Factory>
981
- class ConstructorCall : public Call<Factory> {
1390
+ class ConstructorCall : public ConstructorCallBase<Factory> {
982
1391
  public:
1392
+ typedef typename SpaceVector<Factory, Expression<Factory>*>::type Expressions;
983
1393
  ConstructorCall(Expression<Factory>* target,
984
1394
  Factory* factory)
985
- : Call<Factory>(target, factory) {
1395
+ : target_(target),
1396
+ args_(typename Expressions::allocator_type(factory)) {
986
1397
  }
1398
+ void AddArgument(Expression<Factory>* expr) { args_.push_back(expr); }
1399
+ inline Expression<Factory>* target() const { return target_; }
1400
+ inline const Expressions& args() const { return args_; }
987
1401
  DECLARE_DERIVED_NODE_TYPE(ConstructorCall)
1402
+ private:
1403
+ Expression<Factory>* target_;
1404
+ Expressions args_;
988
1405
  };
989
1406
 
990
1407
  } } } // namespace iv::core::ast