comparison dmd2/staticassert.c @ 758:f04dde6e882c

Added initial D2 support, D2 frontend and changes to codegen to make things compile.
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 11 Nov 2008 01:38:48 +0100
parents
children 638d16625da2
comparison
equal deleted inserted replaced
757:2c730d530c98 758:f04dde6e882c
1
2 // Copyright (c) 1999-2007 by Digital Mars
3 // All Rights Reserved
4 // written by Walter Bright
5 // http://www.digitalmars.com
6 // License for redistribution is by either the Artistic License
7 // in artistic.txt, or the GNU General Public License in gnu.txt.
8 // See the included readme.txt for details.
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <assert.h>
13
14 #include "dsymbol.h"
15 #include "staticassert.h"
16 #include "expression.h"
17 #include "id.h"
18 #include "hdrgen.h"
19 #include "scope.h"
20 #include "template.h"
21
22 /********************************* AttribDeclaration ****************************/
23
24 StaticAssert::StaticAssert(Loc loc, Expression *exp, Expression *msg)
25 : Dsymbol(Id::empty)
26 {
27 this->loc = loc;
28 this->exp = exp;
29 this->msg = msg;
30 }
31
32 Dsymbol *StaticAssert::syntaxCopy(Dsymbol *s)
33 {
34 StaticAssert *sa;
35
36 assert(!s);
37 sa = new StaticAssert(loc, exp->syntaxCopy(), msg ? msg->syntaxCopy() : NULL);
38 return sa;
39 }
40
41 int StaticAssert::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
42 {
43 return 0; // we didn't add anything
44 }
45
46 void StaticAssert::semantic(Scope *sc)
47 {
48 }
49
50 void StaticAssert::semantic2(Scope *sc)
51 {
52 Expression *e;
53
54 //printf("StaticAssert::semantic2() %s\n", toChars());
55 e = exp->semantic(sc);
56 e = e->optimize(WANTvalue | WANTinterpret);
57 if (e->isBool(FALSE))
58 {
59 if (msg)
60 { HdrGenState hgs;
61 OutBuffer buf;
62
63 msg = msg->semantic(sc);
64 msg = msg->optimize(WANTvalue | WANTinterpret);
65 hgs.console = 1;
66 msg->toCBuffer(&buf, &hgs);
67 error("%s", buf.toChars());
68 }
69 else
70 error("is false");
71 if(sc->tinst)
72 sc->tinst->printInstantiationTrace();
73 if (!global.gag)
74 fatal();
75 }
76 else if (!e->isBool(TRUE))
77 {
78 error("(%s) is not evaluatable at compile time", exp->toChars());
79 }
80 }
81
82 int StaticAssert::oneMember(Dsymbol **ps)
83 {
84 //printf("StaticAssert::oneMember())\n");
85 *ps = NULL;
86 return TRUE;
87 }
88
89 void StaticAssert::inlineScan()
90 {
91 }
92
93 void StaticAssert::toObjFile(int multiobj)
94 {
95 }
96
97 const char *StaticAssert::kind()
98 {
99 return "static assert";
100 }
101
102 void StaticAssert::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
103 {
104 buf->writestring(kind());
105 buf->writeByte('(');
106 exp->toCBuffer(buf, hgs);
107 if (msg)
108 {
109 buf->writeByte(',');
110 msg->toCBuffer(buf, hgs);
111 }
112 buf->writestring(");");
113 buf->writenl();
114 }