File indexing completed on 2026-05-10 08:36:59
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #ifndef LLVM_CLANG_SEMA_DECLSPEC_H
0023 #define LLVM_CLANG_SEMA_DECLSPEC_H
0024
0025 #include "clang/AST/DeclCXX.h"
0026 #include "clang/AST/DeclObjCCommon.h"
0027 #include "clang/AST/NestedNameSpecifier.h"
0028 #include "clang/Basic/ExceptionSpecificationType.h"
0029 #include "clang/Basic/Lambda.h"
0030 #include "clang/Basic/OperatorKinds.h"
0031 #include "clang/Basic/Specifiers.h"
0032 #include "clang/Lex/Token.h"
0033 #include "clang/Sema/Ownership.h"
0034 #include "clang/Sema/ParsedAttr.h"
0035 #include "llvm/ADT/STLExtras.h"
0036 #include "llvm/ADT/SmallVector.h"
0037 #include "llvm/Support/Compiler.h"
0038 #include "llvm/Support/ErrorHandling.h"
0039 #include <optional>
0040
0041 namespace clang {
0042 class ASTContext;
0043 class CXXRecordDecl;
0044 class TypeLoc;
0045 class LangOptions;
0046 class IdentifierInfo;
0047 class NamespaceAliasDecl;
0048 class NamespaceDecl;
0049 class ObjCDeclSpec;
0050 class Sema;
0051 class Declarator;
0052 struct TemplateIdAnnotation;
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074 class CXXScopeSpec {
0075 SourceRange Range;
0076 NestedNameSpecifierLocBuilder Builder;
0077 ArrayRef<TemplateParameterList *> TemplateParamLists;
0078
0079 public:
0080 SourceRange getRange() const { return Range; }
0081 void setRange(SourceRange R) { Range = R; }
0082 void setBeginLoc(SourceLocation Loc) { Range.setBegin(Loc); }
0083 void setEndLoc(SourceLocation Loc) { Range.setEnd(Loc); }
0084 SourceLocation getBeginLoc() const { return Range.getBegin(); }
0085 SourceLocation getEndLoc() const { return Range.getEnd(); }
0086
0087 void setTemplateParamLists(ArrayRef<TemplateParameterList *> L) {
0088 TemplateParamLists = L;
0089 }
0090 ArrayRef<TemplateParameterList *> getTemplateParamLists() const {
0091 return TemplateParamLists;
0092 }
0093
0094
0095 NestedNameSpecifier *getScopeRep() const {
0096 return Builder.getRepresentation();
0097 }
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
0111 SourceLocation ColonColonLoc);
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
0125 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
0139 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
0154 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
0155
0156
0157
0158 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173 void MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
0174 SourceLocation SuperLoc, SourceLocation ColonColonLoc);
0175
0176
0177
0178
0179
0180
0181
0182 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
0183 SourceRange R);
0184
0185
0186
0187 void Adopt(NestedNameSpecifierLoc Other);
0188
0189
0190
0191
0192
0193
0194 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205 SourceLocation getLastQualifierNameLoc() const;
0206
0207
0208 bool isEmpty() const { return Range.isInvalid() && getScopeRep() == nullptr; }
0209
0210 bool isNotEmpty() const { return !isEmpty(); }
0211
0212
0213 bool isInvalid() const { return Range.isValid() && getScopeRep() == nullptr; }
0214
0215 bool isValid() const { return getScopeRep() != nullptr; }
0216
0217
0218 void SetInvalid(SourceRange R) {
0219 assert(R.isValid() && "Must have a valid source range");
0220 if (Range.getBegin().isInvalid())
0221 Range.setBegin(R.getBegin());
0222 Range.setEnd(R.getEnd());
0223 Builder.Clear();
0224 }
0225
0226
0227
0228 bool isSet() const { return getScopeRep() != nullptr; }
0229
0230 void clear() {
0231 Range = SourceRange();
0232 Builder.Clear();
0233 }
0234
0235
0236 char *location_data() const { return Builder.getBuffer().first; }
0237
0238
0239
0240 unsigned location_size() const { return Builder.getBuffer().second; }
0241 };
0242
0243
0244
0245
0246
0247 class DeclSpec {
0248 public:
0249
0250
0251 enum SCS {
0252 SCS_unspecified = 0,
0253 SCS_typedef,
0254 SCS_extern,
0255 SCS_static,
0256 SCS_auto,
0257 SCS_register,
0258 SCS_private_extern,
0259 SCS_mutable
0260 };
0261
0262
0263
0264 typedef ThreadStorageClassSpecifier TSCS;
0265 static const TSCS TSCS_unspecified = clang::TSCS_unspecified;
0266 static const TSCS TSCS___thread = clang::TSCS___thread;
0267 static const TSCS TSCS_thread_local = clang::TSCS_thread_local;
0268 static const TSCS TSCS__Thread_local = clang::TSCS__Thread_local;
0269
0270 enum TSC {
0271 TSC_unspecified,
0272 TSC_imaginary,
0273 TSC_complex
0274 };
0275
0276
0277 typedef TypeSpecifierType TST;
0278 static const TST TST_unspecified = clang::TST_unspecified;
0279 static const TST TST_void = clang::TST_void;
0280 static const TST TST_char = clang::TST_char;
0281 static const TST TST_wchar = clang::TST_wchar;
0282 static const TST TST_char8 = clang::TST_char8;
0283 static const TST TST_char16 = clang::TST_char16;
0284 static const TST TST_char32 = clang::TST_char32;
0285 static const TST TST_int = clang::TST_int;
0286 static const TST TST_int128 = clang::TST_int128;
0287 static const TST TST_bitint = clang::TST_bitint;
0288 static const TST TST_half = clang::TST_half;
0289 static const TST TST_BFloat16 = clang::TST_BFloat16;
0290 static const TST TST_float = clang::TST_float;
0291 static const TST TST_double = clang::TST_double;
0292 static const TST TST_float16 = clang::TST_Float16;
0293 static const TST TST_accum = clang::TST_Accum;
0294 static const TST TST_fract = clang::TST_Fract;
0295 static const TST TST_float128 = clang::TST_float128;
0296 static const TST TST_ibm128 = clang::TST_ibm128;
0297 static const TST TST_bool = clang::TST_bool;
0298 static const TST TST_decimal32 = clang::TST_decimal32;
0299 static const TST TST_decimal64 = clang::TST_decimal64;
0300 static const TST TST_decimal128 = clang::TST_decimal128;
0301 static const TST TST_enum = clang::TST_enum;
0302 static const TST TST_union = clang::TST_union;
0303 static const TST TST_struct = clang::TST_struct;
0304 static const TST TST_interface = clang::TST_interface;
0305 static const TST TST_class = clang::TST_class;
0306 static const TST TST_typename = clang::TST_typename;
0307 static const TST TST_typeofType = clang::TST_typeofType;
0308 static const TST TST_typeofExpr = clang::TST_typeofExpr;
0309 static const TST TST_typeof_unqualType = clang::TST_typeof_unqualType;
0310 static const TST TST_typeof_unqualExpr = clang::TST_typeof_unqualExpr;
0311 static const TST TST_decltype = clang::TST_decltype;
0312 static const TST TST_decltype_auto = clang::TST_decltype_auto;
0313 static const TST TST_typename_pack_indexing =
0314 clang::TST_typename_pack_indexing;
0315 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) \
0316 static const TST TST_##Trait = clang::TST_##Trait;
0317 #include "clang/Basic/TransformTypeTraits.def"
0318 static const TST TST_auto = clang::TST_auto;
0319 static const TST TST_auto_type = clang::TST_auto_type;
0320 static const TST TST_unknown_anytype = clang::TST_unknown_anytype;
0321 static const TST TST_atomic = clang::TST_atomic;
0322 #define GENERIC_IMAGE_TYPE(ImgType, Id) \
0323 static const TST TST_##ImgType##_t = clang::TST_##ImgType##_t;
0324 #include "clang/Basic/OpenCLImageTypes.def"
0325 #define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
0326 static const TST TST_##Name = clang::TST_##Name;
0327 #include "clang/Basic/HLSLIntangibleTypes.def"
0328 static const TST TST_error = clang::TST_error;
0329
0330
0331 enum TQ {
0332 TQ_unspecified = 0,
0333 TQ_const = 1,
0334 TQ_restrict = 2,
0335 TQ_volatile = 4,
0336 TQ_unaligned = 8,
0337
0338
0339 TQ_atomic = 16
0340 };
0341
0342
0343
0344 enum ParsedSpecifiers {
0345 PQ_None = 0,
0346 PQ_StorageClassSpecifier = 1,
0347 PQ_TypeSpecifier = 2,
0348 PQ_TypeQualifier = 4,
0349 PQ_FunctionSpecifier = 8
0350
0351 };
0352
0353 enum FriendSpecified : bool { No, Yes };
0354
0355 private:
0356
0357 LLVM_PREFERRED_TYPE(SCS)
0358 unsigned StorageClassSpec : 3;
0359 LLVM_PREFERRED_TYPE(TSCS)
0360 unsigned ThreadStorageClassSpec : 2;
0361 LLVM_PREFERRED_TYPE(bool)
0362 unsigned SCS_extern_in_linkage_spec : 1;
0363
0364
0365 LLVM_PREFERRED_TYPE(TypeSpecifierWidth)
0366 unsigned TypeSpecWidth : 2;
0367 LLVM_PREFERRED_TYPE(TSC)
0368 unsigned TypeSpecComplex : 2;
0369 LLVM_PREFERRED_TYPE(TypeSpecifierSign)
0370 unsigned TypeSpecSign : 2;
0371 LLVM_PREFERRED_TYPE(TST)
0372 unsigned TypeSpecType : 7;
0373 LLVM_PREFERRED_TYPE(bool)
0374 unsigned TypeAltiVecVector : 1;
0375 LLVM_PREFERRED_TYPE(bool)
0376 unsigned TypeAltiVecPixel : 1;
0377 LLVM_PREFERRED_TYPE(bool)
0378 unsigned TypeAltiVecBool : 1;
0379 LLVM_PREFERRED_TYPE(bool)
0380 unsigned TypeSpecOwned : 1;
0381 LLVM_PREFERRED_TYPE(bool)
0382 unsigned TypeSpecPipe : 1;
0383 LLVM_PREFERRED_TYPE(bool)
0384 unsigned TypeSpecSat : 1;
0385 LLVM_PREFERRED_TYPE(bool)
0386 unsigned ConstrainedAuto : 1;
0387
0388
0389 LLVM_PREFERRED_TYPE(TQ)
0390 unsigned TypeQualifiers : 5;
0391
0392
0393 LLVM_PREFERRED_TYPE(bool)
0394 unsigned FS_inline_specified : 1;
0395 LLVM_PREFERRED_TYPE(bool)
0396 unsigned FS_forceinline_specified: 1;
0397 LLVM_PREFERRED_TYPE(bool)
0398 unsigned FS_virtual_specified : 1;
0399 LLVM_PREFERRED_TYPE(bool)
0400 unsigned FS_noreturn_specified : 1;
0401
0402
0403 LLVM_PREFERRED_TYPE(bool)
0404 unsigned FriendSpecifiedFirst : 1;
0405
0406
0407 LLVM_PREFERRED_TYPE(ConstexprSpecKind)
0408 unsigned ConstexprSpecifier : 2;
0409
0410 union {
0411 UnionParsedType TypeRep;
0412 Decl *DeclRep;
0413 Expr *ExprRep;
0414 TemplateIdAnnotation *TemplateIdRep;
0415 };
0416 Expr *PackIndexingExpr = nullptr;
0417
0418
0419 ExplicitSpecifier FS_explicit_specifier;
0420
0421
0422 ParsedAttributes Attrs;
0423
0424
0425 CXXScopeSpec TypeScope;
0426
0427
0428
0429 SourceRange Range;
0430
0431 SourceLocation StorageClassSpecLoc, ThreadStorageClassSpecLoc;
0432 SourceRange TSWRange;
0433 SourceLocation TSCLoc, TSSLoc, TSTLoc, AltiVecLoc, TSSatLoc, EllipsisLoc;
0434
0435
0436
0437
0438 SourceLocation TSTNameLoc;
0439 SourceRange TypeofParensRange;
0440 SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc,
0441 TQ_unalignedLoc;
0442 SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc, FS_noreturnLoc;
0443 SourceLocation FS_explicitCloseParenLoc;
0444 SourceLocation FS_forceinlineLoc;
0445 SourceLocation FriendLoc, ModulePrivateLoc, ConstexprLoc;
0446 SourceLocation TQ_pipeLoc;
0447
0448 WrittenBuiltinSpecs writtenBS;
0449 void SaveWrittenBuiltinSpecs();
0450
0451 ObjCDeclSpec *ObjCQualifiers;
0452
0453 static bool isTypeRep(TST T) {
0454 return T == TST_atomic || T == TST_typename || T == TST_typeofType ||
0455 T == TST_typeof_unqualType || isTransformTypeTrait(T) ||
0456 T == TST_typename_pack_indexing;
0457 }
0458 static bool isExprRep(TST T) {
0459 return T == TST_typeofExpr || T == TST_typeof_unqualExpr ||
0460 T == TST_decltype || T == TST_bitint;
0461 }
0462 static bool isTemplateIdRep(TST T) {
0463 return (T == TST_auto || T == TST_decltype_auto);
0464 }
0465
0466 DeclSpec(const DeclSpec &) = delete;
0467 void operator=(const DeclSpec &) = delete;
0468 public:
0469 static bool isDeclRep(TST T) {
0470 return (T == TST_enum || T == TST_struct ||
0471 T == TST_interface || T == TST_union ||
0472 T == TST_class);
0473 }
0474 static bool isTransformTypeTrait(TST T) {
0475 constexpr std::array<TST, 16> Traits = {
0476 #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) TST_##Trait,
0477 #include "clang/Basic/TransformTypeTraits.def"
0478 };
0479
0480 return T >= Traits.front() && T <= Traits.back();
0481 }
0482
0483 DeclSpec(AttributeFactory &attrFactory)
0484 : StorageClassSpec(SCS_unspecified),
0485 ThreadStorageClassSpec(TSCS_unspecified),
0486 SCS_extern_in_linkage_spec(false),
0487 TypeSpecWidth(static_cast<unsigned>(TypeSpecifierWidth::Unspecified)),
0488 TypeSpecComplex(TSC_unspecified),
0489 TypeSpecSign(static_cast<unsigned>(TypeSpecifierSign::Unspecified)),
0490 TypeSpecType(TST_unspecified), TypeAltiVecVector(false),
0491 TypeAltiVecPixel(false), TypeAltiVecBool(false), TypeSpecOwned(false),
0492 TypeSpecPipe(false), TypeSpecSat(false), ConstrainedAuto(false),
0493 TypeQualifiers(TQ_unspecified), FS_inline_specified(false),
0494 FS_forceinline_specified(false), FS_virtual_specified(false),
0495 FS_noreturn_specified(false), FriendSpecifiedFirst(false),
0496 ConstexprSpecifier(
0497 static_cast<unsigned>(ConstexprSpecKind::Unspecified)),
0498 Attrs(attrFactory), writtenBS(), ObjCQualifiers(nullptr) {}
0499
0500
0501 SCS getStorageClassSpec() const { return (SCS)StorageClassSpec; }
0502 TSCS getThreadStorageClassSpec() const {
0503 return (TSCS)ThreadStorageClassSpec;
0504 }
0505 bool isExternInLinkageSpec() const { return SCS_extern_in_linkage_spec; }
0506 void setExternInLinkageSpec(bool Value) {
0507 SCS_extern_in_linkage_spec = Value;
0508 }
0509
0510 SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; }
0511 SourceLocation getThreadStorageClassSpecLoc() const {
0512 return ThreadStorageClassSpecLoc;
0513 }
0514
0515 void ClearStorageClassSpecs() {
0516 StorageClassSpec = DeclSpec::SCS_unspecified;
0517 ThreadStorageClassSpec = DeclSpec::TSCS_unspecified;
0518 SCS_extern_in_linkage_spec = false;
0519 StorageClassSpecLoc = SourceLocation();
0520 ThreadStorageClassSpecLoc = SourceLocation();
0521 }
0522
0523 void ClearTypeSpecType() {
0524 TypeSpecType = DeclSpec::TST_unspecified;
0525 TypeSpecOwned = false;
0526 TSTLoc = SourceLocation();
0527 }
0528
0529
0530 TypeSpecifierWidth getTypeSpecWidth() const {
0531 return static_cast<TypeSpecifierWidth>(TypeSpecWidth);
0532 }
0533 TSC getTypeSpecComplex() const { return (TSC)TypeSpecComplex; }
0534 TypeSpecifierSign getTypeSpecSign() const {
0535 return static_cast<TypeSpecifierSign>(TypeSpecSign);
0536 }
0537 TST getTypeSpecType() const { return (TST)TypeSpecType; }
0538 bool isTypeAltiVecVector() const { return TypeAltiVecVector; }
0539 bool isTypeAltiVecPixel() const { return TypeAltiVecPixel; }
0540 bool isTypeAltiVecBool() const { return TypeAltiVecBool; }
0541 bool isTypeSpecOwned() const { return TypeSpecOwned; }
0542 bool isTypeRep() const { return isTypeRep((TST) TypeSpecType); }
0543 bool isTypeSpecPipe() const { return TypeSpecPipe; }
0544 bool isTypeSpecSat() const { return TypeSpecSat; }
0545 bool isConstrainedAuto() const { return ConstrainedAuto; }
0546
0547 ParsedType getRepAsType() const {
0548 assert(isTypeRep((TST) TypeSpecType) && "DeclSpec does not store a type");
0549 return TypeRep;
0550 }
0551 Decl *getRepAsDecl() const {
0552 assert(isDeclRep((TST) TypeSpecType) && "DeclSpec does not store a decl");
0553 return DeclRep;
0554 }
0555 Expr *getRepAsExpr() const {
0556 assert(isExprRep((TST) TypeSpecType) && "DeclSpec does not store an expr");
0557 return ExprRep;
0558 }
0559
0560 Expr *getPackIndexingExpr() const {
0561 assert(TypeSpecType == TST_typename_pack_indexing &&
0562 "DeclSpec is not a pack indexing expr");
0563 return PackIndexingExpr;
0564 }
0565
0566 TemplateIdAnnotation *getRepAsTemplateId() const {
0567 assert(isTemplateIdRep((TST) TypeSpecType) &&
0568 "DeclSpec does not store a template id");
0569 return TemplateIdRep;
0570 }
0571 CXXScopeSpec &getTypeSpecScope() { return TypeScope; }
0572 const CXXScopeSpec &getTypeSpecScope() const { return TypeScope; }
0573
0574 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
0575 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
0576 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
0577
0578 SourceLocation getTypeSpecWidthLoc() const { return TSWRange.getBegin(); }
0579 SourceRange getTypeSpecWidthRange() const { return TSWRange; }
0580 SourceLocation getTypeSpecComplexLoc() const { return TSCLoc; }
0581 SourceLocation getTypeSpecSignLoc() const { return TSSLoc; }
0582 SourceLocation getTypeSpecTypeLoc() const { return TSTLoc; }
0583 SourceLocation getAltiVecLoc() const { return AltiVecLoc; }
0584 SourceLocation getTypeSpecSatLoc() const { return TSSatLoc; }
0585
0586 SourceLocation getTypeSpecTypeNameLoc() const {
0587 assert(isDeclRep((TST)TypeSpecType) || isTypeRep((TST)TypeSpecType) ||
0588 isExprRep((TST)TypeSpecType));
0589 return TSTNameLoc;
0590 }
0591
0592 SourceRange getTypeofParensRange() const { return TypeofParensRange; }
0593 void setTypeArgumentRange(SourceRange range) { TypeofParensRange = range; }
0594
0595 bool hasAutoTypeSpec() const {
0596 return (TypeSpecType == TST_auto || TypeSpecType == TST_auto_type ||
0597 TypeSpecType == TST_decltype_auto);
0598 }
0599
0600 bool hasTagDefinition() const;
0601
0602
0603 static const char *getSpecifierName(DeclSpec::TST T,
0604 const PrintingPolicy &Policy);
0605 static const char *getSpecifierName(DeclSpec::TQ Q);
0606 static const char *getSpecifierName(TypeSpecifierSign S);
0607 static const char *getSpecifierName(DeclSpec::TSC C);
0608 static const char *getSpecifierName(TypeSpecifierWidth W);
0609 static const char *getSpecifierName(DeclSpec::SCS S);
0610 static const char *getSpecifierName(DeclSpec::TSCS S);
0611 static const char *getSpecifierName(ConstexprSpecKind C);
0612
0613
0614
0615
0616 unsigned getTypeQualifiers() const { return TypeQualifiers; }
0617 SourceLocation getConstSpecLoc() const { return TQ_constLoc; }
0618 SourceLocation getRestrictSpecLoc() const { return TQ_restrictLoc; }
0619 SourceLocation getVolatileSpecLoc() const { return TQ_volatileLoc; }
0620 SourceLocation getAtomicSpecLoc() const { return TQ_atomicLoc; }
0621 SourceLocation getUnalignedSpecLoc() const { return TQ_unalignedLoc; }
0622 SourceLocation getPipeLoc() const { return TQ_pipeLoc; }
0623 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
0624
0625
0626 void ClearTypeQualifiers() {
0627 TypeQualifiers = 0;
0628 TQ_constLoc = SourceLocation();
0629 TQ_restrictLoc = SourceLocation();
0630 TQ_volatileLoc = SourceLocation();
0631 TQ_atomicLoc = SourceLocation();
0632 TQ_unalignedLoc = SourceLocation();
0633 TQ_pipeLoc = SourceLocation();
0634 }
0635
0636
0637 bool isInlineSpecified() const {
0638 return FS_inline_specified | FS_forceinline_specified;
0639 }
0640 SourceLocation getInlineSpecLoc() const {
0641 return FS_inline_specified ? FS_inlineLoc : FS_forceinlineLoc;
0642 }
0643
0644 ExplicitSpecifier getExplicitSpecifier() const {
0645 return FS_explicit_specifier;
0646 }
0647
0648 bool isVirtualSpecified() const { return FS_virtual_specified; }
0649 SourceLocation getVirtualSpecLoc() const { return FS_virtualLoc; }
0650
0651 bool hasExplicitSpecifier() const {
0652 return FS_explicit_specifier.isSpecified();
0653 }
0654 SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; }
0655 SourceRange getExplicitSpecRange() const {
0656 return FS_explicit_specifier.getExpr()
0657 ? SourceRange(FS_explicitLoc, FS_explicitCloseParenLoc)
0658 : SourceRange(FS_explicitLoc);
0659 }
0660
0661 bool isNoreturnSpecified() const { return FS_noreturn_specified; }
0662 SourceLocation getNoreturnSpecLoc() const { return FS_noreturnLoc; }
0663
0664 void ClearFunctionSpecs() {
0665 FS_inline_specified = false;
0666 FS_inlineLoc = SourceLocation();
0667 FS_forceinline_specified = false;
0668 FS_forceinlineLoc = SourceLocation();
0669 FS_virtual_specified = false;
0670 FS_virtualLoc = SourceLocation();
0671 FS_explicit_specifier = ExplicitSpecifier();
0672 FS_explicitLoc = SourceLocation();
0673 FS_explicitCloseParenLoc = SourceLocation();
0674 FS_noreturn_specified = false;
0675 FS_noreturnLoc = SourceLocation();
0676 }
0677
0678
0679
0680
0681 void forEachCVRUQualifier(
0682 llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle);
0683
0684
0685
0686
0687 void forEachQualifier(
0688 llvm::function_ref<void(TQ, StringRef, SourceLocation)> Handle);
0689
0690
0691 bool hasTypeSpecifier() const {
0692 return getTypeSpecType() != DeclSpec::TST_unspecified ||
0693 getTypeSpecWidth() != TypeSpecifierWidth::Unspecified ||
0694 getTypeSpecComplex() != DeclSpec::TSC_unspecified ||
0695 getTypeSpecSign() != TypeSpecifierSign::Unspecified;
0696 }
0697
0698
0699
0700 unsigned getParsedSpecifiers() const;
0701
0702
0703
0704 bool isEmpty() const {
0705 return getParsedSpecifiers() == DeclSpec::PQ_None;
0706 }
0707
0708 void SetRangeStart(SourceLocation Loc) { Range.setBegin(Loc); }
0709 void SetRangeEnd(SourceLocation Loc) { Range.setEnd(Loc); }
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721 bool SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
0722 const char *&PrevSpec, unsigned &DiagID,
0723 const PrintingPolicy &Policy);
0724 bool SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
0725 const char *&PrevSpec, unsigned &DiagID);
0726 bool SetTypeSpecWidth(TypeSpecifierWidth W, SourceLocation Loc,
0727 const char *&PrevSpec, unsigned &DiagID,
0728 const PrintingPolicy &Policy);
0729 bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec,
0730 unsigned &DiagID);
0731 bool SetTypeSpecSign(TypeSpecifierSign S, SourceLocation Loc,
0732 const char *&PrevSpec, unsigned &DiagID);
0733 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
0734 unsigned &DiagID, const PrintingPolicy &Policy);
0735 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
0736 unsigned &DiagID, ParsedType Rep,
0737 const PrintingPolicy &Policy);
0738 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
0739 unsigned &DiagID, TypeResult Rep,
0740 const PrintingPolicy &Policy) {
0741 if (Rep.isInvalid())
0742 return SetTypeSpecError();
0743 return SetTypeSpecType(T, Loc, PrevSpec, DiagID, Rep.get(), Policy);
0744 }
0745 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
0746 unsigned &DiagID, Decl *Rep, bool Owned,
0747 const PrintingPolicy &Policy);
0748 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
0749 SourceLocation TagNameLoc, const char *&PrevSpec,
0750 unsigned &DiagID, ParsedType Rep,
0751 const PrintingPolicy &Policy);
0752 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
0753 SourceLocation TagNameLoc, const char *&PrevSpec,
0754 unsigned &DiagID, Decl *Rep, bool Owned,
0755 const PrintingPolicy &Policy);
0756 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
0757 unsigned &DiagID, TemplateIdAnnotation *Rep,
0758 const PrintingPolicy &Policy);
0759
0760 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
0761 unsigned &DiagID, Expr *Rep,
0762 const PrintingPolicy &policy);
0763 bool SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
0764 const char *&PrevSpec, unsigned &DiagID,
0765 const PrintingPolicy &Policy);
0766 bool SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
0767 const char *&PrevSpec, unsigned &DiagID,
0768 const PrintingPolicy &Policy);
0769 bool SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
0770 const char *&PrevSpec, unsigned &DiagID,
0771 const PrintingPolicy &Policy);
0772 bool SetTypePipe(bool isPipe, SourceLocation Loc,
0773 const char *&PrevSpec, unsigned &DiagID,
0774 const PrintingPolicy &Policy);
0775 bool SetBitIntType(SourceLocation KWLoc, Expr *BitWidth,
0776 const char *&PrevSpec, unsigned &DiagID,
0777 const PrintingPolicy &Policy);
0778 bool SetTypeSpecSat(SourceLocation Loc, const char *&PrevSpec,
0779 unsigned &DiagID);
0780
0781 void SetPackIndexingExpr(SourceLocation EllipsisLoc, Expr *Pack);
0782
0783 bool SetTypeSpecError();
0784 void UpdateDeclRep(Decl *Rep) {
0785 assert(isDeclRep((TST) TypeSpecType));
0786 DeclRep = Rep;
0787 }
0788 void UpdateTypeRep(ParsedType Rep) {
0789 assert(isTypeRep((TST) TypeSpecType));
0790 TypeRep = Rep;
0791 }
0792 void UpdateExprRep(Expr *Rep) {
0793 assert(isExprRep((TST) TypeSpecType));
0794 ExprRep = Rep;
0795 }
0796
0797 bool SetTypeQual(TQ T, SourceLocation Loc);
0798
0799 bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
0800 unsigned &DiagID, const LangOptions &Lang);
0801
0802 bool setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
0803 unsigned &DiagID);
0804 bool setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
0805 unsigned &DiagID);
0806 bool setFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
0807 unsigned &DiagID);
0808 bool setFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
0809 unsigned &DiagID, ExplicitSpecifier ExplicitSpec,
0810 SourceLocation CloseParenLoc);
0811 bool setFunctionSpecNoreturn(SourceLocation Loc, const char *&PrevSpec,
0812 unsigned &DiagID);
0813
0814 bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
0815 unsigned &DiagID);
0816 bool setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
0817 unsigned &DiagID);
0818 bool SetConstexprSpec(ConstexprSpecKind ConstexprKind, SourceLocation Loc,
0819 const char *&PrevSpec, unsigned &DiagID);
0820
0821 FriendSpecified isFriendSpecified() const {
0822 return static_cast<FriendSpecified>(FriendLoc.isValid());
0823 }
0824
0825 bool isFriendSpecifiedFirst() const { return FriendSpecifiedFirst; }
0826
0827 SourceLocation getFriendSpecLoc() const { return FriendLoc; }
0828
0829 bool isModulePrivateSpecified() const { return ModulePrivateLoc.isValid(); }
0830 SourceLocation getModulePrivateSpecLoc() const { return ModulePrivateLoc; }
0831
0832 ConstexprSpecKind getConstexprSpecifier() const {
0833 return ConstexprSpecKind(ConstexprSpecifier);
0834 }
0835
0836 SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; }
0837 bool hasConstexprSpecifier() const {
0838 return getConstexprSpecifier() != ConstexprSpecKind::Unspecified;
0839 }
0840
0841 void ClearConstexprSpec() {
0842 ConstexprSpecifier = static_cast<unsigned>(ConstexprSpecKind::Unspecified);
0843 ConstexprLoc = SourceLocation();
0844 }
0845
0846 AttributePool &getAttributePool() const {
0847 return Attrs.getPool();
0848 }
0849
0850
0851
0852
0853
0854
0855
0856
0857
0858
0859
0860
0861
0862
0863
0864
0865
0866
0867 void addAttributes(const ParsedAttributesView &AL) {
0868 Attrs.addAll(AL.begin(), AL.end());
0869 }
0870
0871 bool hasAttributes() const { return !Attrs.empty(); }
0872
0873 ParsedAttributes &getAttributes() { return Attrs; }
0874 const ParsedAttributes &getAttributes() const { return Attrs; }
0875
0876 void takeAttributesFrom(ParsedAttributes &attrs) {
0877 Attrs.takeAllFrom(attrs);
0878 }
0879
0880
0881
0882
0883 void Finish(Sema &S, const PrintingPolicy &Policy);
0884
0885 const WrittenBuiltinSpecs& getWrittenBuiltinSpecs() const {
0886 return writtenBS;
0887 }
0888
0889 ObjCDeclSpec *getObjCQualifiers() const { return ObjCQualifiers; }
0890 void setObjCQualifiers(ObjCDeclSpec *quals) { ObjCQualifiers = quals; }
0891
0892
0893
0894
0895 bool isMissingDeclaratorOk();
0896 };
0897
0898
0899
0900 class ObjCDeclSpec {
0901 public:
0902
0903
0904
0905
0906
0907
0908 enum ObjCDeclQualifier {
0909 DQ_None = 0x0,
0910 DQ_In = 0x1,
0911 DQ_Inout = 0x2,
0912 DQ_Out = 0x4,
0913 DQ_Bycopy = 0x8,
0914 DQ_Byref = 0x10,
0915 DQ_Oneway = 0x20,
0916 DQ_CSNullability = 0x40
0917 };
0918
0919 ObjCDeclSpec()
0920 : objcDeclQualifier(DQ_None),
0921 PropertyAttributes(ObjCPropertyAttribute::kind_noattr), Nullability(0),
0922 GetterName(nullptr), SetterName(nullptr) {}
0923
0924 ObjCDeclQualifier getObjCDeclQualifier() const {
0925 return (ObjCDeclQualifier)objcDeclQualifier;
0926 }
0927 void setObjCDeclQualifier(ObjCDeclQualifier DQVal) {
0928 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal);
0929 }
0930 void clearObjCDeclQualifier(ObjCDeclQualifier DQVal) {
0931 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier & ~DQVal);
0932 }
0933
0934 ObjCPropertyAttribute::Kind getPropertyAttributes() const {
0935 return ObjCPropertyAttribute::Kind(PropertyAttributes);
0936 }
0937 void setPropertyAttributes(ObjCPropertyAttribute::Kind PRVal) {
0938 PropertyAttributes =
0939 (ObjCPropertyAttribute::Kind)(PropertyAttributes | PRVal);
0940 }
0941
0942 NullabilityKind getNullability() const {
0943 assert(
0944 ((getObjCDeclQualifier() & DQ_CSNullability) ||
0945 (getPropertyAttributes() & ObjCPropertyAttribute::kind_nullability)) &&
0946 "Objective-C declspec doesn't have nullability");
0947 return static_cast<NullabilityKind>(Nullability);
0948 }
0949
0950 SourceLocation getNullabilityLoc() const {
0951 assert(
0952 ((getObjCDeclQualifier() & DQ_CSNullability) ||
0953 (getPropertyAttributes() & ObjCPropertyAttribute::kind_nullability)) &&
0954 "Objective-C declspec doesn't have nullability");
0955 return NullabilityLoc;
0956 }
0957
0958 void setNullability(SourceLocation loc, NullabilityKind kind) {
0959 assert(
0960 ((getObjCDeclQualifier() & DQ_CSNullability) ||
0961 (getPropertyAttributes() & ObjCPropertyAttribute::kind_nullability)) &&
0962 "Set the nullability declspec or property attribute first");
0963 Nullability = static_cast<unsigned>(kind);
0964 NullabilityLoc = loc;
0965 }
0966
0967 const IdentifierInfo *getGetterName() const { return GetterName; }
0968 IdentifierInfo *getGetterName() { return GetterName; }
0969 SourceLocation getGetterNameLoc() const { return GetterNameLoc; }
0970 void setGetterName(IdentifierInfo *name, SourceLocation loc) {
0971 GetterName = name;
0972 GetterNameLoc = loc;
0973 }
0974
0975 const IdentifierInfo *getSetterName() const { return SetterName; }
0976 IdentifierInfo *getSetterName() { return SetterName; }
0977 SourceLocation getSetterNameLoc() const { return SetterNameLoc; }
0978 void setSetterName(IdentifierInfo *name, SourceLocation loc) {
0979 SetterName = name;
0980 SetterNameLoc = loc;
0981 }
0982
0983 private:
0984
0985
0986
0987 unsigned objcDeclQualifier : 7;
0988
0989
0990 unsigned PropertyAttributes : NumObjCPropertyAttrsBits;
0991
0992 unsigned Nullability : 2;
0993
0994 SourceLocation NullabilityLoc;
0995
0996 IdentifierInfo *GetterName;
0997 IdentifierInfo *SetterName;
0998 SourceLocation GetterNameLoc;
0999 SourceLocation SetterNameLoc;
1000
1001 };
1002
1003
1004 enum class UnqualifiedIdKind {
1005
1006 IK_Identifier,
1007
1008 IK_OperatorFunctionId,
1009
1010 IK_ConversionFunctionId,
1011
1012 IK_LiteralOperatorId,
1013
1014 IK_ConstructorName,
1015
1016 IK_ConstructorTemplateId,
1017
1018 IK_DestructorName,
1019
1020 IK_TemplateId,
1021
1022 IK_ImplicitSelfParam,
1023
1024 IK_DeductionGuideName
1025 };
1026
1027
1028 class UnqualifiedId {
1029 private:
1030 UnqualifiedId(const UnqualifiedId &Other) = delete;
1031 const UnqualifiedId &operator=(const UnqualifiedId &) = delete;
1032
1033
1034 UnqualifiedIdKind Kind;
1035
1036 public:
1037 struct OFI {
1038
1039 OverloadedOperatorKind Operator;
1040
1041
1042
1043
1044
1045
1046
1047
1048 SourceLocation SymbolLocations[3];
1049 };
1050
1051
1052
1053 union {
1054
1055
1056 const IdentifierInfo *Identifier;
1057
1058
1059
1060 struct OFI OperatorFunctionId;
1061
1062
1063
1064 UnionParsedType ConversionFunctionId;
1065
1066
1067
1068 UnionParsedType ConstructorName;
1069
1070
1071
1072 UnionParsedType DestructorName;
1073
1074
1075 UnionParsedTemplateTy TemplateName;
1076
1077
1078
1079
1080 TemplateIdAnnotation *TemplateId;
1081 };
1082
1083
1084
1085
1086 SourceLocation StartLocation;
1087
1088
1089 SourceLocation EndLocation;
1090
1091 UnqualifiedId()
1092 : Kind(UnqualifiedIdKind::IK_Identifier), Identifier(nullptr) {}
1093
1094
1095
1096 void clear() {
1097 Kind = UnqualifiedIdKind::IK_Identifier;
1098 Identifier = nullptr;
1099 StartLocation = SourceLocation();
1100 EndLocation = SourceLocation();
1101 }
1102
1103
1104 bool isValid() const { return StartLocation.isValid(); }
1105
1106
1107 bool isInvalid() const { return !isValid(); }
1108
1109
1110 UnqualifiedIdKind getKind() const { return Kind; }
1111
1112
1113
1114
1115
1116 void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) {
1117 Kind = UnqualifiedIdKind::IK_Identifier;
1118 Identifier = Id;
1119 StartLocation = EndLocation = IdLoc;
1120 }
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131 void setOperatorFunctionId(SourceLocation OperatorLoc,
1132 OverloadedOperatorKind Op,
1133 SourceLocation SymbolLocations[3]);
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143 void setConversionFunctionId(SourceLocation OperatorLoc,
1144 ParsedType Ty,
1145 SourceLocation EndLoc) {
1146 Kind = UnqualifiedIdKind::IK_ConversionFunctionId;
1147 StartLocation = OperatorLoc;
1148 EndLocation = EndLoc;
1149 ConversionFunctionId = Ty;
1150 }
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160 void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc,
1161 SourceLocation IdLoc) {
1162 Kind = UnqualifiedIdKind::IK_LiteralOperatorId;
1163 Identifier = Id;
1164 StartLocation = OpLoc;
1165 EndLocation = IdLoc;
1166 }
1167
1168
1169
1170
1171
1172
1173
1174
1175 void setConstructorName(ParsedType ClassType,
1176 SourceLocation ClassNameLoc,
1177 SourceLocation EndLoc) {
1178 Kind = UnqualifiedIdKind::IK_ConstructorName;
1179 StartLocation = ClassNameLoc;
1180 EndLocation = EndLoc;
1181 ConstructorName = ClassType;
1182 }
1183
1184
1185
1186
1187
1188
1189
1190 void setConstructorTemplateId(TemplateIdAnnotation *TemplateId);
1191
1192
1193
1194
1195
1196
1197
1198 void setDestructorName(SourceLocation TildeLoc,
1199 ParsedType ClassType,
1200 SourceLocation EndLoc) {
1201 Kind = UnqualifiedIdKind::IK_DestructorName;
1202 StartLocation = TildeLoc;
1203 EndLocation = EndLoc;
1204 DestructorName = ClassType;
1205 }
1206
1207
1208
1209
1210
1211
1212 void setTemplateId(TemplateIdAnnotation *TemplateId);
1213
1214
1215
1216
1217
1218
1219 void setDeductionGuideName(ParsedTemplateTy Template,
1220 SourceLocation TemplateLoc) {
1221 Kind = UnqualifiedIdKind::IK_DeductionGuideName;
1222 TemplateName = Template;
1223 StartLocation = EndLocation = TemplateLoc;
1224 }
1225
1226
1227
1228
1229
1230 void setImplicitSelfParam(const IdentifierInfo *Id) {
1231 Kind = UnqualifiedIdKind::IK_ImplicitSelfParam;
1232 Identifier = Id;
1233 StartLocation = EndLocation = SourceLocation();
1234 }
1235
1236
1237 SourceRange getSourceRange() const LLVM_READONLY {
1238 return SourceRange(StartLocation, EndLocation);
1239 }
1240 SourceLocation getBeginLoc() const LLVM_READONLY { return StartLocation; }
1241 SourceLocation getEndLoc() const LLVM_READONLY { return EndLocation; }
1242 };
1243
1244
1245 typedef SmallVector<Token, 4> CachedTokens;
1246
1247
1248
1249
1250
1251 struct DeclaratorChunk {
1252 DeclaratorChunk() {};
1253
1254 enum {
1255 Pointer, Reference, Array, Function, BlockPointer, MemberPointer, Paren, Pipe
1256 } Kind;
1257
1258
1259 SourceLocation Loc;
1260
1261 SourceLocation EndLoc;
1262
1263 SourceRange getSourceRange() const {
1264 if (EndLoc.isInvalid())
1265 return SourceRange(Loc, Loc);
1266 return SourceRange(Loc, EndLoc);
1267 }
1268
1269 ParsedAttributesView AttrList;
1270
1271 struct PointerTypeInfo {
1272
1273 LLVM_PREFERRED_TYPE(DeclSpec::TQ)
1274 unsigned TypeQuals : 5;
1275
1276
1277 SourceLocation ConstQualLoc;
1278
1279
1280 SourceLocation VolatileQualLoc;
1281
1282
1283 SourceLocation RestrictQualLoc;
1284
1285
1286 SourceLocation AtomicQualLoc;
1287
1288
1289 SourceLocation UnalignedQualLoc;
1290
1291 void destroy() {
1292 }
1293 };
1294
1295 struct ReferenceTypeInfo {
1296
1297 bool HasRestrict : 1;
1298
1299 bool LValueRef : 1;
1300 void destroy() {
1301 }
1302 };
1303
1304 struct ArrayTypeInfo {
1305
1306
1307 LLVM_PREFERRED_TYPE(DeclSpec::TQ)
1308 unsigned TypeQuals : 5;
1309
1310
1311 LLVM_PREFERRED_TYPE(bool)
1312 unsigned hasStatic : 1;
1313
1314
1315 LLVM_PREFERRED_TYPE(bool)
1316 unsigned isStar : 1;
1317
1318
1319
1320
1321 Expr *NumElts;
1322
1323 void destroy() {}
1324 };
1325
1326
1327
1328
1329
1330
1331
1332
1333 struct ParamInfo {
1334 const IdentifierInfo *Ident;
1335 SourceLocation IdentLoc;
1336 Decl *Param;
1337
1338
1339
1340
1341
1342
1343 std::unique_ptr<CachedTokens> DefaultArgTokens;
1344
1345 ParamInfo() = default;
1346 ParamInfo(const IdentifierInfo *ident, SourceLocation iloc, Decl *param,
1347 std::unique_ptr<CachedTokens> DefArgTokens = nullptr)
1348 : Ident(ident), IdentLoc(iloc), Param(param),
1349 DefaultArgTokens(std::move(DefArgTokens)) {}
1350 };
1351
1352 struct TypeAndRange {
1353 ParsedType Ty;
1354 SourceRange Range;
1355 };
1356
1357 struct FunctionTypeInfo {
1358
1359
1360
1361 LLVM_PREFERRED_TYPE(bool)
1362 unsigned hasPrototype : 1;
1363
1364
1365
1366
1367 LLVM_PREFERRED_TYPE(bool)
1368 unsigned isVariadic : 1;
1369
1370
1371 LLVM_PREFERRED_TYPE(bool)
1372 unsigned isAmbiguous : 1;
1373
1374
1375
1376 LLVM_PREFERRED_TYPE(bool)
1377 unsigned RefQualifierIsLValueRef : 1;
1378
1379
1380 LLVM_PREFERRED_TYPE(ExceptionSpecificationType)
1381 unsigned ExceptionSpecType : 4;
1382
1383
1384 LLVM_PREFERRED_TYPE(bool)
1385 unsigned DeleteParams : 1;
1386
1387
1388
1389 LLVM_PREFERRED_TYPE(bool)
1390 unsigned HasTrailingReturnType : 1;
1391
1392
1393 SourceLocation LParenLoc;
1394
1395
1396 SourceLocation EllipsisLoc;
1397
1398
1399 SourceLocation RParenLoc;
1400
1401
1402
1403 unsigned NumParams;
1404
1405
1406
1407
1408 unsigned NumExceptionsOrDecls;
1409
1410
1411
1412
1413 SourceLocation RefQualifierLoc;
1414
1415
1416
1417 SourceLocation MutableLoc;
1418
1419
1420 SourceLocation ExceptionSpecLocBeg;
1421
1422
1423 SourceLocation ExceptionSpecLocEnd;
1424
1425
1426
1427
1428 ParamInfo *Params;
1429
1430
1431 DeclSpec *MethodQualifiers;
1432
1433
1434 AttributeFactory *QualAttrFactory;
1435
1436 union {
1437
1438
1439
1440 TypeAndRange *Exceptions;
1441
1442
1443
1444 Expr *NoexceptExpr;
1445
1446
1447
1448 CachedTokens *ExceptionSpecTokens;
1449
1450
1451
1452
1453 NamedDecl **DeclsInPrototype;
1454 };
1455
1456
1457
1458 UnionParsedType TrailingReturnType;
1459
1460
1461
1462 SourceLocation TrailingReturnTypeLoc;
1463
1464
1465
1466
1467 void freeParams() {
1468 for (unsigned I = 0; I < NumParams; ++I)
1469 Params[I].DefaultArgTokens.reset();
1470 if (DeleteParams) {
1471 delete[] Params;
1472 DeleteParams = false;
1473 }
1474 NumParams = 0;
1475 }
1476
1477 void destroy() {
1478 freeParams();
1479 delete QualAttrFactory;
1480 delete MethodQualifiers;
1481 switch (getExceptionSpecType()) {
1482 default:
1483 break;
1484 case EST_Dynamic:
1485 delete[] Exceptions;
1486 break;
1487 case EST_Unparsed:
1488 delete ExceptionSpecTokens;
1489 break;
1490 case EST_None:
1491 if (NumExceptionsOrDecls != 0)
1492 delete[] DeclsInPrototype;
1493 break;
1494 }
1495 }
1496
1497 DeclSpec &getOrCreateMethodQualifiers() {
1498 if (!MethodQualifiers) {
1499 QualAttrFactory = new AttributeFactory();
1500 MethodQualifiers = new DeclSpec(*QualAttrFactory);
1501 }
1502 return *MethodQualifiers;
1503 }
1504
1505
1506
1507
1508 bool isKNRPrototype() const { return !hasPrototype && NumParams != 0; }
1509
1510 SourceLocation getLParenLoc() const { return LParenLoc; }
1511
1512 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
1513
1514 SourceLocation getRParenLoc() const { return RParenLoc; }
1515
1516 SourceLocation getExceptionSpecLocBeg() const {
1517 return ExceptionSpecLocBeg;
1518 }
1519
1520 SourceLocation getExceptionSpecLocEnd() const {
1521 return ExceptionSpecLocEnd;
1522 }
1523
1524 SourceRange getExceptionSpecRange() const {
1525 return SourceRange(getExceptionSpecLocBeg(), getExceptionSpecLocEnd());
1526 }
1527
1528
1529 SourceLocation getRefQualifierLoc() const { return RefQualifierLoc; }
1530
1531
1532 SourceLocation getConstQualifierLoc() const {
1533 assert(MethodQualifiers);
1534 return MethodQualifiers->getConstSpecLoc();
1535 }
1536
1537
1538 SourceLocation getVolatileQualifierLoc() const {
1539 assert(MethodQualifiers);
1540 return MethodQualifiers->getVolatileSpecLoc();
1541 }
1542
1543
1544 SourceLocation getRestrictQualifierLoc() const {
1545 assert(MethodQualifiers);
1546 return MethodQualifiers->getRestrictSpecLoc();
1547 }
1548
1549
1550 SourceLocation getMutableLoc() const { return MutableLoc; }
1551
1552
1553
1554 bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); }
1555
1556
1557
1558 bool hasMutableQualifier() const { return getMutableLoc().isValid(); }
1559
1560
1561 bool hasMethodTypeQualifiers() const {
1562 return MethodQualifiers && (MethodQualifiers->getTypeQualifiers() ||
1563 MethodQualifiers->getAttributes().size());
1564 }
1565
1566
1567 ExceptionSpecificationType getExceptionSpecType() const {
1568 return static_cast<ExceptionSpecificationType>(ExceptionSpecType);
1569 }
1570
1571
1572 unsigned getNumExceptions() const {
1573 assert(ExceptionSpecType != EST_None);
1574 return NumExceptionsOrDecls;
1575 }
1576
1577
1578
1579 ArrayRef<NamedDecl *> getDeclsInPrototype() const {
1580 assert(ExceptionSpecType == EST_None);
1581 return llvm::ArrayRef(DeclsInPrototype, NumExceptionsOrDecls);
1582 }
1583
1584
1585
1586 bool hasTrailingReturnType() const { return HasTrailingReturnType; }
1587
1588
1589 ParsedType getTrailingReturnType() const {
1590 assert(HasTrailingReturnType);
1591 return TrailingReturnType;
1592 }
1593
1594
1595 SourceLocation getTrailingReturnTypeLoc() const {
1596 assert(HasTrailingReturnType);
1597 return TrailingReturnTypeLoc;
1598 }
1599 };
1600
1601 struct BlockPointerTypeInfo {
1602
1603
1604 LLVM_PREFERRED_TYPE(DeclSpec::TQ)
1605 unsigned TypeQuals : 5;
1606
1607 void destroy() {
1608 }
1609 };
1610
1611 struct MemberPointerTypeInfo {
1612
1613 LLVM_PREFERRED_TYPE(DeclSpec::TQ)
1614 unsigned TypeQuals : 5;
1615
1616 SourceLocation StarLoc;
1617
1618
1619 alignas(CXXScopeSpec) char ScopeMem[sizeof(CXXScopeSpec)];
1620 CXXScopeSpec &Scope() {
1621 return *reinterpret_cast<CXXScopeSpec *>(ScopeMem);
1622 }
1623 const CXXScopeSpec &Scope() const {
1624 return *reinterpret_cast<const CXXScopeSpec *>(ScopeMem);
1625 }
1626 void destroy() {
1627 Scope().~CXXScopeSpec();
1628 }
1629 };
1630
1631 struct PipeTypeInfo {
1632
1633 unsigned AccessWrites : 3;
1634
1635 void destroy() {}
1636 };
1637
1638 union {
1639 PointerTypeInfo Ptr;
1640 ReferenceTypeInfo Ref;
1641 ArrayTypeInfo Arr;
1642 FunctionTypeInfo Fun;
1643 BlockPointerTypeInfo Cls;
1644 MemberPointerTypeInfo Mem;
1645 PipeTypeInfo PipeInfo;
1646 };
1647
1648 void destroy() {
1649 switch (Kind) {
1650 case DeclaratorChunk::Function: return Fun.destroy();
1651 case DeclaratorChunk::Pointer: return Ptr.destroy();
1652 case DeclaratorChunk::BlockPointer: return Cls.destroy();
1653 case DeclaratorChunk::Reference: return Ref.destroy();
1654 case DeclaratorChunk::Array: return Arr.destroy();
1655 case DeclaratorChunk::MemberPointer: return Mem.destroy();
1656 case DeclaratorChunk::Paren: return;
1657 case DeclaratorChunk::Pipe: return PipeInfo.destroy();
1658 }
1659 }
1660
1661
1662
1663 const ParsedAttributesView &getAttrs() const { return AttrList; }
1664 ParsedAttributesView &getAttrs() { return AttrList; }
1665
1666
1667 static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc,
1668 SourceLocation ConstQualLoc,
1669 SourceLocation VolatileQualLoc,
1670 SourceLocation RestrictQualLoc,
1671 SourceLocation AtomicQualLoc,
1672 SourceLocation UnalignedQualLoc) {
1673 DeclaratorChunk I;
1674 I.Kind = Pointer;
1675 I.Loc = Loc;
1676 new (&I.Ptr) PointerTypeInfo;
1677 I.Ptr.TypeQuals = TypeQuals;
1678 I.Ptr.ConstQualLoc = ConstQualLoc;
1679 I.Ptr.VolatileQualLoc = VolatileQualLoc;
1680 I.Ptr.RestrictQualLoc = RestrictQualLoc;
1681 I.Ptr.AtomicQualLoc = AtomicQualLoc;
1682 I.Ptr.UnalignedQualLoc = UnalignedQualLoc;
1683 return I;
1684 }
1685
1686
1687 static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc,
1688 bool lvalue) {
1689 DeclaratorChunk I;
1690 I.Kind = Reference;
1691 I.Loc = Loc;
1692 I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0;
1693 I.Ref.LValueRef = lvalue;
1694 return I;
1695 }
1696
1697
1698 static DeclaratorChunk getArray(unsigned TypeQuals,
1699 bool isStatic, bool isStar, Expr *NumElts,
1700 SourceLocation LBLoc, SourceLocation RBLoc) {
1701 DeclaratorChunk I;
1702 I.Kind = Array;
1703 I.Loc = LBLoc;
1704 I.EndLoc = RBLoc;
1705 I.Arr.TypeQuals = TypeQuals;
1706 I.Arr.hasStatic = isStatic;
1707 I.Arr.isStar = isStar;
1708 I.Arr.NumElts = NumElts;
1709 return I;
1710 }
1711
1712
1713
1714 static DeclaratorChunk getFunction(bool HasProto,
1715 bool IsAmbiguous,
1716 SourceLocation LParenLoc,
1717 ParamInfo *Params, unsigned NumParams,
1718 SourceLocation EllipsisLoc,
1719 SourceLocation RParenLoc,
1720 bool RefQualifierIsLvalueRef,
1721 SourceLocation RefQualifierLoc,
1722 SourceLocation MutableLoc,
1723 ExceptionSpecificationType ESpecType,
1724 SourceRange ESpecRange,
1725 ParsedType *Exceptions,
1726 SourceRange *ExceptionRanges,
1727 unsigned NumExceptions,
1728 Expr *NoexceptExpr,
1729 CachedTokens *ExceptionSpecTokens,
1730 ArrayRef<NamedDecl *> DeclsInPrototype,
1731 SourceLocation LocalRangeBegin,
1732 SourceLocation LocalRangeEnd,
1733 Declarator &TheDeclarator,
1734 TypeResult TrailingReturnType =
1735 TypeResult(),
1736 SourceLocation TrailingReturnTypeLoc =
1737 SourceLocation(),
1738 DeclSpec *MethodQualifiers = nullptr);
1739
1740
1741 static DeclaratorChunk getBlockPointer(unsigned TypeQuals,
1742 SourceLocation Loc) {
1743 DeclaratorChunk I;
1744 I.Kind = BlockPointer;
1745 I.Loc = Loc;
1746 I.Cls.TypeQuals = TypeQuals;
1747 return I;
1748 }
1749
1750
1751 static DeclaratorChunk getPipe(unsigned TypeQuals,
1752 SourceLocation Loc) {
1753 DeclaratorChunk I;
1754 I.Kind = Pipe;
1755 I.Loc = Loc;
1756 I.Cls.TypeQuals = TypeQuals;
1757 return I;
1758 }
1759
1760 static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS,
1761 unsigned TypeQuals,
1762 SourceLocation StarLoc,
1763 SourceLocation EndLoc) {
1764 DeclaratorChunk I;
1765 I.Kind = MemberPointer;
1766 I.Loc = SS.getBeginLoc();
1767 I.EndLoc = EndLoc;
1768 new (&I.Mem) MemberPointerTypeInfo;
1769 I.Mem.StarLoc = StarLoc;
1770 I.Mem.TypeQuals = TypeQuals;
1771 new (I.Mem.ScopeMem) CXXScopeSpec(SS);
1772 return I;
1773 }
1774
1775
1776 static DeclaratorChunk getParen(SourceLocation LParenLoc,
1777 SourceLocation RParenLoc) {
1778 DeclaratorChunk I;
1779 I.Kind = Paren;
1780 I.Loc = LParenLoc;
1781 I.EndLoc = RParenLoc;
1782 return I;
1783 }
1784
1785 bool isParen() const {
1786 return Kind == Paren;
1787 }
1788 };
1789
1790
1791
1792 class DecompositionDeclarator {
1793 public:
1794 struct Binding {
1795 IdentifierInfo *Name;
1796 SourceLocation NameLoc;
1797 std::optional<ParsedAttributes> Attrs;
1798 };
1799
1800 private:
1801
1802 SourceLocation LSquareLoc, RSquareLoc;
1803
1804
1805 Binding *Bindings;
1806 unsigned NumBindings : 31;
1807 LLVM_PREFERRED_TYPE(bool)
1808 unsigned DeleteBindings : 1;
1809
1810 friend class Declarator;
1811
1812 public:
1813 DecompositionDeclarator()
1814 : Bindings(nullptr), NumBindings(0), DeleteBindings(false) {}
1815 DecompositionDeclarator(const DecompositionDeclarator &G) = delete;
1816 DecompositionDeclarator &operator=(const DecompositionDeclarator &G) = delete;
1817 ~DecompositionDeclarator() { clear(); }
1818
1819 void clear() {
1820 LSquareLoc = RSquareLoc = SourceLocation();
1821 if (DeleteBindings)
1822 delete[] Bindings;
1823 else
1824 llvm::for_each(llvm::MutableArrayRef(Bindings, NumBindings),
1825 [](Binding &B) { B.Attrs.reset(); });
1826 Bindings = nullptr;
1827 NumBindings = 0;
1828 DeleteBindings = false;
1829 }
1830
1831 ArrayRef<Binding> bindings() const {
1832 return llvm::ArrayRef(Bindings, NumBindings);
1833 }
1834
1835 bool isSet() const { return LSquareLoc.isValid(); }
1836
1837 SourceLocation getLSquareLoc() const { return LSquareLoc; }
1838 SourceLocation getRSquareLoc() const { return RSquareLoc; }
1839 SourceRange getSourceRange() const {
1840 return SourceRange(LSquareLoc, RSquareLoc);
1841 }
1842 };
1843
1844
1845
1846 enum class FunctionDefinitionKind {
1847 Declaration,
1848 Definition,
1849 Defaulted,
1850 Deleted
1851 };
1852
1853 enum class DeclaratorContext {
1854 File,
1855 Prototype,
1856 ObjCResult,
1857 ObjCParameter,
1858 KNRTypeList,
1859 TypeName,
1860 FunctionalCast,
1861 Member,
1862 Block,
1863 ForInit,
1864 SelectionInit,
1865 Condition,
1866 TemplateParam,
1867 CXXNew,
1868 CXXCatch,
1869 ObjCCatch,
1870 BlockLiteral,
1871 LambdaExpr,
1872 LambdaExprParameter,
1873 ConversionId,
1874 TrailingReturn,
1875 TrailingReturnVar,
1876 TemplateArg,
1877 TemplateTypeArg,
1878 AliasDecl,
1879 AliasTemplate,
1880 RequiresExpr,
1881 Association
1882 };
1883
1884
1885
1886 enum class ImplicitTypenameContext {
1887 No,
1888 Yes,
1889 };
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903 class Declarator {
1904
1905 private:
1906 const DeclSpec &DS;
1907 CXXScopeSpec SS;
1908 UnqualifiedId Name;
1909 SourceRange Range;
1910
1911
1912 DeclaratorContext Context;
1913
1914
1915 DecompositionDeclarator BindingGroup;
1916
1917
1918
1919
1920
1921 SmallVector<DeclaratorChunk, 8> DeclTypeInfo;
1922
1923
1924 LLVM_PREFERRED_TYPE(bool)
1925 unsigned InvalidType : 1;
1926
1927
1928 LLVM_PREFERRED_TYPE(bool)
1929 unsigned GroupingParens : 1;
1930
1931
1932
1933
1934
1935 LLVM_PREFERRED_TYPE(FunctionDefinitionKind)
1936 unsigned FunctionDefinition : 2;
1937
1938
1939 LLVM_PREFERRED_TYPE(bool)
1940 unsigned Redeclaration : 1;
1941
1942
1943 LLVM_PREFERRED_TYPE(bool)
1944 unsigned Extension : 1;
1945
1946
1947 LLVM_PREFERRED_TYPE(bool)
1948 unsigned ObjCIvar : 1;
1949
1950
1951 LLVM_PREFERRED_TYPE(bool)
1952 unsigned ObjCWeakProperty : 1;
1953
1954
1955 LLVM_PREFERRED_TYPE(bool)
1956 unsigned InlineStorageUsed : 1;
1957
1958
1959 LLVM_PREFERRED_TYPE(bool)
1960 unsigned HasInitializer : 1;
1961
1962
1963 ParsedAttributes Attrs;
1964
1965
1966
1967 const ParsedAttributesView &DeclarationAttrs;
1968
1969
1970 Expr *AsmLabel;
1971
1972
1973
1974 Expr *TrailingRequiresClause;
1975
1976
1977 ArrayRef<TemplateParameterList *> TemplateParameterLists;
1978
1979
1980
1981
1982 TemplateParameterList *InventedTemplateParameterList;
1983
1984 #ifndef _MSC_VER
1985 union {
1986 #endif
1987
1988
1989
1990 DeclaratorChunk::ParamInfo InlineParams[16];
1991 DecompositionDeclarator::Binding InlineBindings[16];
1992 #ifndef _MSC_VER
1993 };
1994 #endif
1995
1996
1997
1998 SourceLocation CommaLoc;
1999
2000
2001
2002 SourceLocation EllipsisLoc;
2003
2004 Expr *PackIndexingExpr;
2005
2006 friend struct DeclaratorChunk;
2007
2008 public:
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025 Declarator(const DeclSpec &DS, const ParsedAttributesView &DeclarationAttrs,
2026 DeclaratorContext C)
2027 : DS(DS), Range(DS.getSourceRange()), Context(C),
2028 InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error),
2029 GroupingParens(false), FunctionDefinition(static_cast<unsigned>(
2030 FunctionDefinitionKind::Declaration)),
2031 Redeclaration(false), Extension(false), ObjCIvar(false),
2032 ObjCWeakProperty(false), InlineStorageUsed(false),
2033 HasInitializer(false), Attrs(DS.getAttributePool().getFactory()),
2034 DeclarationAttrs(DeclarationAttrs), AsmLabel(nullptr),
2035 TrailingRequiresClause(nullptr),
2036 InventedTemplateParameterList(nullptr) {
2037 assert(llvm::all_of(DeclarationAttrs,
2038 [](const ParsedAttr &AL) {
2039 return (AL.isStandardAttributeSyntax() ||
2040 AL.isRegularKeywordAttribute());
2041 }) &&
2042 "DeclarationAttrs may only contain [[]] and keyword attributes");
2043 }
2044
2045 ~Declarator() {
2046 clear();
2047 }
2048
2049
2050 const DeclSpec &getDeclSpec() const { return DS; }
2051
2052
2053
2054
2055
2056
2057 DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); }
2058
2059 AttributePool &getAttributePool() const {
2060 return Attrs.getPool();
2061 }
2062
2063
2064
2065 const CXXScopeSpec &getCXXScopeSpec() const { return SS; }
2066 CXXScopeSpec &getCXXScopeSpec() { return SS; }
2067
2068
2069 UnqualifiedId &getName() { return Name; }
2070
2071 const DecompositionDeclarator &getDecompositionDeclarator() const {
2072 return BindingGroup;
2073 }
2074
2075 DeclaratorContext getContext() const { return Context; }
2076
2077 bool isPrototypeContext() const {
2078 return (Context == DeclaratorContext::Prototype ||
2079 Context == DeclaratorContext::ObjCParameter ||
2080 Context == DeclaratorContext::ObjCResult ||
2081 Context == DeclaratorContext::LambdaExprParameter);
2082 }
2083
2084
2085 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
2086 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
2087 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
2088
2089 void SetSourceRange(SourceRange R) { Range = R; }
2090
2091
2092 void SetRangeBegin(SourceLocation Loc) {
2093 if (!Loc.isInvalid())
2094 Range.setBegin(Loc);
2095 }
2096
2097 void SetRangeEnd(SourceLocation Loc) {
2098 if (!Loc.isInvalid())
2099 Range.setEnd(Loc);
2100 }
2101
2102
2103
2104 void ExtendWithDeclSpec(const DeclSpec &DS) {
2105 SourceRange SR = DS.getSourceRange();
2106 if (Range.getBegin().isInvalid())
2107 Range.setBegin(SR.getBegin());
2108 if (!SR.getEnd().isInvalid())
2109 Range.setEnd(SR.getEnd());
2110 }
2111
2112
2113 void clear() {
2114 SS.clear();
2115 Name.clear();
2116 Range = DS.getSourceRange();
2117 BindingGroup.clear();
2118
2119 for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i)
2120 DeclTypeInfo[i].destroy();
2121 DeclTypeInfo.clear();
2122 Attrs.clear();
2123 AsmLabel = nullptr;
2124 InlineStorageUsed = false;
2125 HasInitializer = false;
2126 ObjCIvar = false;
2127 ObjCWeakProperty = false;
2128 CommaLoc = SourceLocation();
2129 EllipsisLoc = SourceLocation();
2130 PackIndexingExpr = nullptr;
2131 }
2132
2133
2134
2135
2136 bool mayOmitIdentifier() const {
2137 switch (Context) {
2138 case DeclaratorContext::File:
2139 case DeclaratorContext::KNRTypeList:
2140 case DeclaratorContext::Member:
2141 case DeclaratorContext::Block:
2142 case DeclaratorContext::ForInit:
2143 case DeclaratorContext::SelectionInit:
2144 case DeclaratorContext::Condition:
2145 return false;
2146
2147 case DeclaratorContext::TypeName:
2148 case DeclaratorContext::FunctionalCast:
2149 case DeclaratorContext::AliasDecl:
2150 case DeclaratorContext::AliasTemplate:
2151 case DeclaratorContext::Prototype:
2152 case DeclaratorContext::LambdaExprParameter:
2153 case DeclaratorContext::ObjCParameter:
2154 case DeclaratorContext::ObjCResult:
2155 case DeclaratorContext::TemplateParam:
2156 case DeclaratorContext::CXXNew:
2157 case DeclaratorContext::CXXCatch:
2158 case DeclaratorContext::ObjCCatch:
2159 case DeclaratorContext::BlockLiteral:
2160 case DeclaratorContext::LambdaExpr:
2161 case DeclaratorContext::ConversionId:
2162 case DeclaratorContext::TemplateArg:
2163 case DeclaratorContext::TemplateTypeArg:
2164 case DeclaratorContext::TrailingReturn:
2165 case DeclaratorContext::TrailingReturnVar:
2166 case DeclaratorContext::RequiresExpr:
2167 case DeclaratorContext::Association:
2168 return true;
2169 }
2170 llvm_unreachable("unknown context kind!");
2171 }
2172
2173
2174
2175
2176 bool mayHaveIdentifier() const {
2177 switch (Context) {
2178 case DeclaratorContext::File:
2179 case DeclaratorContext::KNRTypeList:
2180 case DeclaratorContext::Member:
2181 case DeclaratorContext::Block:
2182 case DeclaratorContext::ForInit:
2183 case DeclaratorContext::SelectionInit:
2184 case DeclaratorContext::Condition:
2185 case DeclaratorContext::Prototype:
2186 case DeclaratorContext::LambdaExprParameter:
2187 case DeclaratorContext::TemplateParam:
2188 case DeclaratorContext::CXXCatch:
2189 case DeclaratorContext::ObjCCatch:
2190 case DeclaratorContext::RequiresExpr:
2191 return true;
2192
2193 case DeclaratorContext::TypeName:
2194 case DeclaratorContext::FunctionalCast:
2195 case DeclaratorContext::CXXNew:
2196 case DeclaratorContext::AliasDecl:
2197 case DeclaratorContext::AliasTemplate:
2198 case DeclaratorContext::ObjCParameter:
2199 case DeclaratorContext::ObjCResult:
2200 case DeclaratorContext::BlockLiteral:
2201 case DeclaratorContext::LambdaExpr:
2202 case DeclaratorContext::ConversionId:
2203 case DeclaratorContext::TemplateArg:
2204 case DeclaratorContext::TemplateTypeArg:
2205 case DeclaratorContext::TrailingReturn:
2206 case DeclaratorContext::TrailingReturnVar:
2207 case DeclaratorContext::Association:
2208 return false;
2209 }
2210 llvm_unreachable("unknown context kind!");
2211 }
2212
2213
2214 bool mayHaveDecompositionDeclarator() const {
2215 switch (Context) {
2216 case DeclaratorContext::File:
2217
2218
2219 case DeclaratorContext::Block:
2220 case DeclaratorContext::ForInit:
2221 case DeclaratorContext::SelectionInit:
2222 case DeclaratorContext::Condition:
2223 return true;
2224
2225 case DeclaratorContext::Member:
2226 case DeclaratorContext::Prototype:
2227 case DeclaratorContext::TemplateParam:
2228 case DeclaratorContext::RequiresExpr:
2229
2230 return false;
2231
2232
2233 case DeclaratorContext::KNRTypeList:
2234 case DeclaratorContext::TypeName:
2235 case DeclaratorContext::FunctionalCast:
2236 case DeclaratorContext::AliasDecl:
2237 case DeclaratorContext::AliasTemplate:
2238 case DeclaratorContext::LambdaExprParameter:
2239 case DeclaratorContext::ObjCParameter:
2240 case DeclaratorContext::ObjCResult:
2241 case DeclaratorContext::CXXNew:
2242 case DeclaratorContext::CXXCatch:
2243 case DeclaratorContext::ObjCCatch:
2244 case DeclaratorContext::BlockLiteral:
2245 case DeclaratorContext::LambdaExpr:
2246 case DeclaratorContext::ConversionId:
2247 case DeclaratorContext::TemplateArg:
2248 case DeclaratorContext::TemplateTypeArg:
2249 case DeclaratorContext::TrailingReturn:
2250 case DeclaratorContext::TrailingReturnVar:
2251 case DeclaratorContext::Association:
2252 return false;
2253 }
2254 llvm_unreachable("unknown context kind!");
2255 }
2256
2257
2258
2259 bool mayBeFollowedByCXXDirectInit() const {
2260 if (hasGroupingParens()) return false;
2261
2262 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
2263 return false;
2264
2265 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern &&
2266 Context != DeclaratorContext::File)
2267 return false;
2268
2269
2270 if (Name.getKind() != UnqualifiedIdKind::IK_Identifier)
2271 return false;
2272
2273 switch (Context) {
2274 case DeclaratorContext::File:
2275 case DeclaratorContext::Block:
2276 case DeclaratorContext::ForInit:
2277 case DeclaratorContext::SelectionInit:
2278 case DeclaratorContext::TrailingReturnVar:
2279 return true;
2280
2281 case DeclaratorContext::Condition:
2282
2283
2284
2285 return true;
2286
2287 case DeclaratorContext::KNRTypeList:
2288 case DeclaratorContext::Member:
2289 case DeclaratorContext::Prototype:
2290 case DeclaratorContext::LambdaExprParameter:
2291 case DeclaratorContext::ObjCParameter:
2292 case DeclaratorContext::ObjCResult:
2293 case DeclaratorContext::TemplateParam:
2294 case DeclaratorContext::CXXCatch:
2295 case DeclaratorContext::ObjCCatch:
2296 case DeclaratorContext::TypeName:
2297 case DeclaratorContext::FunctionalCast:
2298 case DeclaratorContext::CXXNew:
2299 case DeclaratorContext::AliasDecl:
2300 case DeclaratorContext::AliasTemplate:
2301 case DeclaratorContext::BlockLiteral:
2302 case DeclaratorContext::LambdaExpr:
2303 case DeclaratorContext::ConversionId:
2304 case DeclaratorContext::TemplateArg:
2305 case DeclaratorContext::TemplateTypeArg:
2306 case DeclaratorContext::TrailingReturn:
2307 case DeclaratorContext::RequiresExpr:
2308 case DeclaratorContext::Association:
2309 return false;
2310 }
2311 llvm_unreachable("unknown context kind!");
2312 }
2313
2314
2315
2316
2317 bool isPastIdentifier() const { return Name.isValid(); }
2318
2319
2320
2321
2322
2323 bool hasName() const {
2324 return Name.getKind() != UnqualifiedIdKind::IK_Identifier ||
2325 Name.Identifier || isDecompositionDeclarator();
2326 }
2327
2328
2329 bool isDecompositionDeclarator() const {
2330 return BindingGroup.isSet();
2331 }
2332
2333 const IdentifierInfo *getIdentifier() const {
2334 if (Name.getKind() == UnqualifiedIdKind::IK_Identifier)
2335 return Name.Identifier;
2336
2337 return nullptr;
2338 }
2339 SourceLocation getIdentifierLoc() const { return Name.StartLocation; }
2340
2341
2342 void SetIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) {
2343 Name.setIdentifier(Id, IdLoc);
2344 }
2345
2346
2347 void setDecompositionBindings(
2348 SourceLocation LSquareLoc,
2349 MutableArrayRef<DecompositionDeclarator::Binding> Bindings,
2350 SourceLocation RSquareLoc);
2351
2352
2353
2354
2355
2356 void AddTypeInfo(const DeclaratorChunk &TI, ParsedAttributes &&attrs,
2357 SourceLocation EndLoc) {
2358 DeclTypeInfo.push_back(TI);
2359 DeclTypeInfo.back().getAttrs().addAll(attrs.begin(), attrs.end());
2360 getAttributePool().takeAllFrom(attrs.getPool());
2361
2362 if (!EndLoc.isInvalid())
2363 SetRangeEnd(EndLoc);
2364 }
2365
2366
2367
2368
2369
2370 void AddTypeInfo(const DeclaratorChunk &TI, AttributePool &OtherPool,
2371 SourceLocation EndLoc) {
2372 DeclTypeInfo.push_back(TI);
2373 getAttributePool().takeFrom(DeclTypeInfo.back().getAttrs(), OtherPool);
2374
2375 if (!EndLoc.isInvalid())
2376 SetRangeEnd(EndLoc);
2377 }
2378
2379
2380
2381 void AddTypeInfo(const DeclaratorChunk &TI, SourceLocation EndLoc) {
2382 DeclTypeInfo.push_back(TI);
2383
2384 assert(TI.AttrList.empty() &&
2385 "Cannot add a declarator chunk with attributes with this overload");
2386
2387 if (!EndLoc.isInvalid())
2388 SetRangeEnd(EndLoc);
2389 }
2390
2391
2392 void AddInnermostTypeInfo(const DeclaratorChunk &TI) {
2393 DeclTypeInfo.insert(DeclTypeInfo.begin(), TI);
2394 }
2395
2396
2397 unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); }
2398
2399
2400
2401 const DeclaratorChunk &getTypeObject(unsigned i) const {
2402 assert(i < DeclTypeInfo.size() && "Invalid type chunk");
2403 return DeclTypeInfo[i];
2404 }
2405 DeclaratorChunk &getTypeObject(unsigned i) {
2406 assert(i < DeclTypeInfo.size() && "Invalid type chunk");
2407 return DeclTypeInfo[i];
2408 }
2409
2410 typedef SmallVectorImpl<DeclaratorChunk>::const_iterator type_object_iterator;
2411 typedef llvm::iterator_range<type_object_iterator> type_object_range;
2412
2413
2414 type_object_range type_objects() const {
2415 return type_object_range(DeclTypeInfo.begin(), DeclTypeInfo.end());
2416 }
2417
2418 void DropFirstTypeObject() {
2419 assert(!DeclTypeInfo.empty() && "No type chunks to drop.");
2420 DeclTypeInfo.front().destroy();
2421 DeclTypeInfo.erase(DeclTypeInfo.begin());
2422 }
2423
2424
2425
2426
2427 const DeclaratorChunk *getInnermostNonParenChunk() const {
2428 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
2429 if (!DeclTypeInfo[i].isParen())
2430 return &DeclTypeInfo[i];
2431 }
2432 return nullptr;
2433 }
2434
2435
2436
2437
2438 const DeclaratorChunk *getOutermostNonParenChunk() const {
2439 for (unsigned i = DeclTypeInfo.size(), i_end = 0; i != i_end; --i) {
2440 if (!DeclTypeInfo[i-1].isParen())
2441 return &DeclTypeInfo[i-1];
2442 }
2443 return nullptr;
2444 }
2445
2446
2447
2448
2449 bool isArrayOfUnknownBound() const {
2450 const DeclaratorChunk *chunk = getInnermostNonParenChunk();
2451 return (chunk && chunk->Kind == DeclaratorChunk::Array &&
2452 !chunk->Arr.NumElts);
2453 }
2454
2455
2456
2457
2458
2459 bool isFunctionDeclarator(unsigned& idx) const {
2460 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
2461 switch (DeclTypeInfo[i].Kind) {
2462 case DeclaratorChunk::Function:
2463 idx = i;
2464 return true;
2465 case DeclaratorChunk::Paren:
2466 continue;
2467 case DeclaratorChunk::Pointer:
2468 case DeclaratorChunk::Reference:
2469 case DeclaratorChunk::Array:
2470 case DeclaratorChunk::BlockPointer:
2471 case DeclaratorChunk::MemberPointer:
2472 case DeclaratorChunk::Pipe:
2473 return false;
2474 }
2475 llvm_unreachable("Invalid type chunk");
2476 }
2477 return false;
2478 }
2479
2480
2481
2482
2483 bool isFunctionDeclarator() const {
2484 unsigned index;
2485 return isFunctionDeclarator(index);
2486 }
2487
2488
2489
2490 DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() {
2491 assert(isFunctionDeclarator() && "Not a function declarator!");
2492 unsigned index = 0;
2493 isFunctionDeclarator(index);
2494 return DeclTypeInfo[index].Fun;
2495 }
2496
2497
2498
2499 const DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() const {
2500 return const_cast<Declarator*>(this)->getFunctionTypeInfo();
2501 }
2502
2503
2504
2505
2506
2507
2508
2509 bool isDeclarationOfFunction() const;
2510
2511
2512
2513 bool isFunctionDeclarationContext() const {
2514 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
2515 return false;
2516
2517 switch (Context) {
2518 case DeclaratorContext::File:
2519 case DeclaratorContext::Member:
2520 case DeclaratorContext::Block:
2521 case DeclaratorContext::ForInit:
2522 case DeclaratorContext::SelectionInit:
2523 return true;
2524
2525 case DeclaratorContext::Condition:
2526 case DeclaratorContext::KNRTypeList:
2527 case DeclaratorContext::TypeName:
2528 case DeclaratorContext::FunctionalCast:
2529 case DeclaratorContext::AliasDecl:
2530 case DeclaratorContext::AliasTemplate:
2531 case DeclaratorContext::Prototype:
2532 case DeclaratorContext::LambdaExprParameter:
2533 case DeclaratorContext::ObjCParameter:
2534 case DeclaratorContext::ObjCResult:
2535 case DeclaratorContext::TemplateParam:
2536 case DeclaratorContext::CXXNew:
2537 case DeclaratorContext::CXXCatch:
2538 case DeclaratorContext::ObjCCatch:
2539 case DeclaratorContext::BlockLiteral:
2540 case DeclaratorContext::LambdaExpr:
2541 case DeclaratorContext::ConversionId:
2542 case DeclaratorContext::TemplateArg:
2543 case DeclaratorContext::TemplateTypeArg:
2544 case DeclaratorContext::TrailingReturn:
2545 case DeclaratorContext::TrailingReturnVar:
2546 case DeclaratorContext::RequiresExpr:
2547 case DeclaratorContext::Association:
2548 return false;
2549 }
2550 llvm_unreachable("unknown context kind!");
2551 }
2552
2553
2554
2555 bool isExpressionContext() const {
2556 switch (Context) {
2557 case DeclaratorContext::File:
2558 case DeclaratorContext::KNRTypeList:
2559 case DeclaratorContext::Member:
2560
2561
2562 case DeclaratorContext::TypeName:
2563
2564 case DeclaratorContext::FunctionalCast:
2565 case DeclaratorContext::AliasDecl:
2566 case DeclaratorContext::AliasTemplate:
2567 case DeclaratorContext::Prototype:
2568 case DeclaratorContext::LambdaExprParameter:
2569 case DeclaratorContext::ObjCParameter:
2570 case DeclaratorContext::ObjCResult:
2571 case DeclaratorContext::TemplateParam:
2572 case DeclaratorContext::CXXNew:
2573 case DeclaratorContext::CXXCatch:
2574 case DeclaratorContext::ObjCCatch:
2575 case DeclaratorContext::BlockLiteral:
2576 case DeclaratorContext::LambdaExpr:
2577 case DeclaratorContext::ConversionId:
2578 case DeclaratorContext::TrailingReturn:
2579 case DeclaratorContext::TrailingReturnVar:
2580 case DeclaratorContext::TemplateTypeArg:
2581 case DeclaratorContext::RequiresExpr:
2582 case DeclaratorContext::Association:
2583 return false;
2584
2585 case DeclaratorContext::Block:
2586 case DeclaratorContext::ForInit:
2587 case DeclaratorContext::SelectionInit:
2588 case DeclaratorContext::Condition:
2589 case DeclaratorContext::TemplateArg:
2590 return true;
2591 }
2592
2593 llvm_unreachable("unknown context kind!");
2594 }
2595
2596
2597
2598 bool isFunctionDeclaratorAFunctionDeclaration() const {
2599 if (!isFunctionDeclarationContext())
2600 return false;
2601
2602 for (unsigned I = 0, N = getNumTypeObjects(); I != N; ++I)
2603 if (getTypeObject(I).Kind != DeclaratorChunk::Paren)
2604 return false;
2605
2606 return true;
2607 }
2608
2609
2610
2611 bool hasTrailingReturnType() const {
2612 for (const auto &Chunk : type_objects())
2613 if (Chunk.Kind == DeclaratorChunk::Function &&
2614 Chunk.Fun.hasTrailingReturnType())
2615 return true;
2616 return false;
2617 }
2618
2619
2620 ParsedType getTrailingReturnType() const {
2621 for (const auto &Chunk : type_objects())
2622 if (Chunk.Kind == DeclaratorChunk::Function &&
2623 Chunk.Fun.hasTrailingReturnType())
2624 return Chunk.Fun.getTrailingReturnType();
2625 return ParsedType();
2626 }
2627
2628
2629 void setTrailingRequiresClause(Expr *TRC) {
2630 TrailingRequiresClause = TRC;
2631
2632 SetRangeEnd(TRC->getEndLoc());
2633 }
2634
2635
2636 Expr *getTrailingRequiresClause() {
2637 return TrailingRequiresClause;
2638 }
2639
2640
2641
2642 bool hasTrailingRequiresClause() const {
2643 return TrailingRequiresClause != nullptr;
2644 }
2645
2646
2647 void setTemplateParameterLists(ArrayRef<TemplateParameterList *> TPLs) {
2648 TemplateParameterLists = TPLs;
2649 }
2650
2651
2652 ArrayRef<TemplateParameterList *> getTemplateParameterLists() const {
2653 return TemplateParameterLists;
2654 }
2655
2656
2657
2658
2659 void setInventedTemplateParameterList(TemplateParameterList *Invented) {
2660 InventedTemplateParameterList = Invented;
2661 }
2662
2663
2664
2665
2666 TemplateParameterList * getInventedTemplateParameterList() const {
2667 return InventedTemplateParameterList;
2668 }
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679 void takeAttributes(ParsedAttributes &attrs) {
2680 Attrs.takeAllFrom(attrs);
2681
2682 if (attrs.Range.getEnd().isValid())
2683 SetRangeEnd(attrs.Range.getEnd());
2684 }
2685
2686 const ParsedAttributes &getAttributes() const { return Attrs; }
2687 ParsedAttributes &getAttributes() { return Attrs; }
2688
2689 const ParsedAttributesView &getDeclarationAttributes() const {
2690 return DeclarationAttrs;
2691 }
2692
2693
2694 bool hasAttributes() const {
2695 if (!getAttributes().empty() || !getDeclarationAttributes().empty() ||
2696 getDeclSpec().hasAttributes())
2697 return true;
2698 for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i)
2699 if (!getTypeObject(i).getAttrs().empty())
2700 return true;
2701 return false;
2702 }
2703
2704 void setAsmLabel(Expr *E) { AsmLabel = E; }
2705 Expr *getAsmLabel() const { return AsmLabel; }
2706
2707 void setExtension(bool Val = true) { Extension = Val; }
2708 bool getExtension() const { return Extension; }
2709
2710 void setObjCIvar(bool Val = true) { ObjCIvar = Val; }
2711 bool isObjCIvar() const { return ObjCIvar; }
2712
2713 void setObjCWeakProperty(bool Val = true) { ObjCWeakProperty = Val; }
2714 bool isObjCWeakProperty() const { return ObjCWeakProperty; }
2715
2716 void setInvalidType(bool Val = true) { InvalidType = Val; }
2717 bool isInvalidType() const {
2718 return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error;
2719 }
2720
2721 void setGroupingParens(bool flag) { GroupingParens = flag; }
2722 bool hasGroupingParens() const { return GroupingParens; }
2723
2724 bool isFirstDeclarator() const { return !CommaLoc.isValid(); }
2725 SourceLocation getCommaLoc() const { return CommaLoc; }
2726 void setCommaLoc(SourceLocation CL) { CommaLoc = CL; }
2727
2728 bool hasEllipsis() const { return EllipsisLoc.isValid(); }
2729 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
2730 void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; }
2731
2732 bool hasPackIndexing() const { return PackIndexingExpr != nullptr; }
2733 Expr *getPackIndexingExpr() const { return PackIndexingExpr; }
2734 void setPackIndexingExpr(Expr *PI) { PackIndexingExpr = PI; }
2735
2736 void setFunctionDefinitionKind(FunctionDefinitionKind Val) {
2737 FunctionDefinition = static_cast<unsigned>(Val);
2738 }
2739
2740 bool isFunctionDefinition() const {
2741 return getFunctionDefinitionKind() != FunctionDefinitionKind::Declaration;
2742 }
2743
2744 FunctionDefinitionKind getFunctionDefinitionKind() const {
2745 return (FunctionDefinitionKind)FunctionDefinition;
2746 }
2747
2748 void setHasInitializer(bool Val = true) { HasInitializer = Val; }
2749 bool hasInitializer() const { return HasInitializer; }
2750
2751
2752 bool isFirstDeclarationOfMember() {
2753 return getContext() == DeclaratorContext::Member &&
2754 !getDeclSpec().isFriendSpecified();
2755 }
2756
2757
2758
2759
2760 bool isStaticMember();
2761
2762 bool isExplicitObjectMemberFunction();
2763
2764
2765 bool isCtorOrDtor();
2766
2767 void setRedeclaration(bool Val) { Redeclaration = Val; }
2768 bool isRedeclaration() const { return Redeclaration; }
2769 };
2770
2771
2772
2773 struct FieldDeclarator {
2774 Declarator D;
2775 Expr *BitfieldSize;
2776 explicit FieldDeclarator(const DeclSpec &DS,
2777 const ParsedAttributes &DeclarationAttrs)
2778 : D(DS, DeclarationAttrs, DeclaratorContext::Member),
2779 BitfieldSize(nullptr) {}
2780 };
2781
2782
2783 class VirtSpecifiers {
2784 public:
2785 enum Specifier {
2786 VS_None = 0,
2787 VS_Override = 1,
2788 VS_Final = 2,
2789 VS_Sealed = 4,
2790
2791 VS_GNU_Final = 8,
2792 VS_Abstract = 16
2793 };
2794
2795 VirtSpecifiers() = default;
2796
2797 bool SetSpecifier(Specifier VS, SourceLocation Loc,
2798 const char *&PrevSpec);
2799
2800 bool isUnset() const { return Specifiers == 0; }
2801
2802 bool isOverrideSpecified() const { return Specifiers & VS_Override; }
2803 SourceLocation getOverrideLoc() const { return VS_overrideLoc; }
2804
2805 bool isFinalSpecified() const { return Specifiers & (VS_Final | VS_Sealed | VS_GNU_Final); }
2806 bool isFinalSpelledSealed() const { return Specifiers & VS_Sealed; }
2807 SourceLocation getFinalLoc() const { return VS_finalLoc; }
2808 SourceLocation getAbstractLoc() const { return VS_abstractLoc; }
2809
2810 void clear() { Specifiers = 0; }
2811
2812 static const char *getSpecifierName(Specifier VS);
2813
2814 SourceLocation getFirstLocation() const { return FirstLocation; }
2815 SourceLocation getLastLocation() const { return LastLocation; }
2816 Specifier getLastSpecifier() const { return LastSpecifier; }
2817
2818 private:
2819 unsigned Specifiers = 0;
2820 Specifier LastSpecifier = VS_None;
2821
2822 SourceLocation VS_overrideLoc, VS_finalLoc, VS_abstractLoc;
2823 SourceLocation FirstLocation;
2824 SourceLocation LastLocation;
2825 };
2826
2827 enum class LambdaCaptureInitKind {
2828 NoInit,
2829 CopyInit,
2830 DirectInit,
2831 ListInit
2832 };
2833
2834
2835 struct LambdaIntroducer {
2836
2837 struct LambdaCapture {
2838 LambdaCaptureKind Kind;
2839 SourceLocation Loc;
2840 IdentifierInfo *Id;
2841 SourceLocation EllipsisLoc;
2842 LambdaCaptureInitKind InitKind;
2843 ExprResult Init;
2844 ParsedType InitCaptureType;
2845 SourceRange ExplicitRange;
2846
2847 LambdaCapture(LambdaCaptureKind Kind, SourceLocation Loc,
2848 IdentifierInfo *Id, SourceLocation EllipsisLoc,
2849 LambdaCaptureInitKind InitKind, ExprResult Init,
2850 ParsedType InitCaptureType,
2851 SourceRange ExplicitRange)
2852 : Kind(Kind), Loc(Loc), Id(Id), EllipsisLoc(EllipsisLoc),
2853 InitKind(InitKind), Init(Init), InitCaptureType(InitCaptureType),
2854 ExplicitRange(ExplicitRange) {}
2855 };
2856
2857 SourceRange Range;
2858 SourceLocation DefaultLoc;
2859 LambdaCaptureDefault Default = LCD_None;
2860 SmallVector<LambdaCapture, 4> Captures;
2861
2862 LambdaIntroducer() = default;
2863
2864 bool hasLambdaCapture() const {
2865 return Captures.size() > 0 || Default != LCD_None;
2866 }
2867
2868
2869 void addCapture(LambdaCaptureKind Kind,
2870 SourceLocation Loc,
2871 IdentifierInfo* Id,
2872 SourceLocation EllipsisLoc,
2873 LambdaCaptureInitKind InitKind,
2874 ExprResult Init,
2875 ParsedType InitCaptureType,
2876 SourceRange ExplicitRange) {
2877 Captures.push_back(LambdaCapture(Kind, Loc, Id, EllipsisLoc, InitKind, Init,
2878 InitCaptureType, ExplicitRange));
2879 }
2880 };
2881
2882 struct InventedTemplateParameterInfo {
2883
2884
2885
2886 unsigned NumExplicitTemplateParams = 0;
2887
2888
2889
2890 unsigned AutoTemplateParameterDepth = 0;
2891
2892
2893
2894
2895
2896
2897
2898
2899 SmallVector<NamedDecl*, 4> TemplateParams;
2900 };
2901
2902 }
2903
2904 #endif