Coverage Report

Created: 2020-09-22 08:39

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/tools/libclang/CXType.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CXType.cpp - Implements 'CXTypes' aspect of libclang ---------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===--------------------------------------------------------------------===//
8
//
9
// This file implements the 'CXTypes' API hooks in the Clang-C library.
10
//
11
//===--------------------------------------------------------------------===//
12
13
#include "CIndexer.h"
14
#include "CXCursor.h"
15
#include "CXString.h"
16
#include "CXTranslationUnit.h"
17
#include "CXType.h"
18
#include "clang/AST/Decl.h"
19
#include "clang/AST/DeclObjC.h"
20
#include "clang/AST/DeclTemplate.h"
21
#include "clang/AST/Expr.h"
22
#include "clang/AST/Type.h"
23
#include "clang/Basic/AddressSpaces.h"
24
#include "clang/Frontend/ASTUnit.h"
25
26
using namespace clang;
27
28
1.76k
static CXTypeKind GetBuiltinTypeKind(const BuiltinType *BT) {
29
1.76k
#define BTCASE(K) case BuiltinType::K: return CXType_##K
30
1.76k
  switch (BT->getKind()) {
31
87
    BTCASE(Void);
32
10
    BTCASE(Bool);
33
0
    BTCASE(Char_U);
34
0
    BTCASE(UChar);
35
0
    BTCASE(Char16);
36
0
    BTCASE(Char32);
37
0
    BTCASE(UShort);
38
31
    BTCASE(UInt);
39
3
    BTCASE(ULong);
40
10
    BTCASE(ULongLong);
41
0
    BTCASE(UInt128);
42
47
    BTCASE(Char_S);
43
0
    BTCASE(SChar);
44
0
    case BuiltinType::WChar_S: return CXType_WChar;
45
0
    case BuiltinType::WChar_U: return CXType_WChar;
46
1
    BTCASE(Short);
47
1.27k
    BTCASE(Int);
48
18
    BTCASE(Long);
49
11
    BTCASE(LongLong);
50
0
    BTCASE(Int128);
51
4
    BTCASE(Half);
52
38
    BTCASE(Float);
53
28
    BTCASE(Double);
54
0
    BTCASE(LongDouble);
55
0
    BTCASE(ShortAccum);
56
0
    BTCASE(Accum);
57
0
    BTCASE(LongAccum);
58
0
    BTCASE(UShortAccum);
59
0
    BTCASE(UAccum);
60
0
    BTCASE(ULongAccum);
61
0
    BTCASE(Float16);
62
0
    BTCASE(Float128);
63
0
    BTCASE(NullPtr);
64
0
    BTCASE(Overload);
65
9
    BTCASE(Dependent);
66
1
    BTCASE(ObjCId);
67
0
    BTCASE(ObjCClass);
68
0
    BTCASE(ObjCSel);
69
180
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) BTCASE(Id);
70
0
#include "clang/Basic/OpenCLImageTypes.def"
71
0
#undef IMAGE_TYPE
72
2
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) BTCASE(Id);
73
5
#include "clang/Basic/OpenCLExtensionTypes.def"
74
4
    BTCASE(OCLSampler);
75
0
    BTCASE(OCLEvent);
76
2
    BTCASE(OCLQueue);
77
2
    BTCASE(OCLReserveID);
78
2
  default:
79
2
    return CXType_Unexposed;
80
1.76k
  }
81
1.76k
#undef BTCASE
82
1.76k
}
83
84
44.5k
static CXTypeKind GetTypeKind(QualType T) {
85
44.5k
  const Type *TP = T.getTypePtrOrNull();
86
44.5k
  if (!TP)
87
35.9k
    return CXType_Invalid;
88
89
6.28k
#define TKCASE(K) case Type::K: return CXType_##K
90
8.59k
  switch (TP->getTypeClass()) {
91
1.76k
    case Type::Builtin:
92
1.76k
      return GetBuiltinTypeKind(cast<BuiltinType>(TP));
93
0
    TKCASE(Complex);
94
338
    TKCASE(Pointer);
95
4
    TKCASE(BlockPointer);
96
62
    TKCASE(LValueReference);
97
0
    TKCASE(RValueReference);
98
1.67k
    TKCASE(Record);
99
324
    TKCASE(Enum);
100
1.19k
    TKCASE(Typedef);
101
324
    TKCASE(ObjCInterface);
102
5
    TKCASE(ObjCObject);
103
179
    TKCASE(ObjCObjectPointer);
104
28
    TKCASE(ObjCTypeParam);
105
163
    TKCASE(FunctionNoProto);
106
1.68k
    TKCASE(FunctionProto);
107
30
    TKCASE(ConstantArray);
108
21
    TKCASE(IncompleteArray);
109
12
    TKCASE(VariableArray);
110
4
    TKCASE(DependentSizedArray);
111
5
    TKCASE(Vector);
112
9
    TKCASE(ExtVector);
113
5
    TKCASE(MemberPointer);
114
41
    TKCASE(Auto);
115
152
    TKCASE(Elaborated);
116
10
    TKCASE(Pipe);
117
9
    TKCASE(Attributed);
118
12
    TKCASE(Atomic);
119
550
    default:
120
550
      return CXType_Unexposed;
121
8.59k
  }
122
8.59k
#undef TKCASE
123
8.59k
}
124
125
126
44.8k
CXType cxtype::MakeCXType(QualType T, CXTranslationUnit TU) {
127
44.8k
  CXTypeKind TK = CXType_Invalid;
128
129
44.8k
  if (TU && !T.isNull()) {
130
    // Handle attributed types as the original type
131
8.91k
    if (auto *ATT = T->getAs<AttributedType>()) {
132
18
      if (!(TU->ParsingOptions & CXTranslationUnit_IncludeAttributedTypes)) {
133
        // Return the equivalent type which represents the canonically
134
        // equivalent type.
135
9
        return MakeCXType(ATT->getEquivalentType(), TU);
136
9
      }
137
8.90k
    }
138
    // Handle paren types as the original type
139
8.90k
    if (auto *PTT = T->getAs<ParenType>()) {
140
12
      return MakeCXType(PTT->getInnerType(), TU);
141
12
    }
142
143
8.89k
    ASTContext &Ctx = cxtu::getASTUnit(TU)->getASTContext();
144
8.89k
    if (Ctx.getLangOpts().ObjC) {
145
1.57k
      QualType UnqualT = T.getUnqualifiedType();
146
1.57k
      if (Ctx.isObjCIdType(UnqualT))
147
257
        TK = CXType_ObjCId;
148
1.32k
      else if (Ctx.isObjCClassType(UnqualT))
149
9
        TK = CXType_ObjCClass;
150
1.31k
      else if (Ctx.isObjCSelType(UnqualT))
151
6
        TK = CXType_ObjCSel;
152
1.57k
    }
153
154
    /* Handle decayed types as the original type */
155
8.89k
    if (const DecayedType *DT = T->getAs<DecayedType>()) {
156
23
      return MakeCXType(DT->getOriginalType(), TU);
157
23
    }
158
44.8k
  }
159
44.8k
  if (TK == CXType_Invalid)
160
44.5k
    TK = GetTypeKind(T);
161
162
35.9k
  CXType CT = { TK, { TK == CXType_Invalid ? nullptr
163
8.87k
                                           : T.getAsOpaquePtr(), TU } };
164
44.8k
  return CT;
165
44.8k
}
166
167
using cxtype::MakeCXType;
168
169
92.2k
static inline QualType GetQualType(CXType CT) {
170
92.2k
  return QualType::getFromOpaquePtr(CT.data[0]);
171
92.2k
}
172
173
27.4k
static inline CXTranslationUnit GetTU(CXType CT) {
174
27.4k
  return static_cast<CXTranslationUnit>(CT.data[1]);
175
27.4k
}
176
177
static Optional<ArrayRef<TemplateArgument>>
178
654
GetTemplateArguments(QualType Type) {
179
654
  assert(!Type.isNull());
180
654
  if (const auto *Specialization = Type->getAs<TemplateSpecializationType>())
181
53
    return Specialization->template_arguments();
182
183
601
  if (const auto *RecordDecl = Type->getAsCXXRecordDecl()) {
184
98
    const auto *TemplateDecl =
185
98
      dyn_cast<ClassTemplateSpecializationDecl>(RecordDecl);
186
98
    if (TemplateDecl)
187
71
      return TemplateDecl->getTemplateArgs().asArray();
188
530
  }
189
190
530
  return None;
191
530
}
192
193
48
static Optional<QualType> TemplateArgumentToQualType(const TemplateArgument &A) {
194
48
  if (A.getKind() == TemplateArgument::Type)
195
44
    return A.getAsType();
196
4
  return None;
197
4
}
198
199
static Optional<QualType>
200
86
FindTemplateArgumentTypeAt(ArrayRef<TemplateArgument> TA, unsigned index) {
201
86
  unsigned current = 0;
202
105
  for (const auto &A : TA) {
203
105
    if (A.getKind() == TemplateArgument::Pack) {
204
9
      if (index < current + A.pack_size())
205
6
        return TemplateArgumentToQualType(A.getPackAsArray()[index - current]);
206
3
      current += A.pack_size();
207
3
      continue;
208
3
    }
209
96
    if (current == index)
210
42
      return TemplateArgumentToQualType(A);
211
54
    current++;
212
54
  }
213
38
  return None;
214
86
}
215
216
16.4k
CXType clang_getCursorType(CXCursor C) {
217
16.4k
  using namespace cxcursor;
218
219
16.4k
  CXTranslationUnit TU = cxcursor::getCursorTU(C);
220
16.4k
  if (!TU)
221
0
    return MakeCXType(QualType(), TU);
222
223
16.4k
  ASTContext &Context = cxtu::getASTUnit(TU)->getASTContext();
224
16.4k
  if (clang_isExpression(C.kind)) {
225
91
    QualType T = cxcursor::getCursorExpr(C)->getType();
226
91
    return MakeCXType(T, TU);
227
91
  }
228
229
16.3k
  if (clang_isDeclaration(C.kind)) {
230
9.17k
    const Decl *D = cxcursor::getCursorDecl(C);
231
9.17k
    if (!D)
232
0
      return MakeCXType(QualType(), TU);
233
234
9.17k
    if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
235
3.02k
      return MakeCXType(Context.getTypeDeclType(TD), TU);
236
6.15k
    if (const ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(D))
237
261
      return MakeCXType(Context.getObjCInterfaceType(ID), TU);
238
5.89k
    if (const DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D))
239
4.05k
      return MakeCXType(DD->getType(), TU);
240
1.84k
    if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
241
235
      return MakeCXType(VD->getType(), TU);
242
1.60k
    if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
243
139
      return MakeCXType(PD->getType(), TU);
244
1.46k
    if (const FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(D))
245
110
      return MakeCXType(FTD->getTemplatedDecl()->getType(), TU);
246
1.35k
    return MakeCXType(QualType(), TU);
247
1.35k
  }
