]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/dmd/staticassert.c
Add D front-end, libphobos library, and D2 testsuite.
[thirdparty/gcc.git] / gcc / d / dmd / staticassert.c
1
2 /* Compiler implementation of the D programming language
3 * Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved
4 * written by Walter Bright
5 * http://www.digitalmars.com
6 * Distributed under the Boost Software License, Version 1.0.
7 * http://www.boost.org/LICENSE_1_0.txt
8 * https://github.com/D-Programming-Language/dmd/blob/master/src/staticassert.c
9 */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <assert.h>
14
15 #include "mars.h"
16 #include "dsymbol.h"
17 #include "staticassert.h"
18 #include "expression.h"
19 #include "id.h"
20 #include "scope.h"
21 #include "template.h"
22 #include "declaration.h"
23
24 Expression *semantic(Expression *e, Scope *sc);
25 bool evalStaticCondition(Scope *sc, Expression *exp, Expression *e, bool &errors);
26
27 /********************************* AttribDeclaration ****************************/
28
29 StaticAssert::StaticAssert(Loc loc, Expression *exp, Expression *msg)
30 : Dsymbol(Id::empty)
31 {
32 this->loc = loc;
33 this->exp = exp;
34 this->msg = msg;
35 }
36
37 Dsymbol *StaticAssert::syntaxCopy(Dsymbol *s)
38 {
39 assert(!s);
40 return new StaticAssert(loc, exp->syntaxCopy(), msg ? msg->syntaxCopy() : NULL);
41 }
42
43 void StaticAssert::addMember(Scope *, ScopeDsymbol *)
44 {
45 // we didn't add anything
46 }
47
48 void StaticAssert::semantic(Scope *)
49 {
50 }
51
52 void StaticAssert::semantic2(Scope *sc)
53 {
54 //printf("StaticAssert::semantic2() %s\n", toChars());
55 ScopeDsymbol *sds = new ScopeDsymbol();
56 sc = sc->push(sds);
57 sc->tinst = NULL;
58 sc->minst = NULL;
59
60 bool errors = false;
61 bool result = evalStaticCondition(sc, exp, exp, errors);
62 sc = sc->pop();
63 if (errors)
64 {
65 errorSupplemental(loc, "while evaluating: static assert(%s)", exp->toChars());
66 }
67 else if (!result)
68 {
69 if (msg)
70 {
71 sc = sc->startCTFE();
72 msg = ::semantic(msg, sc);
73 msg = resolveProperties(sc, msg);
74 sc = sc->endCTFE();
75 msg = msg->ctfeInterpret();
76 if (StringExp * se = msg->toStringExp())
77 {
78 // same with pragma(msg)
79 se = se->toUTF8(sc);
80 error("\"%.*s\"", (int)se->len, (char *)se->string);
81 }
82 else
83 error("%s", msg->toChars());
84 }
85 else
86 error("(%s) is false", exp->toChars());
87 if (sc->tinst)
88 sc->tinst->printInstantiationTrace();
89 if (!global.gag)
90 fatal();
91 }
92 }
93
94 bool StaticAssert::oneMember(Dsymbol **ps, Identifier *)
95 {
96 //printf("StaticAssert::oneMember())\n");
97 *ps = NULL;
98 return true;
99 }
100
101 const char *StaticAssert::kind()
102 {
103 return "static assert";
104 }