comparison src/dil/ast/Node.d @ 806:bcb74c9b895c

Moved out files in the trunk folder to the root.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sun, 09 Mar 2008 00:12:19 +0100
parents trunk/src/dil/ast/Node.d@05dfe88dd3bb
children c60bd5cd61da
comparison
equal deleted inserted replaced
805:a3fab8b74a7d 806:bcb74c9b895c
1 /++
2 Author: Aziz Köksal
3 License: GPL3
4 +/
5 module dil.ast.Node;
6
7 import common;
8
9 public import dil.lexer.Token;
10 public import dil.ast.NodesEnum;
11
12 /// The root class of all D syntax tree elements.
13 abstract class Node
14 {
15 NodeCategory category; /// The category of this node.
16 NodeKind kind; /// The kind of this node.
17 Node[] children; // Will be probably removed sometime.
18 Token* begin, end; /// The begin and end tokens of this node.
19
20 /// Constructs a node object.
21 this(NodeCategory category)
22 {
23 assert(category != NodeCategory.Undefined);
24 this.category = category;
25 }
26
27 void setTokens(Token* begin, Token* end)
28 {
29 this.begin = begin;
30 this.end = end;
31 }
32
33 Class setToks(Class)(Class node)
34 {
35 node.setTokens(this.begin, this.end);
36 return node;
37 }
38
39 void addChild(Node child)
40 {
41 assert(child !is null, "failed in " ~ this.classinfo.name);
42 this.children ~= child;
43 }
44
45 void addOptChild(Node child)
46 {
47 child is null || addChild(child);
48 }
49
50 void addChildren(Node[] children)
51 {
52 assert(children !is null && delegate{
53 foreach (child; children)
54 if (child is null)
55 return false;
56 return true; }(),
57 "failed in " ~ this.classinfo.name
58 );
59 this.children ~= children;
60 }
61
62 void addOptChildren(Node[] children)
63 {
64 children is null || addChildren(children);
65 }
66
67 /// Returns a reference to Class if this node can be cast to it.
68 Class Is(Class)()
69 {
70 if (kind == mixin("NodeKind." ~ typeof(Class).stringof))
71 return cast(Class)cast(void*)this;
72 return null;
73 }
74
75 /// Casts this node to Class.
76 Class to(Class)()
77 {
78 return cast(Class)cast(void*)this;
79 }
80
81 /// Returns a deep copy of this node.
82 abstract Node copy();
83
84 /// Returns a shallow copy of this object.
85 final Node dup()
86 {
87 // Find out the size of this object.
88 alias typeof(this.classinfo.init[0]) byte_t;
89 size_t size = this.classinfo.init.length;
90 // Copy this object's data.
91 byte_t[] data = (cast(byte_t*)this)[0..size].dup;
92 return cast(Node)data.ptr;
93 }
94
95 /// This string is mixed into the constructor of a class that inherits
96 /// from Node. It sets the member kind.
97 const string set_kind = `this.kind = mixin("NodeKind." ~ typeof(this).stringof);`;
98 }