248
249
7.22k
  if (clang_isReference(C.kind)) {
250
242
    switch (C.kind) {
251
2
    case CXCursor_ObjCSuperClassRef: {
252
2
      QualType T
253
2
        = Context.getObjCInterfaceType(getCursorObjCSuperClassRef(C).first);
254
2
      return MakeCXType(T, TU);
255
0
    }
256
257
14
    case CXCursor_ObjCClassRef: {
258
14
      QualType T = Context.getObjCInterfaceType(getCursorObjCClassRef(C).first);
259
14
      return MakeCXType(T, TU);
260
0
    }
261
262
138
    case CXCursor_TypeRef: {
263
138
      QualType T = Context.getTypeDeclType(getCursorTypeRef(C).first);
264
138
      return MakeCXType(T, TU);
265
266
0
    }
267
268
54
    case CXCursor_CXXBaseSpecifier:
269
54
      return cxtype::MakeCXType(getCursorCXXBaseSpecifier(C)->getType(), TU);
270
271
0
    case CXCursor_MemberRef:
272
0
      return cxtype::MakeCXType(getCursorMemberRef(C).first->getType(), TU);
273
274
0
    case CXCursor_VariableRef:
275
0
      return cxtype::MakeCXType(getCursorVariableRef(C).first->getType(), TU);
276
277
34
    case CXCursor_ObjCProtocolRef:
278
34
    case CXCursor_TemplateRef:
279
34
    case CXCursor_NamespaceRef:
280
34
    case CXCursor_OverloadedDeclRef:
281
34
    default:
282
34
      break;
283
34
    }
284
285
34
    return MakeCXType(QualType(), TU);
286
34
  }
287
288
6.97k
  return MakeCXType(QualType(), TU);
289
6.97k
}
290
291
7.41k
CXString clang_getTypeSpelling(CXType CT) {
292
7.41k
  QualType T = GetQualType(CT);
293
7.41k
  if (T.isNull())
294
5.97k
    return cxstring::createEmpty();
295
296
1.43k
  CXTranslationUnit TU = GetTU(CT);
297
1.43k
  SmallString<64> Str;
298
1.43k
  llvm::raw_svector_ostream OS(Str);
299
1.43k
  PrintingPolicy PP(cxtu::getASTUnit(TU)->getASTContext().getLangOpts());
300
301
1.43k
  T.print(OS, PP);
302
303
1.43k
  return cxstring::createDup(OS.str());
304
1.43k
}
305
306
5
CXType clang_getTypedefDeclUnderlyingType(CXCursor C) {
307
5
  using namespace cxcursor;
308
5
  CXTranslationUnit TU = cxcursor::getCursorTU(C);
309
310
5
  if (clang_isDeclaration(C.kind)) {
311
5
    const Decl *D = cxcursor::getCursorDecl(C);
312
313
5
    if (const TypedefNameDecl *TD = dyn_cast_or_null<TypedefNameDecl>(D)) {
314
5
      QualType T = TD->getUnderlyingType();
315
5
      return MakeCXType(T, TU);
316
5
    }
317
318
0
    return MakeCXType(QualType(), TU);
319
0
  }
320
321
0
  return MakeCXType(QualType(), TU);
322
0
}
323
324
0
CXType clang_getEnumDeclIntegerType(CXCursor C) {
325
0
  using namespace cxcursor;
326
0
  CXTranslationUnit TU = cxcursor::getCursorTU(C);
327
328
0
  if (clang_isDeclaration(C.kind)) {
329
0
    const Decl *D = cxcursor::getCursorDecl(C);
330
331
0
    if (const EnumDecl *TD = dyn_cast_or_null<EnumDecl>(D)) {
332
0
      QualType T = TD->getIntegerType();
333
0
      return MakeCXType(T, TU);
334
0
    }
335
336
0
    return MakeCXType(QualType(), TU);
337
0
  }
338
339
0
  return MakeCXType(QualType(), TU);
340
0
}
341
342
0
long long clang_getEnumConstantDeclValue(CXCursor C) {
343
0
  using namespace cxcursor;
344
345
0
  if (clang_isDeclaration(C.kind)) {
346
0
    const Decl *D = cxcursor::getCursorDecl(C);
347
348
0
    if (const EnumConstantDecl *TD = dyn_cast_or_null<EnumConstantDecl>(D)) {
349
0
      return TD->getInitVal().getSExtValue();
350
0
    }
351
352
0
    return LLONG_MIN;
353
0
  }
354
355
0
  return LLONG_MIN;
356
0
}
357
358
0
unsigned long long clang_getEnumConstantDeclUnsignedValue(CXCursor C) {
359
0
  using namespace cxcursor;
360
361
0
  if (clang_isDeclaration(C.kind)) {
362
0
    const Decl *D = cxcursor::getCursorDecl(C);
363
364
0
    if (const EnumConstantDecl *TD = dyn_cast_or_null<EnumConstantDecl>(D)) {
365
0
      return TD->getInitVal().getZExtValue();
366
0
    }
367
368
0
    return ULLONG_MAX;
369
0
  }
370
371
0
  return ULLONG_MAX;
372
0
}
373
374
19
int clang_getFieldDeclBitWidth(CXCursor C) {
375
19
  using namespace cxcursor;
376
377
19
  if (clang_isDeclaration(C.kind)) {
378
19
    const Decl *D = getCursorDecl(C);
379
380
19
    if (const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D)) {
381
19
      if (FD->isBitField())
382
17
        return FD->getBitWidthValue(getCursorContext(C));
383
2
    }
384
19
  }
