@@ -80,7 +80,7 @@ SwiftLookupTable::translateContext(clang::DeclContext *context) {
80
80
return None;
81
81
}
82
82
83
- void SwiftLookupTable::addEntry (DeclName name, clang::NamedDecl *decl ,
83
+ void SwiftLookupTable::addEntry (DeclName name, SingleEntry newEntry ,
84
84
clang::DeclContext *effectiveContext) {
85
85
assert (!Reader && " Cannot modify a lookup table stored on disk" );
86
86
@@ -91,32 +91,42 @@ void SwiftLookupTable::addEntry(DeclName name, clang::NamedDecl *decl,
91
91
92
92
// Find the list of entries for this base name.
93
93
auto &entries = LookupTable[name.getBaseName ().str ()];
94
+ auto decl = newEntry.dyn_cast <clang::NamedDecl *>();
95
+ auto macro = newEntry.dyn_cast <clang::MacroInfo *>();
94
96
for (auto &entry : entries) {
95
97
if (entry.Context == context) {
96
98
// We have entries for this context.
97
99
98
100
// Check whether this entry matches any existing entry.
99
- for (auto &existingEntry : entry.Decls ) {
100
- if (matchesExistingDecl (decl, mapStoredDecl (existingEntry))) return ;
101
+ for (auto &existingEntry : entry.DeclsOrMacros ) {
102
+ if (decl && isDeclEntry (existingEntry) &&
103
+ matchesExistingDecl (decl, mapStoredDecl (existingEntry)))
104
+ return ;
101
105
}
102
106
103
107
// Add an entry to this context.
104
- entry.Decls .push_back (reinterpret_cast <uintptr_t >(decl));
108
+ if (decl)
109
+ entry.DeclsOrMacros .push_back (encodeEntry (decl));
110
+ else
111
+ entry.DeclsOrMacros .push_back (encodeEntry (macro));
105
112
return ;
106
113
}
107
114
}
108
115
109
116
// This is a new context for this name. Add it.
110
- FullTableEntry newEntry;
111
- newEntry.Context = context;;
112
- newEntry.Decls .push_back (reinterpret_cast <uintptr_t >(decl));
113
- entries.push_back (newEntry);
117
+ FullTableEntry entry;
118
+ entry.Context = context;
119
+ if (decl)
120
+ entry.DeclsOrMacros .push_back (encodeEntry (decl));
121
+ else
122
+ entry.DeclsOrMacros .push_back (encodeEntry (macro));
123
+ entries.push_back (entry);
114
124
}
115
125
116
- SmallVector<clang::NamedDecl * , 4 >
126
+ SmallVector<SwiftLookupTable::SingleEntry , 4 >
117
127
SwiftLookupTable::lookup (StringRef baseName,
118
128
clang::DeclContext *searchContext) {
119
- SmallVector<clang::NamedDecl * , 4 > result;
129
+ SmallVector<SwiftLookupTable::SingleEntry , 4 > result;
120
130
121
131
if (baseName.empty ()) return result;
122
132
@@ -147,8 +157,11 @@ SwiftLookupTable::lookup(StringRef baseName,
147
157
if (context && *context != entry.Context ) continue ;
148
158
149
159
// Map each of the declarations.
150
- for (auto &storedDecl : entry.Decls ) {
151
- result.push_back (mapStoredDecl (storedDecl));
160
+ for (auto &stored : entry.DeclsOrMacros ) {
161
+ if (isDeclEntry (stored))
162
+ result.push_back (mapStoredDecl (stored));
163
+ else
164
+ result.push_back (mapStoredMacro (stored));
152
165
}
153
166
}
154
167
@@ -239,13 +252,17 @@ void SwiftLookupTable::dump() const {
239
252
}
240
253
llvm::errs () << " : " ;
241
254
242
- interleave (entry.Decls .begin (), entry.Decls .end (),
243
- [](uint64_t entry) {
244
- if ((entry & 0x01 ) == 0 ) {
245
- auto decl = reinterpret_cast <clang::NamedDecl *>(entry);
246
- printName (decl, llvm::errs ());
255
+ interleave (entry.DeclsOrMacros .begin (), entry.DeclsOrMacros .end (),
256
+ [this ](uintptr_t entry) {
257
+ if (isSerializationIDEntry (entry)) {
258
+ llvm::errs () << (isMacroEntry (entry) ? " macro" : " decl" )
259
+ << " ID #" << getSerializationID (entry);
260
+ } else if (isMacroEntry (entry)) {
261
+ llvm::errs () << " Macro" ;
247
262
} else {
248
- llvm::errs () << " ID #" << (entry >> 1 );
263
+ auto decl = const_cast <SwiftLookupTable *>(this )
264
+ ->mapStoredDecl (entry);
265
+ printName (decl, llvm::errs ());
249
266
}
250
267
},
251
268
[] {
@@ -323,7 +340,8 @@ namespace {
323
340
dataLength += sizeof (uint16_t );
324
341
325
342
// Actual entries.
326
- dataLength += sizeof (clang::serialization::DeclID) * entry.Decls .size ();
343
+ dataLength += (sizeof (clang::serialization::DeclID) *
344
+ entry.DeclsOrMacros .size ());
327
345
}
328
346
329
347
endian::Writer<little> writer (out);
@@ -343,21 +361,28 @@ namespace {
343
361
// # of entries
344
362
writer.write <uint16_t >(data.size ());
345
363
346
- for (auto &entry : data) {
364
+ for (auto &fullEntry : data) {
347
365
// Context.
348
- writer.write <uint8_t >(static_cast <uint8_t >(entry .Context .first ));
349
- if (SwiftLookupTable::contextRequiresName (entry .Context .first )) {
350
- writer.write <uint16_t >(entry .Context .second .size ());
351
- out << entry .Context .second ;
366
+ writer.write <uint8_t >(static_cast <uint8_t >(fullEntry .Context .first ));
367
+ if (SwiftLookupTable::contextRequiresName (fullEntry .Context .first )) {
368
+ writer.write <uint16_t >(fullEntry .Context .second .size ());
369
+ out << fullEntry .Context .second ;
352
370
}
353
371
354
372
// # of entries.
355
- writer.write <uint16_t >(entry.Decls .size ());
356
-
357
- // Write the declarations.
358
- for (auto &declEntry : entry.Decls ) {
359
- auto decl = Table.mapStoredDecl (declEntry);
360
- writer.write <clang::serialization::DeclID>(Writer.getDeclID (decl));
373
+ writer.write <uint16_t >(fullEntry.DeclsOrMacros .size ());
374
+
375
+ // Write the declarations and macros.
376
+ for (auto &entry : fullEntry.DeclsOrMacros ) {
377
+ uint32_t id;
378
+ if (SwiftLookupTable::isDeclEntry (entry)) {
379
+ auto decl = Table.mapStoredDecl (entry);
380
+ id = (Writer.getDeclID (decl) << 2 ) | 0x02 ;
381
+ } else {
382
+ auto macro = Table.mapStoredMacro (entry);
383
+ id = (Writer.getMacroID (macro) << 2 ) | 0x02 | 0x01 ;
384
+ }
385
+ writer.write <uint32_t >(id);
361
386
}
362
387
}
363
388
}
@@ -460,12 +485,12 @@ namespace {
460
485
data += length;
461
486
}
462
487
463
- // Read the declarations.
464
- unsigned numDecls = endian::readNext< uint16_t , little, unaligned>(data);
465
- while (numDecls--) {
466
- auto declID = endian::readNext<clang::serialization::DeclID, little,
467
- unaligned>(data);
468
- entry.Decls .push_back ((declID << 1 ) | 0x01 );
488
+ // Read the declarations and macros .
489
+ unsigned numDeclsOrMacros =
490
+ endian::readNext< uint16_t , little, unaligned>(data);
491
+ while (numDeclsOrMacros--) {
492
+ auto id = endian::readNext< uint32_t , little, unaligned>(data);
493
+ entry.DeclsOrMacros .push_back (id );
469
494
}
470
495
471
496
result.push_back (entry);
@@ -482,24 +507,48 @@ namespace swift {
482
507
llvm::OnDiskIterableChainedHashTable<BaseNameToEntitiesTableReaderInfo>;
483
508
}
484
509
485
- clang::NamedDecl *SwiftLookupTable::mapStoredDecl (uint64_t &entry) {
486
- // If the low bit is unset, we have a pointer. Just cast.
487
- if ((entry & 0x01 ) == 0 ) {
488
- return reinterpret_cast <clang::NamedDecl *>(static_cast <uintptr_t >(entry));
510
+ clang::NamedDecl *SwiftLookupTable::mapStoredDecl (uintptr_t &entry) {
511
+ assert (isDeclEntry (entry) && " Not a declaration entry" );
512
+
513
+ // If we have an AST node here, just cast it.
514
+ if (isASTNodeEntry (entry)) {
515
+ return static_cast <clang::NamedDecl *>(getPointerFromEntry (entry));
489
516
}
490
517
491
518
// Otherwise, resolve the declaration.
492
519
assert (Reader && " Cannot resolve the declaration without a reader" );
493
- clang::serialization::DeclID declID = entry >> 1 ;
520
+ clang::serialization::DeclID declID = getSerializationID ( entry) ;
494
521
auto decl = cast_or_null<clang::NamedDecl>(
495
522
Reader->getASTReader ().GetLocalDecl (Reader->getModuleFile (),
496
523
declID));
497
524
498
525
// Update the entry now that we've resolved the declaration.
499
- entry = reinterpret_cast < uintptr_t > (decl);
526
+ entry = encodeEntry (decl);
500
527
return decl;
501
528
}
502
529
530
+ clang::MacroInfo *SwiftLookupTable::mapStoredMacro (uintptr_t &entry) {
531
+ assert (isMacroEntry (entry) && " Not a macro entry" );
532
+
533
+ // If we have an AST node here, just cast it.
534
+ if (isASTNodeEntry (entry)) {
535
+ return static_cast <clang::MacroInfo *>(getPointerFromEntry (entry));
536
+ }
537
+
538
+ // Otherwise, resolve the macro.
539
+ assert (Reader && " Cannot resolve the macro without a reader" );
540
+ clang::serialization::MacroID macroID = getSerializationID (entry);
541
+ auto macro = cast_or_null<clang::MacroInfo>(
542
+ Reader->getASTReader ().getMacro (
543
+ Reader->getASTReader ().getGlobalMacroID (
544
+ Reader->getModuleFile (),
545
+ macroID)));
546
+
547
+ // Update the entry now that we've resolved the macro.
548
+ entry = encodeEntry (macro);
549
+ return macro;
550
+ }
551
+
503
552
SwiftLookupTableReader::~SwiftLookupTableReader () {
504
553
OnRemove ();
505
554
delete static_cast <SerializedBaseNameToEntitiesTable *>(SerializedTable);
0 commit comments