1
|
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
|
|
20 /********************************* AttribDeclaration ****************************/
|
|
21
|
|
22 StaticAssert::StaticAssert(Loc loc, Expression *exp, Expression *msg)
|
|
23 : Dsymbol(Id::empty)
|
|
24 {
|
|
25 this->loc = loc;
|
|
26 this->exp = exp;
|
|
27 this->msg = msg;
|
|
28 }
|
|
29
|
|
30 Dsymbol *StaticAssert::syntaxCopy(Dsymbol *s)
|
|
31 {
|
|
32 StaticAssert *sa;
|
|
33
|
|
34 assert(!s);
|
|
35 sa = new StaticAssert(loc, exp->syntaxCopy(), msg ? msg->syntaxCopy() : NULL);
|
|
36 return sa;
|
|
37 }
|
|
38
|
|
39 int StaticAssert::addMember(Scope *sc, ScopeDsymbol *sd, int memnum)
|
|
40 {
|
|
41 return 0; // we didn't add anything
|
|
42 }
|
|
43
|
|
44 void StaticAssert::semantic(Scope *sc)
|
|
45 {
|
|
46 }
|
|
47
|
|
48 void StaticAssert::semantic2(Scope *sc)
|
|
49 {
|
|
50 Expression *e;
|
|
51
|
19
|
52 //printf("StaticAssert::semantic2() %s\n", toChars());
|
1
|
53 e = exp->semantic(sc);
|
|
54 e = e->optimize(WANTvalue | WANTinterpret);
|
|
55 if (e->isBool(FALSE))
|
|
56 {
|
|
57 if (msg)
|
|
58 { HdrGenState hgs;
|
|
59 OutBuffer buf;
|
|
60
|
|
61 msg = msg->semantic(sc);
|
|
62 msg = msg->optimize(WANTvalue | WANTinterpret);
|
|
63 hgs.console = 1;
|
|
64 msg->toCBuffer(&buf, &hgs);
|
|
65 error("%s", buf.toChars());
|
|
66 }
|
|
67 else
|
|
68 error("is false");
|
|
69 if (!global.gag)
|
|
70 fatal();
|
|
71 }
|
|
72 else if (!e->isBool(TRUE))
|
|
73 {
|
|
74 error("(%s) is not evaluatable at compile time", exp->toChars());
|
|
75 }
|
|
76 }
|
|
77
|
|
78 int StaticAssert::oneMember(Dsymbol **ps)
|
|
79 {
|
|
80 //printf("StaticAssert::oneMember())\n");
|
|
81 *ps = NULL;
|
|
82 return TRUE;
|
|
83 }
|
|
84
|
|
85 void StaticAssert::inlineScan()
|
|
86 {
|
|
87 }
|
|
88
|
|
89 void StaticAssert::toObjFile()
|
|
90 {
|
|
91 }
|
|
92
|
|
93 char *StaticAssert::kind()
|
|
94 {
|
|
95 return "static assert";
|
|
96 }
|
|
97
|
|
98 void StaticAssert::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
99 {
|
|
100 buf->writestring(kind());
|
|
101 buf->writeByte('(');
|
|
102 exp->toCBuffer(buf, hgs);
|
|
103 if (msg)
|
|
104 {
|
|
105 buf->writeByte(',');
|
|
106 msg->toCBuffer(buf, hgs);
|
|
107 }
|
|
108 buf->writestring(");");
|
|
109 buf->writenl();
|
|
110 }
|