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