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