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 //===----------------------------------------------------------------------===//
10 // This file defines the classes used to store parsed information about
11 // declaration-specifiers and declarators.
13 // static const int volatile x, *y, *(*(*z)[10])(const void *x);
14 // ------------------------- - -- ---------------------------
15 // declaration-specifiers \ | /
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_CLANG_SEMA_DECLSPEC_H
21 #define LLVM_CLANG_SEMA_DECLSPEC_H
23 #include "clang/Sema/AttributeList.h"
24 #include "clang/Sema/Ownership.h"
25 #include "clang/AST/NestedNameSpecifier.h"
26 #include "clang/Lex/Token.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 "llvm/ADT/SmallVector.h"
32 #include "llvm/Support/Compiler.h"
33 #include "llvm/Support/ErrorHandling.h"
39 class DiagnosticsEngine;
41 class NamespaceAliasDecl;
43 class NestedNameSpecifier;
44 class NestedNameSpecifierLoc;
49 struct TemplateIdAnnotation;
51 /// CXXScopeSpec - Represents a C++ nested-name-specifier or a global scope
52 /// specifier. These can be in 3 states:
53 /// 1) Not present, identified by isEmpty()
54 /// 2) Present, identified by isNotEmpty()
55 /// 2.a) Valid, idenified by isValid()
56 /// 2.b) Invalid, identified by isInvalid().
58 /// isSet() is deprecated because it mostly corresponded to "valid" but was
59 /// often used as if it meant "present".
61 /// The actual scope is described by getScopeRep().
64 NestedNameSpecifierLocBuilder Builder;
67 const SourceRange &getRange() const { return Range; }
68 void setRange(const SourceRange &R) { Range = R; }
69 void setBeginLoc(SourceLocation Loc) { Range.setBegin(Loc); }
70 void setEndLoc(SourceLocation Loc) { Range.setEnd(Loc); }
71 SourceLocation getBeginLoc() const { return Range.getBegin(); }
72 SourceLocation getEndLoc() const { return Range.getEnd(); }
74 /// \brief Retrieve the representation of the nested-name-specifier.
75 NestedNameSpecifier *getScopeRep() const {
76 return Builder.getRepresentation();
79 /// \brief Extend the current nested-name-specifier by another
80 /// nested-name-specifier component of the form 'type::'.
82 /// \param Context The AST context in which this nested-name-specifier
85 /// \param TemplateKWLoc The location of the 'template' keyword, if present.
87 /// \param TL The TypeLoc that describes the type preceding the '::'.
89 /// \param ColonColonLoc The location of the trailing '::'.
90 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL,
91 SourceLocation ColonColonLoc);
93 /// \brief Extend the current nested-name-specifier by another
94 /// nested-name-specifier component of the form 'identifier::'.
96 /// \param Context The AST context in which this nested-name-specifier
99 /// \param Identifier The identifier.
101 /// \param IdentifierLoc The location of the identifier.
103 /// \param ColonColonLoc The location of the trailing '::'.
104 void Extend(ASTContext &Context, IdentifierInfo *Identifier,
105 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc);
107 /// \brief Extend the current nested-name-specifier by another
108 /// nested-name-specifier component of the form 'namespace::'.
110 /// \param Context The AST context in which this nested-name-specifier
113 /// \param Namespace The namespace.
115 /// \param NamespaceLoc The location of the namespace name.
117 /// \param ColonColonLoc The location of the trailing '::'.
118 void Extend(ASTContext &Context, NamespaceDecl *Namespace,
119 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc);
121 /// \brief Extend the current nested-name-specifier by another
122 /// nested-name-specifier component of the form 'namespace-alias::'.
124 /// \param Context The AST context in which this nested-name-specifier
127 /// \param Alias The namespace alias.
129 /// \param AliasLoc The location of the namespace alias
132 /// \param ColonColonLoc The location of the trailing '::'.
133 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias,
134 SourceLocation AliasLoc, SourceLocation ColonColonLoc);
136 /// \brief Turn this (empty) nested-name-specifier into the global
137 /// nested-name-specifier '::'.
138 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc);
140 /// \brief Make a new nested-name-specifier from incomplete source-location
143 /// FIXME: This routine should be used very, very rarely, in cases where we
144 /// need to synthesize a nested-name-specifier. Most code should instead use
145 /// \c Adopt() with a proper \c NestedNameSpecifierLoc.
146 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier,
149 /// \brief Adopt an existing nested-name-specifier (with source-range
151 void Adopt(NestedNameSpecifierLoc Other);
153 /// \brief Retrieve a nested-name-specifier with location information, copied
154 /// into the given AST context.
156 /// \param Context The context into which this nested-name-specifier will be
158 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const;
160 /// \brief Retrieve the location of the name in the last qualifier
161 /// in this nested name specifier. For example:
164 SourceLocation getLastQualifierNameLoc() const;
166 /// No scope specifier.
167 bool isEmpty() const { return !Range.isValid(); }
168 /// A scope specifier is present, but may be valid or invalid.
169 bool isNotEmpty() const { return !isEmpty(); }
171 /// An error occurred during parsing of the scope specifier.
172 bool isInvalid() const { return isNotEmpty() && getScopeRep() == 0; }
173 /// A scope specifier is present, and it refers to a real scope.
174 bool isValid() const { return isNotEmpty() && getScopeRep() != 0; }
176 /// \brief Indicate that this nested-name-specifier is invalid.
177 void SetInvalid(SourceRange R) {
178 assert(R.isValid() && "Must have a valid source range");
179 if (Range.getBegin().isInvalid())
180 Range.setBegin(R.getBegin());
181 Range.setEnd(R.getEnd());
185 /// Deprecated. Some call sites intend isNotEmpty() while others intend
187 bool isSet() const { return getScopeRep() != 0; }
190 Range = SourceRange();
194 /// \brief Retrieve the data associated with the source-location information.
195 char *location_data() const { return Builder.getBuffer().first; }
197 /// \brief Retrieve the size of the data associated with source-location
199 unsigned location_size() const { return Builder.getBuffer().second; }
202 /// DeclSpec - This class captures information about "declaration specifiers",
203 /// which encompasses storage-class-specifiers, type-specifiers,
204 /// type-qualifiers, and function-specifiers.
207 // storage-class-specifier
208 // Note: The order of these enumerators is important for diagnostics.
220 // Import type specifier width enumeration and constants.
221 typedef TypeSpecifierWidth TSW;
222 static const TSW TSW_unspecified = clang::TSW_unspecified;
223 static const TSW TSW_short = clang::TSW_short;
224 static const TSW TSW_long = clang::TSW_long;
225 static const TSW TSW_longlong = clang::TSW_longlong;
233 // Import type specifier sign enumeration and constants.
234 typedef TypeSpecifierSign TSS;
235 static const TSS TSS_unspecified = clang::TSS_unspecified;
236 static const TSS TSS_signed = clang::TSS_signed;
237 static const TSS TSS_unsigned = clang::TSS_unsigned;
239 // Import type specifier type enumeration and constants.
240 typedef TypeSpecifierType TST;
241 static const TST TST_unspecified = clang::TST_unspecified;
242 static const TST TST_void = clang::TST_void;
243 static const TST TST_char = clang::TST_char;
244 static const TST TST_wchar = clang::TST_wchar;
245 static const TST TST_char16 = clang::TST_char16;
246 static const TST TST_char32 = clang::TST_char32;
247 static const TST TST_int = clang::TST_int;
248 static const TST TST_int128 = clang::TST_int128;
249 static const TST TST_half = clang::TST_half;
250 static const TST TST_float = clang::TST_float;
251 static const TST TST_double = clang::TST_double;
252 static const TST TST_bool = clang::TST_bool;
253 static const TST TST_decimal32 = clang::TST_decimal32;
254 static const TST TST_decimal64 = clang::TST_decimal64;
255 static const TST TST_decimal128 = clang::TST_decimal128;
256 static const TST TST_enum = clang::TST_enum;
257 static const TST TST_union = clang::TST_union;
258 static const TST TST_struct = clang::TST_struct;
259 static const TST TST_class = clang::TST_class;
260 static const TST TST_typename = clang::TST_typename;
261 static const TST TST_typeofType = clang::TST_typeofType;
262 static const TST TST_typeofExpr = clang::TST_typeofExpr;
263 static const TST TST_decltype = clang::TST_decltype;
264 static const TST TST_underlyingType = clang::TST_underlyingType;
265 static const TST TST_auto = clang::TST_auto;
266 static const TST TST_unknown_anytype = clang::TST_unknown_anytype;
267 static const TST TST_atomic = clang::TST_atomic;
268 static const TST TST_error = clang::TST_error;
271 enum TQ { // NOTE: These flags must be kept in sync with Qualifiers::TQ.
278 /// ParsedSpecifiers - Flags to query which specifiers were applied. This is
279 /// returned by getParsedSpecifiers.
280 enum ParsedSpecifiers {
282 PQ_StorageClassSpecifier = 1,
283 PQ_TypeSpecifier = 2,
284 PQ_TypeQualifier = 4,
285 PQ_FunctionSpecifier = 8
289 // storage-class-specifier
290 /*SCS*/unsigned StorageClassSpec : 3;
291 unsigned SCS_thread_specified : 1;
292 unsigned SCS_extern_in_linkage_spec : 1;
295 /*TSW*/unsigned TypeSpecWidth : 2;
296 /*TSC*/unsigned TypeSpecComplex : 2;
297 /*TSS*/unsigned TypeSpecSign : 2;
298 /*TST*/unsigned TypeSpecType : 5;
299 unsigned TypeAltiVecVector : 1;
300 unsigned TypeAltiVecPixel : 1;
301 unsigned TypeAltiVecBool : 1;
302 unsigned TypeSpecOwned : 1;
305 unsigned TypeQualifiers : 3; // Bitwise OR of TQ.
307 // function-specifier
308 unsigned FS_inline_specified : 1;
309 unsigned FS_virtual_specified : 1;
310 unsigned FS_explicit_specified : 1;
313 unsigned Friend_specified : 1;
315 // constexpr-specifier
316 unsigned Constexpr_specified : 1;
318 /*SCS*/unsigned StorageClassSpecAsWritten : 3;
321 UnionParsedType TypeRep;
327 ParsedAttributes Attrs;
329 // Scope specifier for the type spec, if applicable.
330 CXXScopeSpec TypeScope;
332 // List of protocol qualifiers for objective-c classes. Used for
333 // protocol-qualified interfaces "NString<foo>" and protocol-qualified id
335 Decl * const *ProtocolQualifiers;
336 unsigned NumProtocolQualifiers;
337 SourceLocation ProtocolLAngleLoc;
338 SourceLocation *ProtocolLocs;
340 // SourceLocation info. These are null if the item wasn't specified or if
341 // the setting was synthesized.
344 SourceLocation StorageClassSpecLoc, SCS_threadLoc;
345 SourceLocation TSWLoc, TSCLoc, TSSLoc, TSTLoc, AltiVecLoc;
346 /// TSTNameLoc - If TypeSpecType is any of class, enum, struct, union,
347 /// typename, then this is the location of the named type (if present);
348 /// otherwise, it is the same as TSTLoc. Hence, the pair TSTLoc and
349 /// TSTNameLoc provides source range info for tag types.
350 SourceLocation TSTNameLoc;
351 SourceRange TypeofParensRange;
352 SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc;
353 SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc;
354 SourceLocation FriendLoc, ModulePrivateLoc, ConstexprLoc;
356 WrittenBuiltinSpecs writtenBS;
357 void SaveWrittenBuiltinSpecs();
358 void SaveStorageSpecifierAsWritten();
360 ObjCDeclSpec *ObjCQualifiers;
362 static bool isTypeRep(TST T) {
363 return (T == TST_typename || T == TST_typeofType ||
364 T == TST_underlyingType || T == TST_atomic);
366 static bool isExprRep(TST T) {
367 return (T == TST_typeofExpr || T == TST_decltype);
369 static bool isDeclRep(TST T) {
370 return (T == TST_enum || T == TST_struct ||
371 T == TST_union || T == TST_class);
374 DeclSpec(const DeclSpec&); // DO NOT IMPLEMENT
375 void operator=(const DeclSpec&); // DO NOT IMPLEMENT
378 DeclSpec(AttributeFactory &attrFactory)
379 : StorageClassSpec(SCS_unspecified),
380 SCS_thread_specified(false),
381 SCS_extern_in_linkage_spec(false),
382 TypeSpecWidth(TSW_unspecified),
383 TypeSpecComplex(TSC_unspecified),
384 TypeSpecSign(TSS_unspecified),
385 TypeSpecType(TST_unspecified),
386 TypeAltiVecVector(false),
387 TypeAltiVecPixel(false),
388 TypeAltiVecBool(false),
389 TypeSpecOwned(false),
390 TypeQualifiers(TQ_unspecified),
391 FS_inline_specified(false),
392 FS_virtual_specified(false),
393 FS_explicit_specified(false),
394 Friend_specified(false),
395 Constexpr_specified(false),
396 StorageClassSpecAsWritten(SCS_unspecified),
398 ProtocolQualifiers(0),
399 NumProtocolQualifiers(0),
405 delete [] ProtocolQualifiers;
406 delete [] ProtocolLocs;
408 // storage-class-specifier
409 SCS getStorageClassSpec() const { return (SCS)StorageClassSpec; }
410 bool isThreadSpecified() const { return SCS_thread_specified; }
411 bool isExternInLinkageSpec() const { return SCS_extern_in_linkage_spec; }
412 void setExternInLinkageSpec(bool Value) {
413 SCS_extern_in_linkage_spec = Value;
416 SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; }
417 SourceLocation getThreadSpecLoc() const { return SCS_threadLoc; }
419 void ClearStorageClassSpecs() {
420 StorageClassSpec = DeclSpec::SCS_unspecified;
421 SCS_thread_specified = false;
422 SCS_extern_in_linkage_spec = false;
423 StorageClassSpecLoc = SourceLocation();
424 SCS_threadLoc = SourceLocation();
428 TSW getTypeSpecWidth() const { return (TSW)TypeSpecWidth; }
429 TSC getTypeSpecComplex() const { return (TSC)TypeSpecComplex; }
430 TSS getTypeSpecSign() const { return (TSS)TypeSpecSign; }
431 TST getTypeSpecType() const { return (TST)TypeSpecType; }
432 bool isTypeAltiVecVector() const { return TypeAltiVecVector; }
433 bool isTypeAltiVecPixel() const { return TypeAltiVecPixel; }
434 bool isTypeAltiVecBool() const { return TypeAltiVecBool; }
435 bool isTypeSpecOwned() const { return TypeSpecOwned; }
436 ParsedType getRepAsType() const {
437 assert(isTypeRep((TST) TypeSpecType) && "DeclSpec does not store a type");
440 Decl *getRepAsDecl() const {
441 assert(isDeclRep((TST) TypeSpecType) && "DeclSpec does not store a decl");
444 Expr *getRepAsExpr() const {
445 assert(isExprRep((TST) TypeSpecType) && "DeclSpec does not store an expr");
448 CXXScopeSpec &getTypeSpecScope() { return TypeScope; }
449 const CXXScopeSpec &getTypeSpecScope() const { return TypeScope; }
451 const SourceRange &getSourceRange() const LLVM_READONLY { return Range; }
452 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
453 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
455 SourceLocation getTypeSpecWidthLoc() const { return TSWLoc; }
456 SourceLocation getTypeSpecComplexLoc() const { return TSCLoc; }
457 SourceLocation getTypeSpecSignLoc() const { return TSSLoc; }
458 SourceLocation getTypeSpecTypeLoc() const { return TSTLoc; }
459 SourceLocation getAltiVecLoc() const { return AltiVecLoc; }
461 SourceLocation getTypeSpecTypeNameLoc() const {
462 assert(isDeclRep((TST) TypeSpecType) || TypeSpecType == TST_typename);
466 SourceRange getTypeofParensRange() const { return TypeofParensRange; }
467 void setTypeofParensRange(SourceRange range) { TypeofParensRange = range; }
469 /// getSpecifierName - Turn a type-specifier-type into a string like "_Bool"
471 static const char *getSpecifierName(DeclSpec::TST T);
472 static const char *getSpecifierName(DeclSpec::TQ Q);
473 static const char *getSpecifierName(DeclSpec::TSS S);
474 static const char *getSpecifierName(DeclSpec::TSC C);
475 static const char *getSpecifierName(DeclSpec::TSW W);
476 static const char *getSpecifierName(DeclSpec::SCS S);
480 /// getTypeQualifiers - Return a set of TQs.
481 unsigned getTypeQualifiers() const { return TypeQualifiers; }
482 SourceLocation getConstSpecLoc() const { return TQ_constLoc; }
483 SourceLocation getRestrictSpecLoc() const { return TQ_restrictLoc; }
484 SourceLocation getVolatileSpecLoc() const { return TQ_volatileLoc; }
486 /// \brief Clear out all of the type qualifiers.
487 void ClearTypeQualifiers() {
489 TQ_constLoc = SourceLocation();
490 TQ_restrictLoc = SourceLocation();
491 TQ_volatileLoc = SourceLocation();
494 // function-specifier
495 bool isInlineSpecified() const { return FS_inline_specified; }
496 SourceLocation getInlineSpecLoc() const { return FS_inlineLoc; }
498 bool isVirtualSpecified() const { return FS_virtual_specified; }
499 SourceLocation getVirtualSpecLoc() const { return FS_virtualLoc; }
501 bool isExplicitSpecified() const { return FS_explicit_specified; }
502 SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; }
504 void ClearFunctionSpecs() {
505 FS_inline_specified = false;
506 FS_inlineLoc = SourceLocation();
507 FS_virtual_specified = false;
508 FS_virtualLoc = SourceLocation();
509 FS_explicit_specified = false;
510 FS_explicitLoc = SourceLocation();
513 /// hasTypeSpecifier - Return true if any type-specifier has been found.
514 bool hasTypeSpecifier() const {
515 return getTypeSpecType() != DeclSpec::TST_unspecified ||
516 getTypeSpecWidth() != DeclSpec::TSW_unspecified ||
517 getTypeSpecComplex() != DeclSpec::TSC_unspecified ||
518 getTypeSpecSign() != DeclSpec::TSS_unspecified;
521 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this
522 /// DeclSpec includes.
524 unsigned getParsedSpecifiers() const;
526 SCS getStorageClassSpecAsWritten() const {
527 return (SCS)StorageClassSpecAsWritten;
530 /// isEmpty - Return true if this declaration specifier is completely empty:
531 /// no tokens were parsed in the production of it.
532 bool isEmpty() const {
533 return getParsedSpecifiers() == DeclSpec::PQ_None;
536 void SetRangeStart(SourceLocation Loc) { Range.setBegin(Loc); }
537 void SetRangeEnd(SourceLocation Loc) { Range.setEnd(Loc); }
539 /// These methods set the specified attribute of the DeclSpec and
540 /// return false if there was no error. If an error occurs (for
541 /// example, if we tried to set "auto" on a spec with "extern"
542 /// already set), they return true and set PrevSpec and DiagID
544 /// Diag(Loc, DiagID) << PrevSpec;
545 /// will yield a useful result.
547 /// TODO: use a more general approach that still allows these
548 /// diagnostics to be ignored when desired.
549 bool SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc,
550 const char *&PrevSpec, unsigned &DiagID);
551 bool SetStorageClassSpecThread(SourceLocation Loc, const char *&PrevSpec,
553 bool SetTypeSpecWidth(TSW W, SourceLocation Loc, const char *&PrevSpec,
555 bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec,
557 bool SetTypeSpecSign(TSS S, SourceLocation Loc, const char *&PrevSpec,
559 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
561 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
562 unsigned &DiagID, ParsedType Rep);
563 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
564 unsigned &DiagID, Decl *Rep, bool Owned);
565 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
566 SourceLocation TagNameLoc, const char *&PrevSpec,
567 unsigned &DiagID, ParsedType Rep);
568 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc,
569 SourceLocation TagNameLoc, const char *&PrevSpec,
570 unsigned &DiagID, Decl *Rep, bool Owned);
572 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec,
573 unsigned &DiagID, Expr *Rep);
574 bool SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc,
575 const char *&PrevSpec, unsigned &DiagID);
576 bool SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc,
577 const char *&PrevSpec, unsigned &DiagID);
578 bool SetTypeSpecError();
579 void UpdateDeclRep(Decl *Rep) {
580 assert(isDeclRep((TST) TypeSpecType));
583 void UpdateTypeRep(ParsedType Rep) {
584 assert(isTypeRep((TST) TypeSpecType));
587 void UpdateExprRep(Expr *Rep) {
588 assert(isExprRep((TST) TypeSpecType));
592 bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec,
593 unsigned &DiagID, const LangOptions &Lang);
595 bool SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec,
597 bool SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec,
599 bool SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec,
602 bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec,
604 bool setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec,
606 bool SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec,
609 bool isFriendSpecified() const { return Friend_specified; }
610 SourceLocation getFriendSpecLoc() const { return FriendLoc; }
612 bool isModulePrivateSpecified() const { return ModulePrivateLoc.isValid(); }
613 SourceLocation getModulePrivateSpecLoc() const { return ModulePrivateLoc; }
615 bool isConstexprSpecified() const { return Constexpr_specified; }
616 SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; }
618 void ClearConstexprSpec() {
619 Constexpr_specified = false;
620 ConstexprLoc = SourceLocation();
623 AttributePool &getAttributePool() const {
624 return Attrs.getPool();
627 /// AddAttributes - contatenates two attribute lists.
628 /// The GCC attribute syntax allows for the following:
630 /// short __attribute__(( unused, deprecated ))
631 /// int __attribute__(( may_alias, aligned(16) )) var;
633 /// This declares 4 attributes using 2 lists. The following syntax is
634 /// also allowed and equivalent to the previous declaration.
636 /// short __attribute__((unused)) __attribute__((deprecated))
637 /// int __attribute__((may_alias)) __attribute__((aligned(16))) var;
639 void addAttributes(AttributeList *AL) {
642 void setAttributes(AttributeList *AL) {
646 bool hasAttributes() const { return !Attrs.empty(); }
648 ParsedAttributes &getAttributes() { return Attrs; }
649 const ParsedAttributes &getAttributes() const { return Attrs; }
651 /// TakeAttributes - Return the current attribute list and remove them from
652 /// the DeclSpec so that it doesn't own them.
653 ParsedAttributes takeAttributes() {
654 // The non-const "copy" constructor clears the operand automatically.
658 void takeAttributesFrom(ParsedAttributes &attrs) {
659 Attrs.takeAllFrom(attrs);
662 typedef Decl * const *ProtocolQualifierListTy;
663 ProtocolQualifierListTy getProtocolQualifiers() const {
664 return ProtocolQualifiers;
666 SourceLocation *getProtocolLocs() const { return ProtocolLocs; }
667 unsigned getNumProtocolQualifiers() const {
668 return NumProtocolQualifiers;
670 SourceLocation getProtocolLAngleLoc() const { return ProtocolLAngleLoc; }
671 void setProtocolQualifiers(Decl * const *Protos, unsigned NP,
672 SourceLocation *ProtoLocs,
673 SourceLocation LAngleLoc);
675 /// Finish - This does final analysis of the declspec, issuing diagnostics for
676 /// things like "_Imaginary" (lacking an FP type). After calling this method,
677 /// DeclSpec is guaranteed self-consistent, even if an error occurred.
678 void Finish(DiagnosticsEngine &D, Preprocessor &PP);
680 const WrittenBuiltinSpecs& getWrittenBuiltinSpecs() const {
684 ObjCDeclSpec *getObjCQualifiers() const { return ObjCQualifiers; }
685 void setObjCQualifiers(ObjCDeclSpec *quals) { ObjCQualifiers = quals; }
687 /// isMissingDeclaratorOk - This checks if this DeclSpec can stand alone,
688 /// without a Declarator. Only tag declspecs can stand alone.
689 bool isMissingDeclaratorOk();
692 /// ObjCDeclSpec - This class captures information about
693 /// "declaration specifiers" specific to objective-c
696 /// ObjCDeclQualifier - Qualifier used on types in method
697 /// declarations. Not all combinations are sensible. Parameters
698 /// can be one of { in, out, inout } with one of { bycopy, byref }.
699 /// Returns can either be { oneway } or not.
701 /// This should be kept in sync with Decl::ObjCDeclQualifier.
702 enum ObjCDeclQualifier {
712 /// PropertyAttributeKind - list of property attributes.
713 enum ObjCPropertyAttributeKind {
715 DQ_PR_readonly = 0x01,
718 DQ_PR_readwrite = 0x08,
721 DQ_PR_nonatomic = 0x40,
723 DQ_PR_atomic = 0x100,
725 DQ_PR_strong = 0x400,
726 DQ_PR_unsafe_unretained = 0x800
731 : objcDeclQualifier(DQ_None), PropertyAttributes(DQ_PR_noattr),
732 GetterName(0), SetterName(0) { }
733 ObjCDeclQualifier getObjCDeclQualifier() const { return objcDeclQualifier; }
734 void setObjCDeclQualifier(ObjCDeclQualifier DQVal) {
735 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal);
738 ObjCPropertyAttributeKind getPropertyAttributes() const {
739 return ObjCPropertyAttributeKind(PropertyAttributes);
741 void setPropertyAttributes(ObjCPropertyAttributeKind PRVal) {
743 (ObjCPropertyAttributeKind)(PropertyAttributes | PRVal);
746 const IdentifierInfo *getGetterName() const { return GetterName; }
747 IdentifierInfo *getGetterName() { return GetterName; }
748 void setGetterName(IdentifierInfo *name) { GetterName = name; }
750 const IdentifierInfo *getSetterName() const { return SetterName; }
751 IdentifierInfo *getSetterName() { return SetterName; }
752 void setSetterName(IdentifierInfo *name) { SetterName = name; }
755 // FIXME: These two are unrelated and mutially exclusive. So perhaps
756 // we can put them in a union to reflect their mutual exclusiveness
757 // (space saving is negligible).
758 ObjCDeclQualifier objcDeclQualifier : 6;
760 // NOTE: VC++ treats enums as signed, avoid using ObjCPropertyAttributeKind
761 unsigned PropertyAttributes : 12;
762 IdentifierInfo *GetterName; // getter name of NULL if no getter
763 IdentifierInfo *SetterName; // setter name of NULL if no setter
766 /// \brief Represents a C++ unqualified-id that has been parsed.
767 class UnqualifiedId {
769 const UnqualifiedId &operator=(const UnqualifiedId &); // DO NOT IMPLEMENT
772 /// \brief Describes the kind of unqualified-id parsed.
774 /// \brief An identifier.
776 /// \brief An overloaded operator name, e.g., operator+.
777 IK_OperatorFunctionId,
778 /// \brief A conversion function name, e.g., operator int.
779 IK_ConversionFunctionId,
780 /// \brief A user-defined literal name, e.g., operator "" _i.
781 IK_LiteralOperatorId,
782 /// \brief A constructor name.
784 /// \brief A constructor named via a template-id.
785 IK_ConstructorTemplateId,
786 /// \brief A destructor name.
788 /// \brief A template-id, e.g., f<int>.
790 /// \brief An implicit 'self' parameter
794 /// \brief Anonymous union that holds extra data associated with the
795 /// parsed unqualified-id.
797 /// \brief When Kind == IK_Identifier, the parsed identifier, or when Kind
798 /// == IK_UserLiteralId, the identifier suffix.
799 IdentifierInfo *Identifier;
801 /// \brief When Kind == IK_OperatorFunctionId, the overloaded operator
804 /// \brief The kind of overloaded operator.
805 OverloadedOperatorKind Operator;
807 /// \brief The source locations of the individual tokens that name
808 /// the operator, e.g., the "new", "[", and "]" tokens in
811 /// Different operators have different numbers of tokens in their name,
812 /// up to three. Any remaining source locations in this array will be
813 /// set to an invalid value for operators with fewer than three tokens.
814 unsigned SymbolLocations[3];
815 } OperatorFunctionId;
817 /// \brief When Kind == IK_ConversionFunctionId, the type that the
818 /// conversion function names.
819 UnionParsedType ConversionFunctionId;
821 /// \brief When Kind == IK_ConstructorName, the class-name of the type
822 /// whose constructor is being referenced.
823 UnionParsedType ConstructorName;
825 /// \brief When Kind == IK_DestructorName, the type referred to by the
827 UnionParsedType DestructorName;
829 /// \brief When Kind == IK_TemplateId or IK_ConstructorTemplateId,
830 /// the template-id annotation that contains the template name and
831 /// template arguments.
832 TemplateIdAnnotation *TemplateId;
835 /// \brief The location of the first token that describes this unqualified-id,
836 /// which will be the location of the identifier, "operator" keyword,
837 /// tilde (for a destructor), or the template name of a template-id.
838 SourceLocation StartLocation;
840 /// \brief The location of the last token that describes this unqualified-id.
841 SourceLocation EndLocation;
843 UnqualifiedId() : Kind(IK_Identifier), Identifier(0) { }
845 /// \brief Do not use this copy constructor. It is temporary, and only
846 /// exists because we are holding FieldDeclarators in a SmallVector when we
847 /// don't actually need them.
849 /// FIXME: Kill this copy constructor.
850 UnqualifiedId(const UnqualifiedId &Other)
851 : Kind(IK_Identifier), Identifier(Other.Identifier),
852 StartLocation(Other.StartLocation), EndLocation(Other.EndLocation) {
853 assert(Other.Kind == IK_Identifier && "Cannot copy non-identifiers");
856 /// \brief Destroy this unqualified-id.
857 ~UnqualifiedId() { clear(); }
859 /// \brief Clear out this unqualified-id, setting it to default (invalid)
863 /// \brief Determine whether this unqualified-id refers to a valid name.
864 bool isValid() const { return StartLocation.isValid(); }
866 /// \brief Determine whether this unqualified-id refers to an invalid name.
867 bool isInvalid() const { return !isValid(); }
869 /// \brief Determine what kind of name we have.
870 IdKind getKind() const { return Kind; }
871 void setKind(IdKind kind) { Kind = kind; }
873 /// \brief Specify that this unqualified-id was parsed as an identifier.
875 /// \param Id the parsed identifier.
876 /// \param IdLoc the location of the parsed identifier.
877 void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) {
878 Kind = IK_Identifier;
879 Identifier = const_cast<IdentifierInfo *>(Id);
880 StartLocation = EndLocation = IdLoc;
883 /// \brief Specify that this unqualified-id was parsed as an
884 /// operator-function-id.
886 /// \param OperatorLoc the location of the 'operator' keyword.
888 /// \param Op the overloaded operator.
890 /// \param SymbolLocations the locations of the individual operator symbols
892 void setOperatorFunctionId(SourceLocation OperatorLoc,
893 OverloadedOperatorKind Op,
894 SourceLocation SymbolLocations[3]);
896 /// \brief Specify that this unqualified-id was parsed as a
897 /// conversion-function-id.
899 /// \param OperatorLoc the location of the 'operator' keyword.
901 /// \param Ty the type to which this conversion function is converting.
903 /// \param EndLoc the location of the last token that makes up the type name.
904 void setConversionFunctionId(SourceLocation OperatorLoc,
906 SourceLocation EndLoc) {
907 Kind = IK_ConversionFunctionId;
908 StartLocation = OperatorLoc;
909 EndLocation = EndLoc;
910 ConversionFunctionId = Ty;
913 /// \brief Specific that this unqualified-id was parsed as a
914 /// literal-operator-id.
916 /// \param Id the parsed identifier.
918 /// \param OpLoc the location of the 'operator' keyword.
920 /// \param IdLoc the location of the identifier.
921 void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc,
922 SourceLocation IdLoc) {
923 Kind = IK_LiteralOperatorId;
924 Identifier = const_cast<IdentifierInfo *>(Id);
925 StartLocation = OpLoc;
929 /// \brief Specify that this unqualified-id was parsed as a constructor name.
931 /// \param ClassType the class type referred to by the constructor name.
933 /// \param ClassNameLoc the location of the class name.
935 /// \param EndLoc the location of the last token that makes up the type name.
936 void setConstructorName(ParsedType ClassType,
937 SourceLocation ClassNameLoc,
938 SourceLocation EndLoc) {
939 Kind = IK_ConstructorName;
940 StartLocation = ClassNameLoc;
941 EndLocation = EndLoc;
942 ConstructorName = ClassType;
945 /// \brief Specify that this unqualified-id was parsed as a
946 /// template-id that names a constructor.
948 /// \param TemplateId the template-id annotation that describes the parsed
949 /// template-id. This UnqualifiedId instance will take ownership of the
950 /// \p TemplateId and will free it on destruction.
951 void setConstructorTemplateId(TemplateIdAnnotation *TemplateId);
953 /// \brief Specify that this unqualified-id was parsed as a destructor name.
955 /// \param TildeLoc the location of the '~' that introduces the destructor
958 /// \param ClassType the name of the class referred to by the destructor name.
959 void setDestructorName(SourceLocation TildeLoc,
960 ParsedType ClassType,
961 SourceLocation EndLoc) {
962 Kind = IK_DestructorName;
963 StartLocation = TildeLoc;
964 EndLocation = EndLoc;
965 DestructorName = ClassType;
968 /// \brief Specify that this unqualified-id was parsed as a template-id.
970 /// \param TemplateId the template-id annotation that describes the parsed
971 /// template-id. This UnqualifiedId instance will take ownership of the
972 /// \p TemplateId and will free it on destruction.
973 void setTemplateId(TemplateIdAnnotation *TemplateId);
975 /// \brief Return the source range that covers this unqualified-id.
976 SourceRange getSourceRange() const LLVM_READONLY {
977 return SourceRange(StartLocation, EndLocation);
979 SourceLocation getLocStart() const LLVM_READONLY { return StartLocation; }
980 SourceLocation getLocEnd() const LLVM_READONLY { return EndLocation; }
983 /// CachedTokens - A set of tokens that has been cached for later
985 typedef SmallVector<Token, 4> CachedTokens;
987 /// DeclaratorChunk - One instance of this struct is used for each type in a
988 /// declarator that is parsed.
990 /// This is intended to be a small value object.
991 struct DeclaratorChunk {
993 Pointer, Reference, Array, Function, BlockPointer, MemberPointer, Paren
996 /// Loc - The place where this type was defined.
998 /// EndLoc - If valid, the place where this chunck ends.
999 SourceLocation EndLoc;
1001 struct TypeInfoCommon {
1002 AttributeList *AttrList;
1005 struct PointerTypeInfo : TypeInfoCommon {
1006 /// The type qualifiers: const/volatile/restrict.
1007 unsigned TypeQuals : 3;
1009 /// The location of the const-qualifier, if any.
1010 unsigned ConstQualLoc;
1012 /// The location of the volatile-qualifier, if any.
1013 unsigned VolatileQualLoc;
1015 /// The location of the restrict-qualifier, if any.
1016 unsigned RestrictQualLoc;
1022 struct ReferenceTypeInfo : TypeInfoCommon {
1023 /// The type qualifier: restrict. [GNU] C++ extension
1024 bool HasRestrict : 1;
1025 /// True if this is an lvalue reference, false if it's an rvalue reference.
1031 struct ArrayTypeInfo : TypeInfoCommon {
1032 /// The type qualifiers for the array: const/volatile/restrict.
1033 unsigned TypeQuals : 3;
1035 /// True if this dimension included the 'static' keyword.
1038 /// True if this dimension was [*]. In this case, NumElts is null.
1041 /// This is the size of the array, or null if [] or [*] was specified.
1042 /// Since the parser is multi-purpose, and we don't want to impose a root
1043 /// expression class on all clients, NumElts is untyped.
1049 /// ParamInfo - An array of paraminfo objects is allocated whenever a function
1050 /// declarator is parsed. There are two interesting styles of arguments here:
1051 /// K&R-style identifier lists and parameter type lists. K&R-style identifier
1052 /// lists will have information about the identifier, but no type information.
1053 /// Parameter type lists will have type info (if the actions module provides
1054 /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'.
1056 IdentifierInfo *Ident;
1057 SourceLocation IdentLoc;
1060 /// DefaultArgTokens - When the parameter's default argument
1061 /// cannot be parsed immediately (because it occurs within the
1062 /// declaration of a member function), it will be stored here as a
1063 /// sequence of tokens to be parsed once the class definition is
1064 /// complete. Non-NULL indicates that there is a default argument.
1065 CachedTokens *DefaultArgTokens;
1068 ParamInfo(IdentifierInfo *ident, SourceLocation iloc,
1070 CachedTokens *DefArgTokens = 0)
1071 : Ident(ident), IdentLoc(iloc), Param(param),
1072 DefaultArgTokens(DefArgTokens) {}
1075 struct TypeAndRange {
1080 struct FunctionTypeInfo : TypeInfoCommon {
1081 /// hasPrototype - This is true if the function had at least one typed
1082 /// argument. If the function is () or (a,b,c), then it has no prototype,
1083 /// and is treated as a K&R-style function.
1084 unsigned hasPrototype : 1;
1086 /// isVariadic - If this function has a prototype, and if that
1087 /// proto ends with ',...)', this is true. When true, EllipsisLoc
1088 /// contains the location of the ellipsis.
1089 unsigned isVariadic : 1;
1091 /// \brief Whether the ref-qualifier (if any) is an lvalue reference.
1092 /// Otherwise, it's an rvalue reference.
1093 unsigned RefQualifierIsLValueRef : 1;
1095 /// The type qualifiers: const/volatile/restrict.
1096 /// The qualifier bitmask values are the same as in QualType.
1097 unsigned TypeQuals : 3;
1099 /// ExceptionSpecType - An ExceptionSpecificationType value.
1100 unsigned ExceptionSpecType : 3;
1102 /// DeleteArgInfo - If this is true, we need to delete[] ArgInfo.
1103 unsigned DeleteArgInfo : 1;
1105 /// When isVariadic is true, the location of the ellipsis in the source.
1106 unsigned EllipsisLoc;
1108 /// NumArgs - This is the number of formal arguments provided for the
1112 /// NumExceptions - This is the number of types in the dynamic-exception-
1113 /// decl, if the function has one.
1114 unsigned NumExceptions;
1116 /// \brief The location of the ref-qualifier, if any.
1118 /// If this is an invalid location, there is no ref-qualifier.
1119 unsigned RefQualifierLoc;
1121 /// \brief The location of the const-qualifier, if any.
1123 /// If this is an invalid location, there is no const-qualifier.
1124 unsigned ConstQualifierLoc;
1126 /// \brief The location of the volatile-qualifier, if any.
1128 /// If this is an invalid location, there is no volatile-qualifier.
1129 unsigned VolatileQualifierLoc;
1131 /// \brief The location of the 'mutable' qualifer in a lambda-declarator, if
1133 unsigned MutableLoc;
1135 /// \brief When ExceptionSpecType isn't EST_None or EST_Delayed, the
1136 /// location of the keyword introducing the spec.
1137 unsigned ExceptionSpecLoc;
1139 /// ArgInfo - This is a pointer to a new[]'d array of ParamInfo objects that
1140 /// describe the arguments for this function declarator. This is null if
1141 /// there are no arguments specified.
1145 /// \brief Pointer to a new[]'d array of TypeAndRange objects that
1146 /// contain the types in the function's dynamic exception specification
1147 /// and their locations, if there is one.
1148 TypeAndRange *Exceptions;
1150 /// \brief Pointer to the expression in the noexcept-specifier of this
1151 /// function, if it has one.
1155 /// TrailingReturnType - If this isn't null, it's the trailing return type
1156 /// specified. This is actually a ParsedType, but stored as void* to
1157 /// allow union storage.
1158 void *TrailingReturnType;
1160 /// freeArgs - reset the argument list to having zero arguments. This is
1161 /// used in various places for error recovery.
1163 if (DeleteArgInfo) {
1165 DeleteArgInfo = false;
1173 if (getExceptionSpecType() == EST_Dynamic)
1174 delete[] Exceptions;
1177 /// isKNRPrototype - Return true if this is a K&R style identifier list,
1178 /// like "void foo(a,b,c)". In a function definition, this will be followed
1179 /// by the argument type definitions.
1180 bool isKNRPrototype() const {
1181 return !hasPrototype && NumArgs != 0;
1184 SourceLocation getEllipsisLoc() const {
1185 return SourceLocation::getFromRawEncoding(EllipsisLoc);
1187 SourceLocation getExceptionSpecLoc() const {
1188 return SourceLocation::getFromRawEncoding(ExceptionSpecLoc);
1191 /// \brief Retrieve the location of the ref-qualifier, if any.
1192 SourceLocation getRefQualifierLoc() const {
1193 return SourceLocation::getFromRawEncoding(RefQualifierLoc);
1196 /// \brief Retrieve the location of the ref-qualifier, if any.
1197 SourceLocation getConstQualifierLoc() const {
1198 return SourceLocation::getFromRawEncoding(ConstQualifierLoc);
1201 /// \brief Retrieve the location of the ref-qualifier, if any.
1202 SourceLocation getVolatileQualifierLoc() const {
1203 return SourceLocation::getFromRawEncoding(VolatileQualifierLoc);
1206 /// \brief Retrieve the location of the 'mutable' qualifier, if any.
1207 SourceLocation getMutableLoc() const {
1208 return SourceLocation::getFromRawEncoding(MutableLoc);
1211 /// \brief Determine whether this function declaration contains a
1213 bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); }
1215 /// \brief Determine whether this lambda-declarator contains a 'mutable'
1217 bool hasMutableQualifier() const { return getMutableLoc().isValid(); }
1219 /// \brief Get the type of exception specification this function has.
1220 ExceptionSpecificationType getExceptionSpecType() const {
1221 return static_cast<ExceptionSpecificationType>(ExceptionSpecType);
1225 struct BlockPointerTypeInfo : TypeInfoCommon {
1226 /// For now, sema will catch these as invalid.
1227 /// The type qualifiers: const/volatile/restrict.
1228 unsigned TypeQuals : 3;
1234 struct MemberPointerTypeInfo : TypeInfoCommon {
1235 /// The type qualifiers: const/volatile/restrict.
1236 unsigned TypeQuals : 3;
1237 // CXXScopeSpec has a constructor, so it can't be a direct member.
1238 // So we need some pointer-aligned storage and a bit of trickery.
1241 char Mem[sizeof(CXXScopeSpec)];
1243 CXXScopeSpec &Scope() {
1244 return *reinterpret_cast<CXXScopeSpec*>(ScopeMem.Mem);
1246 const CXXScopeSpec &Scope() const {
1247 return *reinterpret_cast<const CXXScopeSpec*>(ScopeMem.Mem);
1250 Scope().~CXXScopeSpec();
1255 TypeInfoCommon Common;
1256 PointerTypeInfo Ptr;
1257 ReferenceTypeInfo Ref;
1259 FunctionTypeInfo Fun;
1260 BlockPointerTypeInfo Cls;
1261 MemberPointerTypeInfo Mem;
1266 case DeclaratorChunk::Function: return Fun.destroy();
1267 case DeclaratorChunk::Pointer: return Ptr.destroy();
1268 case DeclaratorChunk::BlockPointer: return Cls.destroy();
1269 case DeclaratorChunk::Reference: return Ref.destroy();
1270 case DeclaratorChunk::Array: return Arr.destroy();
1271 case DeclaratorChunk::MemberPointer: return Mem.destroy();
1272 case DeclaratorChunk::Paren: return;
1276 /// getAttrs - If there are attributes applied to this declaratorchunk, return
1278 const AttributeList *getAttrs() const {
1279 return Common.AttrList;
1282 AttributeList *&getAttrListRef() {
1283 return Common.AttrList;
1286 /// getPointer - Return a DeclaratorChunk for a pointer.
1288 static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc,
1289 SourceLocation ConstQualLoc,
1290 SourceLocation VolatileQualLoc,
1291 SourceLocation RestrictQualLoc) {
1295 I.Ptr.TypeQuals = TypeQuals;
1296 I.Ptr.ConstQualLoc = ConstQualLoc.getRawEncoding();
1297 I.Ptr.VolatileQualLoc = VolatileQualLoc.getRawEncoding();
1298 I.Ptr.RestrictQualLoc = RestrictQualLoc.getRawEncoding();
1303 /// getReference - Return a DeclaratorChunk for a reference.
1305 static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc,
1310 I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0;
1311 I.Ref.LValueRef = lvalue;
1316 /// getArray - Return a DeclaratorChunk for an array.
1318 static DeclaratorChunk getArray(unsigned TypeQuals,
1319 bool isStatic, bool isStar, Expr *NumElts,
1320 SourceLocation LBLoc, SourceLocation RBLoc) {
1326 I.Arr.TypeQuals = TypeQuals;
1327 I.Arr.hasStatic = isStatic;
1328 I.Arr.isStar = isStar;
1329 I.Arr.NumElts = NumElts;
1333 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function.
1334 /// "TheDeclarator" is the declarator that this will be added to.
1335 static DeclaratorChunk getFunction(bool hasProto, bool isVariadic,
1336 SourceLocation EllipsisLoc,
1337 ParamInfo *ArgInfo, unsigned NumArgs,
1339 bool RefQualifierIsLvalueRef,
1340 SourceLocation RefQualifierLoc,
1341 SourceLocation ConstQualifierLoc,
1342 SourceLocation VolatileQualifierLoc,
1343 SourceLocation MutableLoc,
1344 ExceptionSpecificationType ESpecType,
1345 SourceLocation ESpecLoc,
1346 ParsedType *Exceptions,
1347 SourceRange *ExceptionRanges,
1348 unsigned NumExceptions,
1350 SourceLocation LocalRangeBegin,
1351 SourceLocation LocalRangeEnd,
1352 Declarator &TheDeclarator,
1353 ParsedType TrailingReturnType =
1356 /// getBlockPointer - Return a DeclaratorChunk for a block.
1358 static DeclaratorChunk getBlockPointer(unsigned TypeQuals,
1359 SourceLocation Loc) {
1361 I.Kind = BlockPointer;
1363 I.Cls.TypeQuals = TypeQuals;
1368 static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS,
1370 SourceLocation Loc) {
1372 I.Kind = MemberPointer;
1374 I.Mem.TypeQuals = TypeQuals;
1376 new (I.Mem.ScopeMem.Mem) CXXScopeSpec(SS);
1380 /// getParen - Return a DeclaratorChunk for a paren.
1382 static DeclaratorChunk getParen(SourceLocation LParenLoc,
1383 SourceLocation RParenLoc) {
1387 I.EndLoc = RParenLoc;
1388 I.Common.AttrList = 0;
1394 /// \brief Described the kind of function definition (if any) provided for
1396 enum FunctionDefinitionKind {
1403 /// Declarator - Information about one declarator, including the parsed type
1404 /// information and the identifier. When the declarator is fully formed, this
1405 /// is turned into the appropriate Decl object.
1407 /// Declarators come in two types: normal declarators and abstract declarators.
1408 /// Abstract declarators are used when parsing types, and don't have an
1409 /// identifier. Normal declarators do have ID's.
1411 /// Instances of this class should be a transient object that lives on the
1412 /// stack, not objects that are allocated in large quantities on the heap.
1416 FileContext, // File scope declaration.
1417 PrototypeContext, // Within a function prototype.
1418 ObjCResultContext, // An ObjC method result type.
1419 ObjCParameterContext,// An ObjC method parameter type.
1420 KNRTypeListContext, // K&R type definition list for formals.
1421 TypeNameContext, // Abstract declarator for types.
1422 MemberContext, // Struct/Union field.
1423 BlockContext, // Declaration within a block in a function.
1424 ForContext, // Declaration within first part of a for loop.
1425 ConditionContext, // Condition declaration in a C++ if/switch/while/for.
1426 TemplateParamContext,// Within a template parameter list.
1427 CXXNewContext, // C++ new-expression.
1428 CXXCatchContext, // C++ catch exception-declaration
1429 ObjCCatchContext, // Objective-C catch exception-declaration
1430 BlockLiteralContext, // Block literal declarator.
1431 LambdaExprContext, // Lambda-expression declarator.
1432 TrailingReturnContext, // C++11 trailing-type-specifier.
1433 TemplateTypeArgContext, // Template type argument.
1434 AliasDeclContext, // C++11 alias-declaration.
1435 AliasTemplateContext // C++11 alias-declaration template.
1444 /// Context - Where we are parsing this declarator.
1448 /// DeclTypeInfo - This holds each type that the declarator includes as it is
1449 /// parsed. This is pushed from the identifier out, which means that element
1450 /// #0 will be the most closely bound to the identifier, and
1451 /// DeclTypeInfo.back() will be the least closely bound.
1452 SmallVector<DeclaratorChunk, 8> DeclTypeInfo;
1454 /// InvalidType - Set by Sema::GetTypeForDeclarator().
1455 bool InvalidType : 1;
1457 /// GroupingParens - Set by Parser::ParseParenDeclarator().
1458 bool GroupingParens : 1;
1460 /// FunctionDefinition - Is this Declarator for a function or member
1461 /// definition and, if so, what kind?
1463 /// Actually a FunctionDefinitionKind.
1464 unsigned FunctionDefinition : 2;
1466 // Redeclaration - Is this Declarator is a redeclaration.
1467 bool Redeclaration : 1;
1469 /// Attrs - Attributes.
1470 ParsedAttributes Attrs;
1472 /// AsmLabel - The asm label, if specified.
1475 /// InlineParams - This is a local array used for the first function decl
1476 /// chunk to avoid going to the heap for the common case when we have one
1477 /// function chunk in the declarator.
1478 DeclaratorChunk::ParamInfo InlineParams[16];
1479 bool InlineParamsUsed;
1481 /// Extension - true if the declaration is preceded by __extension__.
1484 /// \brief If this is the second or subsequent declarator in this declaration,
1485 /// the location of the comma before this declarator.
1486 SourceLocation CommaLoc;
1488 /// \brief If provided, the source location of the ellipsis used to describe
1489 /// this declarator as a parameter pack.
1490 SourceLocation EllipsisLoc;
1492 friend struct DeclaratorChunk;
1495 Declarator(const DeclSpec &ds, TheContext C)
1496 : DS(ds), Range(ds.getSourceRange()), Context(C),
1497 InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error),
1498 GroupingParens(false), FunctionDefinition(FDK_Declaration),
1499 Redeclaration(false),
1500 Attrs(ds.getAttributePool().getFactory()), AsmLabel(0),
1501 InlineParamsUsed(false), Extension(false) {
1508 /// getDeclSpec - Return the declaration-specifier that this declarator was
1510 const DeclSpec &getDeclSpec() const { return DS; }
1512 /// getMutableDeclSpec - Return a non-const version of the DeclSpec. This
1513 /// should be used with extreme care: declspecs can often be shared between
1514 /// multiple declarators, so mutating the DeclSpec affects all of the
1515 /// Declarators. This should only be done when the declspec is known to not
1516 /// be shared or when in error recovery etc.
1517 DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); }
1519 AttributePool &getAttributePool() const {
1520 return Attrs.getPool();
1523 /// getCXXScopeSpec - Return the C++ scope specifier (global scope or
1524 /// nested-name-specifier) that is part of the declarator-id.
1525 const CXXScopeSpec &getCXXScopeSpec() const { return SS; }
1526 CXXScopeSpec &getCXXScopeSpec() { return SS; }
1528 /// \brief Retrieve the name specified by this declarator.
1529 UnqualifiedId &getName() { return Name; }
1531 TheContext getContext() const { return Context; }
1533 bool isPrototypeContext() const {
1534 return (Context == PrototypeContext ||
1535 Context == ObjCParameterContext ||
1536 Context == ObjCResultContext);
1539 /// getSourceRange - Get the source range that spans this declarator.
1540 const SourceRange &getSourceRange() const LLVM_READONLY { return Range; }
1541 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); }
1542 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); }
1544 void SetSourceRange(SourceRange R) { Range = R; }
1545 /// SetRangeBegin - Set the start of the source range to Loc, unless it's
1547 void SetRangeBegin(SourceLocation Loc) {
1548 if (!Loc.isInvalid())
1549 Range.setBegin(Loc);
1551 /// SetRangeEnd - Set the end of the source range to Loc, unless it's invalid.
1552 void SetRangeEnd(SourceLocation Loc) {
1553 if (!Loc.isInvalid())
1556 /// ExtendWithDeclSpec - Extend the declarator source range to include the
1557 /// given declspec, unless its location is invalid. Adopts the range start if
1558 /// the current range start is invalid.
1559 void ExtendWithDeclSpec(const DeclSpec &DS) {
1560 const SourceRange &SR = DS.getSourceRange();
1561 if (Range.getBegin().isInvalid())
1562 Range.setBegin(SR.getBegin());
1563 if (!SR.getEnd().isInvalid())
1564 Range.setEnd(SR.getEnd());
1567 /// clear - Reset the contents of this Declarator.
1571 Range = DS.getSourceRange();
1573 for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i)
1574 DeclTypeInfo[i].destroy();
1575 DeclTypeInfo.clear();
1578 InlineParamsUsed = false;
1579 CommaLoc = SourceLocation();
1580 EllipsisLoc = SourceLocation();
1583 /// mayOmitIdentifier - Return true if the identifier is either optional or
1584 /// not allowed. This is true for typenames, prototypes, and template
1585 /// parameter lists.
1586 bool mayOmitIdentifier() const {
1589 case KNRTypeListContext:
1593 case ConditionContext:
1596 case TypeNameContext:
1597 case AliasDeclContext:
1598 case AliasTemplateContext:
1599 case PrototypeContext:
1600 case ObjCParameterContext:
1601 case ObjCResultContext:
1602 case TemplateParamContext:
1604 case CXXCatchContext:
1605 case ObjCCatchContext:
1606 case BlockLiteralContext:
1607 case LambdaExprContext:
1608 case TemplateTypeArgContext:
1609 case TrailingReturnContext:
1612 llvm_unreachable("unknown context kind!");
1615 /// mayHaveIdentifier - Return true if the identifier is either optional or
1616 /// required. This is true for normal declarators and prototypes, but not
1618 bool mayHaveIdentifier() const {
1621 case KNRTypeListContext:
1625 case ConditionContext:
1626 case PrototypeContext:
1627 case TemplateParamContext:
1628 case CXXCatchContext:
1629 case ObjCCatchContext:
1632 case TypeNameContext:
1634 case AliasDeclContext:
1635 case AliasTemplateContext:
1636 case ObjCParameterContext:
1637 case ObjCResultContext:
1638 case BlockLiteralContext:
1639 case LambdaExprContext:
1640 case TemplateTypeArgContext:
1641 case TrailingReturnContext:
1644 llvm_unreachable("unknown context kind!");
1647 /// mayBeFollowedByCXXDirectInit - Return true if the declarator can be
1648 /// followed by a C++ direct initializer, e.g. "int x(1);".
1649 bool mayBeFollowedByCXXDirectInit() const {
1650 if (hasGroupingParens()) return false;
1652 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef)
1655 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern &&
1656 Context != FileContext)
1659 // Special names can't have direct initializers.
1660 if (Name.getKind() != UnqualifiedId::IK_Identifier)
1669 case ConditionContext:
1670 // This may not be followed by a direct initializer, but it can't be a
1671 // function declaration either, and we'd prefer to perform a tentative
1672 // parse in order to produce the right diagnostic.
1675 case KNRTypeListContext:
1677 case PrototypeContext:
1678 case ObjCParameterContext:
1679 case ObjCResultContext:
1680 case TemplateParamContext:
1681 case CXXCatchContext:
1682 case ObjCCatchContext:
1683 case TypeNameContext:
1685 case AliasDeclContext:
1686 case AliasTemplateContext:
1687 case BlockLiteralContext:
1688 case LambdaExprContext:
1689 case TemplateTypeArgContext:
1690 case TrailingReturnContext:
1693 llvm_unreachable("unknown context kind!");
1696 /// isPastIdentifier - Return true if we have parsed beyond the point where
1698 bool isPastIdentifier() const { return Name.isValid(); }
1700 /// hasName - Whether this declarator has a name, which might be an
1701 /// identifier (accessible via getIdentifier()) or some kind of
1702 /// special C++ name (constructor, destructor, etc.).
1703 bool hasName() const {
1704 return Name.getKind() != UnqualifiedId::IK_Identifier || Name.Identifier;
1707 IdentifierInfo *getIdentifier() const {
1708 if (Name.getKind() == UnqualifiedId::IK_Identifier)
1709 return Name.Identifier;
1713 SourceLocation getIdentifierLoc() const { return Name.StartLocation; }
1715 /// \brief Set the name of this declarator to be the given identifier.
1716 void SetIdentifier(IdentifierInfo *Id, SourceLocation IdLoc) {
1717 Name.setIdentifier(Id, IdLoc);
1720 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to
1721 /// EndLoc, which should be the last token of the chunk.
1722 void AddTypeInfo(const DeclaratorChunk &TI,
1723 ParsedAttributes &attrs,
1724 SourceLocation EndLoc) {
1725 DeclTypeInfo.push_back(TI);
1726 DeclTypeInfo.back().getAttrListRef() = attrs.getList();
1727 getAttributePool().takeAllFrom(attrs.getPool());
1729 if (!EndLoc.isInvalid())
1730 SetRangeEnd(EndLoc);
1733 /// AddInnermostTypeInfo - Add a new innermost chunk to this declarator.
1734 void AddInnermostTypeInfo(const DeclaratorChunk &TI) {
1735 DeclTypeInfo.insert(DeclTypeInfo.begin(), TI);
1738 /// getNumTypeObjects() - Return the number of types applied to this
1740 unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); }
1742 /// Return the specified TypeInfo from this declarator. TypeInfo #0 is
1743 /// closest to the identifier.
1744 const DeclaratorChunk &getTypeObject(unsigned i) const {
1745 assert(i < DeclTypeInfo.size() && "Invalid type chunk");
1746 return DeclTypeInfo[i];
1748 DeclaratorChunk &getTypeObject(unsigned i) {
1749 assert(i < DeclTypeInfo.size() && "Invalid type chunk");
1750 return DeclTypeInfo[i];
1753 void DropFirstTypeObject()
1755 assert(!DeclTypeInfo.empty() && "No type chunks to drop.");
1756 DeclTypeInfo.front().destroy();
1757 DeclTypeInfo.erase(DeclTypeInfo.begin());
1760 /// isArrayOfUnknownBound - This method returns true if the declarator
1761 /// is a declarator for an array of unknown bound (looking through
1763 bool isArrayOfUnknownBound() const {
1764 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
1765 switch (DeclTypeInfo[i].Kind) {
1766 case DeclaratorChunk::Paren:
1768 case DeclaratorChunk::Function:
1769 case DeclaratorChunk::Pointer:
1770 case DeclaratorChunk::Reference:
1771 case DeclaratorChunk::BlockPointer:
1772 case DeclaratorChunk::MemberPointer:
1774 case DeclaratorChunk::Array:
1775 return !DeclTypeInfo[i].Arr.NumElts;
1777 llvm_unreachable("Invalid type chunk");
1782 /// isFunctionDeclarator - This method returns true if the declarator
1783 /// is a function declarator (looking through parentheses).
1784 /// If true is returned, then the reference type parameter idx is
1785 /// assigned with the index of the declaration chunk.
1786 bool isFunctionDeclarator(unsigned& idx) const {
1787 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) {
1788 switch (DeclTypeInfo[i].Kind) {
1789 case DeclaratorChunk::Function:
1792 case DeclaratorChunk::Paren:
1794 case DeclaratorChunk::Pointer:
1795 case DeclaratorChunk::Reference:
1796 case DeclaratorChunk::Array:
1797 case DeclaratorChunk::BlockPointer:
1798 case DeclaratorChunk::MemberPointer:
1801 llvm_unreachable("Invalid type chunk");
1806 /// isFunctionDeclarator - Once this declarator is fully parsed and formed,
1807 /// this method returns true if the identifier is a function declarator
1808 /// (looking through parentheses).
1809 bool isFunctionDeclarator() const {
1811 return isFunctionDeclarator(index);
1814 /// getFunctionTypeInfo - Retrieves the function type info object
1815 /// (looking through parentheses).
1816 DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() {
1817 assert(isFunctionDeclarator() && "Not a function declarator!");
1819 isFunctionDeclarator(index);
1820 return DeclTypeInfo[index].Fun;
1823 /// getFunctionTypeInfo - Retrieves the function type info object
1824 /// (looking through parentheses).
1825 const DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() const {
1826 return const_cast<Declarator*>(this)->getFunctionTypeInfo();
1829 /// \brief Determine whether the declaration that will be produced from
1830 /// this declaration will be a function.
1832 /// A declaration can declare a function even if the declarator itself
1833 /// isn't a function declarator, if the type specifier refers to a function
1834 /// type. This routine checks for both cases.
1835 bool isDeclarationOfFunction() const;
1837 /// takeAttributes - Takes attributes from the given parsed-attributes
1838 /// set and add them to this declarator.
1840 /// These examples both add 3 attributes to "var":
1841 /// short int var __attribute__((aligned(16),common,deprecated));
1842 /// short int x, __attribute__((aligned(16)) var
1843 /// __attribute__((common,deprecated));
1845 /// Also extends the range of the declarator.
1846 void takeAttributes(ParsedAttributes &attrs, SourceLocation lastLoc) {
1847 Attrs.takeAllFrom(attrs);
1849 if (!lastLoc.isInvalid())
1850 SetRangeEnd(lastLoc);
1853 const AttributeList *getAttributes() const { return Attrs.getList(); }
1854 AttributeList *getAttributes() { return Attrs.getList(); }
1856 AttributeList *&getAttrListRef() { return Attrs.getListRef(); }
1858 /// hasAttributes - do we contain any attributes?
1859 bool hasAttributes() const {
1860 if (getAttributes() || getDeclSpec().hasAttributes()) return true;
1861 for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i)
1862 if (getTypeObject(i).getAttrs())
1867 void setAsmLabel(Expr *E) { AsmLabel = E; }
1868 Expr *getAsmLabel() const { return AsmLabel; }
1870 void setExtension(bool Val = true) { Extension = Val; }
1871 bool getExtension() const { return Extension; }
1873 void setInvalidType(bool Val = true) { InvalidType = Val; }
1874 bool isInvalidType() const {
1875 return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error;
1878 void setGroupingParens(bool flag) { GroupingParens = flag; }
1879 bool hasGroupingParens() const { return GroupingParens; }
1881 bool isFirstDeclarator() const { return !CommaLoc.isValid(); }
1882 SourceLocation getCommaLoc() const { return CommaLoc; }
1883 void setCommaLoc(SourceLocation CL) { CommaLoc = CL; }
1885 bool hasEllipsis() const { return EllipsisLoc.isValid(); }
1886 SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
1887 void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; }
1889 void setFunctionDefinitionKind(FunctionDefinitionKind Val) {
1890 FunctionDefinition = Val;
1893 bool isFunctionDefinition() const {
1894 return getFunctionDefinitionKind() != FDK_Declaration;
1897 FunctionDefinitionKind getFunctionDefinitionKind() const {
1898 return (FunctionDefinitionKind)FunctionDefinition;
1901 void setRedeclaration(bool Val) { Redeclaration = Val; }
1902 bool isRedeclaration() const { return Redeclaration; }
1905 /// FieldDeclarator - This little struct is used to capture information about
1906 /// structure field declarators, which is basically just a bitfield size.
1907 struct FieldDeclarator {
1910 explicit FieldDeclarator(DeclSpec &DS) : D(DS, Declarator::MemberContext) {
1915 /// VirtSpecifiers - Represents a C++0x virt-specifier-seq.
1916 class VirtSpecifiers {
1924 VirtSpecifiers() : Specifiers(0) { }
1926 bool SetSpecifier(Specifier VS, SourceLocation Loc,
1927 const char *&PrevSpec);
1929 bool isOverrideSpecified() const { return Specifiers & VS_Override; }
1930 SourceLocation getOverrideLoc() const { return VS_overrideLoc; }
1932 bool isFinalSpecified() const { return Specifiers & VS_Final; }
1933 SourceLocation getFinalLoc() const { return VS_finalLoc; }
1935 void clear() { Specifiers = 0; }
1937 static const char *getSpecifierName(Specifier VS);
1939 SourceLocation getLastLocation() const { return LastLocation; }
1942 unsigned Specifiers;
1944 SourceLocation VS_overrideLoc, VS_finalLoc;
1945 SourceLocation LastLocation;
1948 /// LambdaCapture - An individual capture in a lambda introducer.
1949 struct LambdaCapture {
1950 LambdaCaptureKind Kind;
1953 SourceLocation EllipsisLoc;
1955 LambdaCapture(LambdaCaptureKind Kind, SourceLocation Loc,
1956 IdentifierInfo* Id = 0,
1957 SourceLocation EllipsisLoc = SourceLocation())
1958 : Kind(Kind), Loc(Loc), Id(Id), EllipsisLoc(EllipsisLoc)
1962 /// LambdaIntroducer - Represents a complete lambda introducer.
1963 struct LambdaIntroducer {
1965 SourceLocation DefaultLoc;
1966 LambdaCaptureDefault Default;
1967 llvm::SmallVector<LambdaCapture, 4> Captures;
1970 : Default(LCD_None) {}
1972 /// addCapture - Append a capture in a lambda introducer.
1973 void addCapture(LambdaCaptureKind Kind,
1975 IdentifierInfo* Id = 0,
1976 SourceLocation EllipsisLoc = SourceLocation()) {
1977 Captures.push_back(LambdaCapture(Kind, Loc, Id, EllipsisLoc));
1982 } // end namespace clang