This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
id.d
212 lines (184 loc) · 5.64 KB
/
id.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* C preprocessor
* Copyright: 2013 by Digital Mars
* License: $(LINK2 http://boost.org/LICENSE_1_0.txt, Boost License 1.0).
* Authors: Walter Bright
*/
module id;
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.time;
import std.stdio;
import util;
import macros;
/******************************
* All identifiers become a pointer to an instance
* of this.
*/
struct Id
{
enum buckets_length = 0x8000UL;
__gshared Id*[] buckets;
__gshared uint count;
ustring name;
size_t hash;
Id* next;
private this(ustring name, size_t hash)
{
this.name = name;
this.hash = hash;
}
/******************************
* Reset in between files processed.
*/
static void reset()
{
/* Expect that the same headers will be processed again, with the
* same macros. So leave the table in place - just #undef all the
* entries.
*/
foreach (m; buckets)
{
for (; m; m = m.next)
{
m.flags = 0;
m.text = null;
m.parameters = null;
}
}
}
/*********************
* See if this is a known identifier.
* Returns:
* Id* if it is, null if not
*/
static Id* search(const(uchar)[] name)
{
if (buckets.length)
{
auto hash = getHash(name);
auto bucket = buckets[hash % buckets_length];
while (bucket)
{
if (bucket.hash == hash && bucket.name == name)
return bucket;
bucket = bucket.next;
}
}
return null;
}
/*************************
* Look up name in Id table.
* If it's there, return it.
* If not, create an Id and return it.
*/
static Id* pool(ustring name)
{
if (!buckets.length)
{
buckets = (cast(Id**)calloc((Id*).sizeof, buckets_length))[0 .. buckets_length];
assert(buckets.ptr);
}
auto hash = getHash(name);
auto pbucket = &buckets[hash % buckets_length];
//int depth;
while (*pbucket)
{
//++depth;
if ((*pbucket).hash == hash && (*pbucket).name == name)
return *pbucket;
pbucket = &(*pbucket).next;
}
auto id = new Id(name, hash);
*pbucket = id;
//++count;
//writefln("count: %s depth %s hash %s bucket %s %s", count, depth, hash, hash % buckets.length, cast(string)name);
return id;
}
/****************************
* Define a macro.
* Returns:
* null if a redefinition error
*/
static Id* defineMacro(ustring name, ustring[] parameters, ustring text, uint flags)
{
//writefln("defineMacro(%s, %s, %s, %s)", cast(string)name, cast(string[])parameters, cast(string)text, flags);
auto m = pool(name);
if (m.flags & IDmacro)
{
if ((m.flags ^ flags) & (IDdotdotdot | IDfunctionLike) ||
m.parameters != parameters ||
m.text != text)
{
return null;
}
if (((m.flags ^ flags) & IDpredefined) &&
m.text != text)
{
return null;
}
}
m.flags |= IDmacro | flags;
m.parameters = parameters;
m.text = text;
return m;
}
uint flags; // flags are below
enum
{
// Macros
IDmacro = 1, // it's a macro in good standing
IDdotdotdot = 2, // the macro has a ...
IDfunctionLike = 4, // the macro has ( ), i.e. is function-like
IDpredefined = 8, // the macro is predefined and cannot be #undef'd
IDinuse = 0x10, // macro is currently being expanded
// Predefined
IDlinnum = 0x20,
IDfile = 0x40,
IDcounter = 0x80,
IDpragma = 0x100,
}
ustring text; // replacement text of the macro
ustring[] parameters; // macro parameters
/* Initialize the predefined macros
*/
static void initPredefined()
{
defineMacro(cast(ustring)"__FILE__", null, null, IDpredefined | IDfile);
defineMacro(cast(ustring)"__LINE__", null, null, IDpredefined | IDlinnum);
defineMacro(cast(ustring)"__COUNTER__", null, null, IDpredefined | IDcounter);
uchar[1+26+1] date;
time_t t;
time(&t);
auto p = cast(ubyte*)ctime(&t);
assert(p);
auto len = sprintf(cast(char*)date.ptr,"\"%.24s\"",p);
defineMacro(cast(ustring)"__TIMESTAMP__", null, date[0..len].idup, IDpredefined);
len = sprintf(cast(char*)date.ptr,"\"%.6s %.4s\"",p+4,p+20);
defineMacro(cast(ustring)"__DATE__", null, date[0..len].idup, IDpredefined);
len = sprintf(cast(char*)date.ptr,"\"%.8s\"",p+11);
defineMacro(cast(ustring)"__TIME__", null, date[0..len].idup, IDpredefined);
static ustring[1] params = [cast(ustring)"arg"];
defineMacro(cast(ustring)"_Pragma", params, cast(ustring)("\n#pragma " ~ ESC.start ~ ESC.arg1 ~ "\n"),
IDpredefined | IDpragma | IDfunctionLike);
}
static size_t getHash(const(uchar)[] name)
{
size_t hash = 0;
while (name.length >= size_t.sizeof)
{
hash = hash * 37 + *cast(size_t*)name.ptr;
name = name[size_t.sizeof .. $];
}
foreach (c; name)
hash = hash * 37 + c;
hash ^= (hash >> 20) ^ (hash >> 12);
return hash ^ (hash >> 7) ^ (hash >> 4);
}
}
/*
* Local Variables:
* mode: d
* c-basic-offset: 4
* End:
*/