summaryrefslogtreecommitdiffstats
path: root/lib/Serialization/ASTCommon.cpp
blob: ca826d83d471658c21b71eb727609e94563de832 (plain)
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
//===--- ASTCommon.cpp - Common stuff for ASTReader/ASTWriter----*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//  This file defines common functions that both ASTReader and ASTWriter use.
//
//===----------------------------------------------------------------------===//

#include "ASTCommon.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Serialization/ASTDeserializationListener.h"
#include "llvm/Support/DJB.h"

using namespace clang;

// Give ASTDeserializationListener's VTable a home.
ASTDeserializationListener::~ASTDeserializationListener() { }

serialization::TypeIdx
serialization::TypeIdxFromBuiltin(const BuiltinType *BT) {
  unsigned ID = 0;
  switch (BT->getKind()) {
  case BuiltinType::Void:
    ID = PREDEF_TYPE_VOID_ID;
    break;
  case BuiltinType::Bool:
    ID = PREDEF_TYPE_BOOL_ID;
    break;
  case BuiltinType::Char_U:
    ID = PREDEF_TYPE_CHAR_U_ID;
    break;
  case BuiltinType::UChar:
    ID = PREDEF_TYPE_UCHAR_ID;
    break;
  case BuiltinType::UShort:
    ID = PREDEF_TYPE_USHORT_ID;
    break;
  case BuiltinType::UInt:
    ID = PREDEF_TYPE_UINT_ID;
    break;
  case BuiltinType::ULong:
    ID = PREDEF_TYPE_ULONG_ID;
    break;
  case BuiltinType::ULongLong:
    ID = PREDEF_TYPE_ULONGLONG_ID;
    break;
  case BuiltinType::UInt128:
    ID = PREDEF_TYPE_UINT128_ID;
    break;
  case BuiltinType::Char_S:
    ID = PREDEF_TYPE_CHAR_S_ID;
    break;
  case BuiltinType::SChar:
    ID = PREDEF_TYPE_SCHAR_ID;
    break;
  case BuiltinType::WChar_S:
  case BuiltinType::WChar_U:
    ID = PREDEF_TYPE_WCHAR_ID;
    break;
  case BuiltinType::Short:
    ID = PREDEF_TYPE_SHORT_ID;
    break;
  case BuiltinType::Int:
    ID = PREDEF_TYPE_INT_ID;
    break;
  case BuiltinType::Long:
    ID = PREDEF_TYPE_LONG_ID;
    break;
  case BuiltinType::LongLong:
    ID = PREDEF_TYPE_LONGLONG_ID;
    break;
  case BuiltinType::Int128:
    ID = PREDEF_TYPE_INT128_ID;
    break;
  case BuiltinType::Half:
    ID = PREDEF_TYPE_HALF_ID;
    break;
  case BuiltinType::Float:
    ID = PREDEF_TYPE_FLOAT_ID;
    break;
  case BuiltinType::Double:
    ID = PREDEF_TYPE_DOUBLE_ID;
    break;
  case BuiltinType::LongDouble:
    ID = PREDEF_TYPE_LONGDOUBLE_ID;
    break;
  case BuiltinType::ShortAccum:
    ID = PREDEF_TYPE_SHORT_ACCUM_ID;
    break;
  case BuiltinType::Accum:
    ID = PREDEF_TYPE_ACCUM_ID;
    break;
  case BuiltinType::LongAccum:
    ID = PREDEF_TYPE_LONG_ACCUM_ID;
    break;
  case BuiltinType::UShortAccum:
    ID = PREDEF_TYPE_USHORT_ACCUM_ID;
    break;
  case BuiltinType::UAccum:
    ID = PREDEF_TYPE_UACCUM_ID;
    break;
  case BuiltinType::ULongAccum:
    ID = PREDEF_TYPE_ULONG_ACCUM_ID;
    break;
  case BuiltinType::ShortFract:
    ID = PREDEF_TYPE_SHORT_FRACT_ID;
    break;
  case BuiltinType::Fract:
    ID = PREDEF_TYPE_FRACT_ID;
    break;
  case BuiltinType::LongFract:
    ID = PREDEF_TYPE_LONG_FRACT_ID;
    break;
  case BuiltinType::UShortFract:
    ID = PREDEF_TYPE_USHORT_FRACT_ID;
    break;
  case BuiltinType::UFract:
    ID = PREDEF_TYPE_UFRACT_ID;
    break;
  case BuiltinType::ULongFract:
    ID = PREDEF_TYPE_ULONG_FRACT_ID;
    break;
  case BuiltinType::SatShortAccum:
    ID = PREDEF_TYPE_SAT_SHORT_ACCUM_ID;
    break;
  case BuiltinType::SatAccum:
    ID = PREDEF_TYPE_SAT_ACCUM_ID;
    break;
  case BuiltinType::SatLongAccum:
    ID = PREDEF_TYPE_SAT_LONG_ACCUM_ID;
    break;
  case BuiltinType::SatUShortAccum:
    ID = PREDEF_TYPE_SAT_USHORT_ACCUM_ID;
    break;
  case BuiltinType::SatUAccum:
    ID = PREDEF_TYPE_SAT_UACCUM_ID;
    break;
  case BuiltinType::SatULongAccum:
    ID = PREDEF_TYPE_SAT_ULONG_ACCUM_ID;
    break;
  case BuiltinType::SatShortFract:
    ID = PREDEF_TYPE_SAT_SHORT_FRACT_ID;
    break;
  case BuiltinType::SatFract:
    ID = PREDEF_TYPE_SAT_FRACT_ID;
    break;
  case BuiltinType::SatLongFract:
    ID = PREDEF_TYPE_SAT_LONG_FRACT_ID;
    break;
  case BuiltinType::SatUShortFract:
    ID = PREDEF_TYPE_SAT_USHORT_FRACT_ID;
    break;
  case BuiltinType::SatUFract:
    ID = PREDEF_TYPE_SAT_UFRACT_ID;
    break;
  case BuiltinType::SatULongFract:
    ID = PREDEF_TYPE_SAT_ULONG_FRACT_ID;
    break;
  case BuiltinType::Float16:
    ID = PREDEF_TYPE_FLOAT16_ID;
    break;
  case BuiltinType::Float128:
    ID = PREDEF_TYPE_FLOAT128_ID;
    break;
  case BuiltinType::NullPtr:
    ID = PREDEF_TYPE_NULLPTR_ID;
    break;
  case BuiltinType::Char8:
    ID = PREDEF_TYPE_CHAR8_ID;
    break;
  case BuiltinType::Char16:
    ID = PREDEF_TYPE_CHAR16_ID;
    break;
  case BuiltinType::Char32:
    ID = PREDEF_TYPE_CHAR32_ID;
    break;
  case BuiltinType::Overload:
    ID = PREDEF_TYPE_OVERLOAD_ID;
    break;
  case BuiltinType::BoundMember:
    ID = PREDEF_TYPE_BOUND_MEMBER;
    break;
  case BuiltinType::PseudoObject:
    ID = PREDEF_TYPE_PSEUDO_OBJECT;
    break;
  case BuiltinType::Dependent:
    ID = PREDEF_TYPE_DEPENDENT_ID;
    break;
  case BuiltinType::UnknownAny:
    ID = PREDEF_TYPE_UNKNOWN_ANY;
    break;
  case BuiltinType::ARCUnbridgedCast:
    ID = PREDEF_TYPE_ARC_UNBRIDGED_CAST;
    break;
  case BuiltinType::ObjCId:
    ID = PREDEF_TYPE_OBJC_ID;
    break;
  case BuiltinType::ObjCClass:
    ID = PREDEF_TYPE_OBJC_CLASS;
    break;
  case BuiltinType::ObjCSel:
    ID = PREDEF_TYPE_OBJC_SEL;
    break;
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
  case BuiltinType::Id: \
    ID = PREDEF_TYPE_##Id##_ID; \
    break;
#include "clang/Basic/OpenCLImageTypes.def"
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
  case BuiltinType::Id: \
    ID = PREDEF_TYPE_##Id##_ID; \
    break;
#include "clang/Basic/OpenCLExtensionTypes.def"
  case BuiltinType::OCLSampler:
    ID = PREDEF_TYPE_SAMPLER_ID;
    break;
  case BuiltinType::OCLEvent:
    ID = PREDEF_TYPE_EVENT_ID;
    break;
  case BuiltinType::OCLClkEvent:
    ID = PREDEF_TYPE_CLK_EVENT_ID;
    break;
  case BuiltinType::OCLQueue:
    ID = PREDEF_TYPE_QUEUE_ID;
    break;
  case BuiltinType::OCLReserveID:
    ID = PREDEF_TYPE_RESERVE_ID_ID;
    break;
  case BuiltinType::BuiltinFn:
    ID = PREDEF_TYPE_BUILTIN_FN;
    break;
  case BuiltinType::OMPArraySection:
    ID = PREDEF_TYPE_OMP_ARRAY_SECTION;
    break;
  }

  return TypeIdx(ID);
}

