Coverage Report

Created: 2022-07-16 07:03

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