Mercurial > projects > ddmd
annotate dmd/ConditionalDeclaration.d @ 187:b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author | Abscissa |
---|---|
date | Tue, 07 Jun 2011 23:37:34 -0400 |
parents | e3afd1303184 |
children |
rev | line source |
---|---|
0 | 1 module dmd.ConditionalDeclaration; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.AttribDeclaration; |
5 import dmd.Condition; | |
6 import dmd.Array; | |
7 import dmd.Dsymbol; | |
8 import dmd.Scope; | |
9 import dmd.ScopeDsymbol; | |
10 import dmd.OutBuffer; | |
11 import dmd.HdrGenState; | |
12 | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
13 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
14 |
0 | 15 class ConditionalDeclaration : AttribDeclaration |
16 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
17 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
18 |
0 | 19 Condition condition; |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
20 Dsymbols elsedecl; // array of Dsymbol's for else block |
0 | 21 |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
22 this(Condition condition, Dsymbols decl, Dsymbols elsedecl) |
0 | 23 { |
178 | 24 register(); |
0 | 25 super(decl); |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
26 //printf("ConditionalDeclaration.ConditionalDeclaration()\n"); |
0 | 27 this.condition = condition; |
28 this.elsedecl = elsedecl; | |
29 } | |
30 | |
72 | 31 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 32 { |
79 | 33 ConditionalDeclaration dd; |
34 | |
35 assert(!s); | |
36 dd = new ConditionalDeclaration(condition.syntaxCopy(), | |
37 Dsymbol.arraySyntaxCopy(decl), | |
38 Dsymbol.arraySyntaxCopy(elsedecl)); | |
39 return dd; | |
0 | 40 } |
41 | |
72 | 42 override bool oneMember(Dsymbol* ps) |
0 | 43 { |
13 | 44 //printf("ConditionalDeclaration.oneMember(), inc = %d\n", condition.inc); |
45 if (condition.inc) | |
46 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
47 auto d = condition.include(null, null) ? decl : elsedecl; |
13 | 48 return Dsymbol.oneMembers(d, ps); |
49 } | |
50 *ps = null; | |
51 return true; | |
0 | 52 } |
53 | |
72 | 54 override void emitComment(Scope sc) |
0 | 55 { |
79 | 56 //printf("ConditionalDeclaration.emitComment(sc = %p)\n", sc); |
57 if (condition.inc) | |
58 { | |
59 AttribDeclaration.emitComment(sc); | |
60 } | |
61 else if (sc.docbuf) | |
62 { | |
63 /* If generating doc comment, be careful because if we're inside | |
64 * a template, then include(NULL, NULL) will fail. | |
65 */ | |
66 auto d = decl ? decl : elsedecl; | |
67 foreach(s; d) | |
68 s.emitComment(sc); | |
69 } | |
0 | 70 } |
71 | |
72 // Decide if 'then' or 'else' code should be included | |
73 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
74 override Dsymbols include(Scope sc, ScopeDsymbol sd) |
0 | 75 { |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
76 //printf("ConditionalDeclaration.include()\n"); |
0 | 77 assert(condition); |
78 return condition.include(sc, sd) ? decl : elsedecl; | |
79 } | |
80 | |
79 | 81 override void addComment(string comment) |
0 | 82 { |
83 /* Because addComment is called by the parser, if we called | |
84 * include() it would define a version before it was used. | |
85 * But it's no problem to drill down to both decl and elsedecl, | |
86 * so that's the workaround. | |
87 */ | |
88 | |
89 if (comment) | |
90 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
91 auto d = decl; |
0 | 92 |
93 for (int j = 0; j < 2; j++) | |
94 { | |
95 if (d) | |
96 { | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
97 foreach(s; d) |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
98 //printf("ConditionalDeclaration::addComment %s\n", s.toChars()); |
0 | 99 s.addComment(comment); |
100 } | |
101 d = elsedecl; | |
102 } | |
103 } | |
104 } | |
105 | |
72 | 106 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 107 { |
79 | 108 condition.toCBuffer(buf, hgs); |
109 if (decl || elsedecl) | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
110 { |
79 | 111 buf.writenl(); |
112 buf.writeByte('{'); | |
113 buf.writenl(); | |
114 if (decl) | |
115 { | |
116 foreach (Dsymbol s; decl) | |
117 { | |
118 buf.writestring(" "); | |
119 s.toCBuffer(buf, hgs); | |
120 } | |
121 } | |
122 buf.writeByte('}'); | |
123 if (elsedecl) | |
124 { | |
125 buf.writenl(); | |
126 buf.writestring("else"); | |
127 buf.writenl(); | |
128 buf.writeByte('{'); | |
129 buf.writenl(); | |
130 foreach (Dsymbol s; elsedecl) | |
131 { | |
132 buf.writestring(" "); | |
133 s.toCBuffer(buf, hgs); | |
134 } | |
135 buf.writeByte('}'); | |
136 } | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
137 } |
79 | 138 else |
139 buf.writeByte(':'); | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
140 buf.writenl(); |
79 | 141 } |
142 | |
143 override void toJsonBuffer(OutBuffer buf) | |
144 { | |
145 // writef("ConditionalDeclaration::toJsonBuffer()\n"); | |
146 if (condition.inc) | |
147 { | |
148 super.toJsonBuffer(buf); | |
149 } | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
150 } |
79 | 151 |
152 override void importAll(Scope sc) | |
153 { | |
154 Dsymbols d = include(sc, null); | |
155 | |
156 //writef("\tConditionalDeclaration::importAll '%s', d = %p\n",toChars(), d); | |
157 if (d) | |
158 { | |
159 foreach (s; d) | |
160 s.importAll(sc); | |
161 } | |
19
01cadcfa4842
Implemented CompileExp, ConditionalDeclaration, ModAssignExp, parsingmixin statements, TemplateAliasParameters, TemplateMixins, TypeDArray.
Robert Clipsham <robert@octarineparrot.com>
parents:
0
diff
changeset
|
162 } |
79 | 163 |
164 override void setScope(Scope sc) | |
165 { | |
166 Dsymbols d = include(sc, null); | |
167 | |
168 //writef("\tConditionalDeclaration::setScope '%s', d = %p\n",toChars(), d); | |
169 if (d) | |
170 { | |
171 foreach (s; d) | |
172 s.setScope(sc); | |
173 } | |
174 | |
175 } | |
176 } |