0
|
1 module dmd.VersionCondition;
|
|
2
|
114
|
3 import dmd.common;
|
0
|
4 import dmd.DVCondition;
|
|
5 import dmd.Loc;
|
|
6 import dmd.Module;
|
|
7 import dmd.Scope;
|
|
8 import dmd.ScopeDsymbol;
|
|
9 import dmd.OutBuffer;
|
|
10 import dmd.HdrGenState;
|
|
11 import dmd.Identifier;
|
|
12 import dmd.Global;
|
|
13 import dmd.String;
|
|
14 import dmd.Util : error;
|
|
15
|
|
16 import std.string : startsWith;
|
|
17
|
|
18 // for findCondition
|
|
19 import dmd.Array;
|
|
20 import core.stdc.string;
|
|
21
|
|
22 import std.stdio;
|
|
23
|
|
24 bool findCondition(Array ids, Identifier ident)
|
|
25 {
|
|
26 if (ids !is null) {
|
|
27 foreach (i; 0..ids.dim)
|
|
28 {
|
|
29 string id = (cast(String)ids.data[i]).str;
|
|
30 if (id == ident.toChars()) {
|
|
31 return true;
|
|
32 }
|
|
33 }
|
|
34 }
|
|
35
|
|
36 return false;
|
|
37 }
|
|
38
|
|
39 class VersionCondition : DVCondition
|
|
40 {
|
|
41 static void setGlobalLevel(uint level)
|
|
42 {
|
|
43 global.params.versionlevel = level;
|
|
44 }
|
|
45
|
|
46 static void checkPredefined(Loc loc, string ident)
|
|
47 {
|
96
|
48 version (DMDV2)
|
|
49 {
|
0
|
50 static string[] reserved = [
|
|
51 "DigitalMars", "X86", "X86_64",
|
|
52 "Windows", "Win32", "Win64",
|
|
53 "linux",
|
|
54 /* Although Posix is predefined by D1, disallowing its
|
|
55 * redefinition breaks makefiles and older builds.
|
|
56 */
|
|
57 "Posix",
|
|
58 "D_NET",
|
|
59 "OSX", "FreeBSD",
|
|
60 "Solaris",
|
|
61 "LittleEndian", "BigEndian",
|
|
62 "all",
|
|
63 "none",
|
|
64 ];
|
|
65 } else {
|
|
66 static string[] reserved = [
|
|
67 "DigitalMars", "X86", "X86_64",
|
|
68 "Windows", "Win32", "Win64",
|
|
69 "linux",
|
|
70 "OSX", "FreeBSD",
|
|
71 "Solaris",
|
|
72 "LittleEndian", "BigEndian",
|
|
73 "all",
|
|
74 "none",
|
|
75 ];
|
|
76 }
|
|
77 foreach (reservedIdent; reserved)
|
|
78 {
|
|
79 if (ident == reservedIdent)
|
|
80 goto Lerror;
|
|
81 }
|
|
82
|
|
83 if (ident.startsWith("D_")) {
|
|
84 goto Lerror;
|
|
85 }
|
|
86
|
|
87 return;
|
|
88
|
|
89 Lerror:
|
|
90 error(loc, "version identifier '%s' is reserved and cannot be set", ident);
|
|
91 }
|
|
92
|
|
93 static void addGlobalIdent(string ident)
|
|
94 {
|
|
95 checkPredefined(Loc(0), ident);
|
|
96 addPredefinedGlobalIdent(ident);
|
|
97 }
|
|
98
|
|
99 static void addPredefinedGlobalIdent(string ident)
|
|
100 {
|
|
101 global.params.versionids.push(cast(void*)new String(ident)); ///
|
|
102 }
|
|
103
|
|
104 this(Module mod, uint level, Identifier ident)
|
|
105 {
|
|
106 super(mod, level, ident);
|
|
107 }
|
|
108
|
72
|
109 override final bool include(Scope sc, ScopeDsymbol s)
|
0
|
110 {
|
|
111 //printf("VersionCondition::include() level = %d, versionlevel = %d\n", level, global.params.versionlevel);
|
|
112 //if (ident) printf("\tident = '%s'\n", ident->toChars());
|
|
113 if (inc == 0) {
|
|
114 inc = 2;
|
|
115 if (ident !is null) {
|
|
116 if (findCondition(mod.versionids, ident)) {
|
|
117 inc = 1;
|
|
118 } else if (findCondition(global.params.versionids, ident)) {
|
|
119 inc = 1;
|
|
120 } else {
|
|
121 if (!mod.versionidsNot) {
|
|
122 mod.versionidsNot = new Array();
|
|
123 }
|
|
124 mod.versionidsNot.push(cast(void*)new String(ident.toChars())); //
|
|
125 }
|
|
126 } else if (level <= global.params.versionlevel || level <= mod.versionlevel) {
|
|
127 inc = 1;
|
|
128 }
|
|
129 }
|
|
130
|
|
131 return (inc == 1);
|
|
132 }
|
|
133
|
72
|
134 override final void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
135 {
|
|
136 if (ident !is null) {
|
|
137 buf.printf("version (%s)", ident.toChars());
|
|
138 } else {
|
|
139 buf.printf("version (%u)", level);
|
|
140 }
|
|
141 }
|
72
|
142 }
|