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