385
386
2
  return -1;
387
2
}
388
389
5.18k
CXType clang_getCanonicalType(CXType CT) {
390
5.18k
  if (CT.kind == CXType_Invalid)
391
4.79k
    return CT;
392
393
395
  QualType T = GetQualType(CT);
394
395
  CXTranslationUnit TU = GetTU(CT);
395
396
395
  if (T.isNull())
397
0
    return MakeCXType(QualType(), GetTU(CT));
398
399
395
  return MakeCXType(cxtu::getASTUnit(TU)->getASTContext()
400
395
                        .getCanonicalType(T),
401
395
                    TU);
402
395
}
403
404
5.17k
unsigned clang_isConstQualifiedType(CXType CT) {
405
5.17k
  QualType T = GetQualType(CT);
406
5.17k
  return T.isLocalConstQualified();
407
5.17k
}
408
409
5.17k
unsigned clang_isVolatileQualifiedType(CXType CT) {
410
5.17k
  QualType T = GetQualType(CT);
411
5.17k
  return T.isLocalVolatileQualified();
412
5.17k
}
413
414
5.17k
unsigned clang_isRestrictQualifiedType(CXType CT) {
415
5.17k
  QualType T = GetQualType(CT);
416
5.17k
  return T.isLocalRestrictQualified();
417
5.17k
}
418
419
0
unsigned clang_getAddressSpace(CXType CT) {
420
0
  QualType T = GetQualType(CT);
421
422
  // For non language-specific address space, use separate helper function.
423
0
  if (T.getAddressSpace() >= LangAS::FirstTargetAddressSpace) {
424
0
    return T.getQualifiers().getAddressSpaceAttributePrintValue();
425
0
  }
426
  // FIXME: this function returns either a LangAS or a target AS
427
  // Those values can overlap which makes this function rather unpredictable
428
  // for any caller
429
0
  return (unsigned)T.getAddressSpace();
430
0
}
431
432
0
CXString clang_getTypedefName(CXType CT) {
433
0
  QualType T = GetQualType(CT);
434
0
  const TypedefType *TT = T->getAs<TypedefType>();
435
0
  if (TT) {
436
0
    TypedefNameDecl *TD = TT->getDecl();
437
0
    if (TD)
438
0
      return cxstring::createDup(TD->getNameAsString().c_str());
439
0
  }
440
0
  return cxstring::createEmpty();
441
0
}
442
443
5.17k
CXType clang_getPointeeType(CXType CT) {
444
5.17k
  QualType T = GetQualType(CT);
445
5.17k
  const Type *TP = T.getTypePtrOrNull();
446
447
5.17k
  if (!TP)
448
4.79k
    return MakeCXType(QualType(), GetTU(CT));
449
450
397
try_again:
451
397
  switch (TP->getTypeClass()) {
452
28
    case Type::Pointer:
453
28
      T = cast<PointerType>(TP)->getPointeeType();
454
28
      break;
455
0
    case Type::BlockPointer:
456
0
      T = cast<BlockPointerType>(TP)->getPointeeType();
457
0
      break;
458
0
    case Type::LValueReference:
459
0
    case Type::RValueReference:
460
0
      T = cast<ReferenceType>(TP)->getPointeeType();
461
0
      break;
462
6
    case Type::ObjCObjectPointer:
463
6
      T = cast<ObjCObjectPointerType>(TP)->getPointeeType();
464
6
      break;
465
1
    case Type::MemberPointer:
466
1
      T = cast<MemberPointerType>(TP)->getPointeeType();
467
1
      break;
468
8
    case Type::Auto:
469
8
    case Type::DeducedTemplateSpecialization:
470
8
      TP = cast<DeducedType>(TP)->getDeducedType().getTypePtrOrNull();
471
8
      if (TP)
472
8
        goto try_again;
473
0
      break;
474
354
    default:
475
354
      T = QualType();
476
354
      break;
477
389
  }
478
389
  return MakeCXType(T, GetTU(CT));
479
389
}
480
481
6.60k
CXCursor clang_getTypeDeclaration(CXType CT) {
482
6.60k
  if (CT.kind == CXType_Invalid)
483
5.89k
    return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
484
485
713
  QualType T = GetQualType(CT);
486
713
  const Type *TP = T.getTypePtrOrNull();
487
488
713
  if (!TP)
489
0
    return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
490
491
713
  Decl *D = nullptr;
492
493
747
try_again:
494
747
  switch (TP->getTypeClass()) {
495
72
  case Type::Typedef:
496
72
    D = cast<TypedefType>(TP)->getDecl();
497
72
    break;
498
0
  case Type::ObjCObject:
499
0
    D = cast<ObjCObjectType>(TP)->getInterface();
500
0
    break;
501
23
  case Type::ObjCInterface:
502
23
    D = cast<ObjCInterfaceType>(TP)->getDecl();
503
23
    break;
504
327
  case Type::Record:
505
327
  case Type::Enum:
506
327
    D = cast<TagType>(TP)->getDecl();
507
327
    break;
508
33
  case Type::TemplateSpecialization:
509
33
    if (const RecordType *Record = TP->getAs<RecordType>())
510
24
      D = Record->getDecl();
511
9
    else
512
9
      D = cast<TemplateSpecializationType>(TP)->getTemplateName()
513
9
                                                         .getAsTemplateDecl();
514
33
    break;
515
516
14
  case Type::Auto:
517
14
  case Type::DeducedTemplateSpecialization:
518
14
    TP = cast<DeducedType>(TP)->getDeducedType().getTypePtrOrNull();
519
14
    if (TP)
520
14
      goto try_again;
521
0
    break;
522
523
0
  case Type::InjectedClassName:
524
0
    D = cast<InjectedClassNameType>(TP)->getDecl();
525
0
    break;
526
527
  // FIXME: Template type parameters!      
528
529
20
  case Type::Elaborated:
530
20
    TP = cast<ElaboratedType>(TP)->getNamedType().getTypePtrOrNull();
531
20
    goto try_again;
532
533
258
  default:
534
258
    break;
535
713
  }
536
537
713
  if (!D)
538
258
    return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
539
540
455
  return cxcursor::MakeCXCursor(D, GetTU(CT));
541
455
}
542
543
7.42k
CXString clang_getTypeKindSpelling(enum CXTypeKind K) {
544
7.42k
  const char *s = nullptr;
545
7.42k
#define TKIND(X) case CXType_##X: s = ""  #X  ""; break
546
7.42k
  switch (K) {
547
5.97k
    TKIND(Invalid);
548
84
    TKIND(Unexposed);
549
82
    TKIND(Void);
550
6
    TKIND(Bool);
551
0
    TKIND(Char_U);
552
0
    TKIND(UChar);
553
0
    TKIND(Char16);
554
0
    TKIND(Char32);
555
0
    TKIND(UShort);
556
2
    TKIND(UInt);
557
1
    TKIND(ULong);
558
0
    TKIND(ULongLong);
559
0
    TKIND(UInt128);
560
24
    TKIND(Char_S);
561
0
    TKIND(SChar);
562
0
    case CXType_WChar: s = "WChar"; break;
563
1
    TKIND(Short);
564
225
    TKIND(Int);
565
4
    TKIND(Long);
566
4
    TKIND(LongLong);
567
0
    TKIND(Int128);
568
1
    TKIND(Half);
569
11
    TKIND(Float);
570
12
    TKIND(Double);
571
0
    TKIND(LongDouble);
572
0
    TKIND(ShortAccum);
573
0
    TKIND(Accum);
574
0
    TKIND(LongAccum);
575
0
    TKIND(UShortAccum);
576
0
    TKIND(UAccum);
577
0
    TKIND(ULongAccum);
578
0
    TKIND(Float16);
579
0
    TKIND(Float128);
580
0
    TKIND(NullPtr);
581
0
    TKIND(Overload);
582
0
    TKIND(Dependent);
583
23
    TKIND(ObjCId);
584
4
    TKIND(ObjCClass);
585
3
    TKIND(ObjCSel);
586
0
    TKIND(Complex);
587
68
    TKIND(Pointer);
588
0
    TKIND(BlockPointer);
589
17
    TKIND(LValueReference);
590
0
    TKIND(RValueReference);
591
399
    TKIND(Record);
592
6
    TKIND(Enum);
593
56
    TKIND(Typedef);
594
44
    TKIND(ObjCInterface);
595
5
    TKIND(ObjCObject);
596
35
    TKIND(ObjCObjectPointer);
597
5
    TKIND(ObjCTypeParam);
598
4
    TKIND(FunctionNoProto);
599
140
    TKIND(FunctionProto);
600
7
    TKIND(ConstantArray);
601
8
    TKIND(IncompleteArray);
602
5
    TKIND(VariableArray);
603
1
    TKIND(DependentSizedArray);
604
2
    TKIND(Vector);
605
9
    TKIND(ExtVector);
606
1
    TKIND(MemberPointer);
607
12
    TKIND(Auto);
608
51
    TKIND(Elaborated);
609
4
    TKIND(Pipe);
610
3
    TKIND(Attributed);
611
0
    TKIND(BFloat16);
612
72
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) TKIND(Id);
613
0
#include "clang/Basic/OpenCLImageTypes.def"
614
0
#undef IMAGE_TYPE
615
2
#define EXT_OPAQUE_TYPE(ExtTYpe, Id, Ext) TKIND(Id);
616
2
#include "clang/Basic/OpenCLExtensionTypes.def"
617
3
    TKIND(OCLSampler);
618
0
    TKIND(OCLEvent);
619
2
    TKIND(OCLQueue);
620
2
    TKIND(OCLReserveID);
621
3
    TKIND(Atomic);
622
7.42k
  }
