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