1 //===--- DeclSpec.h - Parsed declaration specifiers -------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file defines the classes used to store parsed information about
12 /// declaration-specifiers and declarators.
15 /// static const int volatile x, *y, *(*(*z)[10])(const void *x);
16 /// ------------------------- - -- ---------------------------
17 /// declaration-specifiers \ | /
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_CLANG_SEMA_DECLSPEC_H
24 #define LLVM_CLANG_SEMA_DECLSPEC_H
26 #include "clang/AST/NestedNameSpecifier.h"
27 #include "clang/Basic/ExceptionSpecificationType.h"
28 #include "clang/Basic/Lambda.h"
29 #include "clang/Basic/OperatorKinds.h"
30 #include "clang/Basic/Specifiers.h"
31 #include "clang/Lex/Token.h"
32 #include "clang/Sema/Ownership.h"
33 #include "clang/Sema/ParsedAttr.h"
34 #include "llvm/ADT/SmallVector.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/ErrorHandling.h"
44 class NamespaceAliasDecl;
49 struct TemplateIdAnnotation;
51 /// Represents a C++ nested-name-specifier or a global scope specifier.
53 /// These can be in 3 states:
54 /// 1) Not present, identified by isEmpty()
55 /// 2) Present, identified by isNotEmpty()
56 /// 2.a) Valid, identified by isValid()
57 /// 2.b) Invalid, identified by isInvalid().
59 /// isSet() is deprecated because it mostly corresponded to "valid" but was
60 /// often used as if it meant "present".
62 /// The actual scope is described by getScopeRep().
65 NestedNameSpecifierLocBuilder Builder;
68 SourceRange getRange() const { return Range; }
69 void setRange(SourceRange R) { Range = R; }
70 void setBeginLoc(SourceLocation Loc) { Range.setBegin(Loc); }
71 void setEndLoc(SourceLocation Loc) { Range.setEnd(Loc); }
72 SourceLocation getBeginLoc() const { return Range.getBegin(); }
73 SourceLocation getEndLoc() const { return Range.getEnd(); }
75 /// Retrieve the representation of the nested-name-specifier.
76 NestedNameSpecifier *getScopeRep() const {
77 return Builder.getRepresentation();
80 /// Extend the current nested-name-specifier by another
81 /// nested-name-specifier component of the form 'type::'.
83 /// \param Context The AST context in which this nested-name-specifier
86 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
88 /// \param TL The TypeLoc that describes the type preceding the '::'.
90 /// \param ColonColonLoc The location of the trailing '::'.
91 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
92 SourceLocation ColonColonLoc);
94 /// Extend the current nested-name-specifier by another
95 /// nested-name-specifier component of the form 'identifier::'.
97 /// \param Context The AST context in which this nested-name-specifier
100 /// \param Identifier The identifier.
102 /// \param IdentifierLoc The location of the identifier.
104 /// \param ColonColonLoc The location of the trailing '::'.
105 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
106 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
108 /// Extend the current nested-name-specifier by another
109 /// nested-name-specifier component of the form 'namespace::'.
111 /// \param Context The AST context in which this nested-name-specifier
114 /// \param Namespace The namespace.
116 /// \param NamespaceLoc The location of the namespace name.
118 /// \param ColonColonLoc The location of the trailing '::'.
119 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
120 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
122 /// Extend the current nested-name-specifier by another
123 /// nested-name-specifier component of the form 'namespace-alias::'.
125 /// \param Context The AST context in which this nested-name-specifier
128 /// \param Alias The namespace alias.
130 /// \param AliasLoc The location of the namespace alias
133 /// \param ColonColonLoc The location of the trailing '::'.
134 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
135 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
137 /// Turn this (empty) nested-name-specifier into the global
138 /// nested-name-specifier '::'.
139 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
141 /// Turns this (empty) nested-name-specifier into '__super'
142 /// nested-name-specifier.
144 /// \param Context The AST context in which this nested-name-specifier
147 /// \param RD The declaration of the class in which nested-name-specifier
150 /// \param SuperLoc The location of the '__super' keyword.
153 /// \param ColonColonLoc The location of the trailing '::'.
154 void MakeSuper(ASTContext &Context, CXXRecordDecl *RD,
155 SourceLocation SuperLoc, SourceLocation ColonColonLoc);
157 /// Make a new nested-name-specifier from incomplete source-location
160 /// FIXME: This routine should be used very, very rarely, in cases where we
161 /// need to synthesize a nested-name-specifier. Most code should instead use
162 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
163 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
166 /// Adopt an existing nested-name-specifier (with source-range
168 void Adopt(NestedNameSpecifierLoc Other);
170 /// Retrieve a nested-name-specifier with location information, copied
171 /// into the given AST context.
173 /// \param Context The context into which this nested-name-specifier will be
175 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
177 /// Retrieve the location of the name in the last qualifier
178 /// in this nested name specifier.
180 /// For example, the location of \c bar
186 SourceLocation getLastQualifierNameLoc() const;
188 /// No scope specifier.
189 bool isEmpty() const { return !Range.isValid(); }
190 /// A scope specifier is present, but may be valid or invalid.
191 bool isNotEmpty() const { return !isEmpty(); }
193 /// An error occurred during parsing of the scope specifier.
194 bool isInvalid() const { return isNotEmpty() && getScopeRep() == nullptr; }
195 /// A scope specifier is present, and it refers to a real scope.
196 bool isValid() const { return isNotEmpty() && getScopeRep() != nullptr; }
198 /// Indicate that this nested-name-specifier is invalid.
199 void SetInvalid(SourceRange R) {
200 assert(R.isValid() && "Must have a valid source range");
201 if (Range.getBegin().isInvalid())
202 Range.setBegin(R.getBegin());
203 Range.setEnd(R.getEnd());
207 /// Deprecated. Some call sites intend isNotEmpty() while others intend
209 bool isSet() const { return getScopeRep() != nullptr; }
212 Range = SourceRange();
216 /// Retrieve the data associated with the source-location information.
217 char *location_data() const { return Builder.getBuffer().first; }
219 /// Retrieve the size of the data associated with source-location
221 unsigned location_size() const { return Builder.getBuffer().second; }
224 /// Captures information about "declaration specifiers".
226 /// "Declaration specifiers" encompasses storage-class-specifiers,
227 /// type-specifiers, type-qualifiers, and function-specifiers.
230 /// storage-class-specifier
231 /// \note The order of these enumerators is important for diagnostics.
243 // Import thread storage class specifier enumeration and constants.
244 // These can be combined with SCS_extern and SCS_static.
245 typedef ThreadStorageClassSpecifier TSCS;
246 static const TSCS TSCS_unspecified = clang::TSCS_unspecified;
247 static const TSCS TSCS___thread = clang::TSCS___thread;
248 static const TSCS TSCS_thread_local = clang::TSCS_thread_local;
249 static const TSCS TSCS__Thread_local = clang::TSCS__Thread_local;
251 // Import type specifier width enumeration and constants.
252 typedef TypeSpecifierWidth TSW;
253 static const TSW TSW_unspecified = clang::TSW_unspecified;
254 static const TSW TSW_short = clang::TSW_short;
255 static const TSW TSW_long = clang::TSW_long;
256 static const TSW TSW_longlong = clang::TSW_longlong;
264 // Import type specifier sign enumeration and constants.
265 typedef TypeSpecifierSign TSS;
266 static const TSS TSS_unspecified = clang::TSS_unspecified;
267 static const TSS TSS_signed = clang::TSS_signed;
268 static const TSS TSS_unsigned = clang::TSS_unsigned;
270 // Import type specifier type enumeration and constants.
271 typedef TypeSpecifierType TST;
272 static const TST TST_unspecified = clang::TST_unspecified;
273 static const TST TST_void = clang::TST_void;
274 static const TST TST_char = clang::TST_char;
275 static const TST TST_wchar = clang::TST_wchar;
276 static const TST TST_char8 = clang::TST_char8;
277 static const TST TST_char16 = clang::TST_char16;
278 static const TST TST_char32 = clang::TST_char32;
279 static const TST TST_int = clang::TST_int;
280 static const TST TST_int128 = clang::TST_int128;
281 static const TST TST_half = clang::TST_half;
282 static const TST TST_float = clang::TST_float;
283 static const TST TST_double = clang::TST_double;
284 static const TST TST_float16 = clang::TST_Float16;
285 static const TST TST_accum = clang::TST_Accum;
286 static const TST TST_fract = clang::TST_Fract;
287 static const TST TST_float128 = clang::TST_float128;
288 static const TST TST_bool = clang::TST_bool;
289 static const TST TST_decimal32 = clang::TST_decimal32;
290 static const TST TST_decimal64 = clang::TST_decimal64;
291 static const TST TST_decimal128 = clang::TST_decimal128;
292 static const TST TST_enum = clang::TST_enum;
293 static const TST TST_union = clang::TST_union;
294 static const TST TST_struct = clang::TST_struct;
295 static const TST TST_interface = clang::TST_interface;
296 static const TST TST_class = clang::TST_class;
297 static const TST TST_typename = clang::TST_typename;
298 static const TST TST_typeofType = clang::TST_typeofType;
299 static const TST TST_typeofExpr = clang::TST_typeofExpr;
300 static const TST TST_decltype = clang::TST_decltype;
301 static const TST TST_decltype_auto = clang::TST_decltype_auto;
302 static const TST TST_underlyingType = clang::TST_underlyingType;
303 static const TST TST_auto = clang::TST_auto;
304 static const TST TST_auto_type = clang::TST_auto_type;
305 static const TST TST_unknown_anytype = clang::TST_unknown_anytype;
306 static const TST TST_atomic = clang::TST_atomic;
307 #define GENERIC_IMAGE_TYPE(ImgType, Id) \
308 static const TST TST_##ImgType##_t = clang::TST_##ImgType##_t;
309 #include "clang/Basic/OpenCLImageTypes.def"
310 static const TST TST_error = clang::TST_error;
313 enum TQ { // NOTE: These flags must be kept in sync with Qualifiers::TQ.
319 // This has no corresponding Qualifiers::TQ value, because it's not treated
320 // as a qualifier in our type system.
324 /// ParsedSpecifiers - Flags to query which specifiers were applied. This is
325 /// returned by getParsedSpecifiers.
326 enum ParsedSpecifiers {
328 PQ_StorageClassSpecifier = 1,
329 PQ_TypeSpecifier = 2,
330 PQ_TypeQualifier = 4,
331 PQ_FunctionSpecifier = 8
332 // FIXME: Attributes should be included here.
336 // storage-class-specifier
337 /*SCS*/unsigned StorageClassSpec : 3;
338 /*TSCS*/unsigned ThreadStorageClassSpec : 2;
339 unsigned SCS_extern_in_linkage_spec : 1;
342 /*TSW*/unsigned TypeSpecWidth : 2;
343 /*TSC*/unsigned TypeSpecComplex : 2;
344 /*TSS*/unsigned TypeSpecSign : 2;
345 /*TST*/unsigned TypeSpecType : 6;
346 unsigned TypeAltiVecVector : 1;
347 unsigned TypeAltiVecPixel : 1;
348 unsigned TypeAltiVecBool : 1;
349 unsigned TypeSpecOwned : 1;
350 unsigned TypeSpecPipe : 1;
351 unsigned TypeSpecSat : 1;
354 unsigned TypeQualifiers : 5; // Bitwise OR of TQ.
356 // function-specifier
357 unsigned FS_inline_specified : 1;
358 unsigned FS_forceinline_specified: 1;
359 unsigned FS_virtual_specified : 1;
360 unsigned FS_explicit_specified : 1;
361 unsigned FS_noreturn_specified : 1;
364 unsigned Friend_specified : 1;
366 // constexpr-specifier
367 unsigned Constexpr_specified : 1;
370 UnionParsedType TypeRep;
376 ParsedAttributes Attrs;
378 // Scope specifier for the type spec, if applicable.
379 CXXScopeSpec TypeScope;
381 // SourceLocation info. These are null if the item wasn't specified or if
382 // the setting was synthesized.
385 SourceLocation StorageClassSpecLoc, ThreadStorageClassSpecLoc;
386 SourceRange TSWRange;
387 SourceLocation TSCLoc, TSSLoc, TSTLoc, AltiVecLoc, TSSatLoc;
388 /// TSTNameLoc - If TypeSpecType is any of class, enum, struct, union,
389 /// typename, then this is the location of the named type (if present);
390 /// otherwise, it is the same as TSTLoc. Hence, the pair TSTLoc and
391 /// TSTNameLoc provides source range info for tag types.
392 SourceLocation TSTNameLoc;
393 SourceRange TypeofParensRange;
394 SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc,
396 SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc, FS_noreturnLoc;
397 SourceLocation FS_forceinlineLoc;
398 SourceLocation FriendLoc, ModulePrivateLoc, ConstexprLoc;
399 SourceLocation TQ_pipeLoc;
401 WrittenBuiltinSpecs writtenBS;
402 void SaveWrittenBuiltinSpecs();
404 ObjCDeclSpec *ObjCQualifiers;
406 static bool isTypeRep(TST T) {
407 return (T == TST_typename || T == TST_typeofType ||
408 T == TST_underlyingType || T == TST_atomic);
410 static bool isExprRep(TST T) {
411 return (T == TST_typeofExpr || T == TST_decltype);
414 DeclSpec(const DeclSpec &) = delete;
415 void operator=(const DeclSpec &) = delete;
417 static bool isDeclRep(TST T) {
418 return (T == TST_enum || T == TST_struct ||
419 T == TST_interface || T == TST_union ||
423 DeclSpec(AttributeFactory &attrFactory)
424 : StorageClassSpec(SCS_unspecified),
425 ThreadStorageClassSpec(TSCS_unspecified),
426 SCS_extern_in_linkage_spec(false),
427 TypeSpecWidth(TSW_unspecified),
428 TypeSpecComplex(TSC_unspecified),
429 TypeSpecSign(TSS_unspecified),
430 TypeSpecType(TST_unspecified),
431 TypeAltiVecVector(false),
432 TypeAltiVecPixel(false),
433 TypeAltiVecBool(false),
434 TypeSpecOwned(false),
437 TypeQualifiers(TQ_unspecified),
438 FS_inline_specified(false),
439 FS_forceinline_specified(false),
440 FS_virtual_specified(false),
441 FS_explicit_specified(false),
442 FS_noreturn_specified(false),
443 Friend_specified(false),
444 Constexpr_specified(false),
447 ObjCQualifiers(nullptr) {
450 // storage-class-specifier
451 SCS getStorageClassSpec() const { return (SCS)StorageClassSpec; }
452 TSCS getThreadStorageClassSpec() const {
453 return (TSCS)ThreadStorageClassSpec;
455 bool isExternInLinkageSpec() const { return SCS_extern_in_linkage_spec; }
456 void setExternInLinkageSpec(bool Value) {
457 SCS_extern_in_linkage_spec = Value;
460 SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; }
461 SourceLocation getThreadStorageClassSpecLoc() const {
462 return ThreadStorageClassSpecLoc;
465 void ClearStorageClassSpecs() {
466 StorageClassSpec = DeclSpec::SCS_unspecified;
467 ThreadStorageClassSpec = DeclSpec::TSCS_unspecified;
468 SCS_extern_in_linkage_spec = false;
469 StorageClassSpecLoc = SourceLocation();
470 ThreadStorageClassSpecLoc = SourceLocation();
473 void ClearTypeSpecType() {
474 TypeSpecType = DeclSpec::TST_unspecified;
475 TypeSpecOwned = false;
476 TSTLoc = SourceLocation();
480 TSW getTypeSpecWidth() const { return (TSW)TypeSpecWidth; }
481 TSC getTypeSpecComplex() const { return (TSC)TypeSpecComplex; }
482 TSS getTypeSpecSign() const { return (TSS)TypeSpecSign; }
483 TST getTypeSpecType() const { return (TST)TypeSpecType; }
484 bool isTypeAltiVecVector() const { return TypeAltiVecVector; }
485 bool isTypeAltiVecPixel() const { return TypeAltiVecPixel; }
486 bool isTypeAltiVecBool() const { return TypeAltiVecBool; }
487 bool isTypeSpecOwned() const { return TypeSpecOwned; }
488 bool isTypeRep() const { return isTypeRep((TST) TypeSpecType); }
489 bool isTypeSpecPipe() const { return TypeSpecPipe; }
490 bool isTypeSpecSat() const { return TypeSpecSat; }
492 ParsedType getRepAsType() const {
493 assert(isTypeRep((TST) TypeSpecType) && "DeclSpec does not store a type");
496 Decl *getRepAsDecl() const {
497 assert(isDeclRep((TST) TypeSpecType) && "DeclSpec does not store a decl");
500 Expr *getRepAsExpr() const {
501 assert(isExprRep((TST) TypeSpecType) && "DeclSpec does not store an expr");
504 CXXScopeSpec &getTypeSpecScope() { return TypeScope; }
505 const CXXScopeSpec &getTypeSpecScope() const { return TypeScope; }
507 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
508 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
509 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
510 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
511 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
513 SourceLocation getTypeSpecWidthLoc() const { return TSWRange.getBegin(); }
514 SourceRange getTypeSpecWidthRange() const { return TSWRange; }
515 SourceLocation getTypeSpecComplexLoc() const { return TSCLoc; }
516 SourceLocation getTypeSpecSignLoc() const { return TSSLoc; }
517 SourceLocation getTypeSpecTypeLoc() const { return TSTLoc; }
518 SourceLocation getAltiVecLoc() const { return AltiVecLoc; }
519 SourceLocation getTypeSpecSatLoc() const { return TSSatLoc; }
521 SourceLocation getTypeSpecTypeNameLoc() const {
522 assert(isDeclRep((TST) TypeSpecType) || TypeSpecType == TST_typename);
526 SourceRange getTypeofParensRange() const { return TypeofParensRange; }
527 void setTypeofParensRange(SourceRange range) { TypeofParensRange = range; }
529 bool hasAutoTypeSpec() const {
530 return (TypeSpecType == TST_auto || TypeSpecType == TST_auto_type ||
531 TypeSpecType == TST_decltype_auto);
534 bool hasTagDefinition() const;
536 /// Turn a type-specifier-type into a string like "_Bool" or "union".
537 static const char *getSpecifierName(DeclSpec::TST T,
538 const PrintingPolicy &Policy);
539 static const char *getSpecifierName(DeclSpec::TQ Q);
540 static const char *getSpecifierName(DeclSpec::TSS S);
541 static const char *getSpecifierName(DeclSpec::TSC C);
542 static const char *getSpecifierName(DeclSpec::TSW W);
543 static const char *getSpecifierName(DeclSpec::SCS S);
544 static const char *getSpecifierName(DeclSpec::TSCS S);
548 /// getTypeQualifiers - Return a set of TQs.
549 unsigned getTypeQualifiers() const { return TypeQualifiers; }
550 SourceLocation getConstSpecLoc() const { return TQ_constLoc; }
551 SourceLocation getRestrictSpecLoc() const { return TQ_restrictLoc; }
552 SourceLocation getVolatileSpecLoc() const { return TQ_volatileLoc; }
553 SourceLocation getAtomicSpecLoc() const { return TQ_atomicLoc; }
554 SourceLocation getUnalignedSpecLoc() const { return TQ_unalignedLoc; }
555 SourceLocation getPipeLoc() const { return TQ_pipeLoc; }
557 /// Clear out all of the type qualifiers.
558 void ClearTypeQualifiers() {
560 TQ_constLoc = SourceLocation();
561 TQ_restrictLoc = SourceLocation();
562 TQ_volatileLoc = SourceLocation();
563 TQ_atomicLoc = SourceLocation();
564 TQ_unalignedLoc = SourceLocation();
565 TQ_pipeLoc = SourceLocation();
568 // function-specifier
569 bool isInlineSpecified() const {
570 return FS_inline_specified | FS_forceinline_specified;
572 SourceLocation getInlineSpecLoc() const {
573 return FS_inline_specified ? FS_inlineLoc : FS_forceinlineLoc;
576 bool isVirtualSpecified() const { return FS_virtual_specified; }
577 SourceLocation getVirtualSpecLoc() const { return FS_virtualLoc; }
579 bool isExplicitSpecified() const { return FS_explicit_specified; }
580 SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; }
582 bool isNoreturnSpecified() const { return FS_noreturn_specified; }
583 SourceLocation getNoreturnSpecLoc() const { return FS_noreturnLoc; }
585 void ClearFunctionSpecs() {
586 FS_inline_specified = false;
587 FS_inlineLoc = SourceLocation();
588 FS_forceinline_specified = false;
589 FS_forceinlineLoc = SourceLocation();
590 FS_virtual_specified = false;
591 FS_virtualLoc = SourceLocation();
592 FS_explicit_specified = false;
593 FS_explicitLoc = SourceLocation();
594 FS_noreturn_specified = false;
595 FS_noreturnLoc = SourceLocation();
598 /// Return true if any type-specifier has been found.
599 bool hasTypeSpecifier() const {
600 return getTypeSpecType() != DeclSpec::TST_unspecified ||
601 getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
602 getTypeSpecComplex() != DeclSpec::TSC_unspecified ||
603 getTypeSpecSign() != DeclSpec::TSS_unspecified;
606 /// Return a bitmask of which flavors of specifiers this
607 /// DeclSpec includes.
608 unsigned getParsedSpecifiers() const;
610 /// isEmpty - Return true if this declaration specifier is completely empty:
611 /// no tokens were parsed in the production of it.
612 bool isEmpty() const {
613 return getParsedSpecifiers() == DeclSpec::PQ_None;
616 void SetRangeStart(SourceLocation Loc) { Range.setBegin(Loc); }
617 void SetRangeEnd(SourceLocation Loc) { Range.setEnd(Loc); }
619 /// These methods set the specified attribute of the DeclSpec and
620 /// return false if there was no error. If an error occurs (for
621 /// example, if we tried to set "auto" on a spec with "extern"
622 /// already set), they return true and set PrevSpec and DiagID
624 /// Diag(Loc, DiagID) << PrevSpec;
625 /// will yield a useful result.
627 /// TODO: use a more general approach that still allows these
628 /// diagnostics to be ignored when desired.
629 bool SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
630 const char *&PrevSpec, unsigned &DiagID,
631 const PrintingPolicy &Policy);
632 bool SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
633 const char *&PrevSpec, unsigned &DiagID);
634 bool SetTypeSpecWidth(TSW W, SourceLocation Loc, const char *&PrevSpec,
635 unsigned &DiagID, const PrintingPolicy &Policy);
636 bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec,
638 bool SetTypeSpecSign(TSS S, SourceLocation Loc, const char *&PrevSpec,
640 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
641 unsigned &DiagID, const PrintingPolicy &Policy);
642 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
643 unsigned &DiagID, ParsedType Rep,
644 const PrintingPolicy &Policy);
645 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
646 unsigned &DiagID, Decl *Rep, bool Owned,
647 const PrintingPolicy &Policy);
648 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
649 SourceLocation TagNameLoc, const char *&PrevSpec,
650 unsigned &DiagID, ParsedType Rep,
651 const PrintingPolicy &Policy);
652 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
653 SourceLocation TagNameLoc, const char *&PrevSpec,
654 unsigned &DiagID, Decl *Rep, bool Owned,
655 const PrintingPolicy &Policy);
657 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
658 unsigned &DiagID, Expr *Rep,
659 const PrintingPolicy &policy);
660 bool SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
661 const char *&PrevSpec, unsigned &DiagID,
662 const PrintingPolicy &Policy);
663 bool SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
664 const char *&PrevSpec, unsigned &DiagID,
665 const PrintingPolicy &Policy);
666 bool SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
667 const char *&PrevSpec, unsigned &DiagID,
668 const PrintingPolicy &Policy);
669 bool SetTypePipe(bool isPipe, SourceLocation Loc,
670 const char *&PrevSpec, unsigned &DiagID,
671 const PrintingPolicy &Policy);
672 bool SetTypeSpecSat(SourceLocation Loc, const char *&PrevSpec,
674 bool SetTypeSpecError();
675 void UpdateDeclRep(Decl *Rep) {
676 assert(isDeclRep((TST) TypeSpecType));
679 void UpdateTypeRep(ParsedType Rep) {
680 assert(isTypeRep((TST) TypeSpecType));
683 void UpdateExprRep(Expr *Rep) {
684 assert(isExprRep((TST) TypeSpecType));
688 bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
689 unsigned &DiagID, const LangOptions &Lang);
691 bool setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
693 bool setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
695 bool setFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
697 bool setFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
699 bool setFunctionSpecNoreturn(SourceLocation Loc, const char *&PrevSpec,
702 bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
704 bool setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
706 bool SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
709 bool isFriendSpecified() const { return Friend_specified; }
710 SourceLocation getFriendSpecLoc() const { return FriendLoc; }
712 bool isModulePrivateSpecified() const { return ModulePrivateLoc.isValid(); }
713 SourceLocation getModulePrivateSpecLoc() const { return ModulePrivateLoc; }
715 bool isConstexprSpecified() const { return Constexpr_specified; }
716 SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; }
718 void ClearConstexprSpec() {
719 Constexpr_specified = false;
720 ConstexprLoc = SourceLocation();
723 AttributePool &getAttributePool() const {
724 return Attrs.getPool();
727 /// Concatenates two attribute lists.
729 /// The GCC attribute syntax allows for the following:
732 /// short __attribute__(( unused, deprecated ))
733 /// int __attribute__(( may_alias, aligned(16) )) var;
736 /// This declares 4 attributes using 2 lists. The following syntax is
737 /// also allowed and equivalent to the previous declaration.
740 /// short __attribute__((unused)) __attribute__((deprecated))
741 /// int __attribute__((may_alias)) __attribute__((aligned(16))) var;
744 void addAttributes(ParsedAttributesView &AL) {
745 Attrs.addAll(AL.begin(), AL.end());
748 bool hasAttributes() const { return !Attrs.empty(); }
750 ParsedAttributes &getAttributes() { return Attrs; }
751 const ParsedAttributes &getAttributes() const { return Attrs; }
753 void takeAttributesFrom(ParsedAttributes &attrs) {
754 Attrs.takeAllFrom(attrs);
757 /// Finish - This does final analysis of the declspec, issuing diagnostics for
758 /// things like "_Imaginary" (lacking an FP type). After calling this method,
759 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
760 void Finish(Sema &S, const PrintingPolicy &Policy);
762 const WrittenBuiltinSpecs& getWrittenBuiltinSpecs() const {
766 ObjCDeclSpec *getObjCQualifiers() const { return ObjCQualifiers; }
767 void setObjCQualifiers(ObjCDeclSpec *quals) { ObjCQualifiers = quals; }
769 /// Checks if this DeclSpec can stand alone, without a Declarator.
771 /// Only tag declspecs can stand alone.
772 bool isMissingDeclaratorOk();
775 /// Captures information about "declaration specifiers" specific to
779 /// ObjCDeclQualifier - Qualifier used on types in method
780 /// declarations. Not all combinations are sensible. Parameters
781 /// can be one of { in, out, inout } with one of { bycopy, byref }.
782 /// Returns can either be { oneway } or not.
784 /// This should be kept in sync with Decl::ObjCDeclQualifier.
785 enum ObjCDeclQualifier {
793 DQ_CSNullability = 0x40
796 /// PropertyAttributeKind - list of property attributes.
797 /// Keep this list in sync with LLVM's Dwarf.h ApplePropertyAttributes.
798 enum ObjCPropertyAttributeKind {
800 DQ_PR_readonly = 0x01,
803 DQ_PR_readwrite = 0x08,
806 DQ_PR_nonatomic = 0x40,
808 DQ_PR_atomic = 0x100,
810 DQ_PR_strong = 0x400,
811 DQ_PR_unsafe_unretained = 0x800,
812 DQ_PR_nullability = 0x1000,
813 DQ_PR_null_resettable = 0x2000,
818 : objcDeclQualifier(DQ_None), PropertyAttributes(DQ_PR_noattr),
819 Nullability(0), GetterName(nullptr), SetterName(nullptr) { }
821 ObjCDeclQualifier getObjCDeclQualifier() const {
822 return (ObjCDeclQualifier)objcDeclQualifier;
824 void setObjCDeclQualifier(ObjCDeclQualifier DQVal) {
825 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal);
827 void clearObjCDeclQualifier(ObjCDeclQualifier DQVal) {
828 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier & ~DQVal);
831 ObjCPropertyAttributeKind getPropertyAttributes() const {
832 return ObjCPropertyAttributeKind(PropertyAttributes);
834 void setPropertyAttributes(ObjCPropertyAttributeKind PRVal) {
836 (ObjCPropertyAttributeKind)(PropertyAttributes | PRVal);
839 NullabilityKind getNullability() const {
840 assert(((getObjCDeclQualifier() & DQ_CSNullability) ||
841 (getPropertyAttributes() & DQ_PR_nullability)) &&
842 "Objective-C declspec doesn't have nullability");
843 return static_cast<NullabilityKind>(Nullability);
846 SourceLocation getNullabilityLoc() const {
847 assert(((getObjCDeclQualifier() & DQ_CSNullability) ||
848 (getPropertyAttributes() & DQ_PR_nullability)) &&
849 "Objective-C declspec doesn't have nullability");
850 return NullabilityLoc;
853 void setNullability(SourceLocation loc, NullabilityKind kind) {
854 assert(((getObjCDeclQualifier() & DQ_CSNullability) ||
855 (getPropertyAttributes() & DQ_PR_nullability)) &&
856 "Set the nullability declspec or property attribute first");
857 Nullability = static_cast<unsigned>(kind);
858 NullabilityLoc = loc;
861 const IdentifierInfo *getGetterName() const { return GetterName; }
862 IdentifierInfo *getGetterName() { return GetterName; }
863 SourceLocation getGetterNameLoc() const { return GetterNameLoc; }
864 void setGetterName(IdentifierInfo *name, SourceLocation loc) {
869 const IdentifierInfo *getSetterName() const { return SetterName; }
870 IdentifierInfo *getSetterName() { return SetterName; }
871 SourceLocation getSetterNameLoc() const { return SetterNameLoc; }
872 void setSetterName(IdentifierInfo *name, SourceLocation loc) {
878 // FIXME: These two are unrelated and mutually exclusive. So perhaps
879 // we can put them in a union to reflect their mutual exclusivity
880 // (space saving is negligible).
881 unsigned objcDeclQualifier : 7;
883 // NOTE: VC++ treats enums as signed, avoid using ObjCPropertyAttributeKind
884 unsigned PropertyAttributes : 15;
886 unsigned Nullability : 2;
888 SourceLocation NullabilityLoc;
890 IdentifierInfo *GetterName; // getter name or NULL if no getter
891 IdentifierInfo *SetterName; // setter name or NULL if no setter
892 SourceLocation GetterNameLoc; // location of the getter attribute's value
893 SourceLocation SetterNameLoc; // location of the setter attribute's value
897 /// Describes the kind of unqualified-id parsed.
898 enum class UnqualifiedIdKind {
901 /// An overloaded operator name, e.g., operator+.
902 IK_OperatorFunctionId,
903 /// A conversion function name, e.g., operator int.
904 IK_ConversionFunctionId,
905 /// A user-defined literal name, e.g., operator "" _i.
906 IK_LiteralOperatorId,
907 /// A constructor name.
909 /// A constructor named via a template-id.
910 IK_ConstructorTemplateId,
911 /// A destructor name.
913 /// A template-id, e.g., f<int>.
915 /// An implicit 'self' parameter
916 IK_ImplicitSelfParam,
917 /// A deduction-guide name (a template-name)
918 IK_DeductionGuideName
921 /// Represents a C++ unqualified-id that has been parsed.
922 class UnqualifiedId {
924 UnqualifiedId(const UnqualifiedId &Other) = delete;
925 const UnqualifiedId &operator=(const UnqualifiedId &) = delete;
928 /// Describes the kind of unqualified-id parsed.
929 UnqualifiedIdKind Kind;
932 /// The kind of overloaded operator.
933 OverloadedOperatorKind Operator;
935 /// The source locations of the individual tokens that name
936 /// the operator, e.g., the "new", "[", and "]" tokens in
939 /// Different operators have different numbers of tokens in their name,
940 /// up to three. Any remaining source locations in this array will be
941 /// set to an invalid value for operators with fewer than three tokens.
942 unsigned SymbolLocations[3];
945 /// Anonymous union that holds extra data associated with the
946 /// parsed unqualified-id.
948 /// When Kind == IK_Identifier, the parsed identifier, or when
949 /// Kind == IK_UserLiteralId, the identifier suffix.
950 IdentifierInfo *Identifier;
952 /// When Kind == IK_OperatorFunctionId, the overloaded operator
954 struct OFI OperatorFunctionId;
956 /// When Kind == IK_ConversionFunctionId, the type that the
957 /// conversion function names.
958 UnionParsedType ConversionFunctionId;
960 /// When Kind == IK_ConstructorName, the class-name of the type
961 /// whose constructor is being referenced.
962 UnionParsedType ConstructorName;
964 /// When Kind == IK_DestructorName, the type referred to by the
966 UnionParsedType DestructorName;
968 /// When Kind == IK_DeductionGuideName, the parsed template-name.
969 UnionParsedTemplateTy TemplateName;
971 /// When Kind == IK_TemplateId or IK_ConstructorTemplateId,
972 /// the template-id annotation that contains the template name and
973 /// template arguments.
974 TemplateIdAnnotation *TemplateId;
977 /// The location of the first token that describes this unqualified-id,
978 /// which will be the location of the identifier, "operator" keyword,
979 /// tilde (for a destructor), or the template name of a template-id.
980 SourceLocation StartLocation;
982 /// The location of the last token that describes this unqualified-id.
983 SourceLocation EndLocation;
986 : Kind(UnqualifiedIdKind::IK_Identifier), Identifier(nullptr) {}
988 /// Clear out this unqualified-id, setting it to default (invalid)
991 Kind = UnqualifiedIdKind::IK_Identifier;
992 Identifier = nullptr;
993 StartLocation = SourceLocation();
994 EndLocation = SourceLocation();
997 /// Determine whether this unqualified-id refers to a valid name.
998 bool isValid() const { return StartLocation.isValid(); }
1000 /// Determine whether this unqualified-id refers to an invalid name.
1001 bool isInvalid() const { return !isValid(); }
1003 /// Determine what kind of name we have.
1004 UnqualifiedIdKind getKind() const { return Kind; }
1005 void setKind(UnqualifiedIdKind kind) { Kind = kind; }
1007 /// Specify that this unqualified-id was parsed as an identifier.
1009 /// \param Id the parsed identifier.
1010 /// \param IdLoc the location of the parsed identifier.
1011 void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) {
1012 Kind = UnqualifiedIdKind::IK_Identifier;
1013 Identifier = const_cast<IdentifierInfo *>(Id);
1014 StartLocation = EndLocation = IdLoc;
1017 /// Specify that this unqualified-id was parsed as an
1018 /// operator-function-id.
1020 /// \param OperatorLoc the location of the 'operator' keyword.
1022 /// \param Op the overloaded operator.
1024 /// \param SymbolLocations the locations of the individual operator symbols
1025 /// in the operator.
1026 void setOperatorFunctionId(SourceLocation OperatorLoc,
1027 OverloadedOperatorKind Op,
1028 SourceLocation SymbolLocations[3]);
1030 /// Specify that this unqualified-id was parsed as a
1031 /// conversion-function-id.
1033 /// \param OperatorLoc the location of the 'operator' keyword.
1035 /// \param Ty the type to which this conversion function is converting.
1037 /// \param EndLoc the location of the last token that makes up the type name.
1038 void setConversionFunctionId(SourceLocation OperatorLoc,
1040 SourceLocation EndLoc) {
1041 Kind = UnqualifiedIdKind::IK_ConversionFunctionId;
1042 StartLocation = OperatorLoc;
1043 EndLocation = EndLoc;
1044 ConversionFunctionId = Ty;
1047 /// Specific that this unqualified-id was parsed as a
1048 /// literal-operator-id.
1050 /// \param Id the parsed identifier.
1052 /// \param OpLoc the location of the 'operator' keyword.
1054 /// \param IdLoc the location of the identifier.
1055 void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc,
1056 SourceLocation IdLoc) {
1057 Kind = UnqualifiedIdKind::IK_LiteralOperatorId;
1058 Identifier = const_cast<IdentifierInfo *>(Id);
1059 StartLocation = OpLoc;
1060 EndLocation = IdLoc;
1063 /// Specify that this unqualified-id was parsed as a constructor name.
1065 /// \param ClassType the class type referred to by the constructor name.
1067 /// \param ClassNameLoc the location of the class name.
1069 /// \param EndLoc the location of the last token that makes up the type name.
1070 void setConstructorName(ParsedType ClassType,
1071 SourceLocation ClassNameLoc,
1072 SourceLocation EndLoc) {
1073 Kind = UnqualifiedIdKind::IK_ConstructorName;
1074 StartLocation = ClassNameLoc;
1075 EndLocation = EndLoc;
1076 ConstructorName = ClassType;
1079 /// Specify that this unqualified-id was parsed as a
1080 /// template-id that names a constructor.
1082 /// \param TemplateId the template-id annotation that describes the parsed
1083 /// template-id. This UnqualifiedId instance will take ownership of the
1084 /// \p TemplateId and will free it on destruction.
1085 void setConstructorTemplateId(TemplateIdAnnotation *TemplateId);
1087 /// Specify that this unqualified-id was parsed as a destructor name.
1089 /// \param TildeLoc the location of the '~' that introduces the destructor
1092 /// \param ClassType the name of the class referred to by the destructor name.
1093 void setDestructorName(SourceLocation TildeLoc,
1094 ParsedType ClassType,
1095 SourceLocation EndLoc) {
1096 Kind = UnqualifiedIdKind::IK_DestructorName;
1097 StartLocation = TildeLoc;
1098 EndLocation = EndLoc;
1099 DestructorName = ClassType;
1102 /// Specify that this unqualified-id was parsed as a template-id.
1104 /// \param TemplateId the template-id annotation that describes the parsed
1105 /// template-id. This UnqualifiedId instance will take ownership of the
1106 /// \p TemplateId and will free it on destruction.
1107 void setTemplateId(TemplateIdAnnotation *TemplateId);
1109 /// Specify that this unqualified-id was parsed as a template-name for
1110 /// a deduction-guide.
1112 /// \param Template The parsed template-name.
1113 /// \param TemplateLoc The location of the parsed template-name.
1114 void setDeductionGuideName(ParsedTemplateTy Template,
1115 SourceLocation TemplateLoc) {
1116 Kind = UnqualifiedIdKind::IK_DeductionGuideName;
1117 TemplateName = Template;
1118 StartLocation = EndLocation = TemplateLoc;
1121 /// Return the source range that covers this unqualified-id.
1122 SourceRange getSourceRange() const LLVM_READONLY {
1123 return SourceRange(StartLocation, EndLocation);
1125 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1126 SourceLocation getBeginLoc() const LLVM_READONLY { return StartLocation; }
1127 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1128 SourceLocation getEndLoc() const LLVM_READONLY { return EndLocation; }
1131 /// A set of tokens that has been cached for later parsing.
1132 typedef SmallVector<Token, 4> CachedTokens;
1134 /// One instance of this struct is used for each type in a
1135 /// declarator that is parsed.
1137 /// This is intended to be a small value object.
1138 struct DeclaratorChunk {
1140 Pointer, Reference, Array, Function, BlockPointer, MemberPointer, Paren, Pipe
1143 /// Loc - The place where this type was defined.
1145 /// EndLoc - If valid, the place where this chunck ends.
1146 SourceLocation EndLoc;
1148 SourceRange getSourceRange() const {
1149 if (EndLoc.isInvalid())
1150 return SourceRange(Loc, Loc);
1151 return SourceRange(Loc, EndLoc);
1154 ParsedAttributesView AttrList;
1156 struct PointerTypeInfo {
1157 /// The type qualifiers: const/volatile/restrict/unaligned/atomic.
1158 unsigned TypeQuals : 5;
1160 /// The location of the const-qualifier, if any.
1161 unsigned ConstQualLoc;
1163 /// The location of the volatile-qualifier, if any.
1164 unsigned VolatileQualLoc;
1166 /// The location of the restrict-qualifier, if any.
1167 unsigned RestrictQualLoc;
1169 /// The location of the _Atomic-qualifier, if any.
1170 unsigned AtomicQualLoc;
1172 /// The location of the __unaligned-qualifier, if any.
1173 unsigned UnalignedQualLoc;
1179 struct ReferenceTypeInfo {
1180 /// The type qualifier: restrict. [GNU] C++ extension
1181 bool HasRestrict : 1;
1182 /// True if this is an lvalue reference, false if it's an rvalue reference.
1188 struct ArrayTypeInfo {
1189 /// The type qualifiers for the array:
1190 /// const/volatile/restrict/__unaligned/_Atomic.
1191 unsigned TypeQuals : 5;
1193 /// True if this dimension included the 'static' keyword.
1194 unsigned hasStatic : 1;
1196 /// True if this dimension was [*]. In this case, NumElts is null.
1197 unsigned isStar : 1;
1199 /// This is the size of the array, or null if [] or [*] was specified.
1200 /// Since the parser is multi-purpose, and we don't want to impose a root
1201 /// expression class on all clients, NumElts is untyped.
1207 /// ParamInfo - An array of paraminfo objects is allocated whenever a function
1208 /// declarator is parsed. There are two interesting styles of parameters
1210 /// K&R-style identifier lists and parameter type lists. K&R-style identifier
1211 /// lists will have information about the identifier, but no type information.
1212 /// Parameter type lists will have type info (if the actions module provides
1213 /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'.
1215 IdentifierInfo *Ident;
1216 SourceLocation IdentLoc;
1219 /// DefaultArgTokens - When the parameter's default argument
1220 /// cannot be parsed immediately (because it occurs within the
1221 /// declaration of a member function), it will be stored here as a
1222 /// sequence of tokens to be parsed once the class definition is
1223 /// complete. Non-NULL indicates that there is a default argument.
1224 std::unique_ptr<CachedTokens> DefaultArgTokens;
1226 ParamInfo() = default;
1227 ParamInfo(IdentifierInfo *ident, SourceLocation iloc,
1229 std::unique_ptr<CachedTokens> DefArgTokens = nullptr)
1230 : Ident(ident), IdentLoc(iloc), Param(param),
1231 DefaultArgTokens(std::move(DefArgTokens)) {}
1234 struct TypeAndRange {
1239 struct FunctionTypeInfo {
1240 /// hasPrototype - This is true if the function had at least one typed
1241 /// parameter. If the function is () or (a,b,c), then it has no prototype,
1242 /// and is treated as a K&R-style function.
1243 unsigned hasPrototype : 1;
1245 /// isVariadic - If this function has a prototype, and if that
1246 /// proto ends with ',...)', this is true. When true, EllipsisLoc
1247 /// contains the location of the ellipsis.
1248 unsigned isVariadic : 1;
1250 /// Can this declaration be a constructor-style initializer?
1251 unsigned isAmbiguous : 1;
1253 /// Whether the ref-qualifier (if any) is an lvalue reference.
1254 /// Otherwise, it's an rvalue reference.
1255 unsigned RefQualifierIsLValueRef : 1;
1257 /// The type qualifiers: const/volatile/restrict/__unaligned
1258 /// The qualifier bitmask values are the same as in QualType.
1259 unsigned TypeQuals : 4;
1261 /// ExceptionSpecType - An ExceptionSpecificationType value.
1262 unsigned ExceptionSpecType : 4;
1264 /// DeleteParams - If this is true, we need to delete[] Params.
1265 unsigned DeleteParams : 1;
1267 /// HasTrailingReturnType - If this is true, a trailing return type was
1269 unsigned HasTrailingReturnType : 1;
1271 /// The location of the left parenthesis in the source.
1274 /// When isVariadic is true, the location of the ellipsis in the source.
1275 unsigned EllipsisLoc;
1277 /// The location of the right parenthesis in the source.
1280 /// NumParams - This is the number of formal parameters specified by the
1284 /// NumExceptionsOrDecls - This is the number of types in the
1285 /// dynamic-exception-decl, if the function has one. In C, this is the
1286 /// number of declarations in the function prototype.
1287 unsigned NumExceptionsOrDecls;
1289 /// The location of the ref-qualifier, if any.
1291 /// If this is an invalid location, there is no ref-qualifier.
1292 unsigned RefQualifierLoc;
1294 /// The location of the const-qualifier, if any.
1296 /// If this is an invalid location, there is no const-qualifier.
1297 unsigned ConstQualifierLoc;
1299 /// The location of the volatile-qualifier, if any.
1301 /// If this is an invalid location, there is no volatile-qualifier.
1302 unsigned VolatileQualifierLoc;
1304 /// The location of the restrict-qualifier, if any.
1306 /// If this is an invalid location, there is no restrict-qualifier.
1307 unsigned RestrictQualifierLoc;
1309 /// The location of the 'mutable' qualifer in a lambda-declarator, if
1311 unsigned MutableLoc;
1313 /// The beginning location of the exception specification, if any.
1314 unsigned ExceptionSpecLocBeg;
1316 /// The end location of the exception specification, if any.
1317 unsigned ExceptionSpecLocEnd;
1319 /// Params - This is a pointer to a new[]'d array of ParamInfo objects that
1320 /// describe the parameters specified by this function declarator. null if
1321 /// there are no parameters specified.
1325 /// Pointer to a new[]'d array of TypeAndRange objects that
1326 /// contain the types in the function's dynamic exception specification
1327 /// and their locations, if there is one.
1328 TypeAndRange *Exceptions;
1330 /// Pointer to the expression in the noexcept-specifier of this
1331 /// function, if it has one.
1334 /// Pointer to the cached tokens for an exception-specification
1335 /// that has not yet been parsed.
1336 CachedTokens *ExceptionSpecTokens;
1338 /// Pointer to a new[]'d array of declarations that need to be available
1339 /// for lookup inside the function body, if one exists. Does not exist in
1341 NamedDecl **DeclsInPrototype;
1344 /// If HasTrailingReturnType is true, this is the trailing return
1346 UnionParsedType TrailingReturnType;
1348 /// Reset the parameter list to having zero parameters.
1350 /// This is used in various places for error recovery.
1352 for (unsigned I = 0; I < NumParams; ++I)
1353 Params[I].DefaultArgTokens.reset();
1356 DeleteParams = false;
1363 switch (getExceptionSpecType()) {
1367 delete[] Exceptions;
1370 delete ExceptionSpecTokens;
1373 if (NumExceptionsOrDecls != 0)
1374 delete[] DeclsInPrototype;
1379 /// isKNRPrototype - Return true if this is a K&R style identifier list,
1380 /// like "void foo(a,b,c)". In a function definition, this will be followed
1381 /// by the parameter type definitions.
1382 bool isKNRPrototype() const { return !hasPrototype && NumParams != 0; }
1384 SourceLocation getLParenLoc() const {
1385 return SourceLocation::getFromRawEncoding(LParenLoc);
1388 SourceLocation getEllipsisLoc() const {
1389 return SourceLocation::getFromRawEncoding(EllipsisLoc);
1392 SourceLocation getRParenLoc() const {
1393 return SourceLocation::getFromRawEncoding(RParenLoc);
1396 SourceLocation getExceptionSpecLocBeg() const {
1397 return SourceLocation::getFromRawEncoding(ExceptionSpecLocBeg);
1400 SourceLocation getExceptionSpecLocEnd() const {
1401 return SourceLocation::getFromRawEncoding(ExceptionSpecLocEnd);
1404 SourceRange getExceptionSpecRange() const {
1405 return SourceRange(getExceptionSpecLocBeg(), getExceptionSpecLocEnd());
1408 /// Retrieve the location of the ref-qualifier, if any.
1409 SourceLocation getRefQualifierLoc() const {
1410 return SourceLocation::getFromRawEncoding(RefQualifierLoc);
1413 /// Retrieve the location of the 'const' qualifier, if any.
1414 SourceLocation getConstQualifierLoc() const {
1415 return SourceLocation::getFromRawEncoding(ConstQualifierLoc);
1418 /// Retrieve the location of the 'volatile' qualifier, if any.
1419 SourceLocation getVolatileQualifierLoc() const {
1420 return SourceLocation::getFromRawEncoding(VolatileQualifierLoc);
1423 /// Retrieve the location of the 'restrict' qualifier, if any.
1424 SourceLocation getRestrictQualifierLoc() const {
1425 return SourceLocation::getFromRawEncoding(RestrictQualifierLoc);
1428 /// Retrieve the location of the 'mutable' qualifier, if any.
1429 SourceLocation getMutableLoc() const {
1430 return SourceLocation::getFromRawEncoding(MutableLoc);
1433 /// Determine whether this function declaration contains a
1435 bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); }
1437 /// Determine whether this lambda-declarator contains a 'mutable'
1439 bool hasMutableQualifier() const { return getMutableLoc().isValid(); }
1441 /// Get the type of exception specification this function has.
1442 ExceptionSpecificationType getExceptionSpecType() const {
1443 return static_cast<ExceptionSpecificationType>(ExceptionSpecType);
1446 /// Get the number of dynamic exception specifications.
1447 unsigned getNumExceptions() const {
1448 assert(ExceptionSpecType != EST_None);
1449 return NumExceptionsOrDecls;
1452 /// Get the non-parameter decls defined within this function
1453 /// prototype. Typically these are tag declarations.
1454 ArrayRef<NamedDecl *> getDeclsInPrototype() const {
1455 assert(ExceptionSpecType == EST_None);
1456 return llvm::makeArrayRef(DeclsInPrototype, NumExceptionsOrDecls);
1459 /// Determine whether this function declarator had a
1460 /// trailing-return-type.
1461 bool hasTrailingReturnType() const { return HasTrailingReturnType; }
1463 /// Get the trailing-return-type for this function declarator.
1464 ParsedType getTrailingReturnType() const { return TrailingReturnType; }
1467 struct BlockPointerTypeInfo {
1468 /// For now, sema will catch these as invalid.
1469 /// The type qualifiers: const/volatile/restrict/__unaligned/_Atomic.
1470 unsigned TypeQuals : 5;
1476 struct MemberPointerTypeInfo {
1477 /// The type qualifiers: const/volatile/restrict/__unaligned/_Atomic.
1478 unsigned TypeQuals : 5;
1479 // CXXScopeSpec has a constructor, so it can't be a direct member.
1480 // So we need some pointer-aligned storage and a bit of trickery.
1481 alignas(CXXScopeSpec) char ScopeMem[sizeof(CXXScopeSpec)];
1482 CXXScopeSpec &Scope() {
1483 return *reinterpret_cast<CXXScopeSpec *>(ScopeMem);
1485 const CXXScopeSpec &Scope() const {
1486 return *reinterpret_cast<const CXXScopeSpec *>(ScopeMem);
1489 Scope().~CXXScopeSpec();
1493 struct PipeTypeInfo {
1494 /// The access writes.
1495 unsigned AccessWrites : 3;
1501 PointerTypeInfo Ptr;
1502 ReferenceTypeInfo Ref;
1504 FunctionTypeInfo Fun;
1505 BlockPointerTypeInfo Cls;
1506 MemberPointerTypeInfo Mem;
1507 PipeTypeInfo PipeInfo;
1512 case DeclaratorChunk::Function: return Fun.destroy();
1513 case DeclaratorChunk::Pointer: return Ptr.destroy();
1514 case DeclaratorChunk::BlockPointer: return Cls.destroy();
1515 case DeclaratorChunk::Reference: return Ref.destroy();
1516 case DeclaratorChunk::Array: return Arr.destroy();
1517 case DeclaratorChunk::MemberPointer: return Mem.destroy();
1518 case DeclaratorChunk::Paren: return;
1519 case DeclaratorChunk::Pipe: return PipeInfo.destroy();
1523 /// If there are attributes applied to this declaratorchunk, return
1525 const ParsedAttributesView &getAttrs() const { return AttrList; }
1526 ParsedAttributesView &getAttrs() { return AttrList; }
1528 /// Return a DeclaratorChunk for a pointer.
1529 static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc,
1530 SourceLocation ConstQualLoc,
1531 SourceLocation VolatileQualLoc,
1532 SourceLocation RestrictQualLoc,
1533 SourceLocation AtomicQualLoc,
1534 SourceLocation UnalignedQualLoc) {
1538 I.Ptr.TypeQuals = TypeQuals;
1539 I.Ptr.ConstQualLoc = ConstQualLoc.getRawEncoding();
1540 I.Ptr.VolatileQualLoc = VolatileQualLoc.getRawEncoding();
1541 I.Ptr.RestrictQualLoc = RestrictQualLoc.getRawEncoding();
1542 I.Ptr.AtomicQualLoc = AtomicQualLoc.getRawEncoding();
1543 I.Ptr.UnalignedQualLoc = UnalignedQualLoc.getRawEncoding();
1547 /// Return a DeclaratorChunk for a reference.
1548 static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc,
1553 I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0;
1554 I.Ref.LValueRef = lvalue;
1558 /// Return a DeclaratorChunk for an array.
1559 static DeclaratorChunk getArray(unsigned TypeQuals,
1560 bool isStatic, bool isStar, Expr *NumElts,
1561 SourceLocation LBLoc, SourceLocation RBLoc) {
1566 I.Arr.TypeQuals = TypeQuals;
1567 I.Arr.hasStatic = isStatic;
1568 I.Arr.isStar = isStar;
1569 I.Arr.NumElts = NumElts;
1573 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
1574 /// "TheDeclarator" is the declarator that this will be added to.
1575 static DeclaratorChunk getFunction(bool HasProto,
1577 SourceLocation LParenLoc,
1578 ParamInfo *Params, unsigned NumParams,
1579 SourceLocation EllipsisLoc,
1580 SourceLocation RParenLoc,
1582 bool RefQualifierIsLvalueRef,
1583 SourceLocation RefQualifierLoc,
1584 SourceLocation ConstQualifierLoc,
1585 SourceLocation VolatileQualifierLoc,
1586 SourceLocation RestrictQualifierLoc,
1587 SourceLocation MutableLoc,
1588 ExceptionSpecificationType ESpecType,
1589 SourceRange ESpecRange,
1590 ParsedType *Exceptions,
1591 SourceRange *ExceptionRanges,
1592 unsigned NumExceptions,
1594 CachedTokens *ExceptionSpecTokens,
1595 ArrayRef<NamedDecl *> DeclsInPrototype,
1596 SourceLocation LocalRangeBegin,
1597 SourceLocation LocalRangeEnd,
1598 Declarator &TheDeclarator,
1599 TypeResult TrailingReturnType =
1602 /// Return a DeclaratorChunk for a block.
1603 static DeclaratorChunk getBlockPointer(unsigned TypeQuals,
1604 SourceLocation Loc) {
1606 I.Kind = BlockPointer;
1608 I.Cls.TypeQuals = TypeQuals;
1612 /// Return a DeclaratorChunk for a block.
1613 static DeclaratorChunk getPipe(unsigned TypeQuals,
1614 SourceLocation Loc) {
1618 I.Cls.TypeQuals = TypeQuals;
1622 static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS,
1624 SourceLocation Loc) {
1626 I.Kind = MemberPointer;
1627 I.Loc = SS.getBeginLoc();
1629 I.Mem.TypeQuals = TypeQuals;
1630 new (I.Mem.ScopeMem) CXXScopeSpec(SS);
1634 /// Return a DeclaratorChunk for a paren.
1635 static DeclaratorChunk getParen(SourceLocation LParenLoc,
1636 SourceLocation RParenLoc) {
1640 I.EndLoc = RParenLoc;
1644 bool isParen() const {
1645 return Kind == Paren;
1649 /// A parsed C++17 decomposition declarator of the form
1650 /// '[' identifier-list ']'
1651 class DecompositionDeclarator {
1654 IdentifierInfo *Name;
1655 SourceLocation NameLoc;
1659 /// The locations of the '[' and ']' tokens.
1660 SourceLocation LSquareLoc, RSquareLoc;
1664 unsigned NumBindings : 31;
1665 unsigned DeleteBindings : 1;
1667 friend class Declarator;
1670 DecompositionDeclarator()
1671 : Bindings(nullptr), NumBindings(0), DeleteBindings(false) {}
1672 DecompositionDeclarator(const DecompositionDeclarator &G) = delete;
1673 DecompositionDeclarator &operator=(const DecompositionDeclarator &G) = delete;
1674 ~DecompositionDeclarator() {
1680 LSquareLoc = RSquareLoc = SourceLocation();
1685 DeleteBindings = false;
1688 ArrayRef<Binding> bindings() const {
1689 return llvm::makeArrayRef(Bindings, NumBindings);
1692 bool isSet() const { return LSquareLoc.isValid(); }
1694 SourceLocation getLSquareLoc() const { return LSquareLoc; }
1695 SourceLocation getRSquareLoc() const { return RSquareLoc; }
1696 SourceRange getSourceRange() const {
1697 return SourceRange(LSquareLoc, RSquareLoc);
1701 /// Described the kind of function definition (if any) provided for
1703 enum FunctionDefinitionKind {
1710 enum class DeclaratorContext {
1711 FileContext, // File scope declaration.
1712 PrototypeContext, // Within a function prototype.
1713 ObjCResultContext, // An ObjC method result type.
1714 ObjCParameterContext,// An ObjC method parameter type.
1715 KNRTypeListContext, // K&R type definition list for formals.
1716 TypeNameContext, // Abstract declarator for types.
1717 FunctionalCastContext, // Type in a C++ functional cast expression.
1718 MemberContext, // Struct/Union field.
1719 BlockContext, // Declaration within a block in a function.
1720 ForContext, // Declaration within first part of a for loop.
1721 InitStmtContext, // Declaration within optional init stmt of if/switch.
1722 ConditionContext, // Condition declaration in a C++ if/switch/while/for.
1723 TemplateParamContext,// Within a template parameter list.
1724 CXXNewContext, // C++ new-expression.
1725 CXXCatchContext, // C++ catch exception-declaration
1726 ObjCCatchContext, // Objective-C catch exception-declaration
1727 BlockLiteralContext, // Block literal declarator.
1728 LambdaExprContext, // Lambda-expression declarator.
1729 LambdaExprParameterContext, // Lambda-expression parameter declarator.
1730 ConversionIdContext, // C++ conversion-type-id.
1731 TrailingReturnContext, // C++11 trailing-type-specifier.
1732 TrailingReturnVarContext, // C++11 trailing-type-specifier for variable.
1733 TemplateArgContext, // Any template argument (in template argument list).
1734 TemplateTypeArgContext, // Template type argument (in default argument).
1735 AliasDeclContext, // C++11 alias-declaration.
1736 AliasTemplateContext // C++11 alias-declaration template.
1740 /// Information about one declarator, including the parsed type
1741 /// information and the identifier.
1743 /// When the declarator is fully formed, this is turned into the appropriate
1746 /// Declarators come in two types: normal declarators and abstract declarators.
1747 /// Abstract declarators are used when parsing types, and don't have an
1748 /// identifier. Normal declarators do have ID's.
1750 /// Instances of this class should be a transient object that lives on the
1751 /// stack, not objects that are allocated in large quantities on the heap.
1760 /// Where we are parsing this declarator.
1761 DeclaratorContext Context;
1763 /// The C++17 structured binding, if any. This is an alternative to a Name.
1764 DecompositionDeclarator BindingGroup;
1766 /// DeclTypeInfo - This holds each type that the declarator includes as it is
1767 /// parsed. This is pushed from the identifier out, which means that element
1768 /// #0 will be the most closely bound to the identifier, and
1769 /// DeclTypeInfo.back() will be the least closely bound.
1770 SmallVector<DeclaratorChunk, 8> DeclTypeInfo;
1772 /// InvalidType - Set by Sema::GetTypeForDeclarator().
1773 unsigned InvalidType : 1;
1775 /// GroupingParens - Set by Parser::ParseParenDeclarator().
1776 unsigned GroupingParens : 1;
1778 /// FunctionDefinition - Is this Declarator for a function or member
1779 /// definition and, if so, what kind?
1781 /// Actually a FunctionDefinitionKind.
1782 unsigned FunctionDefinition : 2;
1784 /// Is this Declarator a redeclaration?
1785 unsigned Redeclaration : 1;
1787 /// true if the declaration is preceded by \c __extension__.
1788 unsigned Extension : 1;
1790 /// Indicates whether this is an Objective-C instance variable.
1791 unsigned ObjCIvar : 1;
1793 /// Indicates whether this is an Objective-C 'weak' property.
1794 unsigned ObjCWeakProperty : 1;
1796 /// Indicates whether the InlineParams / InlineBindings storage has been used.
1797 unsigned InlineStorageUsed : 1;
1799 /// Attrs - Attributes.
1800 ParsedAttributes Attrs;
1802 /// The asm label, if specified.
1808 /// InlineParams - This is a local array used for the first function decl
1809 /// chunk to avoid going to the heap for the common case when we have one
1810 /// function chunk in the declarator.
1811 DeclaratorChunk::ParamInfo InlineParams[16];
1812 DecompositionDeclarator::Binding InlineBindings[16];
1817 /// If this is the second or subsequent declarator in this declaration,
1818 /// the location of the comma before this declarator.
1819 SourceLocation CommaLoc;
1821 /// If provided, the source location of the ellipsis used to describe
1822 /// this declarator as a parameter pack.
1823 SourceLocation EllipsisLoc;
1825 friend struct DeclaratorChunk;
1828 Declarator(const DeclSpec &ds, DeclaratorContext C)
1829 : DS(ds), Range(ds.getSourceRange()), Context(C),
1830 InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error),
1831 GroupingParens(false), FunctionDefinition(FDK_Declaration),
1832 Redeclaration(false), Extension(false), ObjCIvar(false),
1833 ObjCWeakProperty(false), InlineStorageUsed(false),
1834 Attrs(ds.getAttributePool().getFactory()), AsmLabel(nullptr) {}
1839 /// getDeclSpec - Return the declaration-specifier that this declarator was
1841 const DeclSpec &getDeclSpec() const { return DS; }
1843 /// getMutableDeclSpec - Return a non-const version of the DeclSpec. This
1844 /// should be used with extreme care: declspecs can often be shared between
1845 /// multiple declarators, so mutating the DeclSpec affects all of the
1846 /// Declarators. This should only be done when the declspec is known to not
1847 /// be shared or when in error recovery etc.
1848 DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); }
1850 AttributePool &getAttributePool() const {
1851 return Attrs.getPool();
1854 /// getCXXScopeSpec - Return the C++ scope specifier (global scope or
1855 /// nested-name-specifier) that is part of the declarator-id.
1856 const CXXScopeSpec &getCXXScopeSpec() const { return SS; }
1857 CXXScopeSpec &getCXXScopeSpec() { return SS; }
1859 /// Retrieve the name specified by this declarator.
1860 UnqualifiedId &getName() { return Name; }
1862 const DecompositionDeclarator &getDecompositionDeclarator() const {
1863 return BindingGroup;
1866 DeclaratorContext getContext() const { return Context; }
1868 bool isPrototypeContext() const {
1869 return (Context == DeclaratorContext::PrototypeContext ||
1870 Context == DeclaratorContext::ObjCParameterContext ||
1871 Context == DeclaratorContext::ObjCResultContext ||
1872 Context == DeclaratorContext::LambdaExprParameterContext);
1875 /// Get the source range that spans this declarator.
1876 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
1877 SourceLocation getLocStart() const LLVM_READONLY { return getBeginLoc(); }
1878 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
1879 SourceLocation getLocEnd() const LLVM_READONLY { return getEndLoc(); }
1880 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
1882 void SetSourceRange(SourceRange R) { Range = R; }
1883 /// SetRangeBegin - Set the start of the source range to Loc, unless it's
1885 void SetRangeBegin(SourceLocation Loc) {
1886 if (!Loc.isInvalid())
1887 Range.setBegin(Loc);
1889 /// SetRangeEnd - Set the end of the source range to Loc, unless it's invalid.
1890 void SetRangeEnd(SourceLocation Loc) {
1891 if (!Loc.isInvalid())
1894 /// ExtendWithDeclSpec - Extend the declarator source range to include the
1895 /// given declspec, unless its location is invalid. Adopts the range start if
1896 /// the current range start is invalid.
1897 void ExtendWithDeclSpec(const DeclSpec &DS) {
1898 SourceRange SR = DS.getSourceRange();
1899 if (Range.getBegin().isInvalid())
1900 Range.setBegin(SR.getBegin());
1901 if (!SR.getEnd().isInvalid())
1902 Range.setEnd(SR.getEnd());
1905 /// Reset the contents of this Declarator.
1909 Range = DS.getSourceRange();
1910 BindingGroup.clear();
1912 for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i)
1913 DeclTypeInfo[i].destroy();
1914 DeclTypeInfo.clear();
1917 InlineStorageUsed = false;
1919 ObjCWeakProperty = false;
1920 CommaLoc = SourceLocation();
1921 EllipsisLoc = SourceLocation();
1924 /// mayOmitIdentifier - Return true if the identifier is either optional or
1925 /// not allowed. This is true for typenames, prototypes, and template
1926 /// parameter lists.
1927 bool mayOmitIdentifier() const {
1929 case DeclaratorContext::FileContext:
1930 case DeclaratorContext::KNRTypeListContext:
1931 case DeclaratorContext::MemberContext:
1932 case DeclaratorContext::BlockContext:
1933 case DeclaratorContext::ForContext:
1934 case DeclaratorContext::InitStmtContext:
1935 case DeclaratorContext::ConditionContext:
1938 case DeclaratorContext::TypeNameContext:
1939 case DeclaratorContext::FunctionalCastContext:
1940 case DeclaratorContext::AliasDeclContext:
1941 case DeclaratorContext::AliasTemplateContext:
1942 case DeclaratorContext::PrototypeContext:
1943 case DeclaratorContext::LambdaExprParameterContext:
1944 case DeclaratorContext::ObjCParameterContext:
1945 case DeclaratorContext::ObjCResultContext:
1946 case DeclaratorContext::TemplateParamContext:
1947 case DeclaratorContext::CXXNewContext:
1948 case DeclaratorContext::CXXCatchContext:
1949 case DeclaratorContext::ObjCCatchContext:
1950 case DeclaratorContext::BlockLiteralContext:
1951 case DeclaratorContext::LambdaExprContext:
1952 case DeclaratorContext::ConversionIdContext:
1953 case DeclaratorContext::TemplateArgContext:
1954 case DeclaratorContext::TemplateTypeArgContext:
1955 case DeclaratorContext::TrailingReturnContext:
1956 case DeclaratorContext::TrailingReturnVarContext:
1959 llvm_unreachable("unknown context kind!");
1962 /// mayHaveIdentifier - Return true if the identifier is either optional or
1963 /// required. This is true for normal declarators and prototypes, but not
1965 bool mayHaveIdentifier() const {
1967 case DeclaratorContext::FileContext:
1968 case DeclaratorContext::KNRTypeListContext:
1969 case DeclaratorContext::MemberContext:
1970 case DeclaratorContext::BlockContext:
1971 case DeclaratorContext::ForContext:
1972 case DeclaratorContext::InitStmtContext:
1973 case DeclaratorContext::ConditionContext:
1974 case DeclaratorContext::PrototypeContext:
1975 case DeclaratorContext::LambdaExprParameterContext:
1976 case DeclaratorContext::TemplateParamContext:
1977 case DeclaratorContext::CXXCatchContext:
1978 case DeclaratorContext::ObjCCatchContext:
1981 case DeclaratorContext::TypeNameContext:
1982 case DeclaratorContext::FunctionalCastContext:
1983 case DeclaratorContext::CXXNewContext:
1984 case DeclaratorContext::AliasDeclContext:
1985 case DeclaratorContext::AliasTemplateContext:
1986 case DeclaratorContext::ObjCParameterContext:
1987 case DeclaratorContext::ObjCResultContext:
1988 case DeclaratorContext::BlockLiteralContext:
1989 case DeclaratorContext::LambdaExprContext:
1990 case DeclaratorContext::ConversionIdContext:
1991 case DeclaratorContext::TemplateArgContext:
1992 case DeclaratorContext::TemplateTypeArgContext:
1993 case DeclaratorContext::TrailingReturnContext:
1994 case DeclaratorContext::TrailingReturnVarContext:
1997 llvm_unreachable("unknown context kind!");
2000 /// Return true if the context permits a C++17 decomposition declarator.
2001 bool mayHaveDecompositionDeclarator() const {
2003 case DeclaratorContext::FileContext:
2004 // FIXME: It's not clear that the proposal meant to allow file-scope
2005 // structured bindings, but it does.
2006 case DeclaratorContext::BlockContext:
2007 case DeclaratorContext::ForContext:
2008 case DeclaratorContext::InitStmtContext:
2009 case DeclaratorContext::ConditionContext:
2012 case DeclaratorContext::MemberContext:
2013 case DeclaratorContext::PrototypeContext:
2014 case DeclaratorContext::TemplateParamContext:
2018 // These contexts don't allow any kind of non-abstract declarator.
2019 case DeclaratorContext::KNRTypeListContext:
2020 case DeclaratorContext::TypeNameContext:
2021 case DeclaratorContext::FunctionalCastContext:
2022 case DeclaratorContext::AliasDeclContext:
2023 case DeclaratorContext::AliasTemplateContext:
2024 case DeclaratorContext::LambdaExprParameterContext:
2025 case DeclaratorContext::ObjCParameterContext:
2026 case DeclaratorContext::ObjCResultContext:
2027 case DeclaratorContext::CXXNewContext:
2028 case DeclaratorContext::CXXCatchContext:
2029 case DeclaratorContext::ObjCCatchContext:
2030 case DeclaratorContext::BlockLiteralContext:
2031 case DeclaratorContext::LambdaExprContext:
2032 case DeclaratorContext::ConversionIdContext:
2033 case DeclaratorContext::TemplateArgContext:
2034 case DeclaratorContext::TemplateTypeArgContext:
2035 case DeclaratorContext::TrailingReturnContext:
2036 case DeclaratorContext::TrailingReturnVarContext:
2039 llvm_unreachable("unknown context kind!");
2042 /// mayBeFollowedByCXXDirectInit - Return true if the declarator can be
2043 /// followed by a C++ direct initializer, e.g. "int x(1);".
2044 bool mayBeFollowedByCXXDirectInit() const {
2045 if (hasGroupingParens()) return false;
2047 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
2050 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern &&
2051 Context != DeclaratorContext::FileContext)
2054 // Special names can't have direct initializers.
2055 if (Name.getKind() != UnqualifiedIdKind::IK_Identifier)
2059 case DeclaratorContext::FileContext:
2060 case DeclaratorContext::BlockContext:
2061 case DeclaratorContext::ForContext:
2062 case DeclaratorContext::InitStmtContext:
2063 case DeclaratorContext::TrailingReturnVarContext:
2066 case DeclaratorContext::ConditionContext:
2067 // This may not be followed by a direct initializer, but it can't be a
2068 // function declaration either, and we'd prefer to perform a tentative
2069 // parse in order to produce the right diagnostic.
2072 case DeclaratorContext::KNRTypeListContext:
2073 case DeclaratorContext::MemberContext:
2074 case DeclaratorContext::PrototypeContext:
2075 case DeclaratorContext::LambdaExprParameterContext:
2076 case DeclaratorContext::ObjCParameterContext:
2077 case DeclaratorContext::ObjCResultContext:
2078 case DeclaratorContext::TemplateParamContext:
2079 case DeclaratorContext::CXXCatchContext:
2080 case DeclaratorContext::ObjCCatchContext:
2081 case DeclaratorContext::TypeNameContext:
2082 case DeclaratorContext::FunctionalCastContext: // FIXME
2083 case DeclaratorContext::CXXNewContext:
2084 case DeclaratorContext::AliasDeclContext:
2085 case DeclaratorContext::AliasTemplateContext:
2086 case DeclaratorContext::BlockLiteralContext:
2087 case DeclaratorContext::LambdaExprContext:
2088 case DeclaratorContext::ConversionIdContext:
2089 case DeclaratorContext::TemplateArgContext:
2090 case DeclaratorContext::TemplateTypeArgContext:
2091 case DeclaratorContext::TrailingReturnContext:
2094 llvm_unreachable("unknown context kind!");
2097 /// isPastIdentifier - Return true if we have parsed beyond the point where
2098 /// the name would appear. (This may happen even if we haven't actually parsed
2099 /// a name, perhaps because this context doesn't require one.)
2100 bool isPastIdentifier() const { return Name.isValid(); }
2102 /// hasName - Whether this declarator has a name, which might be an
2103 /// identifier (accessible via getIdentifier()) or some kind of
2104 /// special C++ name (constructor, destructor, etc.), or a structured
2105 /// binding (which is not exactly a name, but occupies the same position).
2106 bool hasName() const {
2107 return Name.getKind() != UnqualifiedIdKind::IK_Identifier ||
2108 Name.Identifier || isDecompositionDeclarator();
2111 /// Return whether this declarator is a decomposition declarator.
2112 bool isDecompositionDeclarator() const {
2113 return BindingGroup.isSet();
2116 IdentifierInfo *getIdentifier() const {
2117 if (Name.getKind() == UnqualifiedIdKind::IK_Identifier)
2118 return Name.Identifier;
2122 SourceLocation getIdentifierLoc() const { return Name.StartLocation; }
2124 /// Set the name of this declarator to be the given identifier.
2125 void SetIdentifier(IdentifierInfo *Id, SourceLocation IdLoc) {
2126 Name.setIdentifier(Id, IdLoc);
2129 /// Set the decomposition bindings for this declarator.
2131 setDecompositionBindings(SourceLocation LSquareLoc,
2132 ArrayRef<DecompositionDeclarator::Binding> Bindings,
2133 SourceLocation RSquareLoc);
2135 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to
2136 /// EndLoc, which should be the last token of the chunk.
2137 /// This function takes attrs by R-Value reference because it takes ownership
2138 /// of those attributes from the parameter.
2139 void AddTypeInfo(const DeclaratorChunk &TI, ParsedAttributes &&attrs,
2140 SourceLocation EndLoc) {
2141 DeclTypeInfo.push_back(TI);
2142 DeclTypeInfo.back().getAttrs().addAll(attrs.begin(), attrs.end());
2143 getAttributePool().takeAllFrom(attrs.getPool());
2145 if (!EndLoc.isInvalid())
2146 SetRangeEnd(EndLoc);
2149 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to
2150 /// EndLoc, which should be the last token of the chunk.
2151 void AddTypeInfo(const DeclaratorChunk &TI, SourceLocation EndLoc) {
2152 DeclTypeInfo.push_back(TI);
2154 if (!EndLoc.isInvalid())
2155 SetRangeEnd(EndLoc);
2158 /// Add a new innermost chunk to this declarator.
2159 void AddInnermostTypeInfo(const DeclaratorChunk &TI) {
2160 DeclTypeInfo.insert(DeclTypeInfo.begin(), TI);
2163 /// Return the number of types applied to this declarator.
2164 unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); }
2166 /// Return the specified TypeInfo from this declarator. TypeInfo #0 is
2167 /// closest to the identifier.
2168 const DeclaratorChunk &getTypeObject(unsigned i) const {
2169 assert(i < DeclTypeInfo.size() && "Invalid type chunk");
2170 return DeclTypeInfo[i];
2172 DeclaratorChunk &getTypeObject(unsigned i) {
2173 assert(i < DeclTypeInfo.size() && "Invalid type chunk");
2174 return DeclTypeInfo[i];
2177 typedef SmallVectorImpl<DeclaratorChunk>::const_iterator type_object_iterator;
2178 typedef llvm::iterator_range<type_object_iterator> type_object_range;
2180 /// Returns the range of type objects, from the identifier outwards.
2181 type_object_range type_objects() const {
2182 return type_object_range(DeclTypeInfo.begin(), DeclTypeInfo.end());
2185 void DropFirstTypeObject() {
2186 assert(!DeclTypeInfo.empty() && "No type chunks to drop.");
2187 DeclTypeInfo.front().destroy();
2188 DeclTypeInfo.erase(DeclTypeInfo.begin());
2191 /// Return the innermost (closest to the declarator) chunk of this
2192 /// declarator that is not a parens chunk, or null if there are no
2193 /// non-parens chunks.
2194 const DeclaratorChunk *getInnermostNonParenChunk() const {
2195 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
2196 if (!DeclTypeInfo[i].isParen())
2197 return &DeclTypeInfo[i];
2202 /// Return the outermost (furthest from the declarator) chunk of
2203 /// this declarator that is not a parens chunk, or null if there are
2204 /// no non-parens chunks.
2205 const DeclaratorChunk *getOutermostNonParenChunk() const {
2206 for (unsigned i = DeclTypeInfo.size(), i_end = 0; i != i_end; --i) {
2207 if (!DeclTypeInfo[i-1].isParen())
2208 return &DeclTypeInfo[i-1];
2213 /// isArrayOfUnknownBound - This method returns true if the declarator
2214 /// is a declarator for an array of unknown bound (looking through
2216 bool isArrayOfUnknownBound() const {
2217 const DeclaratorChunk *chunk = getInnermostNonParenChunk();
2218 return (chunk && chunk->Kind == DeclaratorChunk::Array &&
2219 !chunk->Arr.NumElts);
2222 /// isFunctionDeclarator - This method returns true if the declarator
2223 /// is a function declarator (looking through parentheses).
2224 /// If true is returned, then the reference type parameter idx is
2225 /// assigned with the index of the declaration chunk.
2226 bool isFunctionDeclarator(unsigned& idx) const {
2227 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
2228 switch (DeclTypeInfo[i].Kind) {
2229 case DeclaratorChunk::Function:
2232 case DeclaratorChunk::Paren:
2234 case DeclaratorChunk::Pointer:
2235 case DeclaratorChunk::Reference:
2236 case DeclaratorChunk::Array:
2237 case DeclaratorChunk::BlockPointer:
2238 case DeclaratorChunk::MemberPointer:
2239 case DeclaratorChunk::Pipe:
2242 llvm_unreachable("Invalid type chunk");
2247 /// isFunctionDeclarator - Once this declarator is fully parsed and formed,
2248 /// this method returns true if the identifier is a function declarator
2249 /// (looking through parentheses).
2250 bool isFunctionDeclarator() const {
2252 return isFunctionDeclarator(index);
2255 /// getFunctionTypeInfo - Retrieves the function type info object
2256 /// (looking through parentheses).
2257 DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() {
2258 assert(isFunctionDeclarator() && "Not a function declarator!");
2260 isFunctionDeclarator(index);
2261 return DeclTypeInfo[index].Fun;
2264 /// getFunctionTypeInfo - Retrieves the function type info object
2265 /// (looking through parentheses).
2266 const DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() const {
2267 return const_cast<Declarator*>(this)->getFunctionTypeInfo();
2270 /// Determine whether the declaration that will be produced from
2271 /// this declaration will be a function.
2273 /// A declaration can declare a function even if the declarator itself
2274 /// isn't a function declarator, if the type specifier refers to a function
2275 /// type. This routine checks for both cases.
2276 bool isDeclarationOfFunction() const;
2278 /// Return true if this declaration appears in a context where a
2279 /// function declarator would be a function declaration.
2280 bool isFunctionDeclarationContext() const {
2281 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
2285 case DeclaratorContext::FileContext:
2286 case DeclaratorContext::MemberContext:
2287 case DeclaratorContext::BlockContext:
2288 case DeclaratorContext::ForContext:
2289 case DeclaratorContext::InitStmtContext:
2292 case DeclaratorContext::ConditionContext:
2293 case DeclaratorContext::KNRTypeListContext:
2294 case DeclaratorContext::TypeNameContext:
2295 case DeclaratorContext::FunctionalCastContext:
2296 case DeclaratorContext::AliasDeclContext:
2297 case DeclaratorContext::AliasTemplateContext:
2298 case DeclaratorContext::PrototypeContext:
2299 case DeclaratorContext::LambdaExprParameterContext:
2300 case DeclaratorContext::ObjCParameterContext:
2301 case DeclaratorContext::ObjCResultContext:
2302 case DeclaratorContext::TemplateParamContext:
2303 case DeclaratorContext::CXXNewContext:
2304 case DeclaratorContext::CXXCatchContext:
2305 case DeclaratorContext::ObjCCatchContext:
2306 case DeclaratorContext::BlockLiteralContext:
2307 case DeclaratorContext::LambdaExprContext:
2308 case DeclaratorContext::ConversionIdContext:
2309 case DeclaratorContext::TemplateArgContext:
2310 case DeclaratorContext::TemplateTypeArgContext:
2311 case DeclaratorContext::TrailingReturnContext:
2312 case DeclaratorContext::TrailingReturnVarContext:
2315 llvm_unreachable("unknown context kind!");
2318 /// Determine whether this declaration appears in a context where an
2319 /// expression could appear.
2320 bool isExpressionContext() const {
2322 case DeclaratorContext::FileContext:
2323 case DeclaratorContext::KNRTypeListContext:
2324 case DeclaratorContext::MemberContext:
2326 // FIXME: sizeof(...) permits an expression.
2327 case DeclaratorContext::TypeNameContext:
2329 case DeclaratorContext::FunctionalCastContext:
2330 case DeclaratorContext::AliasDeclContext:
2331 case DeclaratorContext::AliasTemplateContext:
2332 case DeclaratorContext::PrototypeContext:
2333 case DeclaratorContext::LambdaExprParameterContext:
2334 case DeclaratorContext::ObjCParameterContext:
2335 case DeclaratorContext::ObjCResultContext:
2336 case DeclaratorContext::TemplateParamContext:
2337 case DeclaratorContext::CXXNewContext:
2338 case DeclaratorContext::CXXCatchContext:
2339 case DeclaratorContext::ObjCCatchContext:
2340 case DeclaratorContext::BlockLiteralContext:
2341 case DeclaratorContext::LambdaExprContext:
2342 case DeclaratorContext::ConversionIdContext:
2343 case DeclaratorContext::TrailingReturnContext:
2344 case DeclaratorContext::TrailingReturnVarContext:
2345 case DeclaratorContext::TemplateTypeArgContext:
2348 case DeclaratorContext::BlockContext:
2349 case DeclaratorContext::ForContext:
2350 case DeclaratorContext::InitStmtContext:
2351 case DeclaratorContext::ConditionContext:
2352 case DeclaratorContext::TemplateArgContext:
2356 llvm_unreachable("unknown context kind!");
2359 /// Return true if a function declarator at this position would be a
2360 /// function declaration.
2361 bool isFunctionDeclaratorAFunctionDeclaration() const {
2362 if (!isFunctionDeclarationContext())
2365 for (unsigned I = 0, N = getNumTypeObjects(); I != N; ++I)
2366 if (getTypeObject(I).Kind != DeclaratorChunk::Paren)
2372 /// Determine whether a trailing return type was written (at any
2373 /// level) within this declarator.
2374 bool hasTrailingReturnType() const {
2375 for (const auto &Chunk : type_objects())
2376 if (Chunk.Kind == DeclaratorChunk::Function &&
2377 Chunk.Fun.hasTrailingReturnType())
2382 /// takeAttributes - Takes attributes from the given parsed-attributes
2383 /// set and add them to this declarator.
2385 /// These examples both add 3 attributes to "var":
2386 /// short int var __attribute__((aligned(16),common,deprecated));
2387 /// short int x, __attribute__((aligned(16)) var
2388 /// __attribute__((common,deprecated));
2390 /// Also extends the range of the declarator.
2391 void takeAttributes(ParsedAttributes &attrs, SourceLocation lastLoc) {
2392 Attrs.takeAllFrom(attrs);
2394 if (!lastLoc.isInvalid())
2395 SetRangeEnd(lastLoc);
2398 const ParsedAttributes &getAttributes() const { return Attrs; }
2399 ParsedAttributes &getAttributes() { return Attrs; }
2401 /// hasAttributes - do we contain any attributes?
2402 bool hasAttributes() const {
2403 if (!getAttributes().empty() || getDeclSpec().hasAttributes())
2405 for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i)
2406 if (!getTypeObject(i).getAttrs().empty())
2411 /// Return a source range list of C++11 attributes associated
2412 /// with the declarator.
2413 void getCXX11AttributeRanges(SmallVectorImpl<SourceRange> &Ranges) {
2414 for (const ParsedAttr &AL : Attrs)
2415 if (AL.isCXX11Attribute())
2416 Ranges.push_back(AL.getRange());
2419 void setAsmLabel(Expr *E) { AsmLabel = E; }
2420 Expr *getAsmLabel() const { return AsmLabel; }
2422 void setExtension(bool Val = true) { Extension = Val; }
2423 bool getExtension() const { return Extension; }
2425 void setObjCIvar(bool Val = true) { ObjCIvar = Val; }
2426 bool isObjCIvar() const { return ObjCIvar; }
2428 void setObjCWeakProperty(bool Val = true) { ObjCWeakProperty = Val; }
2429 bool isObjCWeakProperty() const { return ObjCWeakProperty; }
2431 void setInvalidType(bool Val = true) { InvalidType = Val; }
2432 bool isInvalidType() const {
2433 return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error;
2436 void setGroupingParens(bool flag) { GroupingParens = flag; }
2437 bool hasGroupingParens() const { return GroupingParens; }
2439 bool isFirstDeclarator() const { return !CommaLoc.isValid(); }
2440 SourceLocation getCommaLoc() const { return CommaLoc; }
2441 void setCommaLoc(SourceLocation CL) { CommaLoc = CL; }
2443 bool hasEllipsis() const { return EllipsisLoc.isValid(); }
2444 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
2445 void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; }
2447 void setFunctionDefinitionKind(FunctionDefinitionKind Val) {
2448 FunctionDefinition = Val;
2451 bool isFunctionDefinition() const {
2452 return getFunctionDefinitionKind() != FDK_Declaration;
2455 FunctionDefinitionKind getFunctionDefinitionKind() const {
2456 return (FunctionDefinitionKind)FunctionDefinition;
2459 /// Returns true if this declares a real member and not a friend.
2460 bool isFirstDeclarationOfMember() {
2461 return getContext() == DeclaratorContext::MemberContext &&
2462 !getDeclSpec().isFriendSpecified();
2465 /// Returns true if this declares a static member. This cannot be called on a
2466 /// declarator outside of a MemberContext because we won't know until
2467 /// redeclaration time if the decl is static.
2468 bool isStaticMember();
2470 /// Returns true if this declares a constructor or a destructor.
2471 bool isCtorOrDtor();
2473 void setRedeclaration(bool Val) { Redeclaration = Val; }
2474 bool isRedeclaration() const { return Redeclaration; }
2477 /// This little struct is used to capture information about
2478 /// structure field declarators, which is basically just a bitfield size.
2479 struct FieldDeclarator {
2482 explicit FieldDeclarator(const DeclSpec &DS)
2483 : D(DS, DeclaratorContext::MemberContext),
2484 BitfieldSize(nullptr) {}
2487 /// Represents a C++11 virt-specifier-seq.
2488 class VirtSpecifiers {
2495 // Represents the __final keyword, which is legal for gcc in pre-C++11 mode.
2499 VirtSpecifiers() : Specifiers(0), LastSpecifier(VS_None) { }
2501 bool SetSpecifier(Specifier VS, SourceLocation Loc,
2502 const char *&PrevSpec);
2504 bool isUnset() const { return Specifiers == 0; }
2506 bool isOverrideSpecified() const { return Specifiers & VS_Override; }
2507 SourceLocation getOverrideLoc() const { return VS_overrideLoc; }
2509 bool isFinalSpecified() const { return Specifiers & (VS_Final | VS_Sealed | VS_GNU_Final); }
2510 bool isFinalSpelledSealed() const { return Specifiers & VS_Sealed; }
2511 SourceLocation getFinalLoc() const { return VS_finalLoc; }
2513 void clear() { Specifiers = 0; }
2515 static const char *getSpecifierName(Specifier VS);
2517 SourceLocation getFirstLocation() const { return FirstLocation; }
2518 SourceLocation getLastLocation() const { return LastLocation; }
2519 Specifier getLastSpecifier() const { return LastSpecifier; }
2522 unsigned Specifiers;
2523 Specifier LastSpecifier;
2525 SourceLocation VS_overrideLoc, VS_finalLoc;
2526 SourceLocation FirstLocation;
2527 SourceLocation LastLocation;
2530 enum class LambdaCaptureInitKind {
2532 CopyInit, //!< [a = b], [a = {b}]
2533 DirectInit, //!< [a(b)]
2534 ListInit //!< [a{b}]
2537 /// Represents a complete lambda introducer.
2538 struct LambdaIntroducer {
2539 /// An individual capture in a lambda introducer.
2540 struct LambdaCapture {
2541 LambdaCaptureKind Kind;
2544 SourceLocation EllipsisLoc;
2545 LambdaCaptureInitKind InitKind;
2547 ParsedType InitCaptureType;
2548 SourceRange ExplicitRange;
2550 LambdaCapture(LambdaCaptureKind Kind, SourceLocation Loc,
2551 IdentifierInfo *Id, SourceLocation EllipsisLoc,
2552 LambdaCaptureInitKind InitKind, ExprResult Init,
2553 ParsedType InitCaptureType,
2554 SourceRange ExplicitRange)
2555 : Kind(Kind), Loc(Loc), Id(Id), EllipsisLoc(EllipsisLoc),
2556 InitKind(InitKind), Init(Init), InitCaptureType(InitCaptureType),
2557 ExplicitRange(ExplicitRange) {}
2561 SourceLocation DefaultLoc;
2562 LambdaCaptureDefault Default;
2563 SmallVector<LambdaCapture, 4> Captures;
2566 : Default(LCD_None) {}
2568 /// Append a capture in a lambda introducer.
2569 void addCapture(LambdaCaptureKind Kind,
2572 SourceLocation EllipsisLoc,
2573 LambdaCaptureInitKind InitKind,
2575 ParsedType InitCaptureType,
2576 SourceRange ExplicitRange) {
2577 Captures.push_back(LambdaCapture(Kind, Loc, Id, EllipsisLoc, InitKind, Init,
2578 InitCaptureType, ExplicitRange));
2582 } // end namespace clang
2584 #endif // LLVM_CLANG_SEMA_DECLSPEC_H