623
7.42k
#undef TKIND
624
7.42k
  return cxstring::createRef(s);
625
7.42k
}
626
627
5.17k
unsigned clang_equalTypes(CXType A, CXType B) {
628
5.17k
  return A.data[0] == B.data[0] && 
A.data[1] == B.data[1]5.00k
;
629
5.17k
}
630
631
0
unsigned clang_isFunctionTypeVariadic(CXType X) {
632
0
  QualType T = GetQualType(X);
633
0
  if (T.isNull())
634
0
    return 0;
635
636
0
  if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>())
637
0
    return (unsigned)FD->isVariadic();
638
639
0
  if (T->getAs<FunctionNoProtoType>())
640
0
    return 1;
641
  
642
0
  return 0;
643
0
}
644
645
0
CXCallingConv clang_getFunctionTypeCallingConv(CXType X) {
646
0
  QualType T = GetQualType(X);
647
0
  if (T.isNull())
648
0
    return CXCallingConv_Invalid;
649
  
650
0
  if (const FunctionType *FD = T->getAs<FunctionType>()) {
651
0
#define TCALLINGCONV(X) case CC_##X: return CXCallingConv_##X
652
0
    switch (FD->getCallConv()) {
653
0
      TCALLINGCONV(C);
654
0
      TCALLINGCONV(X86StdCall);
655
0
      TCALLINGCONV(X86FastCall);
656
0
      TCALLINGCONV(X86ThisCall);
657
0
      TCALLINGCONV(X86Pascal);
658
0
      TCALLINGCONV(X86RegCall);
659
0
      TCALLINGCONV(X86VectorCall);
660
0
      TCALLINGCONV(AArch64VectorCall);
661
0
      TCALLINGCONV(Win64);
662
0
      TCALLINGCONV(X86_64SysV);
663
0
      TCALLINGCONV(AAPCS);
664
0
      TCALLINGCONV(AAPCS_VFP);
665
0
      TCALLINGCONV(IntelOclBicc);
666
0
      TCALLINGCONV(Swift);
667
0
      TCALLINGCONV(PreserveMost);
668
0
      TCALLINGCONV(PreserveAll);
669
0
    case CC_SpirFunction: return CXCallingConv_Unexposed;
670
0
    case CC_OpenCLKernel: return CXCallingConv_Unexposed;
671
0
      break;
672
0
    }
673
0
#undef TCALLINGCONV
674
0
  }
675
  
676
0
  return CXCallingConv_Invalid;
677
0
}
678
679
0
int clang_getNumArgTypes(CXType X) {
680
0
  QualType T = GetQualType(X);
681
0
  if (T.isNull())
682
0
    return -1;
683
  
684
0
  if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) {
685
0
    return FD->getNumParams();
686
0
  }
687
  
688
0
  if (T->getAs<FunctionNoProtoType>()) {
689
0
    return 0;
690
0
  }
691
  
692
0
  return -1;
