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/Sema/AttributeList.h"
27 #include "clang/Sema/Ownership.h"
28 #include "clang/AST/NestedNameSpecifier.h"
29 #include "clang/Lex/Token.h"
30 #include "clang/Basic/ExceptionSpecificationType.h"
31 #include "clang/Basic/Lambda.h"
32 #include "clang/Basic/OperatorKinds.h"
33 #include "clang/Basic/Specifiers.h"
34 #include "llvm/ADT/SmallVector.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/ErrorHandling.h"
42 class DiagnosticsEngine
;
44 class NamespaceAliasDecl
;
46 class NestedNameSpecifier
;
47 class NestedNameSpecifierLoc
;
52 struct TemplateIdAnnotation
;
54 /// \brief Represents a C++ nested-name-specifier or a global scope specifier.
56 /// These can be in 3 states:
57 /// 1) Not present, identified by isEmpty()
58 /// 2) Present, identified by isNotEmpty()
59 /// 2.a) Valid, idenified by isValid()
60 /// 2.b) Invalid, identified by isInvalid().
62 /// isSet() is deprecated because it mostly corresponded to "valid" but was
63 /// often used as if it meant "present".
65 /// The actual scope is described by getScopeRep().
68 NestedNameSpecifierLocBuilder Builder
;
71 const SourceRange
&getRange() const { return Range
; }
72 void setRange(const SourceRange
&R
) { Range
= R
; }
73 void setBeginLoc(SourceLocation Loc
) { Range
.setBegin(Loc
); }
74 void setEndLoc(SourceLocation Loc
) { Range
.setEnd(Loc
); }
75 SourceLocation
getBeginLoc() const { return Range
.getBegin(); }
76 SourceLocation
getEndLoc() const { return Range
.getEnd(); }
78 /// \brief Retrieve the representation of the nested-name-specifier.
79 NestedNameSpecifier
*getScopeRep() const {
80 return Builder
.getRepresentation();
83 /// \brief Extend the current nested-name-specifier by another
84 /// nested-name-specifier component of the form 'type::'.
86 /// \param Context The AST context in which this nested-name-specifier
89 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
91 /// \param TL The TypeLoc that describes the type preceding the '::'.
93 /// \param ColonColonLoc The location of the trailing '::'.
94 void Extend(ASTContext
&Context
, SourceLocation TemplateKWLoc
, TypeLoc TL
,
95 SourceLocation ColonColonLoc
);
97 /// \brief Extend the current nested-name-specifier by another
98 /// nested-name-specifier component of the form 'identifier::'.
100 /// \param Context The AST context in which this nested-name-specifier
103 /// \param Identifier The identifier.
105 /// \param IdentifierLoc The location of the identifier.
107 /// \param ColonColonLoc The location of the trailing '::'.
108 void Extend(ASTContext
&Context
, IdentifierInfo
*Identifier
,
109 SourceLocation IdentifierLoc
, SourceLocation ColonColonLoc
);
111 /// \brief Extend the current nested-name-specifier by another
112 /// nested-name-specifier component of the form 'namespace::'.
114 /// \param Context The AST context in which this nested-name-specifier
117 /// \param Namespace The namespace.
119 /// \param NamespaceLoc The location of the namespace name.
121 /// \param ColonColonLoc The location of the trailing '::'.
122 void Extend(ASTContext
&Context
, NamespaceDecl
*Namespace
,
123 SourceLocation NamespaceLoc
, SourceLocation ColonColonLoc
);
125 /// \brief Extend the current nested-name-specifier by another
126 /// nested-name-specifier component of the form 'namespace-alias::'.
128 /// \param Context The AST context in which this nested-name-specifier
131 /// \param Alias The namespace alias.
133 /// \param AliasLoc The location of the namespace alias
136 /// \param ColonColonLoc The location of the trailing '::'.
137 void Extend(ASTContext
&Context
, NamespaceAliasDecl
*Alias
,
138 SourceLocation AliasLoc
, SourceLocation ColonColonLoc
);
140 /// \brief Turn this (empty) nested-name-specifier into the global
141 /// nested-name-specifier '::'.
142 void MakeGlobal(ASTContext
&Context
, SourceLocation ColonColonLoc
);
144 /// \brief Make a new nested-name-specifier from incomplete source-location
147 /// FIXME: This routine should be used very, very rarely, in cases where we
148 /// need to synthesize a nested-name-specifier. Most code should instead use
149 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
150 void MakeTrivial(ASTContext
&Context
, NestedNameSpecifier
*Qualifier
,
153 /// \brief Adopt an existing nested-name-specifier (with source-range
155 void Adopt(NestedNameSpecifierLoc Other
);
157 /// \brief Retrieve a nested-name-specifier with location information, copied
158 /// into the given AST context.
160 /// \param Context The context into which this nested-name-specifier will be
162 NestedNameSpecifierLoc
getWithLocInContext(ASTContext
&Context
) const;
164 /// \brief Retrieve the location of the name in the last qualifier
165 /// in this nested name specifier.
167 /// For example, the location of \c bar
173 SourceLocation
getLastQualifierNameLoc() const;
175 /// No scope specifier.
176 bool isEmpty() const { return !Range
.isValid(); }
177 /// A scope specifier is present, but may be valid or invalid.
178 bool isNotEmpty() const { return !isEmpty(); }
180 /// An error occurred during parsing of the scope specifier.
181 bool isInvalid() const { return isNotEmpty() && getScopeRep() == 0; }
182 /// A scope specifier is present, and it refers to a real scope.
183 bool isValid() const { return isNotEmpty() && getScopeRep() != 0; }
185 /// \brief Indicate that this nested-name-specifier is invalid.
186 void SetInvalid(SourceRange R
) {
187 assert(R
.isValid() && "Must have a valid source range");
188 if (Range
.getBegin().isInvalid())
189 Range
.setBegin(R
.getBegin());
190 Range
.setEnd(R
.getEnd());
194 /// Deprecated. Some call sites intend isNotEmpty() while others intend
196 bool isSet() const { return getScopeRep() != 0; }
199 Range
= SourceRange();
203 /// \brief Retrieve the data associated with the source-location information.
204 char *location_data() const { return Builder
.getBuffer().first
; }
206 /// \brief Retrieve the size of the data associated with source-location
208 unsigned location_size() const { return Builder
.getBuffer().second
; }
211 /// \brief Captures information about "declaration specifiers".
213 /// "Declaration specifiers" encompasses storage-class-specifiers,
214 /// type-specifiers, type-qualifiers, and function-specifiers.
217 /// \brief storage-class-specifier
218 /// \note The order of these enumerators is important for diagnostics.
230 // Import type specifier width enumeration and constants.
231 typedef TypeSpecifierWidth TSW
;
232 static const TSW TSW_unspecified
= clang::TSW_unspecified
;
233 static const TSW TSW_short
= clang::TSW_short
;
234 static const TSW TSW_long
= clang::TSW_long
;
235 static const TSW TSW_longlong
= clang::TSW_longlong
;
243 // Import type specifier sign enumeration and constants.
244 typedef TypeSpecifierSign TSS
;
245 static const TSS TSS_unspecified
= clang::TSS_unspecified
;
246 static const TSS TSS_signed
= clang::TSS_signed
;
247 static const TSS TSS_unsigned
= clang::TSS_unsigned
;
249 // Import type specifier type enumeration and constants.
250 typedef TypeSpecifierType TST
;
251 static const TST TST_unspecified
= clang::TST_unspecified
;
252 static const TST TST_void
= clang::TST_void
;
253 static const TST TST_char
= clang::TST_char
;
254 static const TST TST_wchar
= clang::TST_wchar
;
255 static const TST TST_char16
= clang::TST_char16
;
256 static const TST TST_char32
= clang::TST_char32
;
257 static const TST TST_int
= clang::TST_int
;
258 static const TST TST_int128
= clang::TST_int128
;
259 static const TST TST_half
= clang::TST_half
;
260 static const TST TST_float
= clang::TST_float
;
261 static const TST TST_double
= clang::TST_double
;
262 static const TST TST_bool
= clang::TST_bool
;
263 static const TST TST_decimal32
= clang::TST_decimal32
;
264 static const TST TST_decimal64
= clang::TST_decimal64
;
265 static const TST TST_decimal128
= clang::TST_decimal128
;
266 static const TST TST_enum
= clang::TST_enum
;
267 static const TST TST_union
= clang::TST_union
;
268 static const TST TST_struct
= clang::TST_struct
;
269 static const TST TST_interface
= clang::TST_interface
;
270 static const TST TST_class
= clang::TST_class
;
271 static const TST TST_typename
= clang::TST_typename
;
272 static const TST TST_typeofType
= clang::TST_typeofType
;
273 static const TST TST_typeofExpr
= clang::TST_typeofExpr
;
274 static const TST TST_decltype
= clang::TST_decltype
;
275 static const TST TST_underlyingType
= clang::TST_underlyingType
;
276 static const TST TST_auto
= clang::TST_auto
;
277 static const TST TST_unknown_anytype
= clang::TST_unknown_anytype
;
278 static const TST TST_atomic
= clang::TST_atomic
;
279 static const TST TST_error
= clang::TST_error
;
282 enum TQ
{ // NOTE: These flags must be kept in sync with Qualifiers::TQ.
289 /// ParsedSpecifiers - Flags to query which specifiers were applied. This is
290 /// returned by getParsedSpecifiers.
291 enum ParsedSpecifiers
{
293 PQ_StorageClassSpecifier
= 1,
294 PQ_TypeSpecifier
= 2,
295 PQ_TypeQualifier
= 4,
296 PQ_FunctionSpecifier
= 8
300 // storage-class-specifier
301 /*SCS*/unsigned StorageClassSpec
: 3;
302 unsigned SCS_thread_specified
: 1;
303 unsigned SCS_extern_in_linkage_spec
: 1;
306 /*TSW*/unsigned TypeSpecWidth
: 2;
307 /*TSC*/unsigned TypeSpecComplex
: 2;
308 /*TSS*/unsigned TypeSpecSign
: 2;
309 /*TST*/unsigned TypeSpecType
: 5;
310 unsigned TypeAltiVecVector
: 1;
311 unsigned TypeAltiVecPixel
: 1;
312 unsigned TypeAltiVecBool
: 1;
313 unsigned TypeSpecOwned
: 1;
316 unsigned TypeQualifiers
: 3; // Bitwise OR of TQ.
318 // function-specifier
319 unsigned FS_inline_specified
: 1;
320 unsigned FS_virtual_specified
: 1;
321 unsigned FS_explicit_specified
: 1;
324 unsigned Friend_specified
: 1;
326 // constexpr-specifier
327 unsigned Constexpr_specified
: 1;
329 /*SCS*/unsigned StorageClassSpecAsWritten
: 3;
332 UnionParsedType TypeRep
;
338 ParsedAttributes Attrs
;
340 // Scope specifier for the type spec, if applicable.
341 CXXScopeSpec TypeScope
;
343 // List of protocol qualifiers for objective-c classes. Used for
344 // protocol-qualified interfaces "NString<foo>" and protocol-qualified id
346 Decl
* const *ProtocolQualifiers
;
347 unsigned NumProtocolQualifiers
;
348 SourceLocation ProtocolLAngleLoc
;
349 SourceLocation
*ProtocolLocs
;
351 // SourceLocation info. These are null if the item wasn't specified or if
352 // the setting was synthesized.
355 SourceLocation StorageClassSpecLoc
, SCS_threadLoc
;
356 SourceLocation TSWLoc
, TSCLoc
, TSSLoc
, TSTLoc
, AltiVecLoc
;
357 /// TSTNameLoc - If TypeSpecType is any of class, enum, struct, union,
358 /// typename, then this is the location of the named type (if present);
359 /// otherwise, it is the same as TSTLoc. Hence, the pair TSTLoc and
360 /// TSTNameLoc provides source range info for tag types.
361 SourceLocation TSTNameLoc
;
362 SourceRange TypeofParensRange
;
363 SourceLocation TQ_constLoc
, TQ_restrictLoc
, TQ_volatileLoc
;
364 SourceLocation FS_inlineLoc
, FS_virtualLoc
, FS_explicitLoc
;
365 SourceLocation FriendLoc
, ModulePrivateLoc
, ConstexprLoc
;
367 WrittenBuiltinSpecs writtenBS
;
368 void SaveWrittenBuiltinSpecs();
369 void SaveStorageSpecifierAsWritten();
371 ObjCDeclSpec
*ObjCQualifiers
;
373 static bool isTypeRep(TST T
) {
374 return (T
== TST_typename
|| T
== TST_typeofType
||
375 T
== TST_underlyingType
|| T
== TST_atomic
);
377 static bool isExprRep(TST T
) {
378 return (T
== TST_typeofExpr
|| T
== TST_decltype
);
380 static bool isDeclRep(TST T
) {
381 return (T
== TST_enum
|| T
== TST_struct
||
382 T
== TST_interface
|| T
== TST_union
||
386 DeclSpec(const DeclSpec
&) LLVM_DELETED_FUNCTION
;
387 void operator=(const DeclSpec
&) LLVM_DELETED_FUNCTION
;
390 DeclSpec(AttributeFactory
&attrFactory
)
391 : StorageClassSpec(SCS_unspecified
),
392 SCS_thread_specified(false),
393 SCS_extern_in_linkage_spec(false),
394 TypeSpecWidth(TSW_unspecified
),
395 TypeSpecComplex(TSC_unspecified
),
396 TypeSpecSign(TSS_unspecified
),
397 TypeSpecType(TST_unspecified
),
398 TypeAltiVecVector(false),
399 TypeAltiVecPixel(false),
400 TypeAltiVecBool(false),
401 TypeSpecOwned(false),
402 TypeQualifiers(TQ_unspecified
),
403 FS_inline_specified(false),
404 FS_virtual_specified(false),
405 FS_explicit_specified(false),
406 Friend_specified(false),
407 Constexpr_specified(false),
408 StorageClassSpecAsWritten(SCS_unspecified
),
410 ProtocolQualifiers(0),
411 NumProtocolQualifiers(0),
417 delete [] ProtocolQualifiers
;
418 delete [] ProtocolLocs
;
420 // storage-class-specifier
421 SCS
getStorageClassSpec() const { return (SCS
)StorageClassSpec
; }
422 bool isThreadSpecified() const { return SCS_thread_specified
; }
423 bool isExternInLinkageSpec() const { return SCS_extern_in_linkage_spec
; }
424 void setExternInLinkageSpec(bool Value
) {
425 SCS_extern_in_linkage_spec
= Value
;
428 SourceLocation
getStorageClassSpecLoc() const { return StorageClassSpecLoc
; }
429 SourceLocation
getThreadSpecLoc() const { return SCS_threadLoc
; }
431 void ClearStorageClassSpecs() {
432 StorageClassSpec
= DeclSpec::SCS_unspecified
;
433 SCS_thread_specified
= false;
434 SCS_extern_in_linkage_spec
= false;
435 StorageClassSpecLoc
= SourceLocation();
436 SCS_threadLoc
= SourceLocation();
440 TSW
getTypeSpecWidth() const { return (TSW
)TypeSpecWidth
; }
441 TSC
getTypeSpecComplex() const { return (TSC
)TypeSpecComplex
; }
442 TSS
getTypeSpecSign() const { return (TSS
)TypeSpecSign
; }
443 TST
getTypeSpecType() const { return (TST
)TypeSpecType
; }
444 bool isTypeAltiVecVector() const { return TypeAltiVecVector
; }
445 bool isTypeAltiVecPixel() const { return TypeAltiVecPixel
; }
446 bool isTypeAltiVecBool() const { return TypeAltiVecBool
; }
447 bool isTypeSpecOwned() const { return TypeSpecOwned
; }
448 ParsedType
getRepAsType() const {
449 assert(isTypeRep((TST
) TypeSpecType
) && "DeclSpec does not store a type");
452 Decl
*getRepAsDecl() const {
453 assert(isDeclRep((TST
) TypeSpecType
) && "DeclSpec does not store a decl");
456 Expr
*getRepAsExpr() const {
457 assert(isExprRep((TST
) TypeSpecType
) && "DeclSpec does not store an expr");
460 CXXScopeSpec
&getTypeSpecScope() { return TypeScope
; }
461 const CXXScopeSpec
&getTypeSpecScope() const { return TypeScope
; }
463 const SourceRange
&getSourceRange() const LLVM_READONLY
{ return Range
; }
464 SourceLocation
getLocStart() const LLVM_READONLY
{ return Range
.getBegin(); }
465 SourceLocation
getLocEnd() const LLVM_READONLY
{ return Range
.getEnd(); }
467 SourceLocation
getTypeSpecWidthLoc() const { return TSWLoc
; }
468 SourceLocation
getTypeSpecComplexLoc() const { return TSCLoc
; }
469 SourceLocation
getTypeSpecSignLoc() const { return TSSLoc
; }
470 SourceLocation
getTypeSpecTypeLoc() const { return TSTLoc
; }
471 SourceLocation
getAltiVecLoc() const { return AltiVecLoc
; }
473 SourceLocation
getTypeSpecTypeNameLoc() const {
474 assert(isDeclRep((TST
) TypeSpecType
) || TypeSpecType
== TST_typename
);
478 SourceRange
getTypeofParensRange() const { return TypeofParensRange
; }
479 void setTypeofParensRange(SourceRange range
) { TypeofParensRange
= range
; }
481 /// \brief Turn a type-specifier-type into a string like "_Bool" or "union".
482 static const char *getSpecifierName(DeclSpec::TST T
);
483 static const char *getSpecifierName(DeclSpec::TQ Q
);
484 static const char *getSpecifierName(DeclSpec::TSS S
);
485 static const char *getSpecifierName(DeclSpec::TSC C
);
486 static const char *getSpecifierName(DeclSpec::TSW W
);
487 static const char *getSpecifierName(DeclSpec::SCS S
);
491 /// getTypeQualifiers - Return a set of TQs.
492 unsigned getTypeQualifiers() const { return TypeQualifiers
; }
493 SourceLocation
getConstSpecLoc() const { return TQ_constLoc
; }
494 SourceLocation
getRestrictSpecLoc() const { return TQ_restrictLoc
; }
495 SourceLocation
getVolatileSpecLoc() const { return TQ_volatileLoc
; }
497 /// \brief Clear out all of the type qualifiers.
498 void ClearTypeQualifiers() {
500 TQ_constLoc
= SourceLocation();
501 TQ_restrictLoc
= SourceLocation();
502 TQ_volatileLoc
= SourceLocation();
505 // function-specifier
506 bool isInlineSpecified() const { return FS_inline_specified
; }
507 SourceLocation
getInlineSpecLoc() const { return FS_inlineLoc
; }
509 bool isVirtualSpecified() const { return FS_virtual_specified
; }
510 SourceLocation
getVirtualSpecLoc() const { return FS_virtualLoc
; }
512 bool isExplicitSpecified() const { return FS_explicit_specified
; }
513 SourceLocation
getExplicitSpecLoc() const { return FS_explicitLoc
; }
515 void ClearFunctionSpecs() {
516 FS_inline_specified
= false;
517 FS_inlineLoc
= SourceLocation();
518 FS_virtual_specified
= false;
519 FS_virtualLoc
= SourceLocation();
520 FS_explicit_specified
= false;
521 FS_explicitLoc
= SourceLocation();
524 /// \brief Return true if any type-specifier has been found.
525 bool hasTypeSpecifier() const {
526 return getTypeSpecType() != DeclSpec::TST_unspecified
||
527 getTypeSpecWidth() != DeclSpec::TSW_unspecified
||
528 getTypeSpecComplex() != DeclSpec::TSC_unspecified
||
529 getTypeSpecSign() != DeclSpec::TSS_unspecified
;
532 /// \brief Return a bitmask of which flavors of specifiers this
533 /// DeclSpec includes.
534 unsigned getParsedSpecifiers() const;
536 SCS
getStorageClassSpecAsWritten() const {
537 return (SCS
)StorageClassSpecAsWritten
;
540 /// isEmpty - Return true if this declaration specifier is completely empty:
541 /// no tokens were parsed in the production of it.
542 bool isEmpty() const {
543 return getParsedSpecifiers() == DeclSpec::PQ_None
;
546 void SetRangeStart(SourceLocation Loc
) { Range
.setBegin(Loc
); }
547 void SetRangeEnd(SourceLocation Loc
) { Range
.setEnd(Loc
); }
549 /// These methods set the specified attribute of the DeclSpec and
550 /// return false if there was no error. If an error occurs (for
551 /// example, if we tried to set "auto" on a spec with "extern"
552 /// already set), they return true and set PrevSpec and DiagID
554 /// Diag(Loc, DiagID) << PrevSpec;
555 /// will yield a useful result.
557 /// TODO: use a more general approach that still allows these
558 /// diagnostics to be ignored when desired.
559 bool SetStorageClassSpec(Sema
&S
, SCS SC
, SourceLocation Loc
,
560 const char *&PrevSpec
, unsigned &DiagID
);
561 bool SetStorageClassSpecThread(SourceLocation Loc
, const char *&PrevSpec
,
563 bool SetTypeSpecWidth(TSW W
, SourceLocation Loc
, const char *&PrevSpec
,
565 bool SetTypeSpecComplex(TSC C
, SourceLocation Loc
, const char *&PrevSpec
,
567 bool SetTypeSpecSign(TSS S
, SourceLocation Loc
, const char *&PrevSpec
,
569 bool SetTypeSpecType(TST T
, SourceLocation Loc
, const char *&PrevSpec
,
571 bool SetTypeSpecType(TST T
, SourceLocation Loc
, const char *&PrevSpec
,
572 unsigned &DiagID
, ParsedType Rep
);
573 bool SetTypeSpecType(TST T
, SourceLocation Loc
, const char *&PrevSpec
,
574 unsigned &DiagID
, Decl
*Rep
, bool Owned
);
575 bool SetTypeSpecType(TST T
, SourceLocation TagKwLoc
,
576 SourceLocation TagNameLoc
, const char *&PrevSpec
,
577 unsigned &DiagID
, ParsedType Rep
);
578 bool SetTypeSpecType(TST T
, SourceLocation TagKwLoc
,
579 SourceLocation TagNameLoc
, const char *&PrevSpec
,
580 unsigned &DiagID
, Decl
*Rep
, bool Owned
);
582 bool SetTypeSpecType(TST T
, SourceLocation Loc
, const char *&PrevSpec
,
583 unsigned &DiagID
, Expr
*Rep
);
584 bool SetTypeAltiVecVector(bool isAltiVecVector
, SourceLocation Loc
,
585 const char *&PrevSpec
, unsigned &DiagID
);
586 bool SetTypeAltiVecPixel(bool isAltiVecPixel
, SourceLocation Loc
,
587 const char *&PrevSpec
, unsigned &DiagID
);
588 bool SetTypeSpecError();
589 void UpdateDeclRep(Decl
*Rep
) {
590 assert(isDeclRep((TST
) TypeSpecType
));
593 void UpdateTypeRep(ParsedType Rep
) {
594 assert(isTypeRep((TST
) TypeSpecType
));
597 void UpdateExprRep(Expr
*Rep
) {
598 assert(isExprRep((TST
) TypeSpecType
));
602 bool SetTypeQual(TQ T
, SourceLocation Loc
, const char *&PrevSpec
,
603 unsigned &DiagID
, const LangOptions
&Lang
,
606 bool SetFunctionSpecInline(SourceLocation Loc
, const char *&PrevSpec
,
608 bool SetFunctionSpecVirtual(SourceLocation Loc
, const char *&PrevSpec
,
610 bool SetFunctionSpecExplicit(SourceLocation Loc
, const char *&PrevSpec
,
613 bool SetFriendSpec(SourceLocation Loc
, const char *&PrevSpec
,
615 bool setModulePrivateSpec(SourceLocation Loc
, const char *&PrevSpec
,
617 bool SetConstexprSpec(SourceLocation Loc
, const char *&PrevSpec
,
620 bool isFriendSpecified() const { return Friend_specified
; }
621 SourceLocation
getFriendSpecLoc() const { return FriendLoc
; }
623 bool isModulePrivateSpecified() const { return ModulePrivateLoc
.isValid(); }
624 SourceLocation
getModulePrivateSpecLoc() const { return ModulePrivateLoc
; }
626 bool isConstexprSpecified() const { return Constexpr_specified
; }
627 SourceLocation
getConstexprSpecLoc() const { return ConstexprLoc
; }
629 void ClearConstexprSpec() {
630 Constexpr_specified
= false;
631 ConstexprLoc
= SourceLocation();
634 AttributePool
&getAttributePool() const {
635 return Attrs
.getPool();
638 /// \brief Concatenates two attribute lists.
640 /// The GCC attribute syntax allows for the following:
643 /// short __attribute__(( unused, deprecated ))
644 /// int __attribute__(( may_alias, aligned(16) )) var;
647 /// This declares 4 attributes using 2 lists. The following syntax is
648 /// also allowed and equivalent to the previous declaration.
651 /// short __attribute__((unused)) __attribute__((deprecated))
652 /// int __attribute__((may_alias)) __attribute__((aligned(16))) var;
655 void addAttributes(AttributeList
*AL
) {
658 void setAttributes(AttributeList
*AL
) {
662 bool hasAttributes() const { return !Attrs
.empty(); }
664 ParsedAttributes
&getAttributes() { return Attrs
; }
665 const ParsedAttributes
&getAttributes() const { return Attrs
; }
667 /// \brief Return the current attribute list and remove them from
668 /// the DeclSpec so that it doesn't own them.
669 ParsedAttributes
takeAttributes() {
670 // The non-const "copy" constructor clears the operand automatically.
674 void takeAttributesFrom(ParsedAttributes
&attrs
) {
675 Attrs
.takeAllFrom(attrs
);
678 typedef Decl
* const *ProtocolQualifierListTy
;
679 ProtocolQualifierListTy
getProtocolQualifiers() const {
680 return ProtocolQualifiers
;
682 SourceLocation
*getProtocolLocs() const { return ProtocolLocs
; }
683 unsigned getNumProtocolQualifiers() const {
684 return NumProtocolQualifiers
;
686 SourceLocation
getProtocolLAngleLoc() const { return ProtocolLAngleLoc
; }
687 void setProtocolQualifiers(Decl
* const *Protos
, unsigned NP
,
688 SourceLocation
*ProtoLocs
,
689 SourceLocation LAngleLoc
);
691 /// Finish - This does final analysis of the declspec, issuing diagnostics for
692 /// things like "_Imaginary" (lacking an FP type). After calling this method,
693 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
694 void Finish(DiagnosticsEngine
&D
, Preprocessor
&PP
);
696 const WrittenBuiltinSpecs
& getWrittenBuiltinSpecs() const {
700 ObjCDeclSpec
*getObjCQualifiers() const { return ObjCQualifiers
; }
701 void setObjCQualifiers(ObjCDeclSpec
*quals
) { ObjCQualifiers
= quals
; }
703 /// \brief Checks if this DeclSpec can stand alone, without a Declarator.
705 /// Only tag declspecs can stand alone.
706 bool isMissingDeclaratorOk();
709 /// \brief Captures information about "declaration specifiers" specific to
713 /// ObjCDeclQualifier - Qualifier used on types in method
714 /// declarations. Not all combinations are sensible. Parameters
715 /// can be one of { in, out, inout } with one of { bycopy, byref }.
716 /// Returns can either be { oneway } or not.
718 /// This should be kept in sync with Decl::ObjCDeclQualifier.
719 enum ObjCDeclQualifier
{
729 /// PropertyAttributeKind - list of property attributes.
730 enum ObjCPropertyAttributeKind
{
732 DQ_PR_readonly
= 0x01,
735 DQ_PR_readwrite
= 0x08,
738 DQ_PR_nonatomic
= 0x40,
740 DQ_PR_atomic
= 0x100,
742 DQ_PR_strong
= 0x400,
743 DQ_PR_unsafe_unretained
= 0x800
748 : objcDeclQualifier(DQ_None
), PropertyAttributes(DQ_PR_noattr
),
749 GetterName(0), SetterName(0) { }
750 ObjCDeclQualifier
getObjCDeclQualifier() const { return objcDeclQualifier
; }
751 void setObjCDeclQualifier(ObjCDeclQualifier DQVal
) {
752 objcDeclQualifier
= (ObjCDeclQualifier
) (objcDeclQualifier
| DQVal
);
755 ObjCPropertyAttributeKind
getPropertyAttributes() const {
756 return ObjCPropertyAttributeKind(PropertyAttributes
);
758 void setPropertyAttributes(ObjCPropertyAttributeKind PRVal
) {
760 (ObjCPropertyAttributeKind
)(PropertyAttributes
| PRVal
);
763 const IdentifierInfo
*getGetterName() const { return GetterName
; }
764 IdentifierInfo
*getGetterName() { return GetterName
; }
765 void setGetterName(IdentifierInfo
*name
) { GetterName
= name
; }
767 const IdentifierInfo
*getSetterName() const { return SetterName
; }
768 IdentifierInfo
*getSetterName() { return SetterName
; }
769 void setSetterName(IdentifierInfo
*name
) { SetterName
= name
; }
772 // FIXME: These two are unrelated and mutially exclusive. So perhaps
773 // we can put them in a union to reflect their mutual exclusiveness
774 // (space saving is negligible).
775 ObjCDeclQualifier objcDeclQualifier
: 6;
777 // NOTE: VC++ treats enums as signed, avoid using ObjCPropertyAttributeKind
778 unsigned PropertyAttributes
: 12;
779 IdentifierInfo
*GetterName
; // getter name of NULL if no getter
780 IdentifierInfo
*SetterName
; // setter name of NULL if no setter
783 /// \brief Represents a C++ unqualified-id that has been parsed.
784 class UnqualifiedId
{
786 UnqualifiedId(const UnqualifiedId
&Other
) LLVM_DELETED_FUNCTION
;
787 const UnqualifiedId
&operator=(const UnqualifiedId
&) LLVM_DELETED_FUNCTION
;
790 /// \brief Describes the kind of unqualified-id parsed.
792 /// \brief An identifier.
794 /// \brief An overloaded operator name, e.g., operator+.
795 IK_OperatorFunctionId
,
796 /// \brief A conversion function name, e.g., operator int.
797 IK_ConversionFunctionId
,
798 /// \brief A user-defined literal name, e.g., operator "" _i.
799 IK_LiteralOperatorId
,
800 /// \brief A constructor name.
802 /// \brief A constructor named via a template-id.
803 IK_ConstructorTemplateId
,
804 /// \brief A destructor name.
806 /// \brief A template-id, e.g., f<int>.
808 /// \brief An implicit 'self' parameter
812 /// \brief Anonymous union that holds extra data associated with the
813 /// parsed unqualified-id.
815 /// \brief When Kind == IK_Identifier, the parsed identifier, or when Kind
816 /// == IK_UserLiteralId, the identifier suffix.
817 IdentifierInfo
*Identifier
;
819 /// \brief When Kind == IK_OperatorFunctionId, the overloaded operator
822 /// \brief The kind of overloaded operator.
823 OverloadedOperatorKind Operator
;
825 /// \brief The source locations of the individual tokens that name
826 /// the operator, e.g., the "new", "[", and "]" tokens in
829 /// Different operators have different numbers of tokens in their name,
830 /// up to three. Any remaining source locations in this array will be
831 /// set to an invalid value for operators with fewer than three tokens.
832 unsigned SymbolLocations
[3];
833 } OperatorFunctionId
;
835 /// \brief When Kind == IK_ConversionFunctionId, the type that the
836 /// conversion function names.
837 UnionParsedType ConversionFunctionId
;
839 /// \brief When Kind == IK_ConstructorName, the class-name of the type
840 /// whose constructor is being referenced.
841 UnionParsedType ConstructorName
;
843 /// \brief When Kind == IK_DestructorName, the type referred to by the
845 UnionParsedType DestructorName
;
847 /// \brief When Kind == IK_TemplateId or IK_ConstructorTemplateId,
848 /// the template-id annotation that contains the template name and
849 /// template arguments.
850 TemplateIdAnnotation
*TemplateId
;
853 /// \brief The location of the first token that describes this unqualified-id,
854 /// which will be the location of the identifier, "operator" keyword,
855 /// tilde (for a destructor), or the template name of a template-id.
856 SourceLocation StartLocation
;
858 /// \brief The location of the last token that describes this unqualified-id.
859 SourceLocation EndLocation
;
861 UnqualifiedId() : Kind(IK_Identifier
), Identifier(0) { }
863 /// \brief Clear out this unqualified-id, setting it to default (invalid)
866 Kind
= IK_Identifier
;
868 StartLocation
= SourceLocation();
869 EndLocation
= SourceLocation();
872 /// \brief Determine whether this unqualified-id refers to a valid name.
873 bool isValid() const { return StartLocation
.isValid(); }
875 /// \brief Determine whether this unqualified-id refers to an invalid name.
876 bool isInvalid() const { return !isValid(); }
878 /// \brief Determine what kind of name we have.
879 IdKind
getKind() const { return Kind
; }
880 void setKind(IdKind kind
) { Kind
= kind
; }
882 /// \brief Specify that this unqualified-id was parsed as an identifier.
884 /// \param Id the parsed identifier.
885 /// \param IdLoc the location of the parsed identifier.
886 void setIdentifier(const IdentifierInfo
*Id
, SourceLocation IdLoc
) {
887 Kind
= IK_Identifier
;
888 Identifier
= const_cast<IdentifierInfo
*>(Id
);
889 StartLocation
= EndLocation
= IdLoc
;
892 /// \brief Specify that this unqualified-id was parsed as an
893 /// operator-function-id.
895 /// \param OperatorLoc the location of the 'operator' keyword.
897 /// \param Op the overloaded operator.
899 /// \param SymbolLocations the locations of the individual operator symbols
901 void setOperatorFunctionId(SourceLocation OperatorLoc
,
902 OverloadedOperatorKind Op
,
903 SourceLocation SymbolLocations
[3]);
905 /// \brief Specify that this unqualified-id was parsed as a
906 /// conversion-function-id.
908 /// \param OperatorLoc the location of the 'operator' keyword.
910 /// \param Ty the type to which this conversion function is converting.
912 /// \param EndLoc the location of the last token that makes up the type name.
913 void setConversionFunctionId(SourceLocation OperatorLoc
,
915 SourceLocation EndLoc
) {
916 Kind
= IK_ConversionFunctionId
;
917 StartLocation
= OperatorLoc
;
918 EndLocation
= EndLoc
;
919 ConversionFunctionId
= Ty
;
922 /// \brief Specific that this unqualified-id was parsed as a
923 /// literal-operator-id.
925 /// \param Id the parsed identifier.
927 /// \param OpLoc the location of the 'operator' keyword.
929 /// \param IdLoc the location of the identifier.
930 void setLiteralOperatorId(const IdentifierInfo
*Id
, SourceLocation OpLoc
,
931 SourceLocation IdLoc
) {
932 Kind
= IK_LiteralOperatorId
;
933 Identifier
= const_cast<IdentifierInfo
*>(Id
);
934 StartLocation
= OpLoc
;
938 /// \brief Specify that this unqualified-id was parsed as a constructor name.
940 /// \param ClassType the class type referred to by the constructor name.
942 /// \param ClassNameLoc the location of the class name.
944 /// \param EndLoc the location of the last token that makes up the type name.
945 void setConstructorName(ParsedType ClassType
,
946 SourceLocation ClassNameLoc
,
947 SourceLocation EndLoc
) {
948 Kind
= IK_ConstructorName
;
949 StartLocation
= ClassNameLoc
;
950 EndLocation
= EndLoc
;
951 ConstructorName
= ClassType
;
954 /// \brief Specify that this unqualified-id was parsed as a
955 /// template-id that names a constructor.
957 /// \param TemplateId the template-id annotation that describes the parsed
958 /// template-id. This UnqualifiedId instance will take ownership of the
959 /// \p TemplateId and will free it on destruction.
960 void setConstructorTemplateId(TemplateIdAnnotation
*TemplateId
);
962 /// \brief Specify that this unqualified-id was parsed as a destructor name.
964 /// \param TildeLoc the location of the '~' that introduces the destructor
967 /// \param ClassType the name of the class referred to by the destructor name.
968 void setDestructorName(SourceLocation TildeLoc
,
969 ParsedType ClassType
,
970 SourceLocation EndLoc
) {
971 Kind
= IK_DestructorName
;
972 StartLocation
= TildeLoc
;
973 EndLocation
= EndLoc
;
974 DestructorName
= ClassType
;
977 /// \brief Specify that this unqualified-id was parsed as a template-id.
979 /// \param TemplateId the template-id annotation that describes the parsed
980 /// template-id. This UnqualifiedId instance will take ownership of the
981 /// \p TemplateId and will free it on destruction.
982 void setTemplateId(TemplateIdAnnotation
*TemplateId
);
984 /// \brief Return the source range that covers this unqualified-id.
985 SourceRange
getSourceRange() const LLVM_READONLY
{
986 return SourceRange(StartLocation
, EndLocation
);
988 SourceLocation
getLocStart() const LLVM_READONLY
{ return StartLocation
; }
989 SourceLocation
getLocEnd() const LLVM_READONLY
{ return EndLocation
; }
992 /// \brief A set of tokens that has been cached for later parsing.
993 typedef SmallVector
<Token
, 4> CachedTokens
;
995 /// \brief One instance of this struct is used for each type in a
996 /// declarator that is parsed.
998 /// This is intended to be a small value object.
999 struct DeclaratorChunk
{
1001 Pointer
, Reference
, Array
, Function
, BlockPointer
, MemberPointer
, Paren
1004 /// Loc - The place where this type was defined.
1006 /// EndLoc - If valid, the place where this chunck ends.
1007 SourceLocation EndLoc
;
1009 struct TypeInfoCommon
{
1010 AttributeList
*AttrList
;
1013 struct PointerTypeInfo
: TypeInfoCommon
{
1014 /// The type qualifiers: const/volatile/restrict.
1015 unsigned TypeQuals
: 3;
1017 /// The location of the const-qualifier, if any.
1018 unsigned ConstQualLoc
;
1020 /// The location of the volatile-qualifier, if any.
1021 unsigned VolatileQualLoc
;
1023 /// The location of the restrict-qualifier, if any.
1024 unsigned RestrictQualLoc
;
1030 struct ReferenceTypeInfo
: TypeInfoCommon
{
1031 /// The type qualifier: restrict. [GNU] C++ extension
1032 bool HasRestrict
: 1;
1033 /// True if this is an lvalue reference, false if it's an rvalue reference.
1039 struct ArrayTypeInfo
: TypeInfoCommon
{
1040 /// The type qualifiers for the array: const/volatile/restrict.
1041 unsigned TypeQuals
: 3;
1043 /// True if this dimension included the 'static' keyword.
1046 /// True if this dimension was [*]. In this case, NumElts is null.
1049 /// This is the size of the array, or null if [] or [*] was specified.
1050 /// Since the parser is multi-purpose, and we don't want to impose a root
1051 /// expression class on all clients, NumElts is untyped.
1057 /// ParamInfo - An array of paraminfo objects is allocated whenever a function
1058 /// declarator is parsed. There are two interesting styles of arguments here:
1059 /// K&R-style identifier lists and parameter type lists. K&R-style identifier
1060 /// lists will have information about the identifier, but no type information.
1061 /// Parameter type lists will have type info (if the actions module provides
1062 /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'.
1064 IdentifierInfo
*Ident
;
1065 SourceLocation IdentLoc
;
1068 /// DefaultArgTokens - When the parameter's default argument
1069 /// cannot be parsed immediately (because it occurs within the
1070 /// declaration of a member function), it will be stored here as a
1071 /// sequence of tokens to be parsed once the class definition is
1072 /// complete. Non-NULL indicates that there is a default argument.
1073 CachedTokens
*DefaultArgTokens
;
1076 ParamInfo(IdentifierInfo
*ident
, SourceLocation iloc
,
1078 CachedTokens
*DefArgTokens
= 0)
1079 : Ident(ident
), IdentLoc(iloc
), Param(param
),
1080 DefaultArgTokens(DefArgTokens
) {}
1083 struct TypeAndRange
{
1088 struct FunctionTypeInfo
: TypeInfoCommon
{
1089 /// hasPrototype - This is true if the function had at least one typed
1090 /// argument. If the function is () or (a,b,c), then it has no prototype,
1091 /// and is treated as a K&R-style function.
1092 unsigned hasPrototype
: 1;
1094 /// isVariadic - If this function has a prototype, and if that
1095 /// proto ends with ',...)', this is true. When true, EllipsisLoc
1096 /// contains the location of the ellipsis.
1097 unsigned isVariadic
: 1;
1099 /// Can this declaration be a constructor-style initializer?
1100 unsigned isAmbiguous
: 1;
1102 /// \brief Whether the ref-qualifier (if any) is an lvalue reference.
1103 /// Otherwise, it's an rvalue reference.
1104 unsigned RefQualifierIsLValueRef
: 1;
1106 /// The type qualifiers: const/volatile/restrict.
1107 /// The qualifier bitmask values are the same as in QualType.
1108 unsigned TypeQuals
: 3;
1110 /// ExceptionSpecType - An ExceptionSpecificationType value.
1111 unsigned ExceptionSpecType
: 3;
1113 /// DeleteArgInfo - If this is true, we need to delete[] ArgInfo.
1114 unsigned DeleteArgInfo
: 1;
1116 /// HasTrailingReturnType - If this is true, a trailing return type was
1118 unsigned HasTrailingReturnType
: 1;
1120 /// When isVariadic is true, the location of the ellipsis in the source.
1121 unsigned EllipsisLoc
;
1123 /// NumArgs - This is the number of formal arguments provided for the
1127 /// NumExceptions - This is the number of types in the dynamic-exception-
1128 /// decl, if the function has one.
1129 unsigned NumExceptions
;
1131 /// \brief The location of the ref-qualifier, if any.
1133 /// If this is an invalid location, there is no ref-qualifier.
1134 unsigned RefQualifierLoc
;
1136 /// \brief The location of the const-qualifier, if any.
1138 /// If this is an invalid location, there is no const-qualifier.
1139 unsigned ConstQualifierLoc
;
1141 /// \brief The location of the volatile-qualifier, if any.
1143 /// If this is an invalid location, there is no volatile-qualifier.
1144 unsigned VolatileQualifierLoc
;
1146 /// \brief The location of the 'mutable' qualifer in a lambda-declarator, if
1148 unsigned MutableLoc
;
1150 /// \brief The location of the keyword introducing the spec, if any.
1151 unsigned ExceptionSpecLoc
;
1153 /// ArgInfo - This is a pointer to a new[]'d array of ParamInfo objects that
1154 /// describe the arguments for this function declarator. This is null if
1155 /// there are no arguments specified.
1159 /// \brief Pointer to a new[]'d array of TypeAndRange objects that
1160 /// contain the types in the function's dynamic exception specification
1161 /// and their locations, if there is one.
1162 TypeAndRange
*Exceptions
;
1164 /// \brief Pointer to the expression in the noexcept-specifier of this
1165 /// function, if it has one.
1169 /// \brief If HasTrailingReturnType is true, this is the trailing return
1171 UnionParsedType TrailingReturnType
;
1173 /// \brief Reset the argument list to having zero arguments.
1175 /// This is used in various places for error recovery.
1177 if (DeleteArgInfo
) {
1179 DeleteArgInfo
= false;
1187 if (getExceptionSpecType() == EST_Dynamic
)
1188 delete[] Exceptions
;
1191 /// isKNRPrototype - Return true if this is a K&R style identifier list,
1192 /// like "void foo(a,b,c)". In a function definition, this will be followed
1193 /// by the argument type definitions.
1194 bool isKNRPrototype() const {
1195 return !hasPrototype
&& NumArgs
!= 0;
1198 SourceLocation
getEllipsisLoc() const {
1199 return SourceLocation::getFromRawEncoding(EllipsisLoc
);
1201 SourceLocation
getExceptionSpecLoc() const {
1202 return SourceLocation::getFromRawEncoding(ExceptionSpecLoc
);
1205 /// \brief Retrieve the location of the ref-qualifier, if any.
1206 SourceLocation
getRefQualifierLoc() const {
1207 return SourceLocation::getFromRawEncoding(RefQualifierLoc
);
1210 /// \brief Retrieve the location of the ref-qualifier, if any.
1211 SourceLocation
getConstQualifierLoc() const {
1212 return SourceLocation::getFromRawEncoding(ConstQualifierLoc
);
1215 /// \brief Retrieve the location of the ref-qualifier, if any.
1216 SourceLocation
getVolatileQualifierLoc() const {
1217 return SourceLocation::getFromRawEncoding(VolatileQualifierLoc
);
1220 /// \brief Retrieve the location of the 'mutable' qualifier, if any.
1221 SourceLocation
getMutableLoc() const {
1222 return SourceLocation::getFromRawEncoding(MutableLoc
);
1225 /// \brief Determine whether this function declaration contains a
1227 bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); }
1229 /// \brief Determine whether this lambda-declarator contains a 'mutable'
1231 bool hasMutableQualifier() const { return getMutableLoc().isValid(); }
1233 /// \brief Get the type of exception specification this function has.
1234 ExceptionSpecificationType
getExceptionSpecType() const {
1235 return static_cast<ExceptionSpecificationType
>(ExceptionSpecType
);
1238 /// \brief Determine whether this function declarator had a
1239 /// trailing-return-type.
1240 bool hasTrailingReturnType() const { return HasTrailingReturnType
; }
1242 /// \brief Get the trailing-return-type for this function declarator.
1243 ParsedType
getTrailingReturnType() const { return TrailingReturnType
; }
1246 struct BlockPointerTypeInfo
: TypeInfoCommon
{
1247 /// For now, sema will catch these as invalid.
1248 /// The type qualifiers: const/volatile/restrict.
1249 unsigned TypeQuals
: 3;
1255 struct MemberPointerTypeInfo
: TypeInfoCommon
{
1256 /// The type qualifiers: const/volatile/restrict.
1257 unsigned TypeQuals
: 3;
1258 // CXXScopeSpec has a constructor, so it can't be a direct member.
1259 // So we need some pointer-aligned storage and a bit of trickery.
1262 char Mem
[sizeof(CXXScopeSpec
)];
1264 CXXScopeSpec
&Scope() {
1265 return *reinterpret_cast<CXXScopeSpec
*>(ScopeMem
.Mem
);
1267 const CXXScopeSpec
&Scope() const {
1268 return *reinterpret_cast<const CXXScopeSpec
*>(ScopeMem
.Mem
);
1271 Scope().~CXXScopeSpec();
1276 TypeInfoCommon Common
;
1277 PointerTypeInfo Ptr
;
1278 ReferenceTypeInfo Ref
;
1280 FunctionTypeInfo Fun
;
1281 BlockPointerTypeInfo Cls
;
1282 MemberPointerTypeInfo Mem
;
1287 case DeclaratorChunk::Function
: return Fun
.destroy();
1288 case DeclaratorChunk::Pointer
: return Ptr
.destroy();
1289 case DeclaratorChunk::BlockPointer
: return Cls
.destroy();
1290 case DeclaratorChunk::Reference
: return Ref
.destroy();
1291 case DeclaratorChunk::Array
: return Arr
.destroy();
1292 case DeclaratorChunk::MemberPointer
: return Mem
.destroy();
1293 case DeclaratorChunk::Paren
: return;
1297 /// \brief If there are attributes applied to this declaratorchunk, return
1299 const AttributeList
*getAttrs() const {
1300 return Common
.AttrList
;
1303 AttributeList
*&getAttrListRef() {
1304 return Common
.AttrList
;
1307 /// \brief Return a DeclaratorChunk for a pointer.
1308 static DeclaratorChunk
getPointer(unsigned TypeQuals
, SourceLocation Loc
,
1309 SourceLocation ConstQualLoc
,
1310 SourceLocation VolatileQualLoc
,
1311 SourceLocation RestrictQualLoc
) {
1315 I
.Ptr
.TypeQuals
= TypeQuals
;
1316 I
.Ptr
.ConstQualLoc
= ConstQualLoc
.getRawEncoding();
1317 I
.Ptr
.VolatileQualLoc
= VolatileQualLoc
.getRawEncoding();
1318 I
.Ptr
.RestrictQualLoc
= RestrictQualLoc
.getRawEncoding();
1323 /// \brief Return a DeclaratorChunk for a reference.
1324 static DeclaratorChunk
getReference(unsigned TypeQuals
, SourceLocation Loc
,
1329 I
.Ref
.HasRestrict
= (TypeQuals
& DeclSpec::TQ_restrict
) != 0;
1330 I
.Ref
.LValueRef
= lvalue
;
1335 /// \brief Return a DeclaratorChunk for an array.
1336 static DeclaratorChunk
getArray(unsigned TypeQuals
,
1337 bool isStatic
, bool isStar
, Expr
*NumElts
,
1338 SourceLocation LBLoc
, SourceLocation RBLoc
) {
1344 I
.Arr
.TypeQuals
= TypeQuals
;
1345 I
.Arr
.hasStatic
= isStatic
;
1346 I
.Arr
.isStar
= isStar
;
1347 I
.Arr
.NumElts
= NumElts
;
1351 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
1352 /// "TheDeclarator" is the declarator that this will be added to.
1353 static DeclaratorChunk
getFunction(bool hasProto
, bool isVariadic
,
1355 SourceLocation EllipsisLoc
,
1356 ParamInfo
*ArgInfo
, unsigned NumArgs
,
1358 bool RefQualifierIsLvalueRef
,
1359 SourceLocation RefQualifierLoc
,
1360 SourceLocation ConstQualifierLoc
,
1361 SourceLocation VolatileQualifierLoc
,
1362 SourceLocation MutableLoc
,
1363 ExceptionSpecificationType ESpecType
,
1364 SourceLocation ESpecLoc
,
1365 ParsedType
*Exceptions
,
1366 SourceRange
*ExceptionRanges
,
1367 unsigned NumExceptions
,
1369 SourceLocation LocalRangeBegin
,
1370 SourceLocation LocalRangeEnd
,
1371 Declarator
&TheDeclarator
,
1372 TypeResult TrailingReturnType
=
1375 /// \brief Return a DeclaratorChunk for a block.
1376 static DeclaratorChunk
getBlockPointer(unsigned TypeQuals
,
1377 SourceLocation Loc
) {
1379 I
.Kind
= BlockPointer
;
1381 I
.Cls
.TypeQuals
= TypeQuals
;
1386 static DeclaratorChunk
getMemberPointer(const CXXScopeSpec
&SS
,
1388 SourceLocation Loc
) {
1390 I
.Kind
= MemberPointer
;
1392 I
.Mem
.TypeQuals
= TypeQuals
;
1394 new (I
.Mem
.ScopeMem
.Mem
) CXXScopeSpec(SS
);
1398 /// \brief Return a DeclaratorChunk for a paren.
1399 static DeclaratorChunk
getParen(SourceLocation LParenLoc
,
1400 SourceLocation RParenLoc
) {
1404 I
.EndLoc
= RParenLoc
;
1405 I
.Common
.AttrList
= 0;
1411 /// \brief Described the kind of function definition (if any) provided for
1413 enum FunctionDefinitionKind
{
1420 /// \brief Information about one declarator, including the parsed type
1421 /// information and the identifier.
1423 /// When the declarator is fully formed, this is turned into the appropriate
1426 /// Declarators come in two types: normal declarators and abstract declarators.
1427 /// Abstract declarators are used when parsing types, and don't have an
1428 /// identifier. Normal declarators do have ID's.
1430 /// Instances of this class should be a transient object that lives on the
1431 /// stack, not objects that are allocated in large quantities on the heap.
1435 FileContext
, // File scope declaration.
1436 PrototypeContext
, // Within a function prototype.
1437 ObjCResultContext
, // An ObjC method result type.
1438 ObjCParameterContext
,// An ObjC method parameter type.
1439 KNRTypeListContext
, // K&R type definition list for formals.
1440 TypeNameContext
, // Abstract declarator for types.
1441 MemberContext
, // Struct/Union field.
1442 BlockContext
, // Declaration within a block in a function.
1443 ForContext
, // Declaration within first part of a for loop.
1444 ConditionContext
, // Condition declaration in a C++ if/switch/while/for.
1445 TemplateParamContext
,// Within a template parameter list.
1446 CXXNewContext
, // C++ new-expression.
1447 CXXCatchContext
, // C++ catch exception-declaration
1448 ObjCCatchContext
, // Objective-C catch exception-declaration
1449 BlockLiteralContext
, // Block literal declarator.
1450 LambdaExprContext
, // Lambda-expression declarator.
1451 TrailingReturnContext
, // C++11 trailing-type-specifier.
1452 TemplateTypeArgContext
, // Template type argument.
1453 AliasDeclContext
, // C++11 alias-declaration.
1454 AliasTemplateContext
// C++11 alias-declaration template.
1463 /// \brief Where we are parsing this declarator.
1466 /// DeclTypeInfo - This holds each type that the declarator includes as it is
1467 /// parsed. This is pushed from the identifier out, which means that element
1468 /// #0 will be the most closely bound to the identifier, and
1469 /// DeclTypeInfo.back() will be the least closely bound.
1470 SmallVector
<DeclaratorChunk
, 8> DeclTypeInfo
;
1472 /// InvalidType - Set by Sema::GetTypeForDeclarator().
1473 bool InvalidType
: 1;
1475 /// GroupingParens - Set by Parser::ParseParenDeclarator().
1476 bool GroupingParens
: 1;
1478 /// FunctionDefinition - Is this Declarator for a function or member
1479 /// definition and, if so, what kind?
1481 /// Actually a FunctionDefinitionKind.
1482 unsigned FunctionDefinition
: 2;
1484 /// \brief Is this Declarator a redeclaration?
1485 bool Redeclaration
: 1;
1487 /// Attrs - Attributes.
1488 ParsedAttributes Attrs
;
1490 /// \brief The asm label, if specified.
1493 /// InlineParams - This is a local array used for the first function decl
1494 /// chunk to avoid going to the heap for the common case when we have one
1495 /// function chunk in the declarator.
1496 DeclaratorChunk::ParamInfo InlineParams
[16];
1497 bool InlineParamsUsed
;
1499 /// \brief true if the declaration is preceded by \c __extension__.
1502 /// \brief If this is the second or subsequent declarator in this declaration,
1503 /// the location of the comma before this declarator.
1504 SourceLocation CommaLoc
;
1506 /// \brief If provided, the source location of the ellipsis used to describe
1507 /// this declarator as a parameter pack.
1508 SourceLocation EllipsisLoc
;
1510 friend struct DeclaratorChunk
;
1513 Declarator(const DeclSpec
&ds
, TheContext C
)
1514 : DS(ds
), Range(ds
.getSourceRange()), Context(C
),
1515 InvalidType(DS
.getTypeSpecType() == DeclSpec::TST_error
),
1516 GroupingParens(false), FunctionDefinition(FDK_Declaration
),
1517 Redeclaration(false),
1518 Attrs(ds
.getAttributePool().getFactory()), AsmLabel(0),
1519 InlineParamsUsed(false), Extension(false) {
1526 /// getDeclSpec - Return the declaration-specifier that this declarator was
1528 const DeclSpec
&getDeclSpec() const { return DS
; }
1530 /// getMutableDeclSpec - Return a non-const version of the DeclSpec. This
1531 /// should be used with extreme care: declspecs can often be shared between
1532 /// multiple declarators, so mutating the DeclSpec affects all of the
1533 /// Declarators. This should only be done when the declspec is known to not
1534 /// be shared or when in error recovery etc.
1535 DeclSpec
&getMutableDeclSpec() { return const_cast<DeclSpec
&>(DS
); }
1537 AttributePool
&getAttributePool() const {
1538 return Attrs
.getPool();
1541 /// getCXXScopeSpec - Return the C++ scope specifier (global scope or
1542 /// nested-name-specifier) that is part of the declarator-id.
1543 const CXXScopeSpec
&getCXXScopeSpec() const { return SS
; }
1544 CXXScopeSpec
&getCXXScopeSpec() { return SS
; }
1546 /// \brief Retrieve the name specified by this declarator.
1547 UnqualifiedId
&getName() { return Name
; }
1549 TheContext
getContext() const { return Context
; }
1551 bool isPrototypeContext() const {
1552 return (Context
== PrototypeContext
||
1553 Context
== ObjCParameterContext
||
1554 Context
== ObjCResultContext
);
1557 /// \brief Get the source range that spans this declarator.
1558 const SourceRange
&getSourceRange() const LLVM_READONLY
{ return Range
; }
1559 SourceLocation
getLocStart() const LLVM_READONLY
{ return Range
.getBegin(); }
1560 SourceLocation
getLocEnd() const LLVM_READONLY
{ return Range
.getEnd(); }
1562 void SetSourceRange(SourceRange R
) { Range
= R
; }
1563 /// SetRangeBegin - Set the start of the source range to Loc, unless it's
1565 void SetRangeBegin(SourceLocation Loc
) {
1566 if (!Loc
.isInvalid())
1567 Range
.setBegin(Loc
);
1569 /// SetRangeEnd - Set the end of the source range to Loc, unless it's invalid.
1570 void SetRangeEnd(SourceLocation Loc
) {
1571 if (!Loc
.isInvalid())
1574 /// ExtendWithDeclSpec - Extend the declarator source range to include the
1575 /// given declspec, unless its location is invalid. Adopts the range start if
1576 /// the current range start is invalid.
1577 void ExtendWithDeclSpec(const DeclSpec
&DS
) {
1578 const SourceRange
&SR
= DS
.getSourceRange();
1579 if (Range
.getBegin().isInvalid())
1580 Range
.setBegin(SR
.getBegin());
1581 if (!SR
.getEnd().isInvalid())
1582 Range
.setEnd(SR
.getEnd());
1585 /// \brief Reset the contents of this Declarator.
1589 Range
= DS
.getSourceRange();
1591 for (unsigned i
= 0, e
= DeclTypeInfo
.size(); i
!= e
; ++i
)
1592 DeclTypeInfo
[i
].destroy();
1593 DeclTypeInfo
.clear();
1596 InlineParamsUsed
= false;
1597 CommaLoc
= SourceLocation();
1598 EllipsisLoc
= SourceLocation();
1601 /// mayOmitIdentifier - Return true if the identifier is either optional or
1602 /// not allowed. This is true for typenames, prototypes, and template
1603 /// parameter lists.
1604 bool mayOmitIdentifier() const {
1607 case KNRTypeListContext
:
1611 case ConditionContext
:
1614 case TypeNameContext
:
1615 case AliasDeclContext
:
1616 case AliasTemplateContext
:
1617 case PrototypeContext
:
1618 case ObjCParameterContext
:
1619 case ObjCResultContext
:
1620 case TemplateParamContext
:
1622 case CXXCatchContext
:
1623 case ObjCCatchContext
:
1624 case BlockLiteralContext
:
1625 case LambdaExprContext
:
1626 case TemplateTypeArgContext
:
1627 case TrailingReturnContext
:
1630 llvm_unreachable("unknown context kind!");
1633 /// mayHaveIdentifier - Return true if the identifier is either optional or
1634 /// required. This is true for normal declarators and prototypes, but not
1636 bool mayHaveIdentifier() const {
1639 case KNRTypeListContext
:
1643 case ConditionContext
:
1644 case PrototypeContext
:
1645 case TemplateParamContext
:
1646 case CXXCatchContext
:
1647 case ObjCCatchContext
:
1650 case TypeNameContext
:
1652 case AliasDeclContext
:
1653 case AliasTemplateContext
:
1654 case ObjCParameterContext
:
1655 case ObjCResultContext
:
1656 case BlockLiteralContext
:
1657 case LambdaExprContext
:
1658 case TemplateTypeArgContext
:
1659 case TrailingReturnContext
:
1662 llvm_unreachable("unknown context kind!");
1665 /// mayBeFollowedByCXXDirectInit - Return true if the declarator can be
1666 /// followed by a C++ direct initializer, e.g. "int x(1);".
1667 bool mayBeFollowedByCXXDirectInit() const {
1668 if (hasGroupingParens()) return false;
1670 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef
)
1673 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern
&&
1674 Context
!= FileContext
)
1677 // Special names can't have direct initializers.
1678 if (Name
.getKind() != UnqualifiedId::IK_Identifier
)
1687 case ConditionContext
:
1688 // This may not be followed by a direct initializer, but it can't be a
1689 // function declaration either, and we'd prefer to perform a tentative
1690 // parse in order to produce the right diagnostic.
1693 case KNRTypeListContext
:
1695 case PrototypeContext
:
1696 case ObjCParameterContext
:
1697 case ObjCResultContext
:
1698 case TemplateParamContext
:
1699 case CXXCatchContext
:
1700 case ObjCCatchContext
:
1701 case TypeNameContext
:
1703 case AliasDeclContext
:
1704 case AliasTemplateContext
:
1705 case BlockLiteralContext
:
1706 case LambdaExprContext
:
1707 case TemplateTypeArgContext
:
1708 case TrailingReturnContext
:
1711 llvm_unreachable("unknown context kind!");
1714 /// isPastIdentifier - Return true if we have parsed beyond the point where
1716 bool isPastIdentifier() const { return Name
.isValid(); }
1718 /// hasName - Whether this declarator has a name, which might be an
1719 /// identifier (accessible via getIdentifier()) or some kind of
1720 /// special C++ name (constructor, destructor, etc.).
1721 bool hasName() const {
1722 return Name
.getKind() != UnqualifiedId::IK_Identifier
|| Name
.Identifier
;
1725 IdentifierInfo
*getIdentifier() const {
1726 if (Name
.getKind() == UnqualifiedId::IK_Identifier
)
1727 return Name
.Identifier
;
1731 SourceLocation
getIdentifierLoc() const { return Name
.StartLocation
; }
1733 /// \brief Set the name of this declarator to be the given identifier.
1734 void SetIdentifier(IdentifierInfo
*Id
, SourceLocation IdLoc
) {
1735 Name
.setIdentifier(Id
, IdLoc
);
1738 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to
1739 /// EndLoc, which should be the last token of the chunk.
1740 void AddTypeInfo(const DeclaratorChunk
&TI
,
1741 ParsedAttributes
&attrs
,
1742 SourceLocation EndLoc
) {
1743 DeclTypeInfo
.push_back(TI
);
1744 DeclTypeInfo
.back().getAttrListRef() = attrs
.getList();
1745 getAttributePool().takeAllFrom(attrs
.getPool());
1747 if (!EndLoc
.isInvalid())
1748 SetRangeEnd(EndLoc
);
1751 /// \brief Add a new innermost chunk to this declarator.
1752 void AddInnermostTypeInfo(const DeclaratorChunk
&TI
) {
1753 DeclTypeInfo
.insert(DeclTypeInfo
.begin(), TI
);
1756 /// \brief Return the number of types applied to this declarator.
1757 unsigned getNumTypeObjects() const { return DeclTypeInfo
.size(); }
1759 /// Return the specified TypeInfo from this declarator. TypeInfo #0 is
1760 /// closest to the identifier.
1761 const DeclaratorChunk
&getTypeObject(unsigned i
) const {
1762 assert(i
< DeclTypeInfo
.size() && "Invalid type chunk");
1763 return DeclTypeInfo
[i
];
1765 DeclaratorChunk
&getTypeObject(unsigned i
) {
1766 assert(i
< DeclTypeInfo
.size() && "Invalid type chunk");
1767 return DeclTypeInfo
[i
];
1770 void DropFirstTypeObject()
1772 assert(!DeclTypeInfo
.empty() && "No type chunks to drop.");
1773 DeclTypeInfo
.front().destroy();
1774 DeclTypeInfo
.erase(DeclTypeInfo
.begin());
1777 /// isArrayOfUnknownBound - This method returns true if the declarator
1778 /// is a declarator for an array of unknown bound (looking through
1780 bool isArrayOfUnknownBound() const {
1781 for (unsigned i
= 0, i_end
= DeclTypeInfo
.size(); i
< i_end
; ++i
) {
1782 switch (DeclTypeInfo
[i
].Kind
) {
1783 case DeclaratorChunk::Paren
:
1785 case DeclaratorChunk::Function
:
1786 case DeclaratorChunk::Pointer
:
1787 case DeclaratorChunk::Reference
:
1788 case DeclaratorChunk::BlockPointer
:
1789 case DeclaratorChunk::MemberPointer
:
1791 case DeclaratorChunk::Array
:
1792 return !DeclTypeInfo
[i
].Arr
.NumElts
;
1794 llvm_unreachable("Invalid type chunk");
1799 /// isFunctionDeclarator - This method returns true if the declarator
1800 /// is a function declarator (looking through parentheses).
1801 /// If true is returned, then the reference type parameter idx is
1802 /// assigned with the index of the declaration chunk.
1803 bool isFunctionDeclarator(unsigned& idx
) const {
1804 for (unsigned i
= 0, i_end
= DeclTypeInfo
.size(); i
< i_end
; ++i
) {
1805 switch (DeclTypeInfo
[i
].Kind
) {
1806 case DeclaratorChunk::Function
:
1809 case DeclaratorChunk::Paren
:
1811 case DeclaratorChunk::Pointer
:
1812 case DeclaratorChunk::Reference
:
1813 case DeclaratorChunk::Array
:
1814 case DeclaratorChunk::BlockPointer
:
1815 case DeclaratorChunk::MemberPointer
:
1818 llvm_unreachable("Invalid type chunk");
1823 /// isFunctionDeclarator - Once this declarator is fully parsed and formed,
1824 /// this method returns true if the identifier is a function declarator
1825 /// (looking through parentheses).
1826 bool isFunctionDeclarator() const {
1828 return isFunctionDeclarator(index
);
1831 /// getFunctionTypeInfo - Retrieves the function type info object
1832 /// (looking through parentheses).
1833 DeclaratorChunk::FunctionTypeInfo
&getFunctionTypeInfo() {
1834 assert(isFunctionDeclarator() && "Not a function declarator!");
1836 isFunctionDeclarator(index
);
1837 return DeclTypeInfo
[index
].Fun
;
1840 /// getFunctionTypeInfo - Retrieves the function type info object
1841 /// (looking through parentheses).
1842 const DeclaratorChunk::FunctionTypeInfo
&getFunctionTypeInfo() const {
1843 return const_cast<Declarator
*>(this)->getFunctionTypeInfo();
1846 /// \brief Determine whether the declaration that will be produced from
1847 /// this declaration will be a function.
1849 /// A declaration can declare a function even if the declarator itself
1850 /// isn't a function declarator, if the type specifier refers to a function
1851 /// type. This routine checks for both cases.
1852 bool isDeclarationOfFunction() const;
1854 /// takeAttributes - Takes attributes from the given parsed-attributes
1855 /// set and add them to this declarator.
1857 /// These examples both add 3 attributes to "var":
1858 /// short int var __attribute__((aligned(16),common,deprecated));
1859 /// short int x, __attribute__((aligned(16)) var
1860 /// __attribute__((common,deprecated));
1862 /// Also extends the range of the declarator.
1863 void takeAttributes(ParsedAttributes
&attrs
, SourceLocation lastLoc
) {
1864 Attrs
.takeAllFrom(attrs
);
1866 if (!lastLoc
.isInvalid())
1867 SetRangeEnd(lastLoc
);
1870 const AttributeList
*getAttributes() const { return Attrs
.getList(); }
1871 AttributeList
*getAttributes() { return Attrs
.getList(); }
1873 AttributeList
*&getAttrListRef() { return Attrs
.getListRef(); }
1875 /// hasAttributes - do we contain any attributes?
1876 bool hasAttributes() const {
1877 if (getAttributes() || getDeclSpec().hasAttributes()) return true;
1878 for (unsigned i
= 0, e
= getNumTypeObjects(); i
!= e
; ++i
)
1879 if (getTypeObject(i
).getAttrs())
1884 void setAsmLabel(Expr
*E
) { AsmLabel
= E
; }
1885 Expr
*getAsmLabel() const { return AsmLabel
; }
1887 void setExtension(bool Val
= true) { Extension
= Val
; }
1888 bool getExtension() const { return Extension
; }
1890 void setInvalidType(bool Val
= true) { InvalidType
= Val
; }
1891 bool isInvalidType() const {
1892 return InvalidType
|| DS
.getTypeSpecType() == DeclSpec::TST_error
;
1895 void setGroupingParens(bool flag
) { GroupingParens
= flag
; }
1896 bool hasGroupingParens() const { return GroupingParens
; }
1898 bool isFirstDeclarator() const { return !CommaLoc
.isValid(); }
1899 SourceLocation
getCommaLoc() const { return CommaLoc
; }
1900 void setCommaLoc(SourceLocation CL
) { CommaLoc
= CL
; }
1902 bool hasEllipsis() const { return EllipsisLoc
.isValid(); }
1903 SourceLocation
getEllipsisLoc() const { return EllipsisLoc
; }
1904 void setEllipsisLoc(SourceLocation EL
) { EllipsisLoc
= EL
; }
1906 void setFunctionDefinitionKind(FunctionDefinitionKind Val
) {
1907 FunctionDefinition
= Val
;
1910 bool isFunctionDefinition() const {
1911 return getFunctionDefinitionKind() != FDK_Declaration
;
1914 FunctionDefinitionKind
getFunctionDefinitionKind() const {
1915 return (FunctionDefinitionKind
)FunctionDefinition
;
1918 void setRedeclaration(bool Val
) { Redeclaration
= Val
; }
1919 bool isRedeclaration() const { return Redeclaration
; }
1922 /// \brief This little struct is used to capture information about
1923 /// structure field declarators, which is basically just a bitfield size.
1924 struct FieldDeclarator
{
1927 explicit FieldDeclarator(const DeclSpec
&DS
)
1928 : D(DS
, Declarator::MemberContext
), BitfieldSize(0) { }
1931 /// \brief Represents a C++11 virt-specifier-seq.
1932 class VirtSpecifiers
{
1940 VirtSpecifiers() : Specifiers(0) { }
1942 bool SetSpecifier(Specifier VS
, SourceLocation Loc
,
1943 const char *&PrevSpec
);
1945 bool isOverrideSpecified() const { return Specifiers
& VS_Override
; }
1946 SourceLocation
getOverrideLoc() const { return VS_overrideLoc
; }
1948 bool isFinalSpecified() const { return Specifiers
& VS_Final
; }
1949 SourceLocation
getFinalLoc() const { return VS_finalLoc
; }
1951 void clear() { Specifiers
= 0; }
1953 static const char *getSpecifierName(Specifier VS
);
1955 SourceLocation
getLastLocation() const { return LastLocation
; }
1958 unsigned Specifiers
;
1960 SourceLocation VS_overrideLoc
, VS_finalLoc
;
1961 SourceLocation LastLocation
;
1964 /// \brief An individual capture in a lambda introducer.
1965 struct LambdaCapture
{
1966 LambdaCaptureKind Kind
;
1969 SourceLocation EllipsisLoc
;
1971 LambdaCapture(LambdaCaptureKind Kind
, SourceLocation Loc
,
1972 IdentifierInfo
* Id
= 0,
1973 SourceLocation EllipsisLoc
= SourceLocation())
1974 : Kind(Kind
), Loc(Loc
), Id(Id
), EllipsisLoc(EllipsisLoc
)
1978 /// \brief Represents a complete lambda introducer.
1979 struct LambdaIntroducer
{
1981 SourceLocation DefaultLoc
;
1982 LambdaCaptureDefault Default
;
1983 llvm::SmallVector
<LambdaCapture
, 4> Captures
;
1986 : Default(LCD_None
) {}
1988 /// \brief Append a capture in a lambda introducer.
1989 void addCapture(LambdaCaptureKind Kind
,
1991 IdentifierInfo
* Id
= 0,
1992 SourceLocation EllipsisLoc
= SourceLocation()) {
1993 Captures
.push_back(LambdaCapture(Kind
, Loc
, Id
, EllipsisLoc
));
1998 } // end namespace clang