Coverage Report

Created: 2021-09-21 08:58

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