693
0
}
694
695
0
CXType clang_getArgType(CXType X, unsigned i) {
696
0
  QualType T = GetQualType(X);
697
0
  if (T.isNull())
698
0
    return MakeCXType(QualType(), GetTU(X));
699
700
0
  if (const FunctionProtoType *FD = T->getAs<FunctionProtoType>()) {
701
0
    unsigned numParams = FD->getNumParams();
702
0
    if (i >= numParams)
703
0
      return MakeCXType(QualType(), GetTU(X));
704
705
0
    return MakeCXType(FD->getParamType(i), GetTU(X));
706
0
  }
707
  
708
0
  return MakeCXType(QualType(), GetTU(X));
709
0
}
710
711
2.06k
CXType clang_getResultType(CXType X) {
712
2.06k
  QualType T = GetQualType(X);
713
2.06k
  if (T.isNull())
714
1.19k
    return MakeCXType(QualType(), GetTU(X));
715
  
716
866
  if (const FunctionType *FD = T->getAs<FunctionType>())
717
87
    return MakeCXType(FD->getReturnType(), GetTU(X));
718
719
779
  return MakeCXType(QualType(), GetTU(X));
720
779
}
721
722
5.17k
CXType clang_getCursorResultType(CXCursor C) {
723
5.17k
  if (clang_isDeclaration(C.kind)) {
724
300
    const Decl *D = cxcursor::getCursorDecl(C);
725
300
    if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
726
8
      return MakeCXType(MD->getReturnType(), cxcursor::getCursorTU(C));
727
728
292
    return clang_getResultType(clang_getCursorType(C));
729
292
  }
730
731
4.87k
  return MakeCXType(QualType(), cxcursor::getCursorTU(C));
732
4.87k
}
733
734
// FIXME: We should expose the canThrow(...) result instead of the EST.
735
static CXCursor_ExceptionSpecificationKind
736
1.46k
getExternalExceptionSpecificationKind(ExceptionSpecificationType EST) {
737
1.46k
  switch (EST) {
738
1.43k
  case EST_None:
739
1.43k
    return CXCursor_ExceptionSpecificationKind_None;
740
2
  case EST_DynamicNone:
741
2
    return CXCursor_ExceptionSpecificationKind_DynamicNone;
742
1
  case EST_Dynamic:
743
1
    return CXCursor_ExceptionSpecificationKind_Dynamic;
744
1
  case EST_MSAny:
745
1
    return CXCursor_ExceptionSpecificationKind_MSAny;
746
13
  case EST_BasicNoexcept:
747
13
    return CXCursor_ExceptionSpecificationKind_BasicNoexcept;
748
0
  case EST_NoThrow:
749
0
    return CXCursor_ExceptionSpecificationKind_NoThrow;
750
3
  case EST_NoexceptFalse:
751
3
  case EST_NoexceptTrue:
752
3
  case EST_DependentNoexcept:
753
3
    return CXCursor_ExceptionSpecificationKind_ComputedNoexcept;
754
19
  case EST_Unevaluated:
755
19
    return CXCursor_ExceptionSpecificationKind_Unevaluated;
756
0
  case EST_Uninstantiated:
757
0
    return CXCursor_ExceptionSpecificationKind_Uninstantiated;
758
0
  case EST_Unparsed:
759
0
    return CXCursor_ExceptionSpecificationKind_Unparsed;
760
0
  }
761
0
  llvm_unreachable("invalid EST value");
762
0
}
763
764
6.69k
int clang_getExceptionSpecificationType(CXType X) {
765
6.69k
  QualType T = GetQualType(X);
766
6.69k
  if (T.isNull())
767
1.10k
    return -1;
768
769
5.58k
  if (const auto *FD = T->getAs<FunctionProtoType>())
770
1.46k
    return getExternalExceptionSpecificationKind(FD->getExceptionSpecType());
771
772
4.12k
  return -1;
773
4.12k
}
774
775
79.9k
int clang_getCursorExceptionSpecificationType(CXCursor C) {
776
79.9k
  if (clang_isDeclaration(C.kind))
777
6.69k
    return clang_getExceptionSpecificationType(clang_getCursorType(C));
778
779
73.2k
  return -1;
780
73.2k
}
781
782
5.17k
unsigned clang_isPODType(CXType X) {
783
5.17k
  QualType T = GetQualType(X);
784
5.17k
  if (T.isNull())
785
4.79k
    return 0;
786
  
787
389
  CXTranslationUnit TU = GetTU(X);
788
789
255
  return T.isPODType(cxtu::getASTUnit(TU)->getASTContext()) ? 1 : 
0134
;
790
389
}
791
792
0
CXType clang_getElementType(CXType CT) {
793
0
  QualType ET = QualType();
794
0
  QualType T = GetQualType(CT);
795
0
  const Type *TP = T.getTypePtrOrNull();
796
797
0
  if (TP) {
798
0
    switch (TP->getTypeClass()) {
799
0
    case Type::ConstantArray:
800
0
      ET = cast<ConstantArrayType> (TP)->getElementType();
801
0
      break;
802
0
    case Type::IncompleteArray:
803
0
      ET = cast<IncompleteArrayType> (TP)->getElementType();
804
0
      break;
805
0
    case Type::VariableArray:
806
0
      ET = cast<VariableArrayType> (TP)->getElementType();
807
0
      break;
808
0
    case Type::DependentSizedArray:
809
0
      ET = cast<DependentSizedArrayType> (TP)->getElementType();
810
0
      break;
811
0
    case Type::Vector:
812
0
      ET = cast<VectorType> (TP)->getElementType();
813
0
      break;
814
0
    case Type::ExtVector:
815
0
      ET = cast<ExtVectorType>(TP)->getElementType();
816
0
      break;
817
0
    case Type::Complex:
818
0
      ET = cast<ComplexType> (TP)->getElementType();
819
0
      break;
820
0
    default:
821
0
      break;
822
0
    }
823
0
  }
824
0
  return MakeCXType(ET, GetTU(CT));
825
0
}
826
827
0
long long clang_getNumElements(CXType CT) {
828
0
  long long result = -1;
829
0
  QualType T = GetQualType(CT);
830
0
  const Type *TP = T.getTypePtrOrNull();
831
832
0
  if (TP) {
833
0
    switch (TP->getTypeClass()) {
834
0
    case Type::ConstantArray:
835
0
      result = cast<ConstantArrayType> (TP)->getSize().getSExtValue();
836
0
      break;
837
0
    case Type::Vector:
838
0
      result = cast<VectorType> (TP)->getNumElements();
839
0
      break;
840
0
    case Type::ExtVector:
841
0
      result = cast<ExtVectorType>(TP)->getNumElements();
842
0
      break;
843
0
    default:
844
0
      break;
845
0
    }
846
0
  }
847
0
  return result;
848
0
}
849
850
0
CXType clang_getArrayElementType(CXType CT) {
851
0
  QualType ET = QualType();
852
0
  QualType T = GetQualType(CT);
853
0
  const Type *TP = T.getTypePtrOrNull();
854
855
0
  if (TP) {
856
0
    switch (TP->getTypeClass()) {
857
0
    case Type::ConstantArray:
858
0
      ET = cast<ConstantArrayType> (TP)->getElementType();
859
0
      break;
860
0
    case Type::IncompleteArray:
861
0
      ET = cast<IncompleteArrayType> (TP)->getElementType();
862
0
      break;
863
0
    case Type::VariableArray:
864
0
      ET = cast<VariableArrayType> (TP)->getElementType();
865
0
      break;
866
0
    case Type::DependentSizedArray:
867
0
      ET = cast<DependentSizedArrayType> (TP)->getElementType();
868
0
      break;
869
0
    default:
870
0
      break;
871
0
    }
872
0
  }
873
0
  return MakeCXType(ET, GetTU(CT));
874
0
}
875
876
0
long long clang_getArraySize(CXType CT) {
877
0
  long long result = -1;
878
0
  QualType T = GetQualType(CT);
879
0
  const Type *TP = T.getTypePtrOrNull();
880
881
0
  if (TP) {
882
0
    switch (TP->getTypeClass()) {
883
0
    case Type::ConstantArray:
884
0
      result = cast<ConstantArrayType> (TP)->getSize().getSExtValue();
885
0
      break;
886
0
    default:
887
0
      break;
888
0
    }
889
0
  }
890
0
  return result;
891
0
}
892
893
591
static bool isIncompleteTypeWithAlignment(QualType QT) {
894
591
  return QT->isIncompleteArrayType() || 
!QT->isIncompleteType()587
;
895
591
}
896
897
1.77k
long long clang_Type_getAlignOf(CXType T) {
898
1.77k
  if (T.kind == CXType_Invalid)
899
1.18k
    return CXTypeLayoutError_Invalid;
900
591
  ASTContext &Ctx = cxtu::getASTUnit(GetTU(T))->getASTContext();
901
591
  QualType QT = GetQualType(T);
902
  // [expr.alignof] p1: return size_t value for complete object type, reference
903
  //                    or array.
904
  // [expr.alignof] p3: if reference type, return size of referenced type
905
591
  if (QT->isReferenceType())
906
4
    QT = QT.getNonReferenceType();
907
591
  if (!isIncompleteTypeWithAlignment(QT))
908
94
    return CXTypeLayoutError_Incomplete;
909
497
  if (QT->isDependentType())
910
6
    return CXTypeLayoutError_Dependent;
911
491
  if (const auto *Deduced = dyn_cast<DeducedType>(QT))
912
2
    if (Deduced->getDeducedType().isNull())
913
2
      return CXTypeLayoutError_Undeduced;
914
  // Exceptions by GCC extension - see ASTContext.cpp:1313 getTypeInfoImpl
915
  // if (QT->isFunctionType()) return 4; // Bug #15511 - should be 1
916
  // if (QT->isVoidType()) return 1;
917
489
  return Ctx.getTypeAlignInChars(QT).getQuantity();
918
489
}
919
920
0
CXType clang_Type_getClassType(CXType CT) {
921
0
  QualType ET = QualType();
922
0
  QualType T = GetQualType(CT);
923
0
  const Type *TP = T.getTypePtrOrNull();
924
925
0
  if (TP && TP->getTypeClass() == Type::MemberPointer) {
926
0
    ET = QualType(cast<MemberPointerType> (TP)->getClass(), 0);
927
0
  }
928
0
  return MakeCXType(ET, GetTU(CT));
929
0
}
930
931
1.77k
long long clang_Type_getSizeOf(CXType T) {
932
1.77k
  if (T.kind == CXType_Invalid)
933
1.18k
    return CXTypeLayoutError_Invalid;
934
591
  ASTContext &Ctx = cxtu::getASTUnit(GetTU(T))->getASTContext();
935
591
  QualType QT = GetQualType(T);
936
  // [expr.sizeof] p2: if reference type, return size of referenced type
937
591
  if (QT->isReferenceType())
938
4
    QT = QT.getNonReferenceType();
939
  // [expr.sizeof] p1: return -1 on: func, incomplete, bitfield, incomplete
940
  //                   enumeration
941
  // Note: We get the cxtype, not the cxcursor, so we can't call
942
  //       FieldDecl->isBitField()
943
  // [expr.sizeof] p3: pointer ok, function not ok.
944
  // [gcc extension] lib/AST/ExprConstant.cpp:1372 HandleSizeof : vla == error
945
591
  if (QT->isIncompleteType())
946
98
    return CXTypeLayoutError_Incomplete;
947
493
  if (QT->isDependentType())
948
6
    return CXTypeLayoutError_Dependent;
949
487
  if (!QT->isConstantSizeType())
950
4
    return CXTypeLayoutError_NotConstantSize;
951
483
  if (const auto *Deduced = dyn_cast<DeducedType>(QT))
952
2
    if (Deduced->getDeducedType().isNull())
953
2
      return CXTypeLayoutError_Undeduced;
954
  // [gcc extension] lib/AST/ExprConstant.cpp:1372
955
  //                 HandleSizeof : {voidtype,functype} == 1
956
  // not handled by ASTContext.cpp:1313 getTypeInfoImpl
957
481
  if (QT->isVoidType() || QT->isFunctionType())
958
22
    return 1;
959
459
  return Ctx.getTypeSizeInChars(QT).getQuantity();
960
459
}
961
962
1.22k
static bool isTypeIncompleteForLayout(QualType QT) {
963
1.22k
  return QT->isIncompleteType() && 
!QT->isIncompleteArrayType()22
;
964
1.22k
}
965
966
364
static long long visitRecordForValidation(const RecordDecl *RD) {
967
1.22k
  for (const auto *I : RD->fields()){
968
1.22k
    QualType FQT = I->getType();
969
1.22k
    if (isTypeIncompleteForLayout(FQT))
970
0
      return CXTypeLayoutError_Incomplete;
971
1.22k
    if (FQT->isDependentType())
972
0
      return CXTypeLayoutError_Dependent;
973
    // recurse
974
1.22k
    if (const RecordType *ChildType = I->getType()->getAs<RecordType>()) {
975
114
      if (const RecordDecl *Child = ChildType->getDecl()) {
976
114
        long long ret = visitRecordForValidation(Child);
977
114
        if (ret < 0)
978
0
          return ret;
979
364
      }
980
114
    }
981
    // else try next field
982
1.22k
  }
983
364
  return 0;
984
364
}
985
986
368
static long long validateFieldParentType(CXCursor PC, CXType PT){
987
368
  if (clang_isInvalid(PC.kind))
988
4
    return CXTypeLayoutError_Invalid;
989
364
  const RecordDecl *RD =
990
364
        dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC));
