Mercurial > projects > dil
annotate trunk/src/dil/ast/Visitor.d @ 797:cf2ad5df025c
Added documentation comments.
Removed Lexer.loadKeywords() and revised Lexer.isReservedIdentifier().
Also removed Lexer.getTokens().
Renamed keywords to g_reservedIds.
Renamed classNames to g_classNames.
Added PRE and DMDBUG macros.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Fri, 29 Feb 2008 22:51:24 +0100 |
parents | 5e3ef1b2011c |
children |
rev | line source |
---|---|
635 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
5 module dil.ast.Visitor; | |
6 | |
7 import dil.ast.Node; | |
8 import dil.ast.Declarations, | |
9 dil.ast.Expressions, | |
10 dil.ast.Statements, | |
11 dil.ast.Types, | |
12 dil.ast.Parameters; | |
13 | |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
14 /// Generate visit methods. |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
15 /// |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
16 /// E.g.: |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
17 /// --- |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
18 /// Declaration visit(ClassDeclaration){return null;}; |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
19 /// Expression visit(CommaExpression){return null;}; |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
20 /// --- |
649
3ebe76ad680e
Using SemanticPass1 in main.d do start semantic analysis.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
648
diff
changeset
|
21 char[] generateVisitMethods() |
635 | 22 { |
23 char[] text; | |
797
cf2ad5df025c
Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
24 foreach (className; g_classNames) |
667
1ac758cd952a
Fixed a few things in DefaultVisitor.d and Pass1.d
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
655
diff
changeset
|
25 text ~= "returnType!(\""~className~"\") visit("~className~" node){return node;}\n"; |
635 | 26 return text; |
27 } | |
28 // pragma(msg, generateAbstractVisitMethods()); | |
29 | |
30 /// Gets the appropriate return type for the provided class. | |
31 template returnType(char[] className) | |
32 { | |
33 static if (is(typeof(mixin(className)) : Declaration)) | |
34 alias Declaration returnType; | |
35 else | |
36 static if (is(typeof(mixin(className)) : Statement)) | |
37 alias Statement returnType; | |
38 else | |
39 static if (is(typeof(mixin(className)) : Expression)) | |
40 alias Expression returnType; | |
41 else | |
42 static if (is(typeof(mixin(className)) : TypeNode)) | |
43 alias TypeNode returnType; | |
44 else | |
45 alias Node returnType; | |
46 } | |
47 | |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
48 /// Generate functions which do the second dispatch. |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
49 /// |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
50 /// E.g.: |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
51 /// --- |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
52 /// Expression visitCommaExpression(Visitor visitor, CommaExpression c) |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
53 /// { visitor.visit(c); /* Second dispatch. */ } |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
54 /// --- |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
55 /// The equivalent in the traditional visitor pattern would be: |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
56 /// --- |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
57 /// class CommaExpression : Expression |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
58 /// { |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
59 /// void accept(Visitor visitor) |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
60 /// { visitor.visit(this); } |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
61 /// } |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
62 /// --- |
635 | 63 char[] generateDispatchFunctions() |
64 { | |
65 char[] text; | |
797
cf2ad5df025c
Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
66 foreach (className; g_classNames) |
635 | 67 text ~= "returnType!(\""~className~"\") visit"~className~"(Visitor visitor, "~className~" c)\n" |
68 "{ return visitor.visit(c); }\n"; | |
69 return text; | |
70 } | |
71 // pragma(msg, generateDispatchFunctions()); | |
72 | |
73 /++ | |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
74 Generates an array of function pointers. |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
75 |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
76 --- |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
77 [ |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
78 cast(void*)&visitCommaExpression, |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
79 // etc. |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
80 ] |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
81 --- |
635 | 82 +/ |
83 char[] generateVTable() | |
84 { | |
85 char[] text = "["; | |
797
cf2ad5df025c
Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
86 foreach (className; g_classNames) |
635 | 87 text ~= "cast(void*)&visit"~className~",\n"; |
88 return text[0..$-2]~"]"; // slice away last ",\n" | |
89 } | |
90 // pragma(msg, generateVTable()); | |
91 | |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
92 /// Implements a variation of the visitor pattern. |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
93 /// |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
94 /// Inherited by classes that need to traverse a D syntax tree |
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
95 /// and do computations, transformations and other things on it. |
635 | 96 abstract class Visitor |
97 { | |
649
3ebe76ad680e
Using SemanticPass1 in main.d do start semantic analysis.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
648
diff
changeset
|
98 mixin(generateVisitMethods()); |
635 | 99 |
100 static | |
101 mixin(generateDispatchFunctions()); | |
102 | |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
103 /// The table holding function pointers to the second dispatch functions. |
635 | 104 static const void*[] dispatch_vtable = mixin(generateVTable()); |
797
cf2ad5df025c
Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
769
diff
changeset
|
105 static assert(dispatch_vtable.length == g_classNames.length, "vtable length doesn't match number of classes"); |
635 | 106 |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
107 /// Looks up the second dispatch function for n and returns that. |
757
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
108 Node function(Visitor, Node) getDispatchFunction()(Node n) |
635 | 109 { |
757
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
110 return cast(Node function(Visitor, Node))dispatch_vtable[n.kind]; |
635 | 111 } |
112 | |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
113 /// The main and first dispatch function. |
757
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
114 Node dispatch(Node n) |
769
5e3ef1b2011c
Added and improved documentation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
757
diff
changeset
|
115 { // Second dispatch is done in the called function. |
757
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
116 return getDispatchFunction(n)(this, n); |
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
117 } |
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
118 |
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
119 final: |
648
4ae7b13aaac8
Moved some semantic() methods to SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
641
diff
changeset
|
120 Declaration visit(Declaration n) |
4ae7b13aaac8
Moved some semantic() methods to SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
641
diff
changeset
|
121 { return visitD(n); } |
4ae7b13aaac8
Moved some semantic() methods to SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
641
diff
changeset
|
122 Statement visit(Statement n) |
4ae7b13aaac8
Moved some semantic() methods to SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
641
diff
changeset
|
123 { return visitS(n); } |
4ae7b13aaac8
Moved some semantic() methods to SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
641
diff
changeset
|
124 Expression visit(Expression n) |
4ae7b13aaac8
Moved some semantic() methods to SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
641
diff
changeset
|
125 { return visitE(n); } |
4ae7b13aaac8
Moved some semantic() methods to SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
641
diff
changeset
|
126 TypeNode visit(TypeNode n) |
4ae7b13aaac8
Moved some semantic() methods to SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
641
diff
changeset
|
127 { return visitT(n); } |
4ae7b13aaac8
Moved some semantic() methods to SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
641
diff
changeset
|
128 Node visit(Node n) |
4ae7b13aaac8
Moved some semantic() methods to SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
641
diff
changeset
|
129 { return visitN(n); } |
4ae7b13aaac8
Moved some semantic() methods to SemanticPass1.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
641
diff
changeset
|
130 |
635 | 131 Declaration visitD(Declaration n) |
132 { | |
757
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
133 return cast(Declaration)cast(void*)dispatch(n); |
635 | 134 } |
135 | |
136 Statement visitS(Statement n) | |
137 { | |
757
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
138 return cast(Statement)cast(void*)dispatch(n); |
635 | 139 } |
140 | |
141 Expression visitE(Expression n) | |
142 { | |
757
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
143 return cast(Expression)cast(void*)dispatch(n); |
635 | 144 } |
145 | |
146 TypeNode visitT(TypeNode n) | |
147 { | |
757
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
148 return cast(TypeNode)cast(void*)dispatch(n); |
635 | 149 } |
150 | |
151 Node visitN(Node n) | |
152 { | |
757
e4b60543c5e8
Revised methods in class Visitor.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
754
diff
changeset
|
153 return dispatch(n); |
635 | 154 } |
155 } |