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 /// \brief 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/AttributeList.h"
33 #include "clang/Sema/Ownership.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 /// \brief 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 /// \brief Retrieve the representation of the nested-name-specifier.
76 NestedNameSpecifier *getScopeRep() const {
77 return Builder.getRepresentation();
80 /// \brief 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 /// \brief 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 /// \brief 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 /// \brief 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 /// \brief Turn this (empty) nested-name-specifier into the global
138 /// nested-name-specifier '::'.
139 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
141 /// \brief 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 /// \brief 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 /// \brief Adopt an existing nested-name-specifier (with source-range
168 void Adopt(NestedNameSpecifierLoc Other);
170 /// \brief 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 /// \brief 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 /// \brief 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 /// \brief Retrieve the data associated with the source-location information.
217 char *location_data() const { return Builder.getBuffer().first; }
219 /// \brief Retrieve the size of the data associated with source-location
221 unsigned location_size() const { return Builder.getBuffer().second; }
224 /// \brief Captures information about "declaration specifiers".
226 /// "Declaration specifiers" encompasses storage-class-specifiers,
227 /// type-specifiers, type-qualifiers, and function-specifiers.
230 /// \brief 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_char16 = clang::TST_char16;
277 static const TST TST_char32 = clang::TST_char32;
278 static const TST TST_int = clang::TST_int;
279 static const TST TST_int128 = clang::TST_int128;
280 static const TST TST_half = clang::TST_half;
281 static const TST TST_float = clang::TST_float;
282 static const TST TST_double = clang::TST_double;
283 static const TST TST_float128 = clang::TST_float128;
284 static const TST TST_bool = clang::TST_bool;
285 static const TST TST_decimal32 = clang::TST_decimal32;
286 static const TST TST_decimal64 = clang::TST_decimal64;
287 static const TST TST_decimal128 = clang::TST_decimal128;
288 static const TST TST_enum = clang::TST_enum;
289 static const TST TST_union = clang::TST_union;
290 static const TST TST_struct = clang::TST_struct;
291 static const TST TST_interface = clang::TST_interface;
292 static const TST TST_class = clang::TST_class;
293 static const TST TST_typename = clang::TST_typename;
294 static const TST TST_typeofType = clang::TST_typeofType;
295 static const TST TST_typeofExpr = clang::TST_typeofExpr;
296 static const TST TST_decltype = clang::TST_decltype;
297 static const TST TST_decltype_auto = clang::TST_decltype_auto;
298 static const TST TST_underlyingType = clang::TST_underlyingType;
299 static const TST TST_auto = clang::TST_auto;
300 static const TST TST_auto_type = clang::TST_auto_type;
301 static const TST TST_unknown_anytype = clang::TST_unknown_anytype;
302 static const TST TST_atomic = clang::TST_atomic;
303 #define GENERIC_IMAGE_TYPE(ImgType, Id) \
304 static const TST TST_##ImgType##_t = clang::TST_##ImgType##_t;
305 #include "clang/Basic/OpenCLImageTypes.def"
306 static const TST TST_error = clang::TST_error;
309 enum TQ { // NOTE: These flags must be kept in sync with Qualifiers::TQ.
315 // This has no corresponding Qualifiers::TQ value, because it's not treated
316 // as a qualifier in our type system.
320 /// ParsedSpecifiers - Flags to query which specifiers were applied. This is
321 /// returned by getParsedSpecifiers.
322 enum ParsedSpecifiers {
324 PQ_StorageClassSpecifier = 1,
325 PQ_TypeSpecifier = 2,
326 PQ_TypeQualifier = 4,
327 PQ_FunctionSpecifier = 8
331 // storage-class-specifier
332 /*SCS*/unsigned StorageClassSpec : 3;
333 /*TSCS*/unsigned ThreadStorageClassSpec : 2;
334 unsigned SCS_extern_in_linkage_spec : 1;
337 /*TSW*/unsigned TypeSpecWidth : 2;
338 /*TSC*/unsigned TypeSpecComplex : 2;
339 /*TSS*/unsigned TypeSpecSign : 2;
340 /*TST*/unsigned TypeSpecType : 6;
341 unsigned TypeAltiVecVector : 1;
342 unsigned TypeAltiVecPixel : 1;
343 unsigned TypeAltiVecBool : 1;
344 unsigned TypeSpecOwned : 1;
345 unsigned TypeSpecPipe : 1;
348 unsigned TypeQualifiers : 5; // Bitwise OR of TQ.
350 // function-specifier
351 unsigned FS_inline_specified : 1;
352 unsigned FS_forceinline_specified: 1;
353 unsigned FS_virtual_specified : 1;
354 unsigned FS_explicit_specified : 1;
355 unsigned FS_noreturn_specified : 1;
358 unsigned Friend_specified : 1;
360 // constexpr-specifier
361 unsigned Constexpr_specified : 1;
364 unsigned Concept_specified : 1;
367 UnionParsedType TypeRep;
373 ParsedAttributes Attrs;
375 // Scope specifier for the type spec, if applicable.
376 CXXScopeSpec TypeScope;
378 // SourceLocation info. These are null if the item wasn't specified or if
379 // the setting was synthesized.
382 SourceLocation StorageClassSpecLoc, ThreadStorageClassSpecLoc;
383 SourceRange TSWRange;
384 SourceLocation TSCLoc, TSSLoc, TSTLoc, AltiVecLoc;
385 /// TSTNameLoc - If TypeSpecType is any of class, enum, struct, union,
386 /// typename, then this is the location of the named type (if present);
387 /// otherwise, it is the same as TSTLoc. Hence, the pair TSTLoc and
388 /// TSTNameLoc provides source range info for tag types.
389 SourceLocation TSTNameLoc;
390 SourceRange TypeofParensRange;
391 SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc, TQ_atomicLoc,
393 SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc, FS_noreturnLoc;
394 SourceLocation FS_forceinlineLoc;
395 SourceLocation FriendLoc, ModulePrivateLoc, ConstexprLoc, ConceptLoc;
396 SourceLocation TQ_pipeLoc;
398 WrittenBuiltinSpecs writtenBS;
399 void SaveWrittenBuiltinSpecs();
401 ObjCDeclSpec *ObjCQualifiers;
403 static bool isTypeRep(TST T) {
404 return (T == TST_typename || T == TST_typeofType ||
405 T == TST_underlyingType || T == TST_atomic);
407 static bool isExprRep(TST T) {
408 return (T == TST_typeofExpr || T == TST_decltype);
411 DeclSpec(const DeclSpec &) = delete;
412 void operator=(const DeclSpec &) = delete;
414 static bool isDeclRep(TST T) {
415 return (T == TST_enum || T == TST_struct ||
416 T == TST_interface || T == TST_union ||
420 DeclSpec(AttributeFactory &attrFactory)
421 : StorageClassSpec(SCS_unspecified),
422 ThreadStorageClassSpec(TSCS_unspecified),
423 SCS_extern_in_linkage_spec(false),
424 TypeSpecWidth(TSW_unspecified),
425 TypeSpecComplex(TSC_unspecified),
426 TypeSpecSign(TSS_unspecified),
427 TypeSpecType(TST_unspecified),
428 TypeAltiVecVector(false),
429 TypeAltiVecPixel(false),
430 TypeAltiVecBool(false),
431 TypeSpecOwned(false),
433 TypeQualifiers(TQ_unspecified),
434 FS_inline_specified(false),
435 FS_forceinline_specified(false),
436 FS_virtual_specified(false),
437 FS_explicit_specified(false),
438 FS_noreturn_specified(false),
439 Friend_specified(false),
440 Constexpr_specified(false),
441 Concept_specified(false),
444 ObjCQualifiers(nullptr) {
447 // storage-class-specifier
448 SCS getStorageClassSpec() const { return (SCS)StorageClassSpec; }
449 TSCS getThreadStorageClassSpec() const {
450 return (TSCS)ThreadStorageClassSpec;
452 bool isExternInLinkageSpec() const { return SCS_extern_in_linkage_spec; }
453 void setExternInLinkageSpec(bool Value) {
454 SCS_extern_in_linkage_spec = Value;
457 SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; }
458 SourceLocation getThreadStorageClassSpecLoc() const {
459 return ThreadStorageClassSpecLoc;
462 void ClearStorageClassSpecs() {
463 StorageClassSpec = DeclSpec::SCS_unspecified;
464 ThreadStorageClassSpec = DeclSpec::TSCS_unspecified;
465 SCS_extern_in_linkage_spec = false;
466 StorageClassSpecLoc = SourceLocation();
467 ThreadStorageClassSpecLoc = SourceLocation();
470 void ClearTypeSpecType() {
471 TypeSpecType = DeclSpec::TST_unspecified;
472 TypeSpecOwned = false;
473 TSTLoc = SourceLocation();
477 TSW getTypeSpecWidth() const { return (TSW)TypeSpecWidth; }
478 TSC getTypeSpecComplex() const { return (TSC)TypeSpecComplex; }
479 TSS getTypeSpecSign() const { return (TSS)TypeSpecSign; }
480 TST getTypeSpecType() const { return (TST)TypeSpecType; }
481 bool isTypeAltiVecVector() const { return TypeAltiVecVector; }
482 bool isTypeAltiVecPixel() const { return TypeAltiVecPixel; }
483 bool isTypeAltiVecBool() const { return TypeAltiVecBool; }
484 bool isTypeSpecOwned() const { return TypeSpecOwned; }
485 bool isTypeRep() const { return isTypeRep((TST) TypeSpecType); }
486 bool isTypeSpecPipe() const { return TypeSpecPipe; }
488 ParsedType getRepAsType() const {
489 assert(isTypeRep((TST) TypeSpecType) && "DeclSpec does not store a type");
492 Decl *getRepAsDecl() const {
493 assert(isDeclRep((TST) TypeSpecType) && "DeclSpec does not store a decl");
496 Expr *getRepAsExpr() const {
497 assert(isExprRep((TST) TypeSpecType) && "DeclSpec does not store an expr");
500 CXXScopeSpec &getTypeSpecScope() { return TypeScope; }
501 const CXXScopeSpec &getTypeSpecScope() const { return TypeScope; }
503 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
504 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
505 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
507 SourceLocation getTypeSpecWidthLoc() const { return TSWRange.getBegin(); }
508 SourceRange getTypeSpecWidthRange() const { return TSWRange; }
509 SourceLocation getTypeSpecComplexLoc() const { return TSCLoc; }
510 SourceLocation getTypeSpecSignLoc() const { return TSSLoc; }
511 SourceLocation getTypeSpecTypeLoc() const { return TSTLoc; }
512 SourceLocation getAltiVecLoc() const { return AltiVecLoc; }
514 SourceLocation getTypeSpecTypeNameLoc() const {
515 assert(isDeclRep((TST) TypeSpecType) || TypeSpecType == TST_typename);
519 SourceRange getTypeofParensRange() const { return TypeofParensRange; }
520 void setTypeofParensRange(SourceRange range) { TypeofParensRange = range; }
522 bool containsPlaceholderType() const {
523 return (TypeSpecType == TST_auto || TypeSpecType == TST_auto_type ||
524 TypeSpecType == TST_decltype_auto);
527 bool hasTagDefinition() const;
529 /// \brief Turn a type-specifier-type into a string like "_Bool" or "union".
530 static const char *getSpecifierName(DeclSpec::TST T,
531 const PrintingPolicy &Policy);
532 static const char *getSpecifierName(DeclSpec::TQ Q);
533 static const char *getSpecifierName(DeclSpec::TSS S);
534 static const char *getSpecifierName(DeclSpec::TSC C);
535 static const char *getSpecifierName(DeclSpec::TSW W);
536 static const char *getSpecifierName(DeclSpec::SCS S);
537 static const char *getSpecifierName(DeclSpec::TSCS S);
541 /// getTypeQualifiers - Return a set of TQs.
542 unsigned getTypeQualifiers() const { return TypeQualifiers; }
543 SourceLocation getConstSpecLoc() const { return TQ_constLoc; }
544 SourceLocation getRestrictSpecLoc() const { return TQ_restrictLoc; }
545 SourceLocation getVolatileSpecLoc() const { return TQ_volatileLoc; }
546 SourceLocation getAtomicSpecLoc() const { return TQ_atomicLoc; }
547 SourceLocation getUnalignedSpecLoc() const { return TQ_unalignedLoc; }
548 SourceLocation getPipeLoc() const { return TQ_pipeLoc; }
550 /// \brief Clear out all of the type qualifiers.
551 void ClearTypeQualifiers() {
553 TQ_constLoc = SourceLocation();
554 TQ_restrictLoc = SourceLocation();
555 TQ_volatileLoc = SourceLocation();
556 TQ_atomicLoc = SourceLocation();
557 TQ_unalignedLoc = SourceLocation();
558 TQ_pipeLoc = SourceLocation();
561 // function-specifier
562 bool isInlineSpecified() const {
563 return FS_inline_specified | FS_forceinline_specified;
565 SourceLocation getInlineSpecLoc() const {
566 return FS_inline_specified ? FS_inlineLoc : FS_forceinlineLoc;
569 bool isVirtualSpecified() const { return FS_virtual_specified; }
570 SourceLocation getVirtualSpecLoc() const { return FS_virtualLoc; }
572 bool isExplicitSpecified() const { return FS_explicit_specified; }
573 SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; }
575 bool isNoreturnSpecified() const { return FS_noreturn_specified; }
576 SourceLocation getNoreturnSpecLoc() const { return FS_noreturnLoc; }
578 void ClearFunctionSpecs() {
579 FS_inline_specified = false;
580 FS_inlineLoc = SourceLocation();
581 FS_forceinline_specified = false;
582 FS_forceinlineLoc = SourceLocation();
583 FS_virtual_specified = false;
584 FS_virtualLoc = SourceLocation();
585 FS_explicit_specified = false;
586 FS_explicitLoc = SourceLocation();
587 FS_noreturn_specified = false;
588 FS_noreturnLoc = SourceLocation();
591 /// \brief Return true if any type-specifier has been found.
592 bool hasTypeSpecifier() const {
593 return getTypeSpecType() != DeclSpec::TST_unspecified ||
594 getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
595 getTypeSpecComplex() != DeclSpec::TSC_unspecified ||
596 getTypeSpecSign() != DeclSpec::TSS_unspecified;
599 /// \brief Return a bitmask of which flavors of specifiers this
600 /// DeclSpec includes.
601 unsigned getParsedSpecifiers() const;
603 /// isEmpty - Return true if this declaration specifier is completely empty:
604 /// no tokens were parsed in the production of it.
605 bool isEmpty() const {
606 return getParsedSpecifiers() == DeclSpec::PQ_None;
609 void SetRangeStart(SourceLocation Loc) { Range.setBegin(Loc); }
610 void SetRangeEnd(SourceLocation Loc) { Range.setEnd(Loc); }
612 /// These methods set the specified attribute of the DeclSpec and
613 /// return false if there was no error. If an error occurs (for
614 /// example, if we tried to set "auto" on a spec with "extern"
615 /// already set), they return true and set PrevSpec and DiagID
617 /// Diag(Loc, DiagID) << PrevSpec;
618 /// will yield a useful result.
620 /// TODO: use a more general approach that still allows these
621 /// diagnostics to be ignored when desired.
622 bool SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
623 const char *&PrevSpec, unsigned &DiagID,
624 const PrintingPolicy &Policy);
625 bool SetStorageClassSpecThread(TSCS TSC, SourceLocation Loc,
626 const char *&PrevSpec, unsigned &DiagID);
627 bool SetTypeSpecWidth(TSW W, SourceLocation Loc, const char *&PrevSpec,
628 unsigned &DiagID, const PrintingPolicy &Policy);
629 bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec,
631 bool SetTypeSpecSign(TSS S, SourceLocation Loc, const char *&PrevSpec,
633 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
634 unsigned &DiagID, const PrintingPolicy &Policy);
635 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
636 unsigned &DiagID, ParsedType Rep,
637 const PrintingPolicy &Policy);
638 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
639 unsigned &DiagID, Decl *Rep, bool Owned,
640 const PrintingPolicy &Policy);
641 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
642 SourceLocation TagNameLoc, const char *&PrevSpec,
643 unsigned &DiagID, ParsedType Rep,
644 const PrintingPolicy &Policy);
645 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
646 SourceLocation TagNameLoc, const char *&PrevSpec,
647 unsigned &DiagID, Decl *Rep, bool Owned,
648 const PrintingPolicy &Policy);
650 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
651 unsigned &DiagID, Expr *Rep,
652 const PrintingPolicy &policy);
653 bool SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
654 const char *&PrevSpec, unsigned &DiagID,
655 const PrintingPolicy &Policy);
656 bool SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
657 const char *&PrevSpec, unsigned &DiagID,
658 const PrintingPolicy &Policy);
659 bool SetTypeAltiVecBool(bool isAltiVecBool, SourceLocation Loc,
660 const char *&PrevSpec, unsigned &DiagID,
661 const PrintingPolicy &Policy);
662 bool SetTypePipe(bool isPipe, SourceLocation Loc,
663 const char *&PrevSpec, unsigned &DiagID,
664 const PrintingPolicy &Policy);
665 bool SetTypeSpecError();
666 void UpdateDeclRep(Decl *Rep) {
667 assert(isDeclRep((TST) TypeSpecType));
670 void UpdateTypeRep(ParsedType Rep) {
671 assert(isTypeRep((TST) TypeSpecType));
674 void UpdateExprRep(Expr *Rep) {
675 assert(isExprRep((TST) TypeSpecType));
679 bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
680 unsigned &DiagID, const LangOptions &Lang);
682 bool setFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
684 bool setFunctionSpecForceInline(SourceLocation Loc, const char *&PrevSpec,
686 bool setFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
688 bool setFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
690 bool setFunctionSpecNoreturn(SourceLocation Loc, const char *&PrevSpec,
693 bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
695 bool setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
697 bool SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
699 bool SetConceptSpec(SourceLocation Loc, const char *&PrevSpec,
702 bool isFriendSpecified() const { return Friend_specified; }
703 SourceLocation getFriendSpecLoc() const { return FriendLoc; }
705 bool isModulePrivateSpecified() const { return ModulePrivateLoc.isValid(); }
706 SourceLocation getModulePrivateSpecLoc() const { return ModulePrivateLoc; }
708 bool isConstexprSpecified() const { return Constexpr_specified; }
709 SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; }
711 bool isConceptSpecified() const { return Concept_specified; }
712 SourceLocation getConceptSpecLoc() const { return ConceptLoc; }
714 void ClearConstexprSpec() {
715 Constexpr_specified = false;
716 ConstexprLoc = SourceLocation();
719 void ClearConceptSpec() {
720 Concept_specified = false;
721 ConceptLoc = SourceLocation();
724 AttributePool &getAttributePool() const {
725 return Attrs.getPool();
728 /// \brief Concatenates two attribute lists.
730 /// The GCC attribute syntax allows for the following:
733 /// short __attribute__(( unused, deprecated ))
734 /// int __attribute__(( may_alias, aligned(16) )) var;
737 /// This declares 4 attributes using 2 lists. The following syntax is
738 /// also allowed and equivalent to the previous declaration.
741 /// short __attribute__((unused)) __attribute__((deprecated))
742 /// int __attribute__((may_alias)) __attribute__((aligned(16))) var;
745 void addAttributes(AttributeList *AL) {
749 bool hasAttributes() const { return !Attrs.empty(); }
751 ParsedAttributes &getAttributes() { return Attrs; }
752 const ParsedAttributes &getAttributes() const { return Attrs; }
754 void takeAttributesFrom(ParsedAttributes &attrs) {
755 Attrs.takeAllFrom(attrs);
758 /// Finish - This does final analysis of the declspec, issuing diagnostics for
759 /// things like "_Imaginary" (lacking an FP type). After calling this method,
760 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
761 void Finish(Sema &S, const PrintingPolicy &Policy);
763 const WrittenBuiltinSpecs& getWrittenBuiltinSpecs() const {
767 ObjCDeclSpec *getObjCQualifiers() const { return ObjCQualifiers; }
768 void setObjCQualifiers(ObjCDeclSpec *quals) { ObjCQualifiers = quals; }
770 /// \brief Checks if this DeclSpec can stand alone, without a Declarator.
772 /// Only tag declspecs can stand alone.
773 bool isMissingDeclaratorOk();
776 /// \brief Captures information about "declaration specifiers" specific to
780 /// ObjCDeclQualifier - Qualifier used on types in method
781 /// declarations. Not all combinations are sensible. Parameters
782 /// can be one of { in, out, inout } with one of { bycopy, byref }.
783 /// Returns can either be { oneway } or not.
785 /// This should be kept in sync with Decl::ObjCDeclQualifier.
786 enum ObjCDeclQualifier {
794 DQ_CSNullability = 0x40
797 /// PropertyAttributeKind - list of property attributes.
798 /// Keep this list in sync with LLVM's Dwarf.h ApplePropertyAttributes.
799 enum ObjCPropertyAttributeKind {
801 DQ_PR_readonly = 0x01,
804 DQ_PR_readwrite = 0x08,
807 DQ_PR_nonatomic = 0x40,
809 DQ_PR_atomic = 0x100,
811 DQ_PR_strong = 0x400,
812 DQ_PR_unsafe_unretained = 0x800,
813 DQ_PR_nullability = 0x1000,
814 DQ_PR_null_resettable = 0x2000,
819 : objcDeclQualifier(DQ_None), PropertyAttributes(DQ_PR_noattr),
820 Nullability(0), GetterName(nullptr), SetterName(nullptr) { }
822 ObjCDeclQualifier getObjCDeclQualifier() const { return objcDeclQualifier; }
823 void setObjCDeclQualifier(ObjCDeclQualifier DQVal) {
824 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal);
826 void clearObjCDeclQualifier(ObjCDeclQualifier DQVal) {
827 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier & ~DQVal);
830 ObjCPropertyAttributeKind getPropertyAttributes() const {
831 return ObjCPropertyAttributeKind(PropertyAttributes);
833 void setPropertyAttributes(ObjCPropertyAttributeKind PRVal) {
835 (ObjCPropertyAttributeKind)(PropertyAttributes | PRVal);
838 NullabilityKind getNullability() const {
839 assert(((getObjCDeclQualifier() & DQ_CSNullability) ||
840 (getPropertyAttributes() & DQ_PR_nullability)) &&
841 "Objective-C declspec doesn't have nullability");
842 return static_cast<NullabilityKind>(Nullability);
845 SourceLocation getNullabilityLoc() const {
846 assert(((getObjCDeclQualifier() & DQ_CSNullability) ||
847 (getPropertyAttributes() & DQ_PR_nullability)) &&
848 "Objective-C declspec doesn't have nullability");
849 return NullabilityLoc;
852 void setNullability(SourceLocation loc, NullabilityKind kind) {
853 assert(((getObjCDeclQualifier() & DQ_CSNullability) ||
854 (getPropertyAttributes() & DQ_PR_nullability)) &&
855 "Set the nullability declspec or property attribute first");
856 Nullability = static_cast<unsigned>(kind);
857 NullabilityLoc = loc;
860 const IdentifierInfo *getGetterName() const { return GetterName; }
861 IdentifierInfo *getGetterName() { return GetterName; }
862 void setGetterName(IdentifierInfo *name) { GetterName = name; }
864 const IdentifierInfo *getSetterName() const { return SetterName; }
865 IdentifierInfo *getSetterName() { return SetterName; }
866 void setSetterName(IdentifierInfo *name) { SetterName = name; }
869 // FIXME: These two are unrelated and mutually exclusive. So perhaps
870 // we can put them in a union to reflect their mutual exclusivity
871 // (space saving is negligible).
872 ObjCDeclQualifier objcDeclQualifier : 7;
874 // NOTE: VC++ treats enums as signed, avoid using ObjCPropertyAttributeKind
875 unsigned PropertyAttributes : 15;
877 unsigned Nullability : 2;
879 SourceLocation NullabilityLoc;
881 IdentifierInfo *GetterName; // getter name or NULL if no getter
882 IdentifierInfo *SetterName; // setter name or NULL if no setter
885 /// \brief Represents a C++ unqualified-id that has been parsed.
886 class UnqualifiedId {
888 UnqualifiedId(const UnqualifiedId &Other) = delete;
889 const UnqualifiedId &operator=(const UnqualifiedId &) = delete;
892 /// \brief Describes the kind of unqualified-id parsed.
894 /// \brief An identifier.
896 /// \brief An overloaded operator name, e.g., operator+.
897 IK_OperatorFunctionId,
898 /// \brief A conversion function name, e.g., operator int.
899 IK_ConversionFunctionId,
900 /// \brief A user-defined literal name, e.g., operator "" _i.
901 IK_LiteralOperatorId,
902 /// \brief A constructor name.
904 /// \brief A constructor named via a template-id.
905 IK_ConstructorTemplateId,
906 /// \brief A destructor name.
908 /// \brief A template-id, e.g., f<int>.
910 /// \brief An implicit 'self' parameter
915 /// \brief The kind of overloaded operator.
916 OverloadedOperatorKind Operator;
918 /// \brief The source locations of the individual tokens that name
919 /// the operator, e.g., the "new", "[", and "]" tokens in
922 /// Different operators have different numbers of tokens in their name,
923 /// up to three. Any remaining source locations in this array will be
924 /// set to an invalid value for operators with fewer than three tokens.
925 unsigned SymbolLocations[3];
928 /// \brief Anonymous union that holds extra data associated with the
929 /// parsed unqualified-id.
931 /// \brief When Kind == IK_Identifier, the parsed identifier, or when Kind
932 /// == IK_UserLiteralId, the identifier suffix.
933 IdentifierInfo *Identifier;
935 /// \brief When Kind == IK_OperatorFunctionId, the overloaded operator
937 struct OFI OperatorFunctionId;
939 /// \brief When Kind == IK_ConversionFunctionId, the type that the
940 /// conversion function names.
941 UnionParsedType ConversionFunctionId;
943 /// \brief When Kind == IK_ConstructorName, the class-name of the type
944 /// whose constructor is being referenced.
945 UnionParsedType ConstructorName;
947 /// \brief When Kind == IK_DestructorName, the type referred to by the
949 UnionParsedType DestructorName;
951 /// \brief When Kind == IK_TemplateId or IK_ConstructorTemplateId,
952 /// the template-id annotation that contains the template name and
953 /// template arguments.
954 TemplateIdAnnotation *TemplateId;
957 /// \brief The location of the first token that describes this unqualified-id,
958 /// which will be the location of the identifier, "operator" keyword,
959 /// tilde (for a destructor), or the template name of a template-id.
960 SourceLocation StartLocation;
962 /// \brief The location of the last token that describes this unqualified-id.
963 SourceLocation EndLocation;
965 UnqualifiedId() : Kind(IK_Identifier), Identifier(nullptr) { }
967 /// \brief Clear out this unqualified-id, setting it to default (invalid)
970 Kind = IK_Identifier;
971 Identifier = nullptr;
972 StartLocation = SourceLocation();
973 EndLocation = SourceLocation();
976 /// \brief Determine whether this unqualified-id refers to a valid name.
977 bool isValid() const { return StartLocation.isValid(); }
979 /// \brief Determine whether this unqualified-id refers to an invalid name.
980 bool isInvalid() const { return !isValid(); }
982 /// \brief Determine what kind of name we have.
983 IdKind getKind() const { return Kind; }
984 void setKind(IdKind kind) { Kind = kind; }
986 /// \brief Specify that this unqualified-id was parsed as an identifier.
988 /// \param Id the parsed identifier.
989 /// \param IdLoc the location of the parsed identifier.
990 void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) {
991 Kind = IK_Identifier;
992 Identifier = const_cast<IdentifierInfo *>(Id);
993 StartLocation = EndLocation = IdLoc;
996 /// \brief Specify that this unqualified-id was parsed as an
997 /// operator-function-id.
999 /// \param OperatorLoc the location of the 'operator' keyword.
1001 /// \param Op the overloaded operator.
1003 /// \param SymbolLocations the locations of the individual operator symbols
1004 /// in the operator.
1005 void setOperatorFunctionId(SourceLocation OperatorLoc,
1006 OverloadedOperatorKind Op,
1007 SourceLocation SymbolLocations[3]);
1009 /// \brief Specify that this unqualified-id was parsed as a
1010 /// conversion-function-id.
1012 /// \param OperatorLoc the location of the 'operator' keyword.
1014 /// \param Ty the type to which this conversion function is converting.
1016 /// \param EndLoc the location of the last token that makes up the type name.
1017 void setConversionFunctionId(SourceLocation OperatorLoc,
1019 SourceLocation EndLoc) {
1020 Kind = IK_ConversionFunctionId;
1021 StartLocation = OperatorLoc;
1022 EndLocation = EndLoc;
1023 ConversionFunctionId = Ty;
1026 /// \brief Specific that this unqualified-id was parsed as a
1027 /// literal-operator-id.
1029 /// \param Id the parsed identifier.
1031 /// \param OpLoc the location of the 'operator' keyword.
1033 /// \param IdLoc the location of the identifier.
1034 void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc,
1035 SourceLocation IdLoc) {
1036 Kind = IK_LiteralOperatorId;
1037 Identifier = const_cast<IdentifierInfo *>(Id);
1038 StartLocation = OpLoc;
1039 EndLocation = IdLoc;
1042 /// \brief Specify that this unqualified-id was parsed as a constructor name.
1044 /// \param ClassType the class type referred to by the constructor name.
1046 /// \param ClassNameLoc the location of the class name.
1048 /// \param EndLoc the location of the last token that makes up the type name.
1049 void setConstructorName(ParsedType ClassType,
1050 SourceLocation ClassNameLoc,
1051 SourceLocation EndLoc) {
1052 Kind = IK_ConstructorName;
1053 StartLocation = ClassNameLoc;
1054 EndLocation = EndLoc;
1055 ConstructorName = ClassType;
1058 /// \brief Specify that this unqualified-id was parsed as a
1059 /// template-id that names a constructor.
1061 /// \param TemplateId the template-id annotation that describes the parsed
1062 /// template-id. This UnqualifiedId instance will take ownership of the
1063 /// \p TemplateId and will free it on destruction.
1064 void setConstructorTemplateId(TemplateIdAnnotation *TemplateId);
1066 /// \brief Specify that this unqualified-id was parsed as a destructor name.
1068 /// \param TildeLoc the location of the '~' that introduces the destructor
1071 /// \param ClassType the name of the class referred to by the destructor name.
1072 void setDestructorName(SourceLocation TildeLoc,
1073 ParsedType ClassType,
1074 SourceLocation EndLoc) {
1075 Kind = IK_DestructorName;
1076 StartLocation = TildeLoc;
1077 EndLocation = EndLoc;
1078 DestructorName = ClassType;
1081 /// \brief Specify that this unqualified-id was parsed as a template-id.
1083 /// \param TemplateId the template-id annotation that describes the parsed
1084 /// template-id. This UnqualifiedId instance will take ownership of the
1085 /// \p TemplateId and will free it on destruction.
1086 void setTemplateId(TemplateIdAnnotation *TemplateId);
1088 /// \brief Return the source range that covers this unqualified-id.
1089 SourceRange getSourceRange() const LLVM_READONLY {
1090 return SourceRange(StartLocation, EndLocation);
1092 SourceLocation getLocStart() const LLVM_READONLY { return StartLocation; }
1093 SourceLocation getLocEnd() const LLVM_READONLY { return EndLocation; }
1096 /// \brief A set of tokens that has been cached for later parsing.
1097 typedef SmallVector<Token, 4> CachedTokens;
1099 /// \brief One instance of this struct is used for each type in a
1100 /// declarator that is parsed.
1102 /// This is intended to be a small value object.
1103 struct DeclaratorChunk {
1105 Pointer, Reference, Array, Function, BlockPointer, MemberPointer, Paren, Pipe
1108 /// Loc - The place where this type was defined.
1110 /// EndLoc - If valid, the place where this chunck ends.
1111 SourceLocation EndLoc;
1113 SourceRange getSourceRange() const {
1114 if (EndLoc.isInvalid())
1115 return SourceRange(Loc, Loc);
1116 return SourceRange(Loc, EndLoc);
1119 struct TypeInfoCommon {
1120 AttributeList *AttrList;
1123 struct PointerTypeInfo : TypeInfoCommon {
1124 /// The type qualifiers: const/volatile/restrict/unaligned/atomic.
1125 unsigned TypeQuals : 5;
1127 /// The location of the const-qualifier, if any.
1128 unsigned ConstQualLoc;
1130 /// The location of the volatile-qualifier, if any.
1131 unsigned VolatileQualLoc;
1133 /// The location of the restrict-qualifier, if any.
1134 unsigned RestrictQualLoc;
1136 /// The location of the _Atomic-qualifier, if any.
1137 unsigned AtomicQualLoc;
1139 /// The location of the __unaligned-qualifier, if any.
1140 unsigned UnalignedQualLoc;
1146 struct ReferenceTypeInfo : TypeInfoCommon {
1147 /// The type qualifier: restrict. [GNU] C++ extension
1148 bool HasRestrict : 1;
1149 /// True if this is an lvalue reference, false if it's an rvalue reference.
1155 struct ArrayTypeInfo : TypeInfoCommon {
1156 /// The type qualifiers for the array:
1157 /// const/volatile/restrict/__unaligned/_Atomic.
1158 unsigned TypeQuals : 5;
1160 /// True if this dimension included the 'static' keyword.
1161 unsigned hasStatic : 1;
1163 /// True if this dimension was [*]. In this case, NumElts is null.
1164 unsigned isStar : 1;
1166 /// This is the size of the array, or null if [] or [*] was specified.
1167 /// Since the parser is multi-purpose, and we don't want to impose a root
1168 /// expression class on all clients, NumElts is untyped.
1174 /// ParamInfo - An array of paraminfo objects is allocated whenever a function
1175 /// declarator is parsed. There are two interesting styles of parameters
1177 /// K&R-style identifier lists and parameter type lists. K&R-style identifier
1178 /// lists will have information about the identifier, but no type information.
1179 /// Parameter type lists will have type info (if the actions module provides
1180 /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'.
1182 IdentifierInfo *Ident;
1183 SourceLocation IdentLoc;
1186 /// DefaultArgTokens - When the parameter's default argument
1187 /// cannot be parsed immediately (because it occurs within the
1188 /// declaration of a member function), it will be stored here as a
1189 /// sequence of tokens to be parsed once the class definition is
1190 /// complete. Non-NULL indicates that there is a default argument.
1191 std::unique_ptr<CachedTokens> DefaultArgTokens;
1193 ParamInfo() = default;
1194 ParamInfo(IdentifierInfo *ident, SourceLocation iloc,
1196 std::unique_ptr<CachedTokens> DefArgTokens = nullptr)
1197 : Ident(ident), IdentLoc(iloc), Param(param),
1198 DefaultArgTokens(std::move(DefArgTokens)) {}
1201 struct TypeAndRange {
1206 struct FunctionTypeInfo : TypeInfoCommon {
1207 /// hasPrototype - This is true if the function had at least one typed
1208 /// parameter. If the function is () or (a,b,c), then it has no prototype,
1209 /// and is treated as a K&R-style function.
1210 unsigned hasPrototype : 1;
1212 /// isVariadic - If this function has a prototype, and if that
1213 /// proto ends with ',...)', this is true. When true, EllipsisLoc
1214 /// contains the location of the ellipsis.
1215 unsigned isVariadic : 1;
1217 /// Can this declaration be a constructor-style initializer?
1218 unsigned isAmbiguous : 1;
1220 /// \brief Whether the ref-qualifier (if any) is an lvalue reference.
1221 /// Otherwise, it's an rvalue reference.
1222 unsigned RefQualifierIsLValueRef : 1;
1224 /// The type qualifiers: const/volatile/restrict/__unaligned
1225 /// The qualifier bitmask values are the same as in QualType.
1226 unsigned TypeQuals : 4;
1228 /// ExceptionSpecType - An ExceptionSpecificationType value.
1229 unsigned ExceptionSpecType : 4;
1231 /// DeleteParams - If this is true, we need to delete[] Params.
1232 unsigned DeleteParams : 1;
1234 /// HasTrailingReturnType - If this is true, a trailing return type was
1236 unsigned HasTrailingReturnType : 1;
1238 /// The location of the left parenthesis in the source.
1241 /// When isVariadic is true, the location of the ellipsis in the source.
1242 unsigned EllipsisLoc;
1244 /// The location of the right parenthesis in the source.
1247 /// NumParams - This is the number of formal parameters specified by the
1251 /// NumExceptionsOrDecls - This is the number of types in the
1252 /// dynamic-exception-decl, if the function has one. In C, this is the
1253 /// number of declarations in the function prototype.
1254 unsigned NumExceptionsOrDecls;
1256 /// \brief The location of the ref-qualifier, if any.
1258 /// If this is an invalid location, there is no ref-qualifier.
1259 unsigned RefQualifierLoc;
1261 /// \brief The location of the const-qualifier, if any.
1263 /// If this is an invalid location, there is no const-qualifier.
1264 unsigned ConstQualifierLoc;
1266 /// \brief The location of the volatile-qualifier, if any.
1268 /// If this is an invalid location, there is no volatile-qualifier.
1269 unsigned VolatileQualifierLoc;
1271 /// \brief The location of the restrict-qualifier, if any.
1273 /// If this is an invalid location, there is no restrict-qualifier.
1274 unsigned RestrictQualifierLoc;
1276 /// \brief The location of the 'mutable' qualifer in a lambda-declarator, if
1278 unsigned MutableLoc;
1280 /// \brief The beginning location of the exception specification, if any.
1281 unsigned ExceptionSpecLocBeg;
1283 /// \brief The end location of the exception specification, if any.
1284 unsigned ExceptionSpecLocEnd;
1286 /// Params - This is a pointer to a new[]'d array of ParamInfo objects that
1287 /// describe the parameters specified by this function declarator. null if
1288 /// there are no parameters specified.
1292 /// \brief Pointer to a new[]'d array of TypeAndRange objects that
1293 /// contain the types in the function's dynamic exception specification
1294 /// and their locations, if there is one.
1295 TypeAndRange *Exceptions;
1297 /// \brief Pointer to the expression in the noexcept-specifier of this
1298 /// function, if it has one.
1301 /// \brief Pointer to the cached tokens for an exception-specification
1302 /// that has not yet been parsed.
1303 CachedTokens *ExceptionSpecTokens;
1305 /// Pointer to a new[]'d array of declarations that need to be available
1306 /// for lookup inside the function body, if one exists. Does not exist in
1308 NamedDecl **DeclsInPrototype;
1311 /// \brief If HasTrailingReturnType is true, this is the trailing return
1313 UnionParsedType TrailingReturnType;
1315 /// \brief Reset the parameter list to having zero parameters.
1317 /// This is used in various places for error recovery.
1319 for (unsigned I = 0; I < NumParams; ++I)
1320 Params[I].DefaultArgTokens.reset();
1323 DeleteParams = false;
1331 switch (getExceptionSpecType()) {
1335 delete[] Exceptions;
1338 delete ExceptionSpecTokens;
1341 if (NumExceptionsOrDecls != 0)
1342 delete[] DeclsInPrototype;
1347 /// isKNRPrototype - Return true if this is a K&R style identifier list,
1348 /// like "void foo(a,b,c)". In a function definition, this will be followed
1349 /// by the parameter type definitions.
1350 bool isKNRPrototype() const { return !hasPrototype && NumParams != 0; }
1352 SourceLocation getLParenLoc() const {
1353 return SourceLocation::getFromRawEncoding(LParenLoc);
1356 SourceLocation getEllipsisLoc() const {
1357 return SourceLocation::getFromRawEncoding(EllipsisLoc);
1360 SourceLocation getRParenLoc() const {
1361 return SourceLocation::getFromRawEncoding(RParenLoc);
1364 SourceLocation getExceptionSpecLocBeg() const {
1365 return SourceLocation::getFromRawEncoding(ExceptionSpecLocBeg);
1368 SourceLocation getExceptionSpecLocEnd() const {
1369 return SourceLocation::getFromRawEncoding(ExceptionSpecLocEnd);
1372 SourceRange getExceptionSpecRange() const {
1373 return SourceRange(getExceptionSpecLocBeg(), getExceptionSpecLocEnd());
1376 /// \brief Retrieve the location of the ref-qualifier, if any.
1377 SourceLocation getRefQualifierLoc() const {
1378 return SourceLocation::getFromRawEncoding(RefQualifierLoc);
1381 /// \brief Retrieve the location of the 'const' qualifier, if any.
1382 SourceLocation getConstQualifierLoc() const {
1383 return SourceLocation::getFromRawEncoding(ConstQualifierLoc);
1386 /// \brief Retrieve the location of the 'volatile' qualifier, if any.
1387 SourceLocation getVolatileQualifierLoc() const {
1388 return SourceLocation::getFromRawEncoding(VolatileQualifierLoc);
1391 /// \brief Retrieve the location of the 'restrict' qualifier, if any.
1392 SourceLocation getRestrictQualifierLoc() const {
1393 return SourceLocation::getFromRawEncoding(RestrictQualifierLoc);
1396 /// \brief Retrieve the location of the 'mutable' qualifier, if any.
1397 SourceLocation getMutableLoc() const {
1398 return SourceLocation::getFromRawEncoding(MutableLoc);
1401 /// \brief Determine whether this function declaration contains a
1403 bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); }
1405 /// \brief Determine whether this lambda-declarator contains a 'mutable'
1407 bool hasMutableQualifier() const { return getMutableLoc().isValid(); }
1409 /// \brief Get the type of exception specification this function has.
1410 ExceptionSpecificationType getExceptionSpecType() const {
1411 return static_cast<ExceptionSpecificationType>(ExceptionSpecType);
1414 /// \brief Get the number of dynamic exception specifications.
1415 unsigned getNumExceptions() const {
1416 assert(ExceptionSpecType != EST_None);
1417 return NumExceptionsOrDecls;
1420 /// \brief Get the non-parameter decls defined within this function
1421 /// prototype. Typically these are tag declarations.
1422 ArrayRef<NamedDecl *> getDeclsInPrototype() const {
1423 assert(ExceptionSpecType == EST_None);
1424 return llvm::makeArrayRef(DeclsInPrototype, NumExceptionsOrDecls);
1427 /// \brief Determine whether this function declarator had a
1428 /// trailing-return-type.
1429 bool hasTrailingReturnType() const { return HasTrailingReturnType; }
1431 /// \brief Get the trailing-return-type for this function declarator.
1432 ParsedType getTrailingReturnType() const { return TrailingReturnType; }
1435 struct BlockPointerTypeInfo : TypeInfoCommon {
1436 /// For now, sema will catch these as invalid.
1437 /// The type qualifiers: const/volatile/restrict/__unaligned/_Atomic.
1438 unsigned TypeQuals : 5;
1444 struct MemberPointerTypeInfo : TypeInfoCommon {
1445 /// The type qualifiers: const/volatile/restrict/__unaligned/_Atomic.
1446 unsigned TypeQuals : 5;
1447 // CXXScopeSpec has a constructor, so it can't be a direct member.
1448 // So we need some pointer-aligned storage and a bit of trickery.
1449 alignas(CXXScopeSpec) char ScopeMem[sizeof(CXXScopeSpec)];
1450 CXXScopeSpec &Scope() {
1451 return *reinterpret_cast<CXXScopeSpec *>(ScopeMem);
1453 const CXXScopeSpec &Scope() const {
1454 return *reinterpret_cast<const CXXScopeSpec *>(ScopeMem);
1457 Scope().~CXXScopeSpec();
1461 struct PipeTypeInfo : TypeInfoCommon {
1462 /// The access writes.
1463 unsigned AccessWrites : 3;
1469 TypeInfoCommon Common;
1470 PointerTypeInfo Ptr;
1471 ReferenceTypeInfo Ref;
1473 FunctionTypeInfo Fun;
1474 BlockPointerTypeInfo Cls;
1475 MemberPointerTypeInfo Mem;
1476 PipeTypeInfo PipeInfo;
1481 case DeclaratorChunk::Function: return Fun.destroy();
1482 case DeclaratorChunk::Pointer: return Ptr.destroy();
1483 case DeclaratorChunk::BlockPointer: return Cls.destroy();
1484 case DeclaratorChunk::Reference: return Ref.destroy();
1485 case DeclaratorChunk::Array: return Arr.destroy();
1486 case DeclaratorChunk::MemberPointer: return Mem.destroy();
1487 case DeclaratorChunk::Paren: return;
1488 case DeclaratorChunk::Pipe: return PipeInfo.destroy();
1492 /// \brief If there are attributes applied to this declaratorchunk, return
1494 const AttributeList *getAttrs() const {
1495 return Common.AttrList;
1498 AttributeList *&getAttrListRef() {
1499 return Common.AttrList;
1502 /// \brief Return a DeclaratorChunk for a pointer.
1503 static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc,
1504 SourceLocation ConstQualLoc,
1505 SourceLocation VolatileQualLoc,
1506 SourceLocation RestrictQualLoc,
1507 SourceLocation AtomicQualLoc,
1508 SourceLocation UnalignedQualLoc) {
1512 I.Ptr.TypeQuals = TypeQuals;
1513 I.Ptr.ConstQualLoc = ConstQualLoc.getRawEncoding();
1514 I.Ptr.VolatileQualLoc = VolatileQualLoc.getRawEncoding();
1515 I.Ptr.RestrictQualLoc = RestrictQualLoc.getRawEncoding();
1516 I.Ptr.AtomicQualLoc = AtomicQualLoc.getRawEncoding();
1517 I.Ptr.UnalignedQualLoc = UnalignedQualLoc.getRawEncoding();
1518 I.Ptr.AttrList = nullptr;
1522 /// \brief Return a DeclaratorChunk for a reference.
1523 static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc,
1528 I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0;
1529 I.Ref.LValueRef = lvalue;
1530 I.Ref.AttrList = nullptr;
1534 /// \brief Return a DeclaratorChunk for an array.
1535 static DeclaratorChunk getArray(unsigned TypeQuals,
1536 bool isStatic, bool isStar, Expr *NumElts,
1537 SourceLocation LBLoc, SourceLocation RBLoc) {
1542 I.Arr.AttrList = nullptr;
1543 I.Arr.TypeQuals = TypeQuals;
1544 I.Arr.hasStatic = isStatic;
1545 I.Arr.isStar = isStar;
1546 I.Arr.NumElts = NumElts;
1550 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
1551 /// "TheDeclarator" is the declarator that this will be added to.
1552 static DeclaratorChunk getFunction(bool HasProto,
1554 SourceLocation LParenLoc,
1555 ParamInfo *Params, unsigned NumParams,
1556 SourceLocation EllipsisLoc,
1557 SourceLocation RParenLoc,
1559 bool RefQualifierIsLvalueRef,
1560 SourceLocation RefQualifierLoc,
1561 SourceLocation ConstQualifierLoc,
1562 SourceLocation VolatileQualifierLoc,
1563 SourceLocation RestrictQualifierLoc,
1564 SourceLocation MutableLoc,
1565 ExceptionSpecificationType ESpecType,
1566 SourceRange ESpecRange,
1567 ParsedType *Exceptions,
1568 SourceRange *ExceptionRanges,
1569 unsigned NumExceptions,
1571 CachedTokens *ExceptionSpecTokens,
1572 ArrayRef<NamedDecl *> DeclsInPrototype,
1573 SourceLocation LocalRangeBegin,
1574 SourceLocation LocalRangeEnd,
1575 Declarator &TheDeclarator,
1576 TypeResult TrailingReturnType =
1579 /// \brief Return a DeclaratorChunk for a block.
1580 static DeclaratorChunk getBlockPointer(unsigned TypeQuals,
1581 SourceLocation Loc) {
1583 I.Kind = BlockPointer;
1585 I.Cls.TypeQuals = TypeQuals;
1586 I.Cls.AttrList = nullptr;
1590 /// \brief Return a DeclaratorChunk for a block.
1591 static DeclaratorChunk getPipe(unsigned TypeQuals,
1592 SourceLocation Loc) {
1596 I.Cls.TypeQuals = TypeQuals;
1597 I.Cls.AttrList = nullptr;
1601 static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS,
1603 SourceLocation Loc) {
1605 I.Kind = MemberPointer;
1606 I.Loc = SS.getBeginLoc();
1608 I.Mem.TypeQuals = TypeQuals;
1609 I.Mem.AttrList = nullptr;
1610 new (I.Mem.ScopeMem) CXXScopeSpec(SS);
1614 /// \brief Return a DeclaratorChunk for a paren.
1615 static DeclaratorChunk getParen(SourceLocation LParenLoc,
1616 SourceLocation RParenLoc) {
1620 I.EndLoc = RParenLoc;
1621 I.Common.AttrList = nullptr;
1625 bool isParen() const {
1626 return Kind == Paren;
1630 /// A parsed C++17 decomposition declarator of the form
1631 /// '[' identifier-list ']'
1632 class DecompositionDeclarator {
1635 IdentifierInfo *Name;
1636 SourceLocation NameLoc;
1640 /// The locations of the '[' and ']' tokens.
1641 SourceLocation LSquareLoc, RSquareLoc;
1645 unsigned NumBindings : 31;
1646 unsigned DeleteBindings : 1;
1648 friend class Declarator;
1651 DecompositionDeclarator()
1652 : Bindings(nullptr), NumBindings(0), DeleteBindings(false) {}
1653 DecompositionDeclarator(const DecompositionDeclarator &G) = delete;
1654 DecompositionDeclarator &operator=(const DecompositionDeclarator &G) = delete;
1655 ~DecompositionDeclarator() {
1661 LSquareLoc = RSquareLoc = SourceLocation();
1666 DeleteBindings = false;
1669 ArrayRef<Binding> bindings() const {
1670 return llvm::makeArrayRef(Bindings, NumBindings);
1673 bool isSet() const { return LSquareLoc.isValid(); }
1675 SourceLocation getLSquareLoc() const { return LSquareLoc; }
1676 SourceLocation getRSquareLoc() const { return RSquareLoc; }
1677 SourceRange getSourceRange() const {
1678 return SourceRange(LSquareLoc, RSquareLoc);
1682 /// \brief Described the kind of function definition (if any) provided for
1684 enum FunctionDefinitionKind {
1691 /// \brief Information about one declarator, including the parsed type
1692 /// information and the identifier.
1694 /// When the declarator is fully formed, this is turned into the appropriate
1697 /// Declarators come in two types: normal declarators and abstract declarators.
1698 /// Abstract declarators are used when parsing types, and don't have an
1699 /// identifier. Normal declarators do have ID's.
1701 /// Instances of this class should be a transient object that lives on the
1702 /// stack, not objects that are allocated in large quantities on the heap.
1706 FileContext, // File scope declaration.
1707 PrototypeContext, // Within a function prototype.
1708 ObjCResultContext, // An ObjC method result type.
1709 ObjCParameterContext,// An ObjC method parameter type.
1710 KNRTypeListContext, // K&R type definition list for formals.
1711 TypeNameContext, // Abstract declarator for types.
1712 MemberContext, // Struct/Union field.
1713 BlockContext, // Declaration within a block in a function.
1714 ForContext, // Declaration within first part of a for loop.
1715 InitStmtContext, // Declaration within optional init stmt of if/switch.
1716 ConditionContext, // Condition declaration in a C++ if/switch/while/for.
1717 TemplateParamContext,// Within a template parameter list.
1718 CXXNewContext, // C++ new-expression.
1719 CXXCatchContext, // C++ catch exception-declaration
1720 ObjCCatchContext, // Objective-C catch exception-declaration
1721 BlockLiteralContext, // Block literal declarator.
1722 LambdaExprContext, // Lambda-expression declarator.
1723 LambdaExprParameterContext, // Lambda-expression parameter declarator.
1724 ConversionIdContext, // C++ conversion-type-id.
1725 TrailingReturnContext, // C++11 trailing-type-specifier.
1726 TemplateTypeArgContext, // Template type argument.
1727 AliasDeclContext, // C++11 alias-declaration.
1728 AliasTemplateContext // C++11 alias-declaration template.
1737 /// \brief Where we are parsing this declarator.
1740 /// The C++17 structured binding, if any. This is an alternative to a Name.
1741 DecompositionDeclarator BindingGroup;
1743 /// DeclTypeInfo - This holds each type that the declarator includes as it is
1744 /// parsed. This is pushed from the identifier out, which means that element
1745 /// #0 will be the most closely bound to the identifier, and
1746 /// DeclTypeInfo.back() will be the least closely bound.
1747 SmallVector<DeclaratorChunk, 8> DeclTypeInfo;
1749 /// InvalidType - Set by Sema::GetTypeForDeclarator().
1750 unsigned InvalidType : 1;
1752 /// GroupingParens - Set by Parser::ParseParenDeclarator().
1753 unsigned GroupingParens : 1;
1755 /// FunctionDefinition - Is this Declarator for a function or member
1756 /// definition and, if so, what kind?
1758 /// Actually a FunctionDefinitionKind.
1759 unsigned FunctionDefinition : 2;
1761 /// \brief Is this Declarator a redeclaration?
1762 unsigned Redeclaration : 1;
1764 /// \brief true if the declaration is preceded by \c __extension__.
1765 unsigned Extension : 1;
1767 /// Indicates whether this is an Objective-C instance variable.
1768 unsigned ObjCIvar : 1;
1770 /// Indicates whether this is an Objective-C 'weak' property.
1771 unsigned ObjCWeakProperty : 1;
1773 /// Indicates whether the InlineParams / InlineBindings storage has been used.
1774 unsigned InlineStorageUsed : 1;
1776 /// Attrs - Attributes.
1777 ParsedAttributes Attrs;
1779 /// \brief The asm label, if specified.
1785 /// InlineParams - This is a local array used for the first function decl
1786 /// chunk to avoid going to the heap for the common case when we have one
1787 /// function chunk in the declarator.
1788 DeclaratorChunk::ParamInfo InlineParams[16];
1789 DecompositionDeclarator::Binding InlineBindings[16];
1794 /// \brief If this is the second or subsequent declarator in this declaration,
1795 /// the location of the comma before this declarator.
1796 SourceLocation CommaLoc;
1798 /// \brief If provided, the source location of the ellipsis used to describe
1799 /// this declarator as a parameter pack.
1800 SourceLocation EllipsisLoc;
1802 friend struct DeclaratorChunk;
1805 Declarator(const DeclSpec &ds, TheContext C)
1806 : DS(ds), Range(ds.getSourceRange()), Context(C),
1807 InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error),
1808 GroupingParens(false), FunctionDefinition(FDK_Declaration),
1809 Redeclaration(false), Extension(false), ObjCIvar(false),
1810 ObjCWeakProperty(false), InlineStorageUsed(false),
1811 Attrs(ds.getAttributePool().getFactory()), AsmLabel(nullptr) {}
1816 /// getDeclSpec - Return the declaration-specifier that this declarator was
1818 const DeclSpec &getDeclSpec() const { return DS; }
1820 /// getMutableDeclSpec - Return a non-const version of the DeclSpec. This
1821 /// should be used with extreme care: declspecs can often be shared between
1822 /// multiple declarators, so mutating the DeclSpec affects all of the
1823 /// Declarators. This should only be done when the declspec is known to not
1824 /// be shared or when in error recovery etc.
1825 DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); }
1827 AttributePool &getAttributePool() const {
1828 return Attrs.getPool();
1831 /// getCXXScopeSpec - Return the C++ scope specifier (global scope or
1832 /// nested-name-specifier) that is part of the declarator-id.
1833 const CXXScopeSpec &getCXXScopeSpec() const { return SS; }
1834 CXXScopeSpec &getCXXScopeSpec() { return SS; }
1836 /// \brief Retrieve the name specified by this declarator.
1837 UnqualifiedId &getName() { return Name; }
1839 const DecompositionDeclarator &getDecompositionDeclarator() const {
1840 return BindingGroup;
1843 TheContext getContext() const { return Context; }
1845 bool isPrototypeContext() const {
1846 return (Context == PrototypeContext ||
1847 Context == ObjCParameterContext ||
1848 Context == ObjCResultContext ||
1849 Context == LambdaExprParameterContext);
1852 /// \brief Get the source range that spans this declarator.
1853 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
1854 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
1855 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
1857 void SetSourceRange(SourceRange R) { Range = R; }
1858 /// SetRangeBegin - Set the start of the source range to Loc, unless it's
1860 void SetRangeBegin(SourceLocation Loc) {
1861 if (!Loc.isInvalid())
1862 Range.setBegin(Loc);
1864 /// SetRangeEnd - Set the end of the source range to Loc, unless it's invalid.
1865 void SetRangeEnd(SourceLocation Loc) {
1866 if (!Loc.isInvalid())
1869 /// ExtendWithDeclSpec - Extend the declarator source range to include the
1870 /// given declspec, unless its location is invalid. Adopts the range start if
1871 /// the current range start is invalid.
1872 void ExtendWithDeclSpec(const DeclSpec &DS) {
1873 SourceRange SR = DS.getSourceRange();
1874 if (Range.getBegin().isInvalid())
1875 Range.setBegin(SR.getBegin());
1876 if (!SR.getEnd().isInvalid())
1877 Range.setEnd(SR.getEnd());
1880 /// \brief Reset the contents of this Declarator.
1884 Range = DS.getSourceRange();
1885 BindingGroup.clear();
1887 for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i)
1888 DeclTypeInfo[i].destroy();
1889 DeclTypeInfo.clear();
1892 InlineStorageUsed = false;
1894 ObjCWeakProperty = false;
1895 CommaLoc = SourceLocation();
1896 EllipsisLoc = SourceLocation();
1899 /// mayOmitIdentifier - Return true if the identifier is either optional or
1900 /// not allowed. This is true for typenames, prototypes, and template
1901 /// parameter lists.
1902 bool mayOmitIdentifier() const {
1905 case KNRTypeListContext:
1909 case InitStmtContext:
1910 case ConditionContext:
1913 case TypeNameContext:
1914 case AliasDeclContext:
1915 case AliasTemplateContext:
1916 case PrototypeContext:
1917 case LambdaExprParameterContext:
1918 case ObjCParameterContext:
1919 case ObjCResultContext:
1920 case TemplateParamContext:
1922 case CXXCatchContext:
1923 case ObjCCatchContext:
1924 case BlockLiteralContext:
1925 case LambdaExprContext:
1926 case ConversionIdContext:
1927 case TemplateTypeArgContext:
1928 case TrailingReturnContext:
1931 llvm_unreachable("unknown context kind!");
1934 /// mayHaveIdentifier - Return true if the identifier is either optional or
1935 /// required. This is true for normal declarators and prototypes, but not
1937 bool mayHaveIdentifier() const {
1940 case KNRTypeListContext:
1944 case InitStmtContext:
1945 case ConditionContext:
1946 case PrototypeContext:
1947 case LambdaExprParameterContext:
1948 case TemplateParamContext:
1949 case CXXCatchContext:
1950 case ObjCCatchContext:
1953 case TypeNameContext:
1955 case AliasDeclContext:
1956 case AliasTemplateContext:
1957 case ObjCParameterContext:
1958 case ObjCResultContext:
1959 case BlockLiteralContext:
1960 case LambdaExprContext:
1961 case ConversionIdContext:
1962 case TemplateTypeArgContext:
1963 case TrailingReturnContext:
1966 llvm_unreachable("unknown context kind!");
1969 /// diagnoseIdentifier - Return true if the identifier is prohibited and
1970 /// should be diagnosed (because it cannot be anything else).
1971 bool diagnoseIdentifier() const {
1974 case KNRTypeListContext:
1978 case InitStmtContext:
1979 case ConditionContext:
1980 case PrototypeContext:
1981 case LambdaExprParameterContext:
1982 case TemplateParamContext:
1983 case CXXCatchContext:
1984 case ObjCCatchContext:
1985 case TypeNameContext:
1986 case ConversionIdContext:
1987 case ObjCParameterContext:
1988 case ObjCResultContext:
1989 case BlockLiteralContext:
1991 case LambdaExprContext:
1994 case AliasDeclContext:
1995 case AliasTemplateContext:
1996 case TemplateTypeArgContext:
1997 case TrailingReturnContext:
2000 llvm_unreachable("unknown context kind!");
2003 /// Return true if the context permits a C++17 decomposition declarator.
2004 bool mayHaveDecompositionDeclarator() const {
2007 // FIXME: It's not clear that the proposal meant to allow file-scope
2008 // structured bindings, but it does.
2011 case InitStmtContext:
2014 case ConditionContext:
2016 case PrototypeContext:
2017 case TemplateParamContext:
2021 // These contexts don't allow any kind of non-abstract declarator.
2022 case KNRTypeListContext:
2023 case TypeNameContext:
2024 case AliasDeclContext:
2025 case AliasTemplateContext:
2026 case LambdaExprParameterContext:
2027 case ObjCParameterContext:
2028 case ObjCResultContext:
2030 case CXXCatchContext:
2031 case ObjCCatchContext:
2032 case BlockLiteralContext:
2033 case LambdaExprContext:
2034 case ConversionIdContext:
2035 case TemplateTypeArgContext:
2036 case TrailingReturnContext:
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 != FileContext)
2054 // Special names can't have direct initializers.
2055 if (Name.getKind() != UnqualifiedId::IK_Identifier)
2062 case InitStmtContext:
2065 case ConditionContext:
2066 // This may not be followed by a direct initializer, but it can't be a
2067 // function declaration either, and we'd prefer to perform a tentative
2068 // parse in order to produce the right diagnostic.
2071 case KNRTypeListContext:
2073 case PrototypeContext:
2074 case LambdaExprParameterContext:
2075 case ObjCParameterContext:
2076 case ObjCResultContext:
2077 case TemplateParamContext:
2078 case CXXCatchContext:
2079 case ObjCCatchContext:
2080 case TypeNameContext:
2082 case AliasDeclContext:
2083 case AliasTemplateContext:
2084 case BlockLiteralContext:
2085 case LambdaExprContext:
2086 case ConversionIdContext:
2087 case TemplateTypeArgContext:
2088 case TrailingReturnContext:
2091 llvm_unreachable("unknown context kind!");
2094 /// isPastIdentifier - Return true if we have parsed beyond the point where
2095 /// the name would appear. (This may happen even if we haven't actually parsed
2096 /// a name, perhaps because this context doesn't require one.)
2097 bool isPastIdentifier() const { return Name.isValid(); }
2099 /// hasName - Whether this declarator has a name, which might be an
2100 /// identifier (accessible via getIdentifier()) or some kind of
2101 /// special C++ name (constructor, destructor, etc.), or a structured
2102 /// binding (which is not exactly a name, but occupies the same position).
2103 bool hasName() const {
2104 return Name.getKind() != UnqualifiedId::IK_Identifier || Name.Identifier ||
2105 isDecompositionDeclarator();
2108 /// Return whether this declarator is a decomposition declarator.
2109 bool isDecompositionDeclarator() const {
2110 return BindingGroup.isSet();
2113 IdentifierInfo *getIdentifier() const {
2114 if (Name.getKind() == UnqualifiedId::IK_Identifier)
2115 return Name.Identifier;
2119 SourceLocation getIdentifierLoc() const { return Name.StartLocation; }
2121 /// \brief Set the name of this declarator to be the given identifier.
2122 void SetIdentifier(IdentifierInfo *Id, SourceLocation IdLoc) {
2123 Name.setIdentifier(Id, IdLoc);
2126 /// Set the decomposition bindings for this declarator.
2128 setDecompositionBindings(SourceLocation LSquareLoc,
2129 ArrayRef<DecompositionDeclarator::Binding> Bindings,
2130 SourceLocation RSquareLoc);
2132 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to
2133 /// EndLoc, which should be the last token of the chunk.
2134 void AddTypeInfo(const DeclaratorChunk &TI,
2135 ParsedAttributes &attrs,
2136 SourceLocation EndLoc) {
2137 DeclTypeInfo.push_back(TI);
2138 DeclTypeInfo.back().getAttrListRef() = attrs.getList();
2139 getAttributePool().takeAllFrom(attrs.getPool());
2141 if (!EndLoc.isInvalid())
2142 SetRangeEnd(EndLoc);
2145 /// \brief Add a new innermost chunk to this declarator.
2146 void AddInnermostTypeInfo(const DeclaratorChunk &TI) {
2147 DeclTypeInfo.insert(DeclTypeInfo.begin(), TI);
2150 /// \brief Return the number of types applied to this declarator.
2151 unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); }
2153 /// Return the specified TypeInfo from this declarator. TypeInfo #0 is
2154 /// closest to the identifier.
2155 const DeclaratorChunk &getTypeObject(unsigned i) const {
2156 assert(i < DeclTypeInfo.size() && "Invalid type chunk");
2157 return DeclTypeInfo[i];
2159 DeclaratorChunk &getTypeObject(unsigned i) {
2160 assert(i < DeclTypeInfo.size() && "Invalid type chunk");
2161 return DeclTypeInfo[i];
2164 typedef SmallVectorImpl<DeclaratorChunk>::const_iterator type_object_iterator;
2165 typedef llvm::iterator_range<type_object_iterator> type_object_range;
2167 /// Returns the range of type objects, from the identifier outwards.
2168 type_object_range type_objects() const {
2169 return type_object_range(DeclTypeInfo.begin(), DeclTypeInfo.end());
2172 void DropFirstTypeObject() {
2173 assert(!DeclTypeInfo.empty() && "No type chunks to drop.");
2174 DeclTypeInfo.front().destroy();
2175 DeclTypeInfo.erase(DeclTypeInfo.begin());
2178 /// Return the innermost (closest to the declarator) chunk of this
2179 /// declarator that is not a parens chunk, or null if there are no
2180 /// non-parens chunks.
2181 const DeclaratorChunk *getInnermostNonParenChunk() const {
2182 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
2183 if (!DeclTypeInfo[i].isParen())
2184 return &DeclTypeInfo[i];
2189 /// Return the outermost (furthest from the declarator) chunk of
2190 /// this declarator that is not a parens chunk, or null if there are
2191 /// no non-parens chunks.
2192 const DeclaratorChunk *getOutermostNonParenChunk() const {
2193 for (unsigned i = DeclTypeInfo.size(), i_end = 0; i != i_end; --i) {
2194 if (!DeclTypeInfo[i-1].isParen())
2195 return &DeclTypeInfo[i-1];
2200 /// isArrayOfUnknownBound - This method returns true if the declarator
2201 /// is a declarator for an array of unknown bound (looking through
2203 bool isArrayOfUnknownBound() const {
2204 const DeclaratorChunk *chunk = getInnermostNonParenChunk();
2205 return (chunk && chunk->Kind == DeclaratorChunk::Array &&
2206 !chunk->Arr.NumElts);
2209 /// isFunctionDeclarator - This method returns true if the declarator
2210 /// is a function declarator (looking through parentheses).
2211 /// If true is returned, then the reference type parameter idx is
2212 /// assigned with the index of the declaration chunk.
2213 bool isFunctionDeclarator(unsigned& idx) const {
2214 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
2215 switch (DeclTypeInfo[i].Kind) {
2216 case DeclaratorChunk::Function:
2219 case DeclaratorChunk::Paren:
2221 case DeclaratorChunk::Pointer:
2222 case DeclaratorChunk::Reference:
2223 case DeclaratorChunk::Array:
2224 case DeclaratorChunk::BlockPointer:
2225 case DeclaratorChunk::MemberPointer:
2226 case DeclaratorChunk::Pipe:
2229 llvm_unreachable("Invalid type chunk");
2234 /// isFunctionDeclarator - Once this declarator is fully parsed and formed,
2235 /// this method returns true if the identifier is a function declarator
2236 /// (looking through parentheses).
2237 bool isFunctionDeclarator() const {
2239 return isFunctionDeclarator(index);
2242 /// getFunctionTypeInfo - Retrieves the function type info object
2243 /// (looking through parentheses).
2244 DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() {
2245 assert(isFunctionDeclarator() && "Not a function declarator!");
2247 isFunctionDeclarator(index);
2248 return DeclTypeInfo[index].Fun;
2251 /// getFunctionTypeInfo - Retrieves the function type info object
2252 /// (looking through parentheses).
2253 const DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() const {
2254 return const_cast<Declarator*>(this)->getFunctionTypeInfo();
2257 /// \brief Determine whether the declaration that will be produced from
2258 /// this declaration will be a function.
2260 /// A declaration can declare a function even if the declarator itself
2261 /// isn't a function declarator, if the type specifier refers to a function
2262 /// type. This routine checks for both cases.
2263 bool isDeclarationOfFunction() const;
2265 /// \brief Return true if this declaration appears in a context where a
2266 /// function declarator would be a function declaration.
2267 bool isFunctionDeclarationContext() const {
2268 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
2276 case InitStmtContext:
2279 case ConditionContext:
2280 case KNRTypeListContext:
2281 case TypeNameContext:
2282 case AliasDeclContext:
2283 case AliasTemplateContext:
2284 case PrototypeContext:
2285 case LambdaExprParameterContext:
2286 case ObjCParameterContext:
2287 case ObjCResultContext:
2288 case TemplateParamContext:
2290 case CXXCatchContext:
2291 case ObjCCatchContext:
2292 case BlockLiteralContext:
2293 case LambdaExprContext:
2294 case ConversionIdContext:
2295 case TemplateTypeArgContext:
2296 case TrailingReturnContext:
2299 llvm_unreachable("unknown context kind!");
2302 /// \brief Return true if a function declarator at this position would be a
2303 /// function declaration.
2304 bool isFunctionDeclaratorAFunctionDeclaration() const {
2305 if (!isFunctionDeclarationContext())
2308 for (unsigned I = 0, N = getNumTypeObjects(); I != N; ++I)
2309 if (getTypeObject(I).Kind != DeclaratorChunk::Paren)
2315 /// takeAttributes - Takes attributes from the given parsed-attributes
2316 /// set and add them to this declarator.
2318 /// These examples both add 3 attributes to "var":
2319 /// short int var __attribute__((aligned(16),common,deprecated));
2320 /// short int x, __attribute__((aligned(16)) var
2321 /// __attribute__((common,deprecated));
2323 /// Also extends the range of the declarator.
2324 void takeAttributes(ParsedAttributes &attrs, SourceLocation lastLoc) {
2325 Attrs.takeAllFrom(attrs);
2327 if (!lastLoc.isInvalid())
2328 SetRangeEnd(lastLoc);
2331 const AttributeList *getAttributes() const { return Attrs.getList(); }
2332 AttributeList *getAttributes() { return Attrs.getList(); }
2334 AttributeList *&getAttrListRef() { return Attrs.getListRef(); }
2336 /// hasAttributes - do we contain any attributes?
2337 bool hasAttributes() const {
2338 if (getAttributes() || getDeclSpec().hasAttributes()) return true;
2339 for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i)
2340 if (getTypeObject(i).getAttrs())
2345 /// \brief Return a source range list of C++11 attributes associated
2346 /// with the declarator.
2347 void getCXX11AttributeRanges(SmallVectorImpl<SourceRange> &Ranges) {
2348 AttributeList *AttrList = Attrs.getList();
2350 if (AttrList->isCXX11Attribute())
2351 Ranges.push_back(AttrList->getRange());
2352 AttrList = AttrList->getNext();
2356 void setAsmLabel(Expr *E) { AsmLabel = E; }
2357 Expr *getAsmLabel() const { return AsmLabel; }
2359 void setExtension(bool Val = true) { Extension = Val; }
2360 bool getExtension() const { return Extension; }
2362 void setObjCIvar(bool Val = true) { ObjCIvar = Val; }
2363 bool isObjCIvar() const { return ObjCIvar; }
2365 void setObjCWeakProperty(bool Val = true) { ObjCWeakProperty = Val; }
2366 bool isObjCWeakProperty() const { return ObjCWeakProperty; }
2368 void setInvalidType(bool Val = true) { InvalidType = Val; }
2369 bool isInvalidType() const {
2370 return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error;
2373 void setGroupingParens(bool flag) { GroupingParens = flag; }
2374 bool hasGroupingParens() const { return GroupingParens; }
2376 bool isFirstDeclarator() const { return !CommaLoc.isValid(); }
2377 SourceLocation getCommaLoc() const { return CommaLoc; }
2378 void setCommaLoc(SourceLocation CL) { CommaLoc = CL; }
2380 bool hasEllipsis() const { return EllipsisLoc.isValid(); }
2381 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
2382 void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; }
2384 void setFunctionDefinitionKind(FunctionDefinitionKind Val) {
2385 FunctionDefinition = Val;
2388 bool isFunctionDefinition() const {
2389 return getFunctionDefinitionKind() != FDK_Declaration;
2392 FunctionDefinitionKind getFunctionDefinitionKind() const {
2393 return (FunctionDefinitionKind)FunctionDefinition;
2396 /// Returns true if this declares a real member and not a friend.
2397 bool isFirstDeclarationOfMember() {
2398 return getContext() == MemberContext && !getDeclSpec().isFriendSpecified();
2401 /// Returns true if this declares a static member. This cannot be called on a
2402 /// declarator outside of a MemberContext because we won't know until
2403 /// redeclaration time if the decl is static.
2404 bool isStaticMember();
2406 /// Returns true if this declares a constructor or a destructor.
2407 bool isCtorOrDtor();
2409 void setRedeclaration(bool Val) { Redeclaration = Val; }
2410 bool isRedeclaration() const { return Redeclaration; }
2413 /// \brief This little struct is used to capture information about
2414 /// structure field declarators, which is basically just a bitfield size.
2415 struct FieldDeclarator {
2418 explicit FieldDeclarator(const DeclSpec &DS)
2419 : D(DS, Declarator::MemberContext), BitfieldSize(nullptr) { }
2422 /// \brief Represents a C++11 virt-specifier-seq.
2423 class VirtSpecifiers {
2430 // Represents the __final keyword, which is legal for gcc in pre-C++11 mode.
2434 VirtSpecifiers() : Specifiers(0), LastSpecifier(VS_None) { }
2436 bool SetSpecifier(Specifier VS, SourceLocation Loc,
2437 const char *&PrevSpec);
2439 bool isUnset() const { return Specifiers == 0; }
2441 bool isOverrideSpecified() const { return Specifiers & VS_Override; }
2442 SourceLocation getOverrideLoc() const { return VS_overrideLoc; }
2444 bool isFinalSpecified() const { return Specifiers & (VS_Final | VS_Sealed | VS_GNU_Final); }
2445 bool isFinalSpelledSealed() const { return Specifiers & VS_Sealed; }
2446 SourceLocation getFinalLoc() const { return VS_finalLoc; }
2448 void clear() { Specifiers = 0; }
2450 static const char *getSpecifierName(Specifier VS);
2452 SourceLocation getFirstLocation() const { return FirstLocation; }
2453 SourceLocation getLastLocation() const { return LastLocation; }
2454 Specifier getLastSpecifier() const { return LastSpecifier; }
2457 unsigned Specifiers;
2458 Specifier LastSpecifier;
2460 SourceLocation VS_overrideLoc, VS_finalLoc;
2461 SourceLocation FirstLocation;
2462 SourceLocation LastLocation;
2465 enum class LambdaCaptureInitKind {
2467 CopyInit, //!< [a = b], [a = {b}]
2468 DirectInit, //!< [a(b)]
2469 ListInit //!< [a{b}]
2472 /// \brief Represents a complete lambda introducer.
2473 struct LambdaIntroducer {
2474 /// \brief An individual capture in a lambda introducer.
2475 struct LambdaCapture {
2476 LambdaCaptureKind Kind;
2479 SourceLocation EllipsisLoc;
2480 LambdaCaptureInitKind InitKind;
2482 ParsedType InitCaptureType;
2483 LambdaCapture(LambdaCaptureKind Kind, SourceLocation Loc,
2484 IdentifierInfo *Id, SourceLocation EllipsisLoc,
2485 LambdaCaptureInitKind InitKind, ExprResult Init,
2486 ParsedType InitCaptureType)
2487 : Kind(Kind), Loc(Loc), Id(Id), EllipsisLoc(EllipsisLoc),
2488 InitKind(InitKind), Init(Init), InitCaptureType(InitCaptureType) {}
2492 SourceLocation DefaultLoc;
2493 LambdaCaptureDefault Default;
2494 SmallVector<LambdaCapture, 4> Captures;
2497 : Default(LCD_None) {}
2499 /// \brief Append a capture in a lambda introducer.
2500 void addCapture(LambdaCaptureKind Kind,
2503 SourceLocation EllipsisLoc,
2504 LambdaCaptureInitKind InitKind,
2506 ParsedType InitCaptureType) {
2507 Captures.push_back(LambdaCapture(Kind, Loc, Id, EllipsisLoc, InitKind, Init,
2512 } // end namespace clang
2514 #endif // LLVM_CLANG_SEMA_DECLSPEC_H