DeclSpec.h revision 66cff7257698d5528632917d38f9a3037bb1506d
1//===--- DeclSpec.h - Parsed declaration specifiers -------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9/// 10/// \file 11/// \brief This file defines the classes used to store parsed information about 12/// declaration-specifiers and declarators. 13/// 14/// \verbatim 15/// static const int volatile x, *y, *(*(*z)[10])(const void *x); 16/// ------------------------- - -- --------------------------- 17/// declaration-specifiers \ | / 18/// declarators 19/// \endverbatim 20/// 21//===----------------------------------------------------------------------===// 22 23#ifndef LLVM_CLANG_SEMA_DECLSPEC_H 24#define LLVM_CLANG_SEMA_DECLSPEC_H 25 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" 37 38namespace clang { 39 class ASTContext; 40 class TypeLoc; 41 class LangOptions; 42 class DiagnosticsEngine; 43 class IdentifierInfo; 44 class NamespaceAliasDecl; 45 class NamespaceDecl; 46 class NestedNameSpecifier; 47 class NestedNameSpecifierLoc; 48 class ObjCDeclSpec; 49 class Preprocessor; 50 class Sema; 51 class Declarator; 52 struct TemplateIdAnnotation; 53 54/// \brief Represents a C++ nested-name-specifier or a global scope specifier. 55/// 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(). 61/// 62/// isSet() is deprecated because it mostly corresponded to "valid" but was 63/// often used as if it meant "present". 64/// 65/// The actual scope is described by getScopeRep(). 66class CXXScopeSpec { 67 SourceRange Range; 68 NestedNameSpecifierLocBuilder Builder; 69 70public: 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(); } 77 78 /// \brief Retrieve the representation of the nested-name-specifier. 79 NestedNameSpecifier *getScopeRep() const { 80 return Builder.getRepresentation(); 81 } 82 83 /// \brief Extend the current nested-name-specifier by another 84 /// nested-name-specifier component of the form 'type::'. 85 /// 86 /// \param Context The AST context in which this nested-name-specifier 87 /// resides. 88 /// 89 /// \param TemplateKWLoc The location of the 'template' keyword, if present. 90 /// 91 /// \param TL The TypeLoc that describes the type preceding the '::'. 92 /// 93 /// \param ColonColonLoc The location of the trailing '::'. 94 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL, 95 SourceLocation ColonColonLoc); 96 97 /// \brief Extend the current nested-name-specifier by another 98 /// nested-name-specifier component of the form 'identifier::'. 99 /// 100 /// \param Context The AST context in which this nested-name-specifier 101 /// resides. 102 /// 103 /// \param Identifier The identifier. 104 /// 105 /// \param IdentifierLoc The location of the identifier. 106 /// 107 /// \param ColonColonLoc The location of the trailing '::'. 108 void Extend(ASTContext &Context, IdentifierInfo *Identifier, 109 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc); 110 111 /// \brief Extend the current nested-name-specifier by another 112 /// nested-name-specifier component of the form 'namespace::'. 113 /// 114 /// \param Context The AST context in which this nested-name-specifier 115 /// resides. 116 /// 117 /// \param Namespace The namespace. 118 /// 119 /// \param NamespaceLoc The location of the namespace name. 120 /// 121 /// \param ColonColonLoc The location of the trailing '::'. 122 void Extend(ASTContext &Context, NamespaceDecl *Namespace, 123 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc); 124 125 /// \brief Extend the current nested-name-specifier by another 126 /// nested-name-specifier component of the form 'namespace-alias::'. 127 /// 128 /// \param Context The AST context in which this nested-name-specifier 129 /// resides. 130 /// 131 /// \param Alias The namespace alias. 132 /// 133 /// \param AliasLoc The location of the namespace alias 134 /// name. 135 /// 136 /// \param ColonColonLoc The location of the trailing '::'. 137 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias, 138 SourceLocation AliasLoc, SourceLocation ColonColonLoc); 139 140 /// \brief Turn this (empty) nested-name-specifier into the global 141 /// nested-name-specifier '::'. 142 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc); 143 144 /// \brief Make a new nested-name-specifier from incomplete source-location 145 /// information. 146 /// 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, 151 SourceRange R); 152 153 /// \brief Adopt an existing nested-name-specifier (with source-range 154 /// information). 155 void Adopt(NestedNameSpecifierLoc Other); 156 157 /// \brief Retrieve a nested-name-specifier with location information, copied 158 /// into the given AST context. 159 /// 160 /// \param Context The context into which this nested-name-specifier will be 161 /// copied. 162 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const; 163 164 /// \brief Retrieve the location of the name in the last qualifier 165 /// in this nested name specifier. 166 /// 167 /// For example, the location of \c bar 168 /// in 169 /// \verbatim 170 /// \::foo::bar<0>:: 171 /// ^~~ 172 /// \endverbatim 173 SourceLocation getLastQualifierNameLoc() const; 174 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(); } 179 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; } 184 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()); 191 Builder.Clear(); 192 } 193 194 /// Deprecated. Some call sites intend isNotEmpty() while others intend 195 /// isValid(). 196 bool isSet() const { return getScopeRep() != 0; } 197 198 void clear() { 199 Range = SourceRange(); 200 Builder.Clear(); 201 } 202 203 /// \brief Retrieve the data associated with the source-location information. 204 char *location_data() const { return Builder.getBuffer().first; } 205 206 /// \brief Retrieve the size of the data associated with source-location 207 /// information. 208 unsigned location_size() const { return Builder.getBuffer().second; } 209}; 210 211/// \brief Captures information about "declaration specifiers". 212/// 213/// "Declaration specifiers" encompasses storage-class-specifiers, 214/// type-specifiers, type-qualifiers, and function-specifiers. 215class DeclSpec { 216public: 217 /// \brief storage-class-specifier 218 /// \note The order of these enumerators is important for diagnostics. 219 enum SCS { 220 SCS_unspecified = 0, 221 SCS_typedef, 222 SCS_extern, 223 SCS_static, 224 SCS_auto, 225 SCS_register, 226 SCS_private_extern, 227 SCS_mutable 228 }; 229 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; 236 237 enum TSC { 238 TSC_unspecified, 239 TSC_imaginary, 240 TSC_complex 241 }; 242 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; 248 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; 280 281 // type-qualifiers 282 enum TQ { // NOTE: These flags must be kept in sync with Qualifiers::TQ. 283 TQ_unspecified = 0, 284 TQ_const = 1, 285 TQ_restrict = 2, 286 TQ_volatile = 4 287 }; 288 289 /// ParsedSpecifiers - Flags to query which specifiers were applied. This is 290 /// returned by getParsedSpecifiers. 291 enum ParsedSpecifiers { 292 PQ_None = 0, 293 PQ_StorageClassSpecifier = 1, 294 PQ_TypeSpecifier = 2, 295 PQ_TypeQualifier = 4, 296 PQ_FunctionSpecifier = 8 297 }; 298 299private: 300 // storage-class-specifier 301 /*SCS*/unsigned StorageClassSpec : 3; 302 unsigned SCS_thread_specified : 1; 303 unsigned SCS_extern_in_linkage_spec : 1; 304 305 // type-specifier 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; 314 315 // type-qualifiers 316 unsigned TypeQualifiers : 3; // Bitwise OR of TQ. 317 318 // function-specifier 319 unsigned FS_inline_specified : 1; 320 unsigned FS_virtual_specified : 1; 321 unsigned FS_explicit_specified : 1; 322 323 // friend-specifier 324 unsigned Friend_specified : 1; 325 326 // constexpr-specifier 327 unsigned Constexpr_specified : 1; 328 329 /*SCS*/unsigned StorageClassSpecAsWritten : 3; 330 331 union { 332 UnionParsedType TypeRep; 333 Decl *DeclRep; 334 Expr *ExprRep; 335 }; 336 337 // attributes. 338 ParsedAttributes Attrs; 339 340 // Scope specifier for the type spec, if applicable. 341 CXXScopeSpec TypeScope; 342 343 // List of protocol qualifiers for objective-c classes. Used for 344 // protocol-qualified interfaces "NString<foo>" and protocol-qualified id 345 // "id<foo>". 346 Decl * const *ProtocolQualifiers; 347 unsigned NumProtocolQualifiers; 348 SourceLocation ProtocolLAngleLoc; 349 SourceLocation *ProtocolLocs; 350 351 // SourceLocation info. These are null if the item wasn't specified or if 352 // the setting was synthesized. 353 SourceRange Range; 354 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; 366 367 WrittenBuiltinSpecs writtenBS; 368 void SaveWrittenBuiltinSpecs(); 369 void SaveStorageSpecifierAsWritten(); 370 371 ObjCDeclSpec *ObjCQualifiers; 372 373 static bool isTypeRep(TST T) { 374 return (T == TST_typename || T == TST_typeofType || 375 T == TST_underlyingType || T == TST_atomic); 376 } 377 static bool isExprRep(TST T) { 378 return (T == TST_typeofExpr || T == TST_decltype); 379 } 380 381 DeclSpec(const DeclSpec &) LLVM_DELETED_FUNCTION; 382 void operator=(const DeclSpec &) LLVM_DELETED_FUNCTION; 383public: 384 static bool isDeclRep(TST T) { 385 return (T == TST_enum || T == TST_struct || 386 T == TST_interface || T == TST_union || 387 T == TST_class); 388 } 389 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), 409 Attrs(attrFactory), 410 ProtocolQualifiers(0), 411 NumProtocolQualifiers(0), 412 ProtocolLocs(0), 413 writtenBS(), 414 ObjCQualifiers(0) { 415 } 416 ~DeclSpec() { 417 delete [] ProtocolQualifiers; 418 delete [] ProtocolLocs; 419 } 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; 426 } 427 428 SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; } 429 SourceLocation getThreadSpecLoc() const { return SCS_threadLoc; } 430 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(); 437 } 438 439 // type-specifier 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"); 450 return TypeRep; 451 } 452 Decl *getRepAsDecl() const { 453 assert(isDeclRep((TST) TypeSpecType) && "DeclSpec does not store a decl"); 454 return DeclRep; 455 } 456 Expr *getRepAsExpr() const { 457 assert(isExprRep((TST) TypeSpecType) && "DeclSpec does not store an expr"); 458 return ExprRep; 459 } 460 CXXScopeSpec &getTypeSpecScope() { return TypeScope; } 461 const CXXScopeSpec &getTypeSpecScope() const { return TypeScope; } 462 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(); } 466 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; } 472 473 SourceLocation getTypeSpecTypeNameLoc() const { 474 assert(isDeclRep((TST) TypeSpecType) || TypeSpecType == TST_typename); 475 return TSTNameLoc; 476 } 477 478 SourceRange getTypeofParensRange() const { return TypeofParensRange; } 479 void setTypeofParensRange(SourceRange range) { TypeofParensRange = range; } 480 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); 488 489 // type-qualifiers 490 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; } 496 497 /// \brief Clear out all of the type qualifiers. 498 void ClearTypeQualifiers() { 499 TypeQualifiers = 0; 500 TQ_constLoc = SourceLocation(); 501 TQ_restrictLoc = SourceLocation(); 502 TQ_volatileLoc = SourceLocation(); 503 } 504 505 // function-specifier 506 bool isInlineSpecified() const { return FS_inline_specified; } 507 SourceLocation getInlineSpecLoc() const { return FS_inlineLoc; } 508 509 bool isVirtualSpecified() const { return FS_virtual_specified; } 510 SourceLocation getVirtualSpecLoc() const { return FS_virtualLoc; } 511 512 bool isExplicitSpecified() const { return FS_explicit_specified; } 513 SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; } 514 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(); 522 } 523 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; 530 } 531 532 /// \brief Return a bitmask of which flavors of specifiers this 533 /// DeclSpec includes. 534 unsigned getParsedSpecifiers() const; 535 536 SCS getStorageClassSpecAsWritten() const { 537 return (SCS)StorageClassSpecAsWritten; 538 } 539 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; 544 } 545 546 void SetRangeStart(SourceLocation Loc) { Range.setBegin(Loc); } 547 void SetRangeEnd(SourceLocation Loc) { Range.setEnd(Loc); } 548 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 553 /// such that 554 /// Diag(Loc, DiagID) << PrevSpec; 555 /// will yield a useful result. 556 /// 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, 562 unsigned &DiagID); 563 bool SetTypeSpecWidth(TSW W, SourceLocation Loc, const char *&PrevSpec, 564 unsigned &DiagID); 565 bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec, 566 unsigned &DiagID); 567 bool SetTypeSpecSign(TSS S, SourceLocation Loc, const char *&PrevSpec, 568 unsigned &DiagID); 569 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, 570 unsigned &DiagID); 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); 581 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)); 591 DeclRep = Rep; 592 } 593 void UpdateTypeRep(ParsedType Rep) { 594 assert(isTypeRep((TST) TypeSpecType)); 595 TypeRep = Rep; 596 } 597 void UpdateExprRep(Expr *Rep) { 598 assert(isExprRep((TST) TypeSpecType)); 599 ExprRep = Rep; 600 } 601 602 bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, 603 unsigned &DiagID, const LangOptions &Lang); 604 605 bool SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec, 606 unsigned &DiagID); 607 bool SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec, 608 unsigned &DiagID); 609 bool SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec, 610 unsigned &DiagID); 611 612 bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, 613 unsigned &DiagID); 614 bool setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec, 615 unsigned &DiagID); 616 bool SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec, 617 unsigned &DiagID); 618 619 bool isFriendSpecified() const { return Friend_specified; } 620 SourceLocation getFriendSpecLoc() const { return FriendLoc; } 621 622 bool isModulePrivateSpecified() const { return ModulePrivateLoc.isValid(); } 623 SourceLocation getModulePrivateSpecLoc() const { return ModulePrivateLoc; } 624 625 bool isConstexprSpecified() const { return Constexpr_specified; } 626 SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; } 627 628 void ClearConstexprSpec() { 629 Constexpr_specified = false; 630 ConstexprLoc = SourceLocation(); 631 } 632 633 AttributePool &getAttributePool() const { 634 return Attrs.getPool(); 635 } 636 637 /// \brief Concatenates two attribute lists. 638 /// 639 /// The GCC attribute syntax allows for the following: 640 /// 641 /// \code 642 /// short __attribute__(( unused, deprecated )) 643 /// int __attribute__(( may_alias, aligned(16) )) var; 644 /// \endcode 645 /// 646 /// This declares 4 attributes using 2 lists. The following syntax is 647 /// also allowed and equivalent to the previous declaration. 648 /// 649 /// \code 650 /// short __attribute__((unused)) __attribute__((deprecated)) 651 /// int __attribute__((may_alias)) __attribute__((aligned(16))) var; 652 /// \endcode 653 /// 654 void addAttributes(AttributeList *AL) { 655 Attrs.addAll(AL); 656 } 657 void setAttributes(AttributeList *AL) { 658 Attrs.set(AL); 659 } 660 661 bool hasAttributes() const { return !Attrs.empty(); } 662 663 ParsedAttributes &getAttributes() { return Attrs; } 664 const ParsedAttributes &getAttributes() const { return Attrs; } 665 666 /// \brief Return the current attribute list and remove them from 667 /// the DeclSpec so that it doesn't own them. 668 ParsedAttributes takeAttributes() { 669 // The non-const "copy" constructor clears the operand automatically. 670 return Attrs; 671 } 672 673 void takeAttributesFrom(ParsedAttributes &attrs) { 674 Attrs.takeAllFrom(attrs); 675 } 676 677 typedef Decl * const *ProtocolQualifierListTy; 678 ProtocolQualifierListTy getProtocolQualifiers() const { 679 return ProtocolQualifiers; 680 } 681 SourceLocation *getProtocolLocs() const { return ProtocolLocs; } 682 unsigned getNumProtocolQualifiers() const { 683 return NumProtocolQualifiers; 684 } 685 SourceLocation getProtocolLAngleLoc() const { return ProtocolLAngleLoc; } 686 void setProtocolQualifiers(Decl * const *Protos, unsigned NP, 687 SourceLocation *ProtoLocs, 688 SourceLocation LAngleLoc); 689 690 /// Finish - This does final analysis of the declspec, issuing diagnostics for 691 /// things like "_Imaginary" (lacking an FP type). After calling this method, 692 /// DeclSpec is guaranteed self-consistent, even if an error occurred. 693 void Finish(DiagnosticsEngine &D, Preprocessor &PP); 694 695 const WrittenBuiltinSpecs& getWrittenBuiltinSpecs() const { 696 return writtenBS; 697 } 698 699 ObjCDeclSpec *getObjCQualifiers() const { return ObjCQualifiers; } 700 void setObjCQualifiers(ObjCDeclSpec *quals) { ObjCQualifiers = quals; } 701 702 /// \brief Checks if this DeclSpec can stand alone, without a Declarator. 703 /// 704 /// Only tag declspecs can stand alone. 705 bool isMissingDeclaratorOk(); 706}; 707 708/// \brief Captures information about "declaration specifiers" specific to 709/// Objective-C. 710class ObjCDeclSpec { 711public: 712 /// ObjCDeclQualifier - Qualifier used on types in method 713 /// declarations. Not all combinations are sensible. Parameters 714 /// can be one of { in, out, inout } with one of { bycopy, byref }. 715 /// Returns can either be { oneway } or not. 716 /// 717 /// This should be kept in sync with Decl::ObjCDeclQualifier. 718 enum ObjCDeclQualifier { 719 DQ_None = 0x0, 720 DQ_In = 0x1, 721 DQ_Inout = 0x2, 722 DQ_Out = 0x4, 723 DQ_Bycopy = 0x8, 724 DQ_Byref = 0x10, 725 DQ_Oneway = 0x20 726 }; 727 728 /// PropertyAttributeKind - list of property attributes. 729 enum ObjCPropertyAttributeKind { 730 DQ_PR_noattr = 0x0, 731 DQ_PR_readonly = 0x01, 732 DQ_PR_getter = 0x02, 733 DQ_PR_assign = 0x04, 734 DQ_PR_readwrite = 0x08, 735 DQ_PR_retain = 0x10, 736 DQ_PR_copy = 0x20, 737 DQ_PR_nonatomic = 0x40, 738 DQ_PR_setter = 0x80, 739 DQ_PR_atomic = 0x100, 740 DQ_PR_weak = 0x200, 741 DQ_PR_strong = 0x400, 742 DQ_PR_unsafe_unretained = 0x800 743 }; 744 745 746 ObjCDeclSpec() 747 : objcDeclQualifier(DQ_None), PropertyAttributes(DQ_PR_noattr), 748 GetterName(0), SetterName(0) { } 749 ObjCDeclQualifier getObjCDeclQualifier() const { return objcDeclQualifier; } 750 void setObjCDeclQualifier(ObjCDeclQualifier DQVal) { 751 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal); 752 } 753 754 ObjCPropertyAttributeKind getPropertyAttributes() const { 755 return ObjCPropertyAttributeKind(PropertyAttributes); 756 } 757 void setPropertyAttributes(ObjCPropertyAttributeKind PRVal) { 758 PropertyAttributes = 759 (ObjCPropertyAttributeKind)(PropertyAttributes | PRVal); 760 } 761 762 const IdentifierInfo *getGetterName() const { return GetterName; } 763 IdentifierInfo *getGetterName() { return GetterName; } 764 void setGetterName(IdentifierInfo *name) { GetterName = name; } 765 766 const IdentifierInfo *getSetterName() const { return SetterName; } 767 IdentifierInfo *getSetterName() { return SetterName; } 768 void setSetterName(IdentifierInfo *name) { SetterName = name; } 769 770private: 771 // FIXME: These two are unrelated and mutially exclusive. So perhaps 772 // we can put them in a union to reflect their mutual exclusiveness 773 // (space saving is negligible). 774 ObjCDeclQualifier objcDeclQualifier : 6; 775 776 // NOTE: VC++ treats enums as signed, avoid using ObjCPropertyAttributeKind 777 unsigned PropertyAttributes : 12; 778 IdentifierInfo *GetterName; // getter name of NULL if no getter 779 IdentifierInfo *SetterName; // setter name of NULL if no setter 780}; 781 782/// \brief Represents a C++ unqualified-id that has been parsed. 783class UnqualifiedId { 784private: 785 UnqualifiedId(const UnqualifiedId &Other) LLVM_DELETED_FUNCTION; 786 const UnqualifiedId &operator=(const UnqualifiedId &) LLVM_DELETED_FUNCTION; 787 788public: 789 /// \brief Describes the kind of unqualified-id parsed. 790 enum IdKind { 791 /// \brief An identifier. 792 IK_Identifier, 793 /// \brief An overloaded operator name, e.g., operator+. 794 IK_OperatorFunctionId, 795 /// \brief A conversion function name, e.g., operator int. 796 IK_ConversionFunctionId, 797 /// \brief A user-defined literal name, e.g., operator "" _i. 798 IK_LiteralOperatorId, 799 /// \brief A constructor name. 800 IK_ConstructorName, 801 /// \brief A constructor named via a template-id. 802 IK_ConstructorTemplateId, 803 /// \brief A destructor name. 804 IK_DestructorName, 805 /// \brief A template-id, e.g., f<int>. 806 IK_TemplateId, 807 /// \brief An implicit 'self' parameter 808 IK_ImplicitSelfParam 809 } Kind; 810 811 /// \brief Anonymous union that holds extra data associated with the 812 /// parsed unqualified-id. 813 union { 814 /// \brief When Kind == IK_Identifier, the parsed identifier, or when Kind 815 /// == IK_UserLiteralId, the identifier suffix. 816 IdentifierInfo *Identifier; 817 818 /// \brief When Kind == IK_OperatorFunctionId, the overloaded operator 819 /// that we parsed. 820 struct { 821 /// \brief The kind of overloaded operator. 822 OverloadedOperatorKind Operator; 823 824 /// \brief The source locations of the individual tokens that name 825 /// the operator, e.g., the "new", "[", and "]" tokens in 826 /// operator new []. 827 /// 828 /// Different operators have different numbers of tokens in their name, 829 /// up to three. Any remaining source locations in this array will be 830 /// set to an invalid value for operators with fewer than three tokens. 831 unsigned SymbolLocations[3]; 832 } OperatorFunctionId; 833 834 /// \brief When Kind == IK_ConversionFunctionId, the type that the 835 /// conversion function names. 836 UnionParsedType ConversionFunctionId; 837 838 /// \brief When Kind == IK_ConstructorName, the class-name of the type 839 /// whose constructor is being referenced. 840 UnionParsedType ConstructorName; 841 842 /// \brief When Kind == IK_DestructorName, the type referred to by the 843 /// class-name. 844 UnionParsedType DestructorName; 845 846 /// \brief When Kind == IK_TemplateId or IK_ConstructorTemplateId, 847 /// the template-id annotation that contains the template name and 848 /// template arguments. 849 TemplateIdAnnotation *TemplateId; 850 }; 851 852 /// \brief The location of the first token that describes this unqualified-id, 853 /// which will be the location of the identifier, "operator" keyword, 854 /// tilde (for a destructor), or the template name of a template-id. 855 SourceLocation StartLocation; 856 857 /// \brief The location of the last token that describes this unqualified-id. 858 SourceLocation EndLocation; 859 860 UnqualifiedId() : Kind(IK_Identifier), Identifier(0) { } 861 862 /// \brief Clear out this unqualified-id, setting it to default (invalid) 863 /// state. 864 void clear() { 865 Kind = IK_Identifier; 866 Identifier = 0; 867 StartLocation = SourceLocation(); 868 EndLocation = SourceLocation(); 869 } 870 871 /// \brief Determine whether this unqualified-id refers to a valid name. 872 bool isValid() const { return StartLocation.isValid(); } 873 874 /// \brief Determine whether this unqualified-id refers to an invalid name. 875 bool isInvalid() const { return !isValid(); } 876 877 /// \brief Determine what kind of name we have. 878 IdKind getKind() const { return Kind; } 879 void setKind(IdKind kind) { Kind = kind; } 880 881 /// \brief Specify that this unqualified-id was parsed as an identifier. 882 /// 883 /// \param Id the parsed identifier. 884 /// \param IdLoc the location of the parsed identifier. 885 void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) { 886 Kind = IK_Identifier; 887 Identifier = const_cast<IdentifierInfo *>(Id); 888 StartLocation = EndLocation = IdLoc; 889 } 890 891 /// \brief Specify that this unqualified-id was parsed as an 892 /// operator-function-id. 893 /// 894 /// \param OperatorLoc the location of the 'operator' keyword. 895 /// 896 /// \param Op the overloaded operator. 897 /// 898 /// \param SymbolLocations the locations of the individual operator symbols 899 /// in the operator. 900 void setOperatorFunctionId(SourceLocation OperatorLoc, 901 OverloadedOperatorKind Op, 902 SourceLocation SymbolLocations[3]); 903 904 /// \brief Specify that this unqualified-id was parsed as a 905 /// conversion-function-id. 906 /// 907 /// \param OperatorLoc the location of the 'operator' keyword. 908 /// 909 /// \param Ty the type to which this conversion function is converting. 910 /// 911 /// \param EndLoc the location of the last token that makes up the type name. 912 void setConversionFunctionId(SourceLocation OperatorLoc, 913 ParsedType Ty, 914 SourceLocation EndLoc) { 915 Kind = IK_ConversionFunctionId; 916 StartLocation = OperatorLoc; 917 EndLocation = EndLoc; 918 ConversionFunctionId = Ty; 919 } 920 921 /// \brief Specific that this unqualified-id was parsed as a 922 /// literal-operator-id. 923 /// 924 /// \param Id the parsed identifier. 925 /// 926 /// \param OpLoc the location of the 'operator' keyword. 927 /// 928 /// \param IdLoc the location of the identifier. 929 void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc, 930 SourceLocation IdLoc) { 931 Kind = IK_LiteralOperatorId; 932 Identifier = const_cast<IdentifierInfo *>(Id); 933 StartLocation = OpLoc; 934 EndLocation = IdLoc; 935 } 936 937 /// \brief Specify that this unqualified-id was parsed as a constructor name. 938 /// 939 /// \param ClassType the class type referred to by the constructor name. 940 /// 941 /// \param ClassNameLoc the location of the class name. 942 /// 943 /// \param EndLoc the location of the last token that makes up the type name. 944 void setConstructorName(ParsedType ClassType, 945 SourceLocation ClassNameLoc, 946 SourceLocation EndLoc) { 947 Kind = IK_ConstructorName; 948 StartLocation = ClassNameLoc; 949 EndLocation = EndLoc; 950 ConstructorName = ClassType; 951 } 952 953 /// \brief Specify that this unqualified-id was parsed as a 954 /// template-id that names a constructor. 955 /// 956 /// \param TemplateId the template-id annotation that describes the parsed 957 /// template-id. This UnqualifiedId instance will take ownership of the 958 /// \p TemplateId and will free it on destruction. 959 void setConstructorTemplateId(TemplateIdAnnotation *TemplateId); 960 961 /// \brief Specify that this unqualified-id was parsed as a destructor name. 962 /// 963 /// \param TildeLoc the location of the '~' that introduces the destructor 964 /// name. 965 /// 966 /// \param ClassType the name of the class referred to by the destructor name. 967 void setDestructorName(SourceLocation TildeLoc, 968 ParsedType ClassType, 969 SourceLocation EndLoc) { 970 Kind = IK_DestructorName; 971 StartLocation = TildeLoc; 972 EndLocation = EndLoc; 973 DestructorName = ClassType; 974 } 975 976 /// \brief Specify that this unqualified-id was parsed as a template-id. 977 /// 978 /// \param TemplateId the template-id annotation that describes the parsed 979 /// template-id. This UnqualifiedId instance will take ownership of the 980 /// \p TemplateId and will free it on destruction. 981 void setTemplateId(TemplateIdAnnotation *TemplateId); 982 983 /// \brief Return the source range that covers this unqualified-id. 984 SourceRange getSourceRange() const LLVM_READONLY { 985 return SourceRange(StartLocation, EndLocation); 986 } 987 SourceLocation getLocStart() const LLVM_READONLY { return StartLocation; } 988 SourceLocation getLocEnd() const LLVM_READONLY { return EndLocation; } 989}; 990 991/// \brief A set of tokens that has been cached for later parsing. 992typedef SmallVector<Token, 4> CachedTokens; 993 994/// \brief One instance of this struct is used for each type in a 995/// declarator that is parsed. 996/// 997/// This is intended to be a small value object. 998struct DeclaratorChunk { 999 enum { 1000 Pointer, Reference, Array, Function, BlockPointer, MemberPointer, Paren 1001 } Kind; 1002 1003 /// Loc - The place where this type was defined. 1004 SourceLocation Loc; 1005 /// EndLoc - If valid, the place where this chunck ends. 1006 SourceLocation EndLoc; 1007 1008 struct TypeInfoCommon { 1009 AttributeList *AttrList; 1010 }; 1011 1012 struct PointerTypeInfo : TypeInfoCommon { 1013 /// The type qualifiers: const/volatile/restrict. 1014 unsigned TypeQuals : 3; 1015 1016 /// The location of the const-qualifier, if any. 1017 unsigned ConstQualLoc; 1018 1019 /// The location of the volatile-qualifier, if any. 1020 unsigned VolatileQualLoc; 1021 1022 /// The location of the restrict-qualifier, if any. 1023 unsigned RestrictQualLoc; 1024 1025 void destroy() { 1026 } 1027 }; 1028 1029 struct ReferenceTypeInfo : TypeInfoCommon { 1030 /// The type qualifier: restrict. [GNU] C++ extension 1031 bool HasRestrict : 1; 1032 /// True if this is an lvalue reference, false if it's an rvalue reference. 1033 bool LValueRef : 1; 1034 void destroy() { 1035 } 1036 }; 1037 1038 struct ArrayTypeInfo : TypeInfoCommon { 1039 /// The type qualifiers for the array: const/volatile/restrict. 1040 unsigned TypeQuals : 3; 1041 1042 /// True if this dimension included the 'static' keyword. 1043 bool hasStatic : 1; 1044 1045 /// True if this dimension was [*]. In this case, NumElts is null. 1046 bool isStar : 1; 1047 1048 /// This is the size of the array, or null if [] or [*] was specified. 1049 /// Since the parser is multi-purpose, and we don't want to impose a root 1050 /// expression class on all clients, NumElts is untyped. 1051 Expr *NumElts; 1052 1053 void destroy() {} 1054 }; 1055 1056 /// ParamInfo - An array of paraminfo objects is allocated whenever a function 1057 /// declarator is parsed. There are two interesting styles of arguments here: 1058 /// K&R-style identifier lists and parameter type lists. K&R-style identifier 1059 /// lists will have information about the identifier, but no type information. 1060 /// Parameter type lists will have type info (if the actions module provides 1061 /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'. 1062 struct ParamInfo { 1063 IdentifierInfo *Ident; 1064 SourceLocation IdentLoc; 1065 Decl *Param; 1066 1067 /// DefaultArgTokens - When the parameter's default argument 1068 /// cannot be parsed immediately (because it occurs within the 1069 /// declaration of a member function), it will be stored here as a 1070 /// sequence of tokens to be parsed once the class definition is 1071 /// complete. Non-NULL indicates that there is a default argument. 1072 CachedTokens *DefaultArgTokens; 1073 1074 ParamInfo() {} 1075 ParamInfo(IdentifierInfo *ident, SourceLocation iloc, 1076 Decl *param, 1077 CachedTokens *DefArgTokens = 0) 1078 : Ident(ident), IdentLoc(iloc), Param(param), 1079 DefaultArgTokens(DefArgTokens) {} 1080 }; 1081 1082 struct TypeAndRange { 1083 ParsedType Ty; 1084 SourceRange Range; 1085 }; 1086 1087 struct FunctionTypeInfo : TypeInfoCommon { 1088 /// hasPrototype - This is true if the function had at least one typed 1089 /// argument. If the function is () or (a,b,c), then it has no prototype, 1090 /// and is treated as a K&R-style function. 1091 unsigned hasPrototype : 1; 1092 1093 /// isVariadic - If this function has a prototype, and if that 1094 /// proto ends with ',...)', this is true. When true, EllipsisLoc 1095 /// contains the location of the ellipsis. 1096 unsigned isVariadic : 1; 1097 1098 /// Can this declaration be a constructor-style initializer? 1099 unsigned isAmbiguous : 1; 1100 1101 /// \brief Whether the ref-qualifier (if any) is an lvalue reference. 1102 /// Otherwise, it's an rvalue reference. 1103 unsigned RefQualifierIsLValueRef : 1; 1104 1105 /// The type qualifiers: const/volatile/restrict. 1106 /// The qualifier bitmask values are the same as in QualType. 1107 unsigned TypeQuals : 3; 1108 1109 /// ExceptionSpecType - An ExceptionSpecificationType value. 1110 unsigned ExceptionSpecType : 3; 1111 1112 /// DeleteArgInfo - If this is true, we need to delete[] ArgInfo. 1113 unsigned DeleteArgInfo : 1; 1114 1115 /// HasTrailingReturnType - If this is true, a trailing return type was 1116 /// specified. 1117 unsigned HasTrailingReturnType : 1; 1118 1119 /// The location of the left parenthesis in the source. 1120 unsigned LParenLoc; 1121 1122 /// When isVariadic is true, the location of the ellipsis in the source. 1123 unsigned EllipsisLoc; 1124 1125 /// The location of the right parenthesis in the source. 1126 unsigned RParenLoc; 1127 1128 /// NumArgs - This is the number of formal arguments provided for the 1129 /// declarator. 1130 unsigned NumArgs; 1131 1132 /// NumExceptions - This is the number of types in the dynamic-exception- 1133 /// decl, if the function has one. 1134 unsigned NumExceptions; 1135 1136 /// \brief The location of the ref-qualifier, if any. 1137 /// 1138 /// If this is an invalid location, there is no ref-qualifier. 1139 unsigned RefQualifierLoc; 1140 1141 /// \brief The location of the const-qualifier, if any. 1142 /// 1143 /// If this is an invalid location, there is no const-qualifier. 1144 unsigned ConstQualifierLoc; 1145 1146 /// \brief The location of the volatile-qualifier, if any. 1147 /// 1148 /// If this is an invalid location, there is no volatile-qualifier. 1149 unsigned VolatileQualifierLoc; 1150 1151 /// \brief The location of the 'mutable' qualifer in a lambda-declarator, if 1152 /// any. 1153 unsigned MutableLoc; 1154 1155 /// \brief The location of the keyword introducing the spec, if any. 1156 unsigned ExceptionSpecLoc; 1157 1158 /// ArgInfo - This is a pointer to a new[]'d array of ParamInfo objects that 1159 /// describe the arguments for this function declarator. This is null if 1160 /// there are no arguments specified. 1161 ParamInfo *ArgInfo; 1162 1163 union { 1164 /// \brief Pointer to a new[]'d array of TypeAndRange objects that 1165 /// contain the types in the function's dynamic exception specification 1166 /// and their locations, if there is one. 1167 TypeAndRange *Exceptions; 1168 1169 /// \brief Pointer to the expression in the noexcept-specifier of this 1170 /// function, if it has one. 1171 Expr *NoexceptExpr; 1172 }; 1173 1174 /// \brief If HasTrailingReturnType is true, this is the trailing return 1175 /// type specified. 1176 UnionParsedType TrailingReturnType; 1177 1178 /// \brief Reset the argument list to having zero arguments. 1179 /// 1180 /// This is used in various places for error recovery. 1181 void freeArgs() { 1182 if (DeleteArgInfo) { 1183 delete[] ArgInfo; 1184 DeleteArgInfo = false; 1185 } 1186 NumArgs = 0; 1187 } 1188 1189 void destroy() { 1190 if (DeleteArgInfo) 1191 delete[] ArgInfo; 1192 if (getExceptionSpecType() == EST_Dynamic) 1193 delete[] Exceptions; 1194 } 1195 1196 /// isKNRPrototype - Return true if this is a K&R style identifier list, 1197 /// like "void foo(a,b,c)". In a function definition, this will be followed 1198 /// by the argument type definitions. 1199 bool isKNRPrototype() const { 1200 return !hasPrototype && NumArgs != 0; 1201 } 1202 1203 SourceLocation getLParenLoc() const { 1204 return SourceLocation::getFromRawEncoding(LParenLoc); 1205 } 1206 1207 SourceLocation getEllipsisLoc() const { 1208 return SourceLocation::getFromRawEncoding(EllipsisLoc); 1209 } 1210 1211 SourceLocation getRParenLoc() const { 1212 return SourceLocation::getFromRawEncoding(RParenLoc); 1213 } 1214 1215 SourceLocation getExceptionSpecLoc() const { 1216 return SourceLocation::getFromRawEncoding(ExceptionSpecLoc); 1217 } 1218 1219 /// \brief Retrieve the location of the ref-qualifier, if any. 1220 SourceLocation getRefQualifierLoc() const { 1221 return SourceLocation::getFromRawEncoding(RefQualifierLoc); 1222 } 1223 1224 /// \brief Retrieve the location of the ref-qualifier, if any. 1225 SourceLocation getConstQualifierLoc() const { 1226 return SourceLocation::getFromRawEncoding(ConstQualifierLoc); 1227 } 1228 1229 /// \brief Retrieve the location of the ref-qualifier, if any. 1230 SourceLocation getVolatileQualifierLoc() const { 1231 return SourceLocation::getFromRawEncoding(VolatileQualifierLoc); 1232 } 1233 1234 /// \brief Retrieve the location of the 'mutable' qualifier, if any. 1235 SourceLocation getMutableLoc() const { 1236 return SourceLocation::getFromRawEncoding(MutableLoc); 1237 } 1238 1239 /// \brief Determine whether this function declaration contains a 1240 /// ref-qualifier. 1241 bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); } 1242 1243 /// \brief Determine whether this lambda-declarator contains a 'mutable' 1244 /// qualifier. 1245 bool hasMutableQualifier() const { return getMutableLoc().isValid(); } 1246 1247 /// \brief Get the type of exception specification this function has. 1248 ExceptionSpecificationType getExceptionSpecType() const { 1249 return static_cast<ExceptionSpecificationType>(ExceptionSpecType); 1250 } 1251 1252 /// \brief Determine whether this function declarator had a 1253 /// trailing-return-type. 1254 bool hasTrailingReturnType() const { return HasTrailingReturnType; } 1255 1256 /// \brief Get the trailing-return-type for this function declarator. 1257 ParsedType getTrailingReturnType() const { return TrailingReturnType; } 1258 }; 1259 1260 struct BlockPointerTypeInfo : TypeInfoCommon { 1261 /// For now, sema will catch these as invalid. 1262 /// The type qualifiers: const/volatile/restrict. 1263 unsigned TypeQuals : 3; 1264 1265 void destroy() { 1266 } 1267 }; 1268 1269 struct MemberPointerTypeInfo : TypeInfoCommon { 1270 /// The type qualifiers: const/volatile/restrict. 1271 unsigned TypeQuals : 3; 1272 // CXXScopeSpec has a constructor, so it can't be a direct member. 1273 // So we need some pointer-aligned storage and a bit of trickery. 1274 union { 1275 void *Aligner; 1276 char Mem[sizeof(CXXScopeSpec)]; 1277 } ScopeMem; 1278 CXXScopeSpec &Scope() { 1279 return *reinterpret_cast<CXXScopeSpec*>(ScopeMem.Mem); 1280 } 1281 const CXXScopeSpec &Scope() const { 1282 return *reinterpret_cast<const CXXScopeSpec*>(ScopeMem.Mem); 1283 } 1284 void destroy() { 1285 Scope().~CXXScopeSpec(); 1286 } 1287 }; 1288 1289 union { 1290 TypeInfoCommon Common; 1291 PointerTypeInfo Ptr; 1292 ReferenceTypeInfo Ref; 1293 ArrayTypeInfo Arr; 1294 FunctionTypeInfo Fun; 1295 BlockPointerTypeInfo Cls; 1296 MemberPointerTypeInfo Mem; 1297 }; 1298 1299 void destroy() { 1300 switch (Kind) { 1301 case DeclaratorChunk::Function: return Fun.destroy(); 1302 case DeclaratorChunk::Pointer: return Ptr.destroy(); 1303 case DeclaratorChunk::BlockPointer: return Cls.destroy(); 1304 case DeclaratorChunk::Reference: return Ref.destroy(); 1305 case DeclaratorChunk::Array: return Arr.destroy(); 1306 case DeclaratorChunk::MemberPointer: return Mem.destroy(); 1307 case DeclaratorChunk::Paren: return; 1308 } 1309 } 1310 1311 /// \brief If there are attributes applied to this declaratorchunk, return 1312 /// them. 1313 const AttributeList *getAttrs() const { 1314 return Common.AttrList; 1315 } 1316 1317 AttributeList *&getAttrListRef() { 1318 return Common.AttrList; 1319 } 1320 1321 /// \brief Return a DeclaratorChunk for a pointer. 1322 static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc, 1323 SourceLocation ConstQualLoc, 1324 SourceLocation VolatileQualLoc, 1325 SourceLocation RestrictQualLoc) { 1326 DeclaratorChunk I; 1327 I.Kind = Pointer; 1328 I.Loc = Loc; 1329 I.Ptr.TypeQuals = TypeQuals; 1330 I.Ptr.ConstQualLoc = ConstQualLoc.getRawEncoding(); 1331 I.Ptr.VolatileQualLoc = VolatileQualLoc.getRawEncoding(); 1332 I.Ptr.RestrictQualLoc = RestrictQualLoc.getRawEncoding(); 1333 I.Ptr.AttrList = 0; 1334 return I; 1335 } 1336 1337 /// \brief Return a DeclaratorChunk for a reference. 1338 static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc, 1339 bool lvalue) { 1340 DeclaratorChunk I; 1341 I.Kind = Reference; 1342 I.Loc = Loc; 1343 I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0; 1344 I.Ref.LValueRef = lvalue; 1345 I.Ref.AttrList = 0; 1346 return I; 1347 } 1348 1349 /// \brief Return a DeclaratorChunk for an array. 1350 static DeclaratorChunk getArray(unsigned TypeQuals, 1351 bool isStatic, bool isStar, Expr *NumElts, 1352 SourceLocation LBLoc, SourceLocation RBLoc) { 1353 DeclaratorChunk I; 1354 I.Kind = Array; 1355 I.Loc = LBLoc; 1356 I.EndLoc = RBLoc; 1357 I.Arr.AttrList = 0; 1358 I.Arr.TypeQuals = TypeQuals; 1359 I.Arr.hasStatic = isStatic; 1360 I.Arr.isStar = isStar; 1361 I.Arr.NumElts = NumElts; 1362 return I; 1363 } 1364 1365 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function. 1366 /// "TheDeclarator" is the declarator that this will be added to. 1367 static DeclaratorChunk getFunction(bool hasProto, 1368 bool isAmbiguous, 1369 SourceLocation LParenLoc, 1370 ParamInfo *ArgInfo, unsigned NumArgs, 1371 SourceLocation EllipsisLoc, 1372 SourceLocation RParenLoc, 1373 unsigned TypeQuals, 1374 bool RefQualifierIsLvalueRef, 1375 SourceLocation RefQualifierLoc, 1376 SourceLocation ConstQualifierLoc, 1377 SourceLocation VolatileQualifierLoc, 1378 SourceLocation MutableLoc, 1379 ExceptionSpecificationType ESpecType, 1380 SourceLocation ESpecLoc, 1381 ParsedType *Exceptions, 1382 SourceRange *ExceptionRanges, 1383 unsigned NumExceptions, 1384 Expr *NoexceptExpr, 1385 SourceLocation LocalRangeBegin, 1386 SourceLocation LocalRangeEnd, 1387 Declarator &TheDeclarator, 1388 TypeResult TrailingReturnType = 1389 TypeResult()); 1390 1391 /// \brief Return a DeclaratorChunk for a block. 1392 static DeclaratorChunk getBlockPointer(unsigned TypeQuals, 1393 SourceLocation Loc) { 1394 DeclaratorChunk I; 1395 I.Kind = BlockPointer; 1396 I.Loc = Loc; 1397 I.Cls.TypeQuals = TypeQuals; 1398 I.Cls.AttrList = 0; 1399 return I; 1400 } 1401 1402 static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS, 1403 unsigned TypeQuals, 1404 SourceLocation Loc) { 1405 DeclaratorChunk I; 1406 I.Kind = MemberPointer; 1407 I.Loc = Loc; 1408 I.Mem.TypeQuals = TypeQuals; 1409 I.Mem.AttrList = 0; 1410 new (I.Mem.ScopeMem.Mem) CXXScopeSpec(SS); 1411 return I; 1412 } 1413 1414 /// \brief Return a DeclaratorChunk for a paren. 1415 static DeclaratorChunk getParen(SourceLocation LParenLoc, 1416 SourceLocation RParenLoc) { 1417 DeclaratorChunk I; 1418 I.Kind = Paren; 1419 I.Loc = LParenLoc; 1420 I.EndLoc = RParenLoc; 1421 I.Common.AttrList = 0; 1422 return I; 1423 } 1424 1425}; 1426 1427/// \brief Described the kind of function definition (if any) provided for 1428/// a function. 1429enum FunctionDefinitionKind { 1430 FDK_Declaration, 1431 FDK_Definition, 1432 FDK_Defaulted, 1433 FDK_Deleted 1434}; 1435 1436/// \brief Information about one declarator, including the parsed type 1437/// information and the identifier. 1438/// 1439/// When the declarator is fully formed, this is turned into the appropriate 1440/// Decl object. 1441/// 1442/// Declarators come in two types: normal declarators and abstract declarators. 1443/// Abstract declarators are used when parsing types, and don't have an 1444/// identifier. Normal declarators do have ID's. 1445/// 1446/// Instances of this class should be a transient object that lives on the 1447/// stack, not objects that are allocated in large quantities on the heap. 1448class Declarator { 1449public: 1450 enum TheContext { 1451 FileContext, // File scope declaration. 1452 PrototypeContext, // Within a function prototype. 1453 ObjCResultContext, // An ObjC method result type. 1454 ObjCParameterContext,// An ObjC method parameter type. 1455 KNRTypeListContext, // K&R type definition list for formals. 1456 TypeNameContext, // Abstract declarator for types. 1457 MemberContext, // Struct/Union field. 1458 BlockContext, // Declaration within a block in a function. 1459 ForContext, // Declaration within first part of a for loop. 1460 ConditionContext, // Condition declaration in a C++ if/switch/while/for. 1461 TemplateParamContext,// Within a template parameter list. 1462 CXXNewContext, // C++ new-expression. 1463 CXXCatchContext, // C++ catch exception-declaration 1464 ObjCCatchContext, // Objective-C catch exception-declaration 1465 BlockLiteralContext, // Block literal declarator. 1466 LambdaExprContext, // Lambda-expression declarator. 1467 TrailingReturnContext, // C++11 trailing-type-specifier. 1468 TemplateTypeArgContext, // Template type argument. 1469 AliasDeclContext, // C++11 alias-declaration. 1470 AliasTemplateContext // C++11 alias-declaration template. 1471 }; 1472 1473private: 1474 const DeclSpec &DS; 1475 CXXScopeSpec SS; 1476 UnqualifiedId Name; 1477 SourceRange Range; 1478 1479 /// \brief Where we are parsing this declarator. 1480 TheContext Context; 1481 1482 /// DeclTypeInfo - This holds each type that the declarator includes as it is 1483 /// parsed. This is pushed from the identifier out, which means that element 1484 /// #0 will be the most closely bound to the identifier, and 1485 /// DeclTypeInfo.back() will be the least closely bound. 1486 SmallVector<DeclaratorChunk, 8> DeclTypeInfo; 1487 1488 /// InvalidType - Set by Sema::GetTypeForDeclarator(). 1489 bool InvalidType : 1; 1490 1491 /// GroupingParens - Set by Parser::ParseParenDeclarator(). 1492 bool GroupingParens : 1; 1493 1494 /// FunctionDefinition - Is this Declarator for a function or member 1495 /// definition and, if so, what kind? 1496 /// 1497 /// Actually a FunctionDefinitionKind. 1498 unsigned FunctionDefinition : 2; 1499 1500 /// \brief Is this Declarator a redeclaration? 1501 bool Redeclaration : 1; 1502 1503 /// Attrs - Attributes. 1504 ParsedAttributes Attrs; 1505 1506 /// \brief The asm label, if specified. 1507 Expr *AsmLabel; 1508 1509 /// InlineParams - This is a local array used for the first function decl 1510 /// chunk to avoid going to the heap for the common case when we have one 1511 /// function chunk in the declarator. 1512 DeclaratorChunk::ParamInfo InlineParams[16]; 1513 bool InlineParamsUsed; 1514 1515 /// \brief true if the declaration is preceded by \c __extension__. 1516 bool Extension : 1; 1517 1518 /// \brief If this is the second or subsequent declarator in this declaration, 1519 /// the location of the comma before this declarator. 1520 SourceLocation CommaLoc; 1521 1522 /// \brief If provided, the source location of the ellipsis used to describe 1523 /// this declarator as a parameter pack. 1524 SourceLocation EllipsisLoc; 1525 1526 friend struct DeclaratorChunk; 1527 1528public: 1529 Declarator(const DeclSpec &ds, TheContext C) 1530 : DS(ds), Range(ds.getSourceRange()), Context(C), 1531 InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error), 1532 GroupingParens(false), FunctionDefinition(FDK_Declaration), 1533 Redeclaration(false), 1534 Attrs(ds.getAttributePool().getFactory()), AsmLabel(0), 1535 InlineParamsUsed(false), Extension(false) { 1536 } 1537 1538 ~Declarator() { 1539 clear(); 1540 } 1541 1542 /// getDeclSpec - Return the declaration-specifier that this declarator was 1543 /// declared with. 1544 const DeclSpec &getDeclSpec() const { return DS; } 1545 1546 /// getMutableDeclSpec - Return a non-const version of the DeclSpec. This 1547 /// should be used with extreme care: declspecs can often be shared between 1548 /// multiple declarators, so mutating the DeclSpec affects all of the 1549 /// Declarators. This should only be done when the declspec is known to not 1550 /// be shared or when in error recovery etc. 1551 DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); } 1552 1553 AttributePool &getAttributePool() const { 1554 return Attrs.getPool(); 1555 } 1556 1557 /// getCXXScopeSpec - Return the C++ scope specifier (global scope or 1558 /// nested-name-specifier) that is part of the declarator-id. 1559 const CXXScopeSpec &getCXXScopeSpec() const { return SS; } 1560 CXXScopeSpec &getCXXScopeSpec() { return SS; } 1561 1562 /// \brief Retrieve the name specified by this declarator. 1563 UnqualifiedId &getName() { return Name; } 1564 1565 TheContext getContext() const { return Context; } 1566 1567 bool isPrototypeContext() const { 1568 return (Context == PrototypeContext || 1569 Context == ObjCParameterContext || 1570 Context == ObjCResultContext); 1571 } 1572 1573 /// \brief Get the source range that spans this declarator. 1574 const SourceRange &getSourceRange() const LLVM_READONLY { return Range; } 1575 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); } 1576 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); } 1577 1578 void SetSourceRange(SourceRange R) { Range = R; } 1579 /// SetRangeBegin - Set the start of the source range to Loc, unless it's 1580 /// invalid. 1581 void SetRangeBegin(SourceLocation Loc) { 1582 if (!Loc.isInvalid()) 1583 Range.setBegin(Loc); 1584 } 1585 /// SetRangeEnd - Set the end of the source range to Loc, unless it's invalid. 1586 void SetRangeEnd(SourceLocation Loc) { 1587 if (!Loc.isInvalid()) 1588 Range.setEnd(Loc); 1589 } 1590 /// ExtendWithDeclSpec - Extend the declarator source range to include the 1591 /// given declspec, unless its location is invalid. Adopts the range start if 1592 /// the current range start is invalid. 1593 void ExtendWithDeclSpec(const DeclSpec &DS) { 1594 const SourceRange &SR = DS.getSourceRange(); 1595 if (Range.getBegin().isInvalid()) 1596 Range.setBegin(SR.getBegin()); 1597 if (!SR.getEnd().isInvalid()) 1598 Range.setEnd(SR.getEnd()); 1599 } 1600 1601 /// \brief Reset the contents of this Declarator. 1602 void clear() { 1603 SS.clear(); 1604 Name.clear(); 1605 Range = DS.getSourceRange(); 1606 1607 for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i) 1608 DeclTypeInfo[i].destroy(); 1609 DeclTypeInfo.clear(); 1610 Attrs.clear(); 1611 AsmLabel = 0; 1612 InlineParamsUsed = false; 1613 CommaLoc = SourceLocation(); 1614 EllipsisLoc = SourceLocation(); 1615 } 1616 1617 /// mayOmitIdentifier - Return true if the identifier is either optional or 1618 /// not allowed. This is true for typenames, prototypes, and template 1619 /// parameter lists. 1620 bool mayOmitIdentifier() const { 1621 switch (Context) { 1622 case FileContext: 1623 case KNRTypeListContext: 1624 case MemberContext: 1625 case BlockContext: 1626 case ForContext: 1627 case ConditionContext: 1628 return false; 1629 1630 case TypeNameContext: 1631 case AliasDeclContext: 1632 case AliasTemplateContext: 1633 case PrototypeContext: 1634 case ObjCParameterContext: 1635 case ObjCResultContext: 1636 case TemplateParamContext: 1637 case CXXNewContext: 1638 case CXXCatchContext: 1639 case ObjCCatchContext: 1640 case BlockLiteralContext: 1641 case LambdaExprContext: 1642 case TemplateTypeArgContext: 1643 case TrailingReturnContext: 1644 return true; 1645 } 1646 llvm_unreachable("unknown context kind!"); 1647 } 1648 1649 /// mayHaveIdentifier - Return true if the identifier is either optional or 1650 /// required. This is true for normal declarators and prototypes, but not 1651 /// typenames. 1652 bool mayHaveIdentifier() const { 1653 switch (Context) { 1654 case FileContext: 1655 case KNRTypeListContext: 1656 case MemberContext: 1657 case BlockContext: 1658 case ForContext: 1659 case ConditionContext: 1660 case PrototypeContext: 1661 case TemplateParamContext: 1662 case CXXCatchContext: 1663 case ObjCCatchContext: 1664 return true; 1665 1666 case TypeNameContext: 1667 case CXXNewContext: 1668 case AliasDeclContext: 1669 case AliasTemplateContext: 1670 case ObjCParameterContext: 1671 case ObjCResultContext: 1672 case BlockLiteralContext: 1673 case LambdaExprContext: 1674 case TemplateTypeArgContext: 1675 case TrailingReturnContext: 1676 return false; 1677 } 1678 llvm_unreachable("unknown context kind!"); 1679 } 1680 1681 /// mayBeFollowedByCXXDirectInit - Return true if the declarator can be 1682 /// followed by a C++ direct initializer, e.g. "int x(1);". 1683 bool mayBeFollowedByCXXDirectInit() const { 1684 if (hasGroupingParens()) return false; 1685 1686 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) 1687 return false; 1688 1689 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern && 1690 Context != FileContext) 1691 return false; 1692 1693 // Special names can't have direct initializers. 1694 if (Name.getKind() != UnqualifiedId::IK_Identifier) 1695 return false; 1696 1697 switch (Context) { 1698 case FileContext: 1699 case BlockContext: 1700 case ForContext: 1701 return true; 1702 1703 case ConditionContext: 1704 // This may not be followed by a direct initializer, but it can't be a 1705 // function declaration either, and we'd prefer to perform a tentative 1706 // parse in order to produce the right diagnostic. 1707 return true; 1708 1709 case KNRTypeListContext: 1710 case MemberContext: 1711 case PrototypeContext: 1712 case ObjCParameterContext: 1713 case ObjCResultContext: 1714 case TemplateParamContext: 1715 case CXXCatchContext: 1716 case ObjCCatchContext: 1717 case TypeNameContext: 1718 case CXXNewContext: 1719 case AliasDeclContext: 1720 case AliasTemplateContext: 1721 case BlockLiteralContext: 1722 case LambdaExprContext: 1723 case TemplateTypeArgContext: 1724 case TrailingReturnContext: 1725 return false; 1726 } 1727 llvm_unreachable("unknown context kind!"); 1728 } 1729 1730 /// isPastIdentifier - Return true if we have parsed beyond the point where 1731 /// the 1732 bool isPastIdentifier() const { return Name.isValid(); } 1733 1734 /// hasName - Whether this declarator has a name, which might be an 1735 /// identifier (accessible via getIdentifier()) or some kind of 1736 /// special C++ name (constructor, destructor, etc.). 1737 bool hasName() const { 1738 return Name.getKind() != UnqualifiedId::IK_Identifier || Name.Identifier; 1739 } 1740 1741 IdentifierInfo *getIdentifier() const { 1742 if (Name.getKind() == UnqualifiedId::IK_Identifier) 1743 return Name.Identifier; 1744 1745 return 0; 1746 } 1747 SourceLocation getIdentifierLoc() const { return Name.StartLocation; } 1748 1749 /// \brief Set the name of this declarator to be the given identifier. 1750 void SetIdentifier(IdentifierInfo *Id, SourceLocation IdLoc) { 1751 Name.setIdentifier(Id, IdLoc); 1752 } 1753 1754 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to 1755 /// EndLoc, which should be the last token of the chunk. 1756 void AddTypeInfo(const DeclaratorChunk &TI, 1757 ParsedAttributes &attrs, 1758 SourceLocation EndLoc) { 1759 DeclTypeInfo.push_back(TI); 1760 DeclTypeInfo.back().getAttrListRef() = attrs.getList(); 1761 getAttributePool().takeAllFrom(attrs.getPool()); 1762 1763 if (!EndLoc.isInvalid()) 1764 SetRangeEnd(EndLoc); 1765 } 1766 1767 /// \brief Add a new innermost chunk to this declarator. 1768 void AddInnermostTypeInfo(const DeclaratorChunk &TI) { 1769 DeclTypeInfo.insert(DeclTypeInfo.begin(), TI); 1770 } 1771 1772 /// \brief Return the number of types applied to this declarator. 1773 unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); } 1774 1775 /// Return the specified TypeInfo from this declarator. TypeInfo #0 is 1776 /// closest to the identifier. 1777 const DeclaratorChunk &getTypeObject(unsigned i) const { 1778 assert(i < DeclTypeInfo.size() && "Invalid type chunk"); 1779 return DeclTypeInfo[i]; 1780 } 1781 DeclaratorChunk &getTypeObject(unsigned i) { 1782 assert(i < DeclTypeInfo.size() && "Invalid type chunk"); 1783 return DeclTypeInfo[i]; 1784 } 1785 1786 void DropFirstTypeObject() 1787 { 1788 assert(!DeclTypeInfo.empty() && "No type chunks to drop."); 1789 DeclTypeInfo.front().destroy(); 1790 DeclTypeInfo.erase(DeclTypeInfo.begin()); 1791 } 1792 1793 /// isArrayOfUnknownBound - This method returns true if the declarator 1794 /// is a declarator for an array of unknown bound (looking through 1795 /// parentheses). 1796 bool isArrayOfUnknownBound() const { 1797 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) { 1798 switch (DeclTypeInfo[i].Kind) { 1799 case DeclaratorChunk::Paren: 1800 continue; 1801 case DeclaratorChunk::Function: 1802 case DeclaratorChunk::Pointer: 1803 case DeclaratorChunk::Reference: 1804 case DeclaratorChunk::BlockPointer: 1805 case DeclaratorChunk::MemberPointer: 1806 return false; 1807 case DeclaratorChunk::Array: 1808 return !DeclTypeInfo[i].Arr.NumElts; 1809 } 1810 llvm_unreachable("Invalid type chunk"); 1811 } 1812 return false; 1813 } 1814 1815 /// isFunctionDeclarator - This method returns true if the declarator 1816 /// is a function declarator (looking through parentheses). 1817 /// If true is returned, then the reference type parameter idx is 1818 /// assigned with the index of the declaration chunk. 1819 bool isFunctionDeclarator(unsigned& idx) const { 1820 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) { 1821 switch (DeclTypeInfo[i].Kind) { 1822 case DeclaratorChunk::Function: 1823 idx = i; 1824 return true; 1825 case DeclaratorChunk::Paren: 1826 continue; 1827 case DeclaratorChunk::Pointer: 1828 case DeclaratorChunk::Reference: 1829 case DeclaratorChunk::Array: 1830 case DeclaratorChunk::BlockPointer: 1831 case DeclaratorChunk::MemberPointer: 1832 return false; 1833 } 1834 llvm_unreachable("Invalid type chunk"); 1835 } 1836 return false; 1837 } 1838 1839 /// isFunctionDeclarator - Once this declarator is fully parsed and formed, 1840 /// this method returns true if the identifier is a function declarator 1841 /// (looking through parentheses). 1842 bool isFunctionDeclarator() const { 1843 unsigned index; 1844 return isFunctionDeclarator(index); 1845 } 1846 1847 /// getFunctionTypeInfo - Retrieves the function type info object 1848 /// (looking through parentheses). 1849 DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() { 1850 assert(isFunctionDeclarator() && "Not a function declarator!"); 1851 unsigned index = 0; 1852 isFunctionDeclarator(index); 1853 return DeclTypeInfo[index].Fun; 1854 } 1855 1856 /// getFunctionTypeInfo - Retrieves the function type info object 1857 /// (looking through parentheses). 1858 const DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() const { 1859 return const_cast<Declarator*>(this)->getFunctionTypeInfo(); 1860 } 1861 1862 /// \brief Determine whether the declaration that will be produced from 1863 /// this declaration will be a function. 1864 /// 1865 /// A declaration can declare a function even if the declarator itself 1866 /// isn't a function declarator, if the type specifier refers to a function 1867 /// type. This routine checks for both cases. 1868 bool isDeclarationOfFunction() const; 1869 1870 /// takeAttributes - Takes attributes from the given parsed-attributes 1871 /// set and add them to this declarator. 1872 /// 1873 /// These examples both add 3 attributes to "var": 1874 /// short int var __attribute__((aligned(16),common,deprecated)); 1875 /// short int x, __attribute__((aligned(16)) var 1876 /// __attribute__((common,deprecated)); 1877 /// 1878 /// Also extends the range of the declarator. 1879 void takeAttributes(ParsedAttributes &attrs, SourceLocation lastLoc) { 1880 Attrs.takeAllFrom(attrs); 1881 1882 if (!lastLoc.isInvalid()) 1883 SetRangeEnd(lastLoc); 1884 } 1885 1886 const AttributeList *getAttributes() const { return Attrs.getList(); } 1887 AttributeList *getAttributes() { return Attrs.getList(); } 1888 1889 AttributeList *&getAttrListRef() { return Attrs.getListRef(); } 1890 1891 /// hasAttributes - do we contain any attributes? 1892 bool hasAttributes() const { 1893 if (getAttributes() || getDeclSpec().hasAttributes()) return true; 1894 for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i) 1895 if (getTypeObject(i).getAttrs()) 1896 return true; 1897 return false; 1898 } 1899 1900 void setAsmLabel(Expr *E) { AsmLabel = E; } 1901 Expr *getAsmLabel() const { return AsmLabel; } 1902 1903 void setExtension(bool Val = true) { Extension = Val; } 1904 bool getExtension() const { return Extension; } 1905 1906 void setInvalidType(bool Val = true) { InvalidType = Val; } 1907 bool isInvalidType() const { 1908 return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error; 1909 } 1910 1911 void setGroupingParens(bool flag) { GroupingParens = flag; } 1912 bool hasGroupingParens() const { return GroupingParens; } 1913 1914 bool isFirstDeclarator() const { return !CommaLoc.isValid(); } 1915 SourceLocation getCommaLoc() const { return CommaLoc; } 1916 void setCommaLoc(SourceLocation CL) { CommaLoc = CL; } 1917 1918 bool hasEllipsis() const { return EllipsisLoc.isValid(); } 1919 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 1920 void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; } 1921 1922 void setFunctionDefinitionKind(FunctionDefinitionKind Val) { 1923 FunctionDefinition = Val; 1924 } 1925 1926 bool isFunctionDefinition() const { 1927 return getFunctionDefinitionKind() != FDK_Declaration; 1928 } 1929 1930 FunctionDefinitionKind getFunctionDefinitionKind() const { 1931 return (FunctionDefinitionKind)FunctionDefinition; 1932 } 1933 1934 void setRedeclaration(bool Val) { Redeclaration = Val; } 1935 bool isRedeclaration() const { return Redeclaration; } 1936}; 1937 1938/// \brief This little struct is used to capture information about 1939/// structure field declarators, which is basically just a bitfield size. 1940struct FieldDeclarator { 1941 Declarator D; 1942 Expr *BitfieldSize; 1943 explicit FieldDeclarator(const DeclSpec &DS) 1944 : D(DS, Declarator::MemberContext), BitfieldSize(0) { } 1945}; 1946 1947/// \brief Represents a C++11 virt-specifier-seq. 1948class VirtSpecifiers { 1949public: 1950 enum Specifier { 1951 VS_None = 0, 1952 VS_Override = 1, 1953 VS_Final = 2 1954 }; 1955 1956 VirtSpecifiers() : Specifiers(0) { } 1957 1958 bool SetSpecifier(Specifier VS, SourceLocation Loc, 1959 const char *&PrevSpec); 1960 1961 bool isOverrideSpecified() const { return Specifiers & VS_Override; } 1962 SourceLocation getOverrideLoc() const { return VS_overrideLoc; } 1963 1964 bool isFinalSpecified() const { return Specifiers & VS_Final; } 1965 SourceLocation getFinalLoc() const { return VS_finalLoc; } 1966 1967 void clear() { Specifiers = 0; } 1968 1969 static const char *getSpecifierName(Specifier VS); 1970 1971 SourceLocation getLastLocation() const { return LastLocation; } 1972 1973private: 1974 unsigned Specifiers; 1975 1976 SourceLocation VS_overrideLoc, VS_finalLoc; 1977 SourceLocation LastLocation; 1978}; 1979 1980/// \brief An individual capture in a lambda introducer. 1981struct LambdaCapture { 1982 LambdaCaptureKind Kind; 1983 SourceLocation Loc; 1984 IdentifierInfo* Id; 1985 SourceLocation EllipsisLoc; 1986 1987 LambdaCapture(LambdaCaptureKind Kind, SourceLocation Loc, 1988 IdentifierInfo* Id = 0, 1989 SourceLocation EllipsisLoc = SourceLocation()) 1990 : Kind(Kind), Loc(Loc), Id(Id), EllipsisLoc(EllipsisLoc) 1991 {} 1992}; 1993 1994/// \brief Represents a complete lambda introducer. 1995struct LambdaIntroducer { 1996 SourceRange Range; 1997 SourceLocation DefaultLoc; 1998 LambdaCaptureDefault Default; 1999 llvm::SmallVector<LambdaCapture, 4> Captures; 2000 2001 LambdaIntroducer() 2002 : Default(LCD_None) {} 2003 2004 /// \brief Append a capture in a lambda introducer. 2005 void addCapture(LambdaCaptureKind Kind, 2006 SourceLocation Loc, 2007 IdentifierInfo* Id = 0, 2008 SourceLocation EllipsisLoc = SourceLocation()) { 2009 Captures.push_back(LambdaCapture(Kind, Loc, Id, EllipsisLoc)); 2010 } 2011 2012}; 2013 2014} // end namespace clang 2015 2016#endif 2017