unsigned serialization::ComputeHash(Selector Sel) {
  unsigned N = Sel.getNumArgs();
  if (N == 0)
    ++N;
  unsigned R = 5381;
  for (unsigned I = 0; I != N; ++I)
    if (IdentifierInfo *II = Sel.getIdentifierInfoForSlot(I))
      R = llvm::djbHash(II->getName(), R);
  return R;
}

const DeclContext *
serialization::getDefinitiveDeclContext(const DeclContext *DC) {
  switch (DC->getDeclKind()) {
  // These entities may have multiple definitions.
  case Decl::TranslationUnit:
  case Decl::ExternCContext:
  case Decl::Namespace:
  case Decl::LinkageSpec:
  case Decl::Export:
    return nullptr;

  // C/C++ tag types can only be defined in one place.
  case Decl::Enum:
  case Decl::Record:
    if (const TagDecl *Def = cast<TagDecl>(DC)->getDefinition())
      return Def;
    return nullptr;

  // FIXME: These can be defined in one place... except special member
  // functions and out-of-line definitions.
  case Decl::CXXRecord:
  case Decl::ClassTemplateSpecialization:
  case Decl::ClassTemplatePartialSpecialization:
    return nullptr;

  // Each function, method, and block declaration is its own DeclContext.
  case Decl::Function:
  case Decl::CXXMethod:
  case Decl::CXXConstructor:
  case Decl::CXXDestructor:
  case Decl::CXXConversion:
  case Decl::ObjCMethod:
  case Decl::Block:
  case Decl::Captured:
    // Objective C categories, category implementations, and class
    // implementations can only be defined in one place.
  case Decl::ObjCCategory:
  case Decl::ObjCCategoryImpl:
  case Decl::ObjCImplementation:
    return DC;

  case Decl::ObjCProtocol:
    if (const ObjCProtocolDecl *Def
          = cast<ObjCProtocolDecl>(DC)->getDefinition())
      return Def;
    return nullptr;

  // FIXME: These are defined in one place, but properties in class extensions
  // end up being back-patched into the main interface. See
  // Sema::HandlePropertyInClassExtension for the offending code.
  case Decl::ObjCInterface:
    return nullptr;

  default:
    llvm_unreachable("Unhandled DeclContext in AST reader");
  }

  llvm_unreachable("Unhandled decl kind");
}

bool serialization::isRedeclarableDeclKind(unsigned Kind) {
  switch (static_cast<Decl::Kind>(Kind)) {
  case Decl::TranslationUnit:
  case Decl::ExternCContext:
    // Special case of a "merged" declaration.
    return true;

  case Decl::Namespace:
  case Decl::NamespaceAlias:
  case Decl::Typedef:
  case Decl::TypeAlias:
  case Decl::Enum:
  case Decl::Record:
  case Decl::CXXRecord:
  case Decl::ClassTemplateSpecialization:
  case Decl::ClassTemplatePartialSpecialization:
  case Decl::VarTemplateSpecialization:
  case Decl::VarTemplatePartialSpecialization:
  case Decl::Function:
  case Decl::CXXDeductionGuide:
  case Decl::CXXMethod:
  case Decl::CXXConstructor:
  case Decl::CXXDestructor:
  case Decl::CXXConversion:
  case Decl::UsingShadow:
  case Decl::ConstructorUsingShadow:
  case Decl::Var:
  case Decl::FunctionTemplate:
  case Decl::ClassTemplate:
  case Decl::VarTemplate:
  case Decl::TypeAliasTemplate:
  case Decl::ObjCProtocol:
  case Decl::ObjCInterface:
  case Decl::Empty:
    return true;

  // Never redeclarable.
  case Decl::UsingDirective:
  case Decl::Label:
  case Decl::UnresolvedUsingTypename:
  case Decl::TemplateTypeParm:
  case Decl::EnumConstant:
  case Decl::UnresolvedUsingValue:
  case Decl::IndirectField:
  case Decl::Field:
  case Decl::MSProperty:
  case Decl::ObjCIvar:
  case Decl::ObjCAtDefsField:
  case Decl::NonTypeTemplateParm:
  case Decl::TemplateTemplateParm:
  case Decl::Using:
  case Decl::UsingPack:
  case Decl::ObjCMethod:
  case Decl::ObjCCategory:
  case Decl::ObjCCategoryImpl:
  case Decl::ObjCImplementation:
  case Decl::ObjCProperty:
  case Decl::ObjCCompatibleAlias:
  case Decl::LinkageSpec:
  case Decl::Export:
  case Decl::ObjCPropertyImpl:
  case Decl::PragmaComment:
  case Decl::PragmaDetectMismatch:
  case Decl::FileScopeAsm:
  case Decl::AccessSpec:
  case Decl::Friend:
  case Decl::FriendTemplate:
  case Decl::StaticAssert:
  case Decl::Block:
  case Decl::Captured:
  case Decl::ClassScopeFunctionSpecialization:
  case Decl::Import:
  case Decl::OMPThreadPrivate:
  case Decl::OMPRequires:
  case Decl::OMPCapturedExpr:
  case Decl::OMPDeclareReduction:
  case Decl::BuiltinTemplate:
  case Decl::Decomposition:
  case Decl::Binding:
    return false;

  // These indirectly derive from Redeclarable<T> but are not actually
  // redeclarable.
  case Decl::ImplicitParam:
  case Decl::ParmVar:
  case Decl::ObjCTypeParam:
    return false;
  }

  llvm_unreachable("Unhandled declaration kind");
}

bool serialization::needsAnonymousDeclarationNumber(const NamedDecl *D) {
  // Friend declarations in dependent contexts aren't anonymous in the usual
  // sense, but they cannot be found by name lookup in their semantic context
  // (or indeed in any context), so we treat them as anonymous.
  //
  // This doesn't apply to friend tag decls; Sema makes those available to name
  // lookup in the surrounding context.
  if (D->getFriendObjectKind() &&
      D->getLexicalDeclContext()->isDependentContext() && !isa<TagDecl>(D)) {
    // For function templates and class templates, the template is numbered and
    // not its pattern.
    if (auto *FD = dyn_cast<FunctionDecl>(D))
      return !FD->getDescribedFunctionTemplate();
    if (auto *RD = dyn_cast<CXXRecordDecl>(D))
      return !RD->getDescribedClassTemplate();
    return true;
  }

  // At block scope, we number everything that we need to deduplicate, since we
  // can't just use name matching to keep things lined up.
  // FIXME: This is only necessary for an inline function or a template or
  // similar.
  if (D->getLexicalDeclContext()->isFunctionOrMethod()) {
    if (auto *VD = dyn_cast<VarDecl>(D))
      return VD->isStaticLocal();
    // FIXME: What about CapturedDecls (and declarations nested within them)?
    return isa<TagDecl>(D) || isa<BlockDecl>(D);
  }

  // Otherwise, we only care about anonymous class members / block-scope decls.
  // FIXME: We need to handle lambdas and blocks within inline / templated
  // variables too.
  if (D->getDeclName() || !isa<CXXRecordDecl>(D->getLexicalDeclContext()))
    return false;
  return isa<TagDecl>(D) || isa<FieldDecl>(D);
}