991
  // validate parent declaration
992
364
  if (!RD || 
RD->isInvalidDecl()360
)
993
114
    return CXTypeLayoutError_Invalid;
994
250
  RD = RD->getDefinition();
995
250
  if (!RD)
996
0
    return CXTypeLayoutError_Incomplete;
997
250
  if (RD->isInvalidDecl())
998
0
    return CXTypeLayoutError_Invalid;
999
  // validate parent type
1000
250
  QualType RT = GetQualType(PT);
1001
250
  if (RT->isIncompleteType())
1002
0
    return CXTypeLayoutError_Incomplete;
1003
250
  if (RT->isDependentType())
1004
0
    return CXTypeLayoutError_Dependent;
1005
  // We recurse into all record fields to detect incomplete and dependent types.
1006
250
  long long Error = visitRecordForValidation(RD);
1007
250
  if (Error < 0)
1008
0
    return Error;
1009
250
  return 0;
1010
250
}
1011
1012
184
long long clang_Type_getOffsetOf(CXType PT, const char *S) {
1013
  // check that PT is not incomplete/dependent
1014
184
  CXCursor PC = clang_getTypeDeclaration(PT);
1015
184
  long long Error = validateFieldParentType(PC,PT);
1016
184
  if (Error < 0)
1017
56
    return Error;
1018
128
  if (!S)
1019
0
    return CXTypeLayoutError_InvalidFieldName;
1020
  // lookup field
1021
128
  ASTContext &Ctx = cxtu::getASTUnit(GetTU(PT))->getASTContext();
1022
128
  IdentifierInfo *II = &Ctx.Idents.get(S);
1023
128
  DeclarationName FieldName(II);
1024
128
  const RecordDecl *RD =
1025
128
        dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC));
1026
  // verified in validateFieldParentType
1027
128
  RD = RD->getDefinition();
1028
128
  RecordDecl::lookup_result Res = RD->lookup(FieldName);
1029
  // If a field of the parent record is incomplete, lookup will fail.
1030
  // and we would return InvalidFieldName instead of Incomplete.
1031
  // But this erroneous results does protects again a hidden assertion failure
1032
  // in the RecordLayoutBuilder
1033
128
  if (Res.size() != 1)
1034
14
    return CXTypeLayoutError_InvalidFieldName;
1035
114
  if (const FieldDecl *FD = dyn_cast<FieldDecl>(Res.front()))
1036
98
    return Ctx.getFieldOffset(FD);
1037
16
  if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Res.front()))
1038
16
    return Ctx.getFieldOffset(IFD);
1039
  // we don't want any other Decl Type.
1040
0
  return CXTypeLayoutError_InvalidFieldName;
1041
0
}
1042
1043
5.17k
CXType clang_Type_getModifiedType(CXType CT) {
1044
5.17k
  QualType T = GetQualType(CT);
1045
5.17k
  if (T.isNull())
1046
4.79k
    return MakeCXType(QualType(), GetTU(CT));
1047
1048
389
  if (auto *ATT = T->getAs<AttributedType>())
1049
3
    return MakeCXType(ATT->getModifiedType(), GetTU(CT));
1050
1051
386
  return MakeCXType(QualType(), GetTU(CT));
1052
386
}
1053
1054
184
long long clang_Cursor_getOffsetOfField(CXCursor C) {
1055
184
  if (clang_isDeclaration(C.kind)) {
1056
    // we need to validate the parent type
1057
184
    CXCursor PC = clang_getCursorSemanticParent(C);
1058
184
    CXType PT = clang_getCursorType(PC);
1059
184
    long long Error = validateFieldParentType(PC,PT);
1060
184
    if (Error < 0)
1061
62
      return Error;
1062
    // proceed with the offset calculation
1063
122
    const Decl *D = cxcursor::getCursorDecl(C);
1064
122
    ASTContext &Ctx = cxcursor::getCursorContext(C);
1065
122
    if (const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(D))
1066
122
      return Ctx.getFieldOffset(FD);
1067
0
    if (const IndirectFieldDecl *IFD = dyn_cast_or_null<IndirectFieldDecl>(D))
1068
0
      return Ctx.getFieldOffset(IFD);
1069
0
  }
1070
0
  return -1;
1071
0
}
1072
1073
5.17k
enum CXRefQualifierKind clang_Type_getCXXRefQualifier(CXType T) {
1074
5.17k
  QualType QT = GetQualType(T);
1075
5.17k
  if (QT.isNull())
1076
4.79k
    return CXRefQualifier_None;
1077
389
  const FunctionProtoType *FD = QT->getAs<FunctionProtoType>();
1078
389
  if (!FD)
1079
325
    return CXRefQualifier_None;
1080
64
  switch (FD->getRefQualifier()) {
1081
62
    case RQ_None:
1082
62
      return CXRefQualifier_None;
1083
1
    case RQ_LValue:
1084
1
      return CXRefQualifier_LValue;
1085
1
    case RQ_RValue:
1086
1
      return CXRefQualifier_RValue;
1087
0
  }
1088
0
  return CXRefQualifier_None;
1089
0
}
1090
1091
1.75k
unsigned clang_Cursor_isBitField(CXCursor C) {
1092
1.75k
  if (!clang_isDeclaration(C.kind))
1093
1.31k
    return 0;
1094
441
  const FieldDecl *FD = dyn_cast_or_null<FieldDecl>(cxcursor::getCursorDecl(C));
1095
441
  if (!FD)
1096
257
    return 0;
1097
184
  return FD->isBitField();
1098
184
}
1099
1100
0
CXString clang_getDeclObjCTypeEncoding(CXCursor C) {
1101
0
  if (!clang_isDeclaration(C.kind))
1102
0
    return cxstring::createEmpty();
1103
1104
0
  const Decl *D = cxcursor::getCursorDecl(C);
1105
0
  ASTContext &Ctx = cxcursor::getCursorContext(C);
1106
0
  std::string encoding;
1107
1108
0
  if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D))  {
1109
0
    encoding = Ctx.getObjCEncodingForMethodDecl(OMD);
1110
0
  } else if (const ObjCPropertyDecl *OPD = dyn_cast<ObjCPropertyDecl>(D))
1111
0
    encoding = Ctx.getObjCEncodingForPropertyDecl(OPD, nullptr);
1112
0
  else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
1113
0
    encoding = Ctx.getObjCEncodingForFunctionDecl(FD);
1114
0
  else {
1115
0
    QualType Ty;
1116
0
    if (const TypeDecl *TD = dyn_cast<TypeDecl>(D))
1117
0
      Ty = Ctx.getTypeDeclType(TD);
1118
0
    if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
1119
0
      Ty = VD->getType();
1120
0
    else return cxstring::createRef("?");
1121
0
    Ctx.getObjCEncodingForType(Ty, encoding);
1122
0
  }
1123
1124
0
  return cxstring::createDup(encoding);
1125
0
}
1126
1127
38
static unsigned GetTemplateArgumentArraySize(ArrayRef<TemplateArgument> TA) {
1128
38
  unsigned size = TA.size();
1129
38
  for (const auto &Arg : TA)
1130
45
    if (Arg.getKind() == TemplateArgument::Pack)
1131
3
      size += Arg.pack_size() - 1;
1132
38
  return size;
1133
38
}
1134
1135
5.35k
int clang_Type_getNumTemplateArguments(CXType CT) {
1136
5.35k
  QualType T = GetQualType(CT);
1137
5.35k
  if (T.isNull())
1138
4.79k
    return -1;
1139
1140
568
  auto TA = GetTemplateArguments(T);
1141
568
  if (!TA)
1142
530
    return -1;
1143
1144
38
  return GetTemplateArgumentArraySize(TA.getValue());
1145
38
}
1146
1147
86
CXType clang_Type_getTemplateArgumentAsType(CXType CT, unsigned index) {
1148
86
  QualType T = GetQualType(CT);
1149
86
  if (T.isNull())
1150
0
    return MakeCXType(QualType(), GetTU(CT));
1151
1152
86
  auto TA = GetTemplateArguments(T);
1153
86
  if (!TA)
1154
0
    return MakeCXType(QualType(), GetTU(CT));
1155
1156
86
  Optional<QualType> QT = FindTemplateArgumentTypeAt(TA.getValue(), index);
1157
86
  return MakeCXType(QT.getValueOr(QualType()), GetTU(CT));
1158
86
}
1159
1160
5.17k
CXType clang_Type_getObjCObjectBaseType(CXType CT) {
1161
5.17k
  QualType T = GetQualType(CT);
1162
5.17k
  if (T.isNull())
1163
5.14k
    return MakeCXType(QualType(), GetTU(CT));
1164
1165
35
  const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T);
1166
35
  if (!OT)
1167
29
    return MakeCXType(QualType(), GetTU(CT));
1168
1169
6
  return MakeCXType(OT->getBaseType(), GetTU(CT));
1170
6
}
1171
1172
5.17k
unsigned clang_Type_getNumObjCProtocolRefs(CXType CT) {
1173
5.17k
  QualType T = GetQualType(CT);
1174
5.17k
  if (T.isNull())
1175
5.14k
    return 0;
1176
1177
35
  const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T);
1178
35
  if (!OT)
1179
29
    return 0;
1180
1181
6
  return OT->getNumProtocols();
1182
6
}
1183
1184
3
CXCursor clang_Type_getObjCProtocolDecl(CXType CT, unsigned i) {
1185
3
  QualType T = GetQualType(CT);
1186
3
  if (T.isNull())
1187
0
    return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
1188
1189
3
  const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T);
1190
3
  if (!OT)
1191
0
    return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
1192
1193
3
  const ObjCProtocolDecl *PD = OT->getProtocol(i);
1194
3
  if (!PD)
1195
0
    return cxcursor::MakeCXCursorInvalid(CXCursor_NoDeclFound);
1196
1197
3
  return cxcursor::MakeCXCursor(PD, GetTU(CT));
1198
3
}
1199
1200
5.17k
unsigned clang_Type_getNumObjCTypeArgs(CXType CT) {
1201
5.17k
  QualType T = GetQualType(CT);
1202
5.17k
  if (T.isNull())
1203
5.14k
    return 0;
1204
1205
35
  const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T);
1206
35
  if (!OT)
1207
29
    return 0;
1208
1209
6
  return OT->getTypeArgs().size();
1210
6
}
1211
1212
4
CXType clang_Type_getObjCTypeArg(CXType CT, unsigned i) {
1213
4
  QualType T = GetQualType(CT);
1214
4
  if (T.isNull())
1215
0
    return MakeCXType(QualType(), GetTU(CT));
1216
1217
4
  const ObjCObjectType *OT = dyn_cast<ObjCObjectType>(T);
1218
4
  if (!OT)
1219
0
    return MakeCXType(QualType(), GetTU(CT));
1220
1221
4
  const ArrayRef<QualType> TA = OT->getTypeArgs();
1222
4
  if ((size_t)i >= TA.size())
1223
0
    return MakeCXType(QualType(), GetTU(CT));
1224
1225
4
  return MakeCXType(TA[i], GetTU(CT));
1226
4
}
1227
1228
unsigned clang_Type_visitFields(CXType PT,
1229
                                CXFieldVisitor visitor,
1230
5.17k
                                CXClientData client_data){
1231
5.17k
  CXCursor PC = clang_getTypeDeclaration(PT);
1232
5.17k
  if (clang_isInvalid(PC.kind))
1233
5.03k
    return false;
1234
140
  const RecordDecl *RD =
1235
140
        dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC));
1236
140
  if (!RD || 
RD->isInvalidDecl()55
)
1237
85
    return false;
1238
55
  RD = RD->getDefinition();
1239
55
  if (!RD || 
RD->isInvalidDecl()54
)
1240
1
    return false;
1241
1242
54
  for (RecordDecl::field_iterator I = RD->field_begin(), E = RD->field_end();
1243
107
       I != E; 
++I53
){
1244
53
    const FieldDecl *FD = dyn_cast_or_null<FieldDecl>((*I));
1245
    // Callback to the client.
1246
53
    switch (visitor(cxcursor::MakeCXCursor(FD, GetTU(PT)), client_data)){
1247
0
    case CXVisit_Break:
1248
0
      return true;
1249
53
    case CXVisit_Continue:
1250
53
      break;
1251
53
    }
1252
53
  }
1253
54
  return true;
1254
54
}
1255
1256
5.44k
unsigned clang_Cursor_isAnonymous(CXCursor C){
1257
5.44k
  if (!clang_isDeclaration(C.kind))
1258
4.87k
    return 0;
1259
570
  const Decl *D = cxcursor::getCursorDecl(C);
1260
570
  if (const NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(D)) {
1261
5
    return ND->isAnonymousNamespace();
1262
565
  } else if (const TagDecl *TD = dyn_cast_or_null<TagDecl>(D)) {
1263
295
    return TD->getTypedefNameForAnonDecl() == nullptr &&
1264
295
           TD->getIdentifier() == nullptr;
1265
295
  }
1266
1267
270
  return 0;
1268
270
}
1269
1270
5.17k
unsigned clang_Cursor_isAnonymousRecordDecl(CXCursor C){
1271
5.17k
  if (!clang_isDeclaration(C.kind))
1272
4.87k
    return 0;
1273
300
  const Decl *D = cxcursor::getCursorDecl(C);
1274
300
  if (const RecordDecl *FD = dyn_cast_or_null<RecordDecl>(D))
1275
27
    return FD->isAnonymousStructOrUnion();
1276
273
  return 0;
1277
273
}
1278
1279
5.17k
unsigned clang_Cursor_isInlineNamespace(CXCursor C) {
1280
5.17k
  if (!clang_isDeclaration(C.kind))
1281
4.87k
    return 0;
1282
300
  const Decl *D = cxcursor::getCursorDecl(C);
1283
300
  const NamespaceDecl *ND = dyn_cast_or_null<NamespaceDecl>(D);
1284
295
  return ND ? 
ND->isInline()5
: 0;
1285
300
}
1286
1287
0
CXType clang_Type_getNamedType(CXType CT){
1288
0
  QualType T = GetQualType(CT);
1289
0
  const Type *TP = T.getTypePtrOrNull();
1290
1291
0
  if (TP && TP->getTypeClass() == Type::Elaborated)
1292
0
    return MakeCXType(cast<ElaboratedType>(TP)->getNamedType(), GetTU(CT));
1293
1294
0
  return MakeCXType(QualType(), GetTU(CT));
1295
0
}
1296
1297
709
unsigned clang_Type_isTransparentTagTypedef(CXType TT){
1298
709
  QualType T = GetQualType(TT);
1299
709
  if (auto *TT = dyn_cast_or_null<TypedefType>(T.getTypePtrOrNull())) {
1300
709
    if (auto *D = TT->getDecl())
1301
709
      return D->isTransparentTag();
1302
0
  }
1303
0
  return false;
1304
0
}
1305
1306
10.4k
enum CXTypeNullabilityKind clang_Type_getNullability(CXType CT) {
1307
10.4k
  QualType T = GetQualType(CT);
1308
10.4k
  if (T.isNull())
1309
9.89k
    return CXTypeNullability_Invalid;
1310
1311
528
  ASTContext &Ctx = cxtu::getASTUnit(GetTU(CT))->getASTContext();
1312
528
  if (auto nullability = T->getNullability(Ctx)) {
1313
3
    switch (*nullability) {
1314
1
      case NullabilityKind::NonNull:
1315
1
        return CXTypeNullability_NonNull;
1316
1
      case NullabilityKind::Nullable:
1317
1
        return CXTypeNullability_Nullable;
1318
1
      case NullabilityKind::Unspecified:
1319
1
        return CXTypeNullability_Unspecified;
1320
525
    }
1321
525
  }
1322
525
  return CXTypeNullability_Invalid;
1323
525
}
1324
1325
5.17k
CXType clang_Type_getValueType(CXType CT) {
1326
5.17k
  QualType T = GetQualType(CT);
1327
1328
5.17k
  if (T.isNull() || 
!T->isAtomicType()389
)
1329
5.17k
      return MakeCXType(QualType(), GetTU(CT));
1330
1331
3
  const auto *AT = T->castAs<AtomicType>();
1332
3
  return MakeCXType(AT->getValueType(), GetTU(CT));
1333
3
}