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