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