1
/**
2
 * Defines a `Dsymbol` for `version = identifier` and `debug = identifier` statements.
3
 *
4
 * Specification: $(LINK2 https://dlang.org/spec/version.html#version-specification, Version Specification),
5
 *                $(LINK2 https://dlang.org/spec/version.html#debug_specification, Debug Specification).
6
 *
7
 * Copyright:   Copyright (C) 1999-2020 by The D Language Foundation, All Rights Reserved
8
 * Authors:     $(LINK2 http://www.digitalmars.com, Walter Bright)
9
 * License:     $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
10
 * Source:      $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/dversion.d, _dversion.d)
11
 * Documentation:  https://dlang.org/phobos/dmd_dversion.html
12
 * Coverage:    https://codecov.io/gh/dlang/dmd/src/master/src/dmd/dversion.d
13
 */
14

15
module dmd.dversion;
16

17
import dmd.arraytypes;
18
import dmd.cond;
19
import dmd.dmodule;
20
import dmd.dscope;
21
import dmd.dsymbol;
22
import dmd.dsymbolsem;
23
import dmd.globals;
24
import dmd.identifier;
25
import dmd.root.outbuffer;
26
import dmd.visitor;
27

28
/***********************************************************
29
 * DebugSymbol's happen for statements like:
30
 *      debug = identifier;
31
 *      debug = integer;
32
 */
33
extern (C++) final class DebugSymbol : Dsymbol
34
{
35
    uint level;
36

37 1
    extern (D) this(const ref Loc loc, Identifier ident)
38
    {
39 1
        super(loc, ident);
40
    }
41

42 1
    extern (D) this(const ref Loc loc, uint level)
43
    {
44 1
        super(loc, null);
45 1
        this.level = level;
46
    }
47

48
    override Dsymbol syntaxCopy(Dsymbol s)
49
    {
50 1
        assert(!s);
51 1
        auto ds = new DebugSymbol(loc, ident);
52 1
        ds.comment = comment;
53 1
        ds.level = level;
54 1
        return ds;
55
    }
56

57
    override const(char)* toChars() const nothrow
58
    {
59 1
        if (ident)
60 1
            return ident.toChars();
61
        else
62
        {
63 1
            OutBuffer buf;
64 1
            buf.print(level);
65 1
            return buf.extractChars();
66
        }
67
    }
68

69
    override void addMember(Scope* sc, ScopeDsymbol sds)
70
    {
71
        //printf("DebugSymbol::addMember('%s') %s\n", sds.toChars(), toChars());
72 1
        Module m = sds.isModule();
73
        // Do not add the member to the symbol table,
74
        // just make sure subsequent debug declarations work.
75 1
        if (ident)
76
        {
77 1
            if (!m)
78
            {
79 1
                error("declaration must be at module level");
80 1
                errors = true;
81
            }
82
            else
83
            {
84 1
                if (findCondition(m.debugidsNot, ident))
85
                {
86 1
                    error("defined after use");
87 1
                    errors = true;
88
                }
89 1
                if (!m.debugids)
90 1
                    m.debugids = new Identifiers();
91 1
                m.debugids.push(ident);
92
            }
93
        }
94
        else
95
        {
96 1
            if (!m)
97
            {
98 1
                error("level declaration must be at module level");
99 1
                errors = true;
100
            }
101
            else
102 1
                m.debuglevel = level;
103
        }
104
    }
105

106
    override const(char)* kind() const nothrow
107
    {
108 1
        return "debug";
109
    }
110

111
    override inout(DebugSymbol) isDebugSymbol() inout
112
    {
113 1
        return this;
114
    }
115

116
    override void accept(Visitor v)
117
    {
118 1
        v.visit(this);
119
    }
120
}
121

122
/***********************************************************
123
 * VersionSymbol's happen for statements like:
124
 *      version = identifier;
125
 *      version = integer;
126
 */
127
extern (C++) final class VersionSymbol : Dsymbol
128
{
129
    uint level;
130

131 1
    extern (D) this(const ref Loc loc, Identifier ident)
132
    {
133 1
        super(loc, ident);
134
    }
135

136 1
    extern (D) this(const ref Loc loc, uint level)
137
    {
138 1
        super(loc, null);
139 1
        this.level = level;
140
    }
141

142
    override Dsymbol syntaxCopy(Dsymbol s)
143
    {
144 1
        assert(!s);
145 1
        auto ds = ident ? new VersionSymbol(loc, ident)
146 1
                        : new VersionSymbol(loc, level);
147 1
        ds.comment = comment;
148 1
        return ds;
149
    }
150

151
    override const(char)* toChars() const nothrow
152
    {
153 1
        if (ident)
154 1
            return ident.toChars();
155
        else
156
        {
157 1
            OutBuffer buf;
158 1
            buf.print(level);
159 1
            return buf.extractChars();
160
        }
161
    }
162

163
    override void addMember(Scope* sc, ScopeDsymbol sds)
164
    {
165
        //printf("VersionSymbol::addMember('%s') %s\n", sds.toChars(), toChars());
166 1
        Module m = sds.isModule();
167
        // Do not add the member to the symbol table,
168
        // just make sure subsequent debug declarations work.
169 1
        if (ident)
170
        {
171 1
            VersionCondition.checkReserved(loc, ident.toString());
172 1
            if (!m)
173
            {
174 1
                error("declaration must be at module level");
175 1
                errors = true;
176
            }
177
            else
178
            {
179 1
                if (findCondition(m.versionidsNot, ident))
180
                {
181 1
                    error("defined after use");
182 1
                    errors = true;
183
                }
184 1
                if (!m.versionids)
185 1
                    m.versionids = new Identifiers();
186 1
                m.versionids.push(ident);
187
            }
188
        }
189
        else
190
        {
191 1
            if (!m)
192
            {
193 1
                error("level declaration must be at module level");
194 1
                errors = true;
195
            }
196
            else
197 1
                m.versionlevel = level;
198
        }
199
    }
200

201
    override const(char)* kind() const nothrow
202
    {
203 1
        return "version";
204
    }
205

206
    override inout(VersionSymbol) isVersionSymbol() inout
207
    {
208 1
        return this;
209
    }
210

211
    override void accept(Visitor v)
212
    {
213 1
        v.visit(this);
214
    }
215
}

Read our documentation on viewing source code .

Loading