-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathModuleFileCoreTableInfo.h
615 lines (519 loc) · 21 KB
/
ModuleFileCoreTableInfo.h
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//===--- ModuleFileCoreTableInfo.h - Hash table info structures -*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SERIALIZATION_MODULEFILECORETABLEINFO_H
#define SWIFT_SERIALIZATION_MODULEFILECORETABLEINFO_H
#include "ModuleFileSharedCore.h"
#include "DocFormat.h"
#include "SourceInfoFormat.h"
#include "swift/AST/RawComment.h"
#include "llvm/Support/DJB.h"
namespace swift {
/// Used to deserialize entries in the on-disk decl hash table.
class ModuleFileSharedCore::DeclTableInfo {
public:
using internal_key_type = std::pair<DeclBaseName::Kind, StringRef>;
using external_key_type = DeclBaseName;
using data_type = SmallVector<std::pair<uint8_t, DeclID>, 8>;
using hash_value_type = uint32_t;
using offset_type = unsigned;
internal_key_type GetInternalKey(external_key_type ID) {
if (ID.getKind() == DeclBaseName::Kind::Normal) {
return {DeclBaseName::Kind::Normal, ID.getIdentifier().str()};
} else {
return {ID.getKind(), StringRef()};
}
}
hash_value_type ComputeHash(internal_key_type key) {
if (key.first == DeclBaseName::Kind::Normal) {
return llvm::djbHash(key.second, serialization::SWIFTMODULE_HASH_SEED);
} else {
return (hash_value_type)key.first;
}
}
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
return lhs == rhs;
}
static std::pair<unsigned, unsigned> ReadKeyDataLength(const uint8_t *&data) {
using namespace llvm::support;
unsigned keyLength = endian::readNext<uint16_t, little, unaligned>(data);
unsigned dataLength = endian::readNext<uint16_t, little, unaligned>(data);
return { keyLength, dataLength };
}
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
using namespace swift::serialization;
using namespace llvm::support;
uint8_t kind = endian::readNext<uint8_t, little, unaligned>(data);
switch (kind) {
case static_cast<uint8_t>(DeclNameKind::Normal): {
StringRef str(reinterpret_cast<const char *>(data),
length - sizeof(uint8_t));
return {DeclBaseName::Kind::Normal, str};
}
case static_cast<uint8_t>(DeclNameKind::Subscript):
return {DeclBaseName::Kind::Subscript, StringRef()};
case static_cast<uint8_t>(DeclNameKind::Destructor):
return {DeclBaseName::Kind::Destructor, StringRef()};
default:
llvm_unreachable("Unknown DeclNameKind");
}
}
static data_type ReadData(internal_key_type key, const uint8_t *data,
unsigned length) {
using namespace llvm::support;
data_type result;
while (length > 0) {
uint8_t kind = *data++;
DeclID offset = endian::readNext<uint32_t, little, unaligned>(data);
result.push_back({ kind, offset });
length -= 5;
}
return result;
}
};
/// Used to deserialize entries in the on-disk decl hash table.
class ModuleFileSharedCore::ExtensionTableInfo {
const ModuleFileSharedCore &Core;
public:
using internal_key_type = StringRef;
using external_key_type = Identifier;
using data_type = SmallVector<std::pair<StringRef, DeclID>, 8>;
using hash_value_type = uint32_t;
using offset_type = unsigned;
internal_key_type GetInternalKey(external_key_type ID) {
return ID.str();
}
hash_value_type ComputeHash(internal_key_type key) {
return llvm::djbHash(key, serialization::SWIFTMODULE_HASH_SEED);
}
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
return lhs == rhs;
}
static std::pair<unsigned, unsigned> ReadKeyDataLength(const uint8_t *&data) {
using namespace llvm::support;
unsigned keyLength = endian::readNext<uint16_t, little, unaligned>(data);
unsigned dataLength = endian::readNext<uint16_t, little, unaligned>(data);
return { keyLength, dataLength };
}
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
return StringRef(reinterpret_cast<const char *>(data), length);
}
data_type ReadData(internal_key_type key, const uint8_t *data,
unsigned length) {
using namespace llvm::support;
data_type result;
const uint8_t *limit = data + length;
while (data < limit) {
DeclID offset = endian::readNext<uint32_t, little, unaligned>(data);
int32_t nameIDOrLength =
endian::readNext<int32_t, little, unaligned>(data);
StringRef moduleNameOrMangledBase;
if (nameIDOrLength < 0) {
StringRef moduleName = Core.getModuleNameFromID(-nameIDOrLength);
if (!moduleName.empty())
moduleNameOrMangledBase = moduleName;
} else {
moduleNameOrMangledBase =
StringRef(reinterpret_cast<const char *>(data), nameIDOrLength);
data += nameIDOrLength;
}
result.push_back({ moduleNameOrMangledBase, offset });
}
return result;
}
explicit ExtensionTableInfo(const ModuleFileSharedCore &core) : Core(core) {}
};
/// Used to deserialize entries in the on-disk decl hash table.
class ModuleFileSharedCore::LocalDeclTableInfo {
public:
using internal_key_type = StringRef;
using external_key_type = internal_key_type;
using data_type = DeclID;
using hash_value_type = uint32_t;
using offset_type = unsigned;
internal_key_type GetInternalKey(external_key_type ID) {
return ID;
}
external_key_type GetExternalKey(internal_key_type ID) {
return ID;
}
hash_value_type ComputeHash(internal_key_type key) {
return llvm::djbHash(key, serialization::SWIFTMODULE_HASH_SEED);
}
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
return lhs == rhs;
}
static std::pair<unsigned, unsigned> ReadKeyDataLength(const uint8_t *&data) {
using namespace llvm::support;
unsigned keyLength = endian::readNext<uint16_t, little, unaligned>(data);
return { keyLength, sizeof(uint32_t) };
}
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
return StringRef(reinterpret_cast<const char *>(data), length);
}
static data_type ReadData(internal_key_type key, const uint8_t *data,
unsigned length) {
using namespace llvm::support;
return endian::readNext<uint32_t, little, unaligned>(data);
}
};
class ModuleFileSharedCore::NestedTypeDeclsTableInfo {
public:
using internal_key_type = StringRef;
using external_key_type = Identifier;
using data_type = SmallVector<std::pair<DeclID, DeclID>, 4>;
using hash_value_type = uint32_t;
using offset_type = unsigned;
internal_key_type GetInternalKey(external_key_type ID) {
return ID.str();
}
hash_value_type ComputeHash(internal_key_type key) {
return llvm::djbHash(key, serialization::SWIFTMODULE_HASH_SEED);
}
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
return lhs == rhs;
}
static std::pair<unsigned, unsigned> ReadKeyDataLength(const uint8_t *&data) {
using namespace llvm::support;
unsigned keyLength = endian::readNext<uint16_t, little, unaligned>(data);
unsigned dataLength = endian::readNext<uint16_t, little, unaligned>(data);
return { keyLength, dataLength };
}
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
return StringRef(reinterpret_cast<const char *>(data), length);
}
static data_type ReadData(internal_key_type key, const uint8_t *data,
unsigned length) {
using namespace llvm::support;
data_type result;
while (length > 0) {
DeclID parentID = endian::readNext<uint32_t, little, unaligned>(data);
DeclID childID = endian::readNext<uint32_t, little, unaligned>(data);
result.push_back({ parentID, childID });
length -= sizeof(uint32_t) * 2;
}
return result;
}
};
// Indexing the members of all Decls (well, NominalTypeDecls anyway) is
// accomplished by a 2-level hashtable scheme. The outer table here maps from
// DeclBaseNames to serialization::BitOffsets of sub-tables. The sub-tables --
// SerializedDeclMembersTables, one table per name -- map from the enclosing
// (NominalTypeDecl) DeclID to a vector of DeclIDs of members of the nominal
// with the name. See DeclMembersTableInfo below.
class ModuleFileSharedCore::DeclMemberNamesTableInfo {
public:
using internal_key_type = std::pair<DeclBaseName::Kind, StringRef>;
using external_key_type = DeclBaseName;
using data_type = serialization::BitOffset;
using hash_value_type = uint32_t;
using offset_type = unsigned;
internal_key_type GetInternalKey(external_key_type ID) {
if (ID.getKind() == DeclBaseName::Kind::Normal) {
return {DeclBaseName::Kind::Normal, ID.getIdentifier().str()};
} else {
return {ID.getKind(), StringRef()};
}
}
hash_value_type ComputeHash(internal_key_type key) {
if (key.first == DeclBaseName::Kind::Normal) {
return llvm::djbHash(key.second, serialization::SWIFTMODULE_HASH_SEED);
} else {
return (hash_value_type)key.first;
}
}
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
return lhs == rhs;
}
static std::pair<unsigned, unsigned> ReadKeyDataLength(const uint8_t *&data) {
using namespace llvm::support;
unsigned keyLength = endian::readNext<uint16_t, little, unaligned>(data);
return { keyLength, sizeof(uint32_t) };
}
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
using namespace swift::serialization;
using namespace llvm::support;
uint8_t kind = endian::readNext<uint8_t, little, unaligned>(data);
switch (kind) {
case static_cast<uint8_t>(DeclNameKind::Normal): {
StringRef str(reinterpret_cast<const char *>(data),
length - sizeof(uint8_t));
return {DeclBaseName::Kind::Normal, str};
}
case static_cast<uint8_t>(DeclNameKind::Subscript):
return {DeclBaseName::Kind::Subscript, StringRef()};
case static_cast<uint8_t>(DeclNameKind::Destructor):
return {DeclBaseName::Kind::Destructor, StringRef()};
case static_cast<uint8_t>(DeclNameKind::Constructor):
return {DeclBaseName::Kind::Constructor, StringRef()};
default:
llvm_unreachable("Unknown DeclNameKind");
}
}
static data_type ReadData(internal_key_type key, const uint8_t *data,
unsigned length) {
using namespace llvm::support;
assert(length == sizeof(uint32_t));
return endian::readNext<uint32_t, little, unaligned>(data);
}
};
// Second half of the 2-level member name lookup scheme, see
// DeclMemberNamesTableInfo above. There is one of these tables for each member
// DeclBaseName N in the module, and it maps from enclosing DeclIDs (say: each
// NominalTypeDecl T that has members named N) to the set of N-named members of
// T. In other words, there are no names in this table: the names are one level
// up, this table just maps { Owner-DeclID => [Member-DeclID, ...] }.
class ModuleFileSharedCore::DeclMembersTableInfo {
public:
using internal_key_type = uint32_t;
using external_key_type = DeclID;
using data_type = SmallVector<DeclID, 2>;
using hash_value_type = uint32_t;
using offset_type = unsigned;
internal_key_type GetInternalKey(external_key_type ID) {
return ID;
}
hash_value_type ComputeHash(internal_key_type key) {
return llvm::hash_value(key);
}
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
return lhs == rhs;
}
static std::pair<unsigned, unsigned> ReadKeyDataLength(const uint8_t *&data) {
using namespace llvm::support;
unsigned dataLength = endian::readNext<uint16_t, little, unaligned>(data);
return { sizeof(uint32_t), dataLength };
}
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
using namespace llvm::support;
return endian::readNext<uint32_t, little, unaligned>(data);
}
static data_type ReadData(internal_key_type key, const uint8_t *data,
unsigned length) {
using namespace llvm::support;
data_type result;
while (length > 0) {
DeclID declID = endian::readNext<uint32_t, little, unaligned>(data);
result.push_back(declID);
length -= sizeof(uint32_t);
}
return result;
}
};
/// Used to deserialize entries in the on-disk Objective-C method table.
class ModuleFileSharedCore::ObjCMethodTableInfo {
public:
using internal_key_type = std::string;
using external_key_type = ObjCSelector;
using data_type = SmallVector<std::tuple<std::string, bool, DeclID>, 8>;
using hash_value_type = uint32_t;
using offset_type = unsigned;
internal_key_type GetInternalKey(external_key_type ID) {
llvm::SmallString<32> scratch;
return ID.getString(scratch).str();
}
hash_value_type ComputeHash(internal_key_type key) {
return llvm::djbHash(key, serialization::SWIFTMODULE_HASH_SEED);
}
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
return lhs == rhs;
}
static std::pair<unsigned, unsigned> ReadKeyDataLength(const uint8_t *&data) {
using namespace llvm::support;
unsigned keyLength = endian::readNext<uint16_t, little, unaligned>(data);
unsigned dataLength = endian::readNext<uint32_t, little, unaligned>(data);
return { keyLength, dataLength };
}
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
return std::string(reinterpret_cast<const char *>(data), length);
}
static data_type ReadData(internal_key_type key, const uint8_t *data,
unsigned length) {
using namespace llvm::support;
const constexpr auto recordSize = sizeof(uint32_t) + 1 + sizeof(uint32_t);
data_type result;
while (length > 0) {
unsigned ownerLen = endian::readNext<uint32_t, little, unaligned>(data);
bool isInstanceMethod = *data++ != 0;
DeclID methodID = endian::readNext<uint32_t, little, unaligned>(data);
std::string ownerName((const char *)data, ownerLen);
result.push_back(
std::make_tuple(std::move(ownerName), isInstanceMethod, methodID));
data += ownerLen;
length -= (recordSize + ownerLen);
}
return result;
}
};
/// Used to deserialize entries in the on-disk derivative function configuration
/// table.
class ModuleFileSharedCore::DerivativeFunctionConfigTableInfo {
public:
using internal_key_type = StringRef;
using external_key_type = internal_key_type;
using data_type =
SmallVector<std::pair<std::string, serialization::GenericSignatureID>, 8>;
using hash_value_type = uint32_t;
using offset_type = unsigned;
external_key_type GetExternalKey(internal_key_type ID) { return ID; }
internal_key_type GetInternalKey(external_key_type ID) { return ID; }
hash_value_type ComputeHash(internal_key_type key) {
return llvm::djbHash(key, serialization::SWIFTMODULE_HASH_SEED);
}
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
return lhs == rhs;
}
static std::pair<unsigned, unsigned> ReadKeyDataLength(const uint8_t *&data) {
using namespace llvm::support;
unsigned keyLength = endian::readNext<uint16_t, little, unaligned>(data);
unsigned dataLength = endian::readNext<uint16_t, little, unaligned>(data);
return {keyLength, dataLength};
}
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
return StringRef(reinterpret_cast<const char *>(data), length);
}
static data_type ReadData(internal_key_type key, const uint8_t *data,
unsigned length) {
using namespace llvm::support;
data_type result;
const uint8_t *limit = data + length;
while (data < limit) {
DeclID genSigId = endian::readNext<uint32_t, little, unaligned>(data);
int32_t nameLength = endian::readNext<int32_t, little, unaligned>(data);
std::string mangledName(reinterpret_cast<const char *>(data), nameLength);
data += nameLength;
result.push_back({std::string(mangledName), genSigId});
}
return result;
}
};
struct ModuleFileSharedCore::DeserializedCommentInfo {
SmallVector<SingleRawComment, 2> RawCommentStore;
CommentInfo Info;
};
class ModuleFileSharedCore::DeclCommentTableInfo {
public:
using internal_key_type = StringRef;
using external_key_type = StringRef;
using data_type = std::unique_ptr<DeserializedCommentInfo>;
using hash_value_type = uint32_t;
using offset_type = unsigned;
internal_key_type GetInternalKey(external_key_type key) {
return key;
}
hash_value_type ComputeHash(internal_key_type key) {
assert(!key.empty());
return llvm::djbHash(key, serialization::SWIFTDOC_HASH_SEED_5_1);
}
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
return lhs == rhs;
}
static std::pair<unsigned, unsigned> ReadKeyDataLength(const uint8_t *&data) {
using namespace llvm::support;
unsigned keyLength = endian::readNext<uint32_t, little, unaligned>(data);
unsigned dataLength = endian::readNext<uint32_t, little, unaligned>(data);
return { keyLength, dataLength };
}
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
return StringRef(reinterpret_cast<const char *>(data), length);
}
data_type ReadData(internal_key_type key, const uint8_t *data,
unsigned length) {
using namespace llvm::support;
data_type result = std::make_unique<DeserializedCommentInfo>();
{
unsigned BriefSize = endian::readNext<uint32_t, little, unaligned>(data);
result->Info.Brief = StringRef(reinterpret_cast<const char *>(data), BriefSize);
data += BriefSize;
}
unsigned NumComments = endian::readNext<uint32_t, little, unaligned>(data);
for (unsigned i = 0; i != NumComments; ++i) {
unsigned StartColumn =
endian::readNext<uint32_t, little, unaligned>(data);
unsigned RawSize = endian::readNext<uint32_t, little, unaligned>(data);
auto RawText = StringRef(reinterpret_cast<const char *>(data), RawSize);
data += RawSize;
result->RawCommentStore.emplace_back(RawText, StartColumn);
}
result->Info.Raw = RawComment(result->RawCommentStore);
result->Info.Group = endian::readNext<uint32_t, little, unaligned>(data);
result->Info.SourceOrder = endian::readNext<uint32_t, little, unaligned>(data);
return result;
}
};
class ModuleFileSharedCore::DeclUSRTableInfo {
public:
using internal_key_type = StringRef;
using external_key_type = StringRef;
using data_type = uint32_t;
using hash_value_type = uint32_t;
using offset_type = unsigned;
internal_key_type GetInternalKey(external_key_type key) { return key; }
hash_value_type ComputeHash(internal_key_type key) {
assert(!key.empty());
return llvm::djbHash(key, serialization::SWIFTSOURCEINFO_HASH_SEED);
}
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
return lhs == rhs;
}
static std::pair<unsigned, unsigned> ReadKeyDataLength(const uint8_t *&data) {
using namespace llvm::support;
unsigned keyLength = endian::readNext<uint32_t, little, unaligned>(data);
unsigned dataLength = 4;
return { keyLength, dataLength };
}
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
return StringRef(reinterpret_cast<const char*>(data), length);
}
data_type ReadData(internal_key_type key, const uint8_t *data, unsigned length) {
using namespace llvm::support;
assert(length == 4);
return endian::readNext<uint32_t, little, unaligned>(data);
}
};
/// Serialization for the table mapping module-level declIDs for serialized
/// iterable decl contexts to their corresponding \c Fingerprint values.
class ModuleFileSharedCore::DeclFingerprintsTableInfo {
public:
using internal_key_type = uint32_t;
using external_key_type = DeclID;
using data_type = swift::Fingerprint;
using hash_value_type = uint32_t;
using offset_type = unsigned;
internal_key_type GetInternalKey(external_key_type ID) { return ID; }
hash_value_type ComputeHash(internal_key_type key) {
return llvm::hash_value(key);
}
static bool EqualKey(internal_key_type lhs, internal_key_type rhs) {
return lhs == rhs;
}
static std::pair<unsigned, unsigned> ReadKeyDataLength(const uint8_t *&data) {
using namespace llvm::support;
const unsigned dataLen = Fingerprint::DIGEST_LENGTH;
return {sizeof(uint32_t), dataLen};
}
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
using namespace llvm::support;
return endian::readNext<uint32_t, little, unaligned>(data);
}
static data_type ReadData(internal_key_type key, const uint8_t *data,
unsigned length) {
using namespace llvm::support;
auto str = llvm::StringRef{reinterpret_cast<const char *>(data),
Fingerprint::DIGEST_LENGTH};
if (auto fp = Fingerprint::fromString(str))
return fp.value();
llvm::errs() << "Unconvertable fingerprint '" << str << "'\n";
abort();
}
};
} // end namespace swift
#endif