Coverage Report

Created: 2020-02-18 08:44

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