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
|
|
52 e = exp->semantic(sc);
|
|
53 e = e->optimize(WANTvalue | WANTinterpret);
|
|
54 if (e->isBool(FALSE))
|
|
55 {
|
|
56 if (msg)
|
|
57 { HdrGenState hgs;
|
|
58 OutBuffer buf;
|
|
59
|
|
60 msg = msg->semantic(sc);
|
|
61 msg = msg->optimize(WANTvalue | WANTinterpret);
|
|
62 hgs.console = 1;
|
|
63 msg->toCBuffer(&buf, &hgs);
|
|
64 error("%s", buf.toChars());
|
|
65 }
|
|
66 else
|
|
67 error("is false");
|
|
68 if (!global.gag)
|
|
69 fatal();
|
|
70 }
|
|
71 else if (!e->isBool(TRUE))
|
|
72 {
|
|
73 error("(%s) is not evaluatable at compile time", exp->toChars());
|
|
74 }
|
|
75 }
|
|
76
|
|
77 int StaticAssert::oneMember(Dsymbol **ps)
|
|
78 {
|
|
79 //printf("StaticAssert::oneMember())\n");
|
|
80 *ps = NULL;
|
|
81 return TRUE;
|
|
82 }
|
|
83
|
|
84 void StaticAssert::inlineScan()
|
|
85 {
|
|
86 }
|
|
87
|
|
88 void StaticAssert::toObjFile()
|
|
89 {
|
|
90 }
|
|
91
|
|
92 char *StaticAssert::kind()
|
|
93 {
|
|
94 return "static assert";
|
|
95 }
|
|
96
|
|
97 void StaticAssert::toCBuffer(OutBuffer *buf, HdrGenState *hgs)
|
|
98 {
|
|
99 buf->writestring(kind());
|
|
100 buf->writeByte('(');
|
|
101 exp->toCBuffer(buf, hgs);
|
|
102 if (msg)
|
|
103 {
|
|
104 buf->writeByte(',');
|
|
105 msg->toCBuffer(buf, hgs);
|
|
106 }
|
|
107 buf->writestring(");");
|
|
108 buf->writenl();
|
|
109 }
|