Makefile.in: host_alias to host
[delight/core.git] / dmd2 / cond.c
blob51f72d2fcd6f4b2c65ea7779d451eed84a634c7c
2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2006 by Digital Mars
4 // All Rights Reserved
5 // written by Walter Bright
6 // http://www.digitalmars.com
7 // License for redistribution is by either the Artistic License
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
9 // See the included readme.txt for details.
11 #include <stdio.h>
12 #include <assert.h>
14 #include "id.h"
15 #include "init.h"
16 #include "declaration.h"
17 #include "identifier.h"
18 #include "expression.h"
19 #include "cond.h"
20 #include "module.h"
21 #include "template.h"
22 #include "lexer.h"
23 #ifdef _DH
24 #include "mtype.h"
25 #include "scope.h"
26 #endif
28 int findCondition(Array *ids, Identifier *ident)
30 if (ids)
32 for (int i = 0; i < ids->dim; i++)
34 char *id = (char *)ids->data[i];
36 if (strcmp(id, ident->toChars()) == 0)
37 return TRUE;
41 return FALSE;
44 /* ============================================================ */
46 Condition::Condition(Loc loc)
48 this->loc = loc;
49 inc = 0;
52 /* ============================================================ */
54 DVCondition::DVCondition(Module *mod, unsigned level, Identifier *ident)
55 : Condition(0)
57 this->mod = mod;
58 this->level = level;
59 this->ident = ident;
62 Condition *DVCondition::syntaxCopy()
64 return this; // don't need to copy
67 /* ============================================================ */
69 void DebugCondition::setGlobalLevel(unsigned level)
71 global.params.debuglevel = level;
74 void DebugCondition::addGlobalIdent(char *ident)
76 if (!global.params.debugids)
77 global.params.debugids = new Array();
78 global.params.debugids->push(ident);
82 DebugCondition::DebugCondition(Module *mod, unsigned level, Identifier *ident)
83 : DVCondition(mod, level, ident)
87 int DebugCondition::include(Scope *sc, ScopeDsymbol *s)
89 //printf("DebugCondition::include() level = %d, debuglevel = %d\n", level, global.params.debuglevel);
90 if (inc == 0)
92 inc = 2;
93 if (ident)
95 if (findCondition(mod->debugids, ident))
96 inc = 1;
97 else if (findCondition(global.params.debugids, ident))
98 inc = 1;
99 else
100 { if (!mod->debugidsNot)
101 mod->debugidsNot = new Array();
102 mod->debugidsNot->push(ident->toChars());
105 else if (level <= global.params.debuglevel || level <= mod->debuglevel)
106 inc = 1;
108 return (inc == 1);
111 void DebugCondition::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
113 if (ident)
114 buf->printf("debug (%s)", ident->toChars());
115 else
116 buf->printf("debug (%u)", level);
119 /* ============================================================ */
121 void VersionCondition::setGlobalLevel(unsigned level)
123 global.params.versionlevel = level;
126 void VersionCondition::checkPredefined(Loc loc, char *ident)
128 static char* reserved[] =
130 "DigitalMars", "X86", "X86_64",
131 "Windows", "Win32", "Win64",
132 "linux",
133 "LittleEndian", "BigEndian",
134 "all",
135 "none",
138 for (unsigned i = 0; i < sizeof(reserved) / sizeof(reserved[0]); i++)
140 if (strcmp(ident, reserved[i]) == 0)
141 goto Lerror;
144 if (ident[0] == 'D' && ident[1] == '_')
145 goto Lerror;
147 return;
149 Lerror:
150 error(loc, "version identifier '%s' is reserved and cannot be set", ident);
153 void VersionCondition::addGlobalIdent(char *ident)
155 checkPredefined(0, ident);
156 addPredefinedGlobalIdent(ident);
159 void VersionCondition::addPredefinedGlobalIdent(char *ident)
161 if (!global.params.versionids)
162 global.params.versionids = new Array();
163 global.params.versionids->push(ident);
167 VersionCondition::VersionCondition(Module *mod, unsigned level, Identifier *ident)
168 : DVCondition(mod, level, ident)
172 int VersionCondition::include(Scope *sc, ScopeDsymbol *s)
174 //printf("VersionCondition::include() level = %d, versionlevel = %d\n", level, global.params.versionlevel);
175 //if (ident) printf("\tident = '%s'\n", ident->toChars());
176 if (inc == 0)
178 inc = 2;
179 if (ident)
181 if (findCondition(mod->versionids, ident))
182 inc = 1;
183 else if (findCondition(global.params.versionids, ident))
184 inc = 1;
185 else
187 if (!mod->versionidsNot)
188 mod->versionidsNot = new Array();
189 mod->versionidsNot->push(ident->toChars());
192 else if (level <= global.params.versionlevel || level <= mod->versionlevel)
193 inc = 1;
195 return (inc == 1);
198 void VersionCondition::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
200 if (ident)
201 buf->printf("version (%s)", ident->toChars());
202 else
203 buf->printf("version (%u)", level);
207 /**************************** StaticIfCondition *******************************/
209 StaticIfCondition::StaticIfCondition(Loc loc, Expression *exp)
210 : Condition(loc)
212 this->exp = exp;
215 Condition *StaticIfCondition::syntaxCopy()
217 return new StaticIfCondition(loc, exp->syntaxCopy());
220 int StaticIfCondition::include(Scope *sc, ScopeDsymbol *s)
222 #if 0
223 printf("StaticIfCondition::include(sc = %p, s = %p)\n", sc, s);
224 if (s)
226 printf("\ts = '%s', kind = %s\n", s->toChars(), s->kind());
228 #endif
229 if (inc == 0)
231 if (!sc)
233 error(loc, "static if conditional cannot be at global scope");
234 inc = 2;
235 return 0;
238 sc = sc->push(sc->scopesym);
239 sc->sd = s; // s gets any addMember()
240 sc->flags |= SCOPEstaticif;
241 Expression *e = exp->semantic(sc);
242 sc->pop();
243 e = e->optimize(WANTvalue | WANTinterpret);
244 if (e->isBool(TRUE))
245 inc = 1;
246 else if (e->isBool(FALSE))
247 inc = 2;
248 else
250 e->error("expression %s is not constant or does not evaluate to a bool", e->toChars());
251 inc = 2;
254 return (inc == 1);
257 void StaticIfCondition::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
259 buf->writestring("static if(");
260 exp->toCBuffer(buf, hgs);
261 buf->writeByte(')');
265 /**************************** IftypeCondition *******************************/
267 IftypeCondition::IftypeCondition(Loc loc, Type *targ, Identifier *id, enum TOK tok, Type *tspec)
268 : Condition(loc)
270 this->targ = targ;
271 this->id = id;
272 this->tok = tok;
273 this->tspec = tspec;
276 Condition *IftypeCondition::syntaxCopy()
278 return new IftypeCondition(loc,
279 targ->syntaxCopy(),
281 tok,
282 tspec ? tspec->syntaxCopy() : NULL);
285 int IftypeCondition::include(Scope *sc, ScopeDsymbol *sd)
287 //printf("IftypeCondition::include()\n");
288 if (inc == 0)
290 if (!sc)
292 error(loc, "iftype conditional cannot be at global scope");
293 inc = 2;
294 return 0;
296 unsigned errors = global.errors;
297 global.gag++; // suppress printing of error messages
298 targ = targ->semantic(loc, sc);
299 global.gag--;
300 if (errors != global.errors) // if any errors happened
301 { inc = 2; // then condition is false
302 global.errors = errors;
304 else if (id && tspec)
306 /* Evaluate to TRUE if targ matches tspec.
307 * If TRUE, declare id as an alias for the specialized type.
310 MATCH m;
311 TemplateTypeParameter tp(loc, id, NULL, NULL);
313 TemplateParameters parameters;
314 parameters.setDim(1);
315 parameters.data[0] = (void *)&tp;
317 Objects dedtypes;
318 dedtypes.setDim(1);
320 m = targ->deduceType(NULL, tspec, &parameters, &dedtypes);
321 if (m == MATCHnomatch ||
322 (m != MATCHexact && tok == TOKequal))
323 inc = 2;
324 else
326 inc = 1;
327 Type *tded = (Type *)dedtypes.data[0];
328 if (!tded)
329 tded = targ;
330 Dsymbol *s = new AliasDeclaration(loc, id, tded);
331 s->semantic(sc);
332 sc->insert(s);
333 if (sd)
334 s->addMember(sc, sd, 1);
337 else if (id)
339 /* Declare id as an alias for type targ. Evaluate to TRUE
341 Dsymbol *s = new AliasDeclaration(loc, id, targ);
342 s->semantic(sc);
343 sc->insert(s);
344 if (sd)
345 s->addMember(sc, sd, 1);
346 inc = 1;
348 else if (tspec)
350 /* Evaluate to TRUE if targ matches tspec
352 tspec = tspec->semantic(loc, sc);
353 //printf("targ = %s\n", targ->toChars());
354 //printf("tspec = %s\n", tspec->toChars());
355 if (tok == TOKcolon)
356 { if (targ->implicitConvTo(tspec))
357 inc = 1;
358 else
359 inc = 2;
361 else /* == */
362 { if (targ->equals(tspec))
363 inc = 1;
364 else
365 inc = 2;
368 else
369 inc = 1;
370 //printf("inc = %d\n", inc);
372 return (inc == 1);
375 void IftypeCondition::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
377 buf->writestring("iftype(");
378 targ->toCBuffer(buf, id, hgs);
379 if (tspec)
381 if (tok == TOKcolon)
382 buf->writestring(" : ");
383 else
384 buf->writestring(" == ");
385 tspec->toCBuffer(buf, NULL, hgs);
387 buf->writeByte(')');