DeclSpec.h revision 162e1c1b487352434552147967c3dd296ebee2f7
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 declarations 656 enum ObjCDeclQualifier { 657 DQ_None = 0x0, 658 DQ_In = 0x1, 659 DQ_Inout = 0x2, 660 DQ_Out = 0x4, 661 DQ_Bycopy = 0x8, 662 DQ_Byref = 0x10, 663 DQ_Oneway = 0x20 664 }; 665 666 /// PropertyAttributeKind - list of property attributes. 667 enum ObjCPropertyAttributeKind { DQ_PR_noattr = 0x0, 668 DQ_PR_readonly = 0x01, 669 DQ_PR_getter = 0x02, 670 DQ_PR_assign = 0x04, 671 DQ_PR_readwrite = 0x08, 672 DQ_PR_retain = 0x10, 673 DQ_PR_copy = 0x20, 674 DQ_PR_nonatomic = 0x40, 675 DQ_PR_setter = 0x80, 676 DQ_PR_atomic = 0x100 677 }; 678 679 680 ObjCDeclSpec() 681 : objcDeclQualifier(DQ_None), PropertyAttributes(DQ_PR_noattr), 682 GetterName(0), SetterName(0) { } 683 ObjCDeclQualifier getObjCDeclQualifier() const { return objcDeclQualifier; } 684 void setObjCDeclQualifier(ObjCDeclQualifier DQVal) { 685 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal); 686 } 687 688 ObjCPropertyAttributeKind getPropertyAttributes() const { 689 return ObjCPropertyAttributeKind(PropertyAttributes); 690 } 691 void setPropertyAttributes(ObjCPropertyAttributeKind PRVal) { 692 PropertyAttributes = 693 (ObjCPropertyAttributeKind)(PropertyAttributes | PRVal); 694 } 695 696 const IdentifierInfo *getGetterName() const { return GetterName; } 697 IdentifierInfo *getGetterName() { return GetterName; } 698 void setGetterName(IdentifierInfo *name) { GetterName = name; } 699 700 const IdentifierInfo *getSetterName() const { return SetterName; } 701 IdentifierInfo *getSetterName() { return SetterName; } 702 void setSetterName(IdentifierInfo *name) { SetterName = name; } 703private: 704 // FIXME: These two are unrelated and mutially exclusive. So perhaps 705 // we can put them in a union to reflect their mutual exclusiveness 706 // (space saving is negligible). 707 ObjCDeclQualifier objcDeclQualifier : 6; 708 709 // NOTE: VC++ treats enums as signed, avoid using ObjCPropertyAttributeKind 710 unsigned PropertyAttributes : 9; 711 IdentifierInfo *GetterName; // getter name of NULL if no getter 712 IdentifierInfo *SetterName; // setter name of NULL if no setter 713}; 714 715/// \brief Represents a C++ unqualified-id that has been parsed. 716class UnqualifiedId { 717private: 718 const UnqualifiedId &operator=(const UnqualifiedId &); // DO NOT IMPLEMENT 719 720public: 721 /// \brief Describes the kind of unqualified-id parsed. 722 enum IdKind { 723 /// \brief An identifier. 724 IK_Identifier, 725 /// \brief An overloaded operator name, e.g., operator+. 726 IK_OperatorFunctionId, 727 /// \brief A conversion function name, e.g., operator int. 728 IK_ConversionFunctionId, 729 /// \brief A user-defined literal name, e.g., operator "" _i. 730 IK_LiteralOperatorId, 731 /// \brief A constructor name. 732 IK_ConstructorName, 733 /// \brief A constructor named via a template-id. 734 IK_ConstructorTemplateId, 735 /// \brief A destructor name. 736 IK_DestructorName, 737 /// \brief A template-id, e.g., f<int>. 738 IK_TemplateId 739 } Kind; 740 741 /// \brief Anonymous union that holds extra data associated with the 742 /// parsed unqualified-id. 743 union { 744 /// \brief When Kind == IK_Identifier, the parsed identifier, or when Kind 745 /// == IK_UserLiteralId, the identifier suffix. 746 IdentifierInfo *Identifier; 747 748 /// \brief When Kind == IK_OperatorFunctionId, the overloaded operator 749 /// that we parsed. 750 struct { 751 /// \brief The kind of overloaded operator. 752 OverloadedOperatorKind Operator; 753 754 /// \brief The source locations of the individual tokens that name 755 /// the operator, e.g., the "new", "[", and "]" tokens in 756 /// operator new []. 757 /// 758 /// Different operators have different numbers of tokens in their name, 759 /// up to three. Any remaining source locations in this array will be 760 /// set to an invalid value for operators with fewer than three tokens. 761 unsigned SymbolLocations[3]; 762 } OperatorFunctionId; 763 764 /// \brief When Kind == IK_ConversionFunctionId, the type that the 765 /// conversion function names. 766 UnionParsedType ConversionFunctionId; 767 768 /// \brief When Kind == IK_ConstructorName, the class-name of the type 769 /// whose constructor is being referenced. 770 UnionParsedType ConstructorName; 771 772 /// \brief When Kind == IK_DestructorName, the type referred to by the 773 /// class-name. 774 UnionParsedType DestructorName; 775 776 /// \brief When Kind == IK_TemplateId or IK_ConstructorTemplateId, 777 /// the template-id annotation that contains the template name and 778 /// template arguments. 779 TemplateIdAnnotation *TemplateId; 780 }; 781 782 /// \brief The location of the first token that describes this unqualified-id, 783 /// which will be the location of the identifier, "operator" keyword, 784 /// tilde (for a destructor), or the template name of a template-id. 785 SourceLocation StartLocation; 786 787 /// \brief The location of the last token that describes this unqualified-id. 788 SourceLocation EndLocation; 789 790 UnqualifiedId() : Kind(IK_Identifier), Identifier(0) { } 791 792 /// \brief Do not use this copy constructor. It is temporary, and only 793 /// exists because we are holding FieldDeclarators in a SmallVector when we 794 /// don't actually need them. 795 /// 796 /// FIXME: Kill this copy constructor. 797 UnqualifiedId(const UnqualifiedId &Other) 798 : Kind(IK_Identifier), Identifier(Other.Identifier), 799 StartLocation(Other.StartLocation), EndLocation(Other.EndLocation) { 800 assert(Other.Kind == IK_Identifier && "Cannot copy non-identifiers"); 801 } 802 803 /// \brief Destroy this unqualified-id. 804 ~UnqualifiedId() { clear(); } 805 806 /// \brief Clear out this unqualified-id, setting it to default (invalid) 807 /// state. 808 void clear(); 809 810 /// \brief Determine whether this unqualified-id refers to a valid name. 811 bool isValid() const { return StartLocation.isValid(); } 812 813 /// \brief Determine whether this unqualified-id refers to an invalid name. 814 bool isInvalid() const { return !isValid(); } 815 816 /// \brief Determine what kind of name we have. 817 IdKind getKind() const { return Kind; } 818 819 /// \brief Specify that this unqualified-id was parsed as an identifier. 820 /// 821 /// \param Id the parsed identifier. 822 /// \param IdLoc the location of the parsed identifier. 823 void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) { 824 Kind = IK_Identifier; 825 Identifier = const_cast<IdentifierInfo *>(Id); 826 StartLocation = EndLocation = IdLoc; 827 } 828 829 /// \brief Specify that this unqualified-id was parsed as an 830 /// operator-function-id. 831 /// 832 /// \param OperatorLoc the location of the 'operator' keyword. 833 /// 834 /// \param Op the overloaded operator. 835 /// 836 /// \param SymbolLocations the locations of the individual operator symbols 837 /// in the operator. 838 void setOperatorFunctionId(SourceLocation OperatorLoc, 839 OverloadedOperatorKind Op, 840 SourceLocation SymbolLocations[3]); 841 842 /// \brief Specify that this unqualified-id was parsed as a 843 /// conversion-function-id. 844 /// 845 /// \param OperatorLoc the location of the 'operator' keyword. 846 /// 847 /// \param Ty the type to which this conversion function is converting. 848 /// 849 /// \param EndLoc the location of the last token that makes up the type name. 850 void setConversionFunctionId(SourceLocation OperatorLoc, 851 ParsedType Ty, 852 SourceLocation EndLoc) { 853 Kind = IK_ConversionFunctionId; 854 StartLocation = OperatorLoc; 855 EndLocation = EndLoc; 856 ConversionFunctionId = Ty; 857 } 858 859 /// \brief Specific that this unqualified-id was parsed as a 860 /// literal-operator-id. 861 /// 862 /// \param Id the parsed identifier. 863 /// 864 /// \param OpLoc the location of the 'operator' keyword. 865 /// 866 /// \param IdLoc the location of the identifier. 867 void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc, 868 SourceLocation IdLoc) { 869 Kind = IK_LiteralOperatorId; 870 Identifier = const_cast<IdentifierInfo *>(Id); 871 StartLocation = OpLoc; 872 EndLocation = IdLoc; 873 } 874 875 /// \brief Specify that this unqualified-id was parsed as a constructor name. 876 /// 877 /// \param ClassType the class type referred to by the constructor name. 878 /// 879 /// \param ClassNameLoc the location of the class name. 880 /// 881 /// \param EndLoc the location of the last token that makes up the type name. 882 void setConstructorName(ParsedType ClassType, 883 SourceLocation ClassNameLoc, 884 SourceLocation EndLoc) { 885 Kind = IK_ConstructorName; 886 StartLocation = ClassNameLoc; 887 EndLocation = EndLoc; 888 ConstructorName = ClassType; 889 } 890 891 /// \brief Specify that this unqualified-id was parsed as a 892 /// template-id that names a constructor. 893 /// 894 /// \param TemplateId the template-id annotation that describes the parsed 895 /// template-id. This UnqualifiedId instance will take ownership of the 896 /// \p TemplateId and will free it on destruction. 897 void setConstructorTemplateId(TemplateIdAnnotation *TemplateId); 898 899 /// \brief Specify that this unqualified-id was parsed as a destructor name. 900 /// 901 /// \param TildeLoc the location of the '~' that introduces the destructor 902 /// name. 903 /// 904 /// \param ClassType the name of the class referred to by the destructor name. 905 void setDestructorName(SourceLocation TildeLoc, 906 ParsedType ClassType, 907 SourceLocation EndLoc) { 908 Kind = IK_DestructorName; 909 StartLocation = TildeLoc; 910 EndLocation = EndLoc; 911 DestructorName = ClassType; 912 } 913 914 /// \brief Specify that this unqualified-id was parsed as a template-id. 915 /// 916 /// \param TemplateId the template-id annotation that describes the parsed 917 /// template-id. This UnqualifiedId instance will take ownership of the 918 /// \p TemplateId and will free it on destruction. 919 void setTemplateId(TemplateIdAnnotation *TemplateId); 920 921 /// \brief Return the source range that covers this unqualified-id. 922 SourceRange getSourceRange() const { 923 return SourceRange(StartLocation, EndLocation); 924 } 925}; 926 927/// CachedTokens - A set of tokens that has been cached for later 928/// parsing. 929typedef llvm::SmallVector<Token, 4> CachedTokens; 930 931/// DeclaratorChunk - One instance of this struct is used for each type in a 932/// declarator that is parsed. 933/// 934/// This is intended to be a small value object. 935struct DeclaratorChunk { 936 enum { 937 Pointer, Reference, Array, Function, BlockPointer, MemberPointer, Paren 938 } Kind; 939 940 /// Loc - The place where this type was defined. 941 SourceLocation Loc; 942 /// EndLoc - If valid, the place where this chunck ends. 943 SourceLocation EndLoc; 944 945 struct TypeInfoCommon { 946 AttributeList *AttrList; 947 }; 948 949 struct PointerTypeInfo : TypeInfoCommon { 950 /// The type qualifiers: const/volatile/restrict. 951 unsigned TypeQuals : 3; 952 953 /// The location of the const-qualifier, if any. 954 unsigned ConstQualLoc; 955 956 /// The location of the volatile-qualifier, if any. 957 unsigned VolatileQualLoc; 958 959 /// The location of the restrict-qualifier, if any. 960 unsigned RestrictQualLoc; 961 962 void destroy() { 963 } 964 }; 965 966 struct ReferenceTypeInfo : TypeInfoCommon { 967 /// The type qualifier: restrict. [GNU] C++ extension 968 bool HasRestrict : 1; 969 /// True if this is an lvalue reference, false if it's an rvalue reference. 970 bool LValueRef : 1; 971 void destroy() { 972 } 973 }; 974 975 struct ArrayTypeInfo : TypeInfoCommon { 976 /// The type qualifiers for the array: const/volatile/restrict. 977 unsigned TypeQuals : 3; 978 979 /// True if this dimension included the 'static' keyword. 980 bool hasStatic : 1; 981 982 /// True if this dimension was [*]. In this case, NumElts is null. 983 bool isStar : 1; 984 985 /// This is the size of the array, or null if [] or [*] was specified. 986 /// Since the parser is multi-purpose, and we don't want to impose a root 987 /// expression class on all clients, NumElts is untyped. 988 Expr *NumElts; 989 990 void destroy() {} 991 }; 992 993 /// ParamInfo - An array of paraminfo objects is allocated whenever a function 994 /// declarator is parsed. There are two interesting styles of arguments here: 995 /// K&R-style identifier lists and parameter type lists. K&R-style identifier 996 /// lists will have information about the identifier, but no type information. 997 /// Parameter type lists will have type info (if the actions module provides 998 /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'. 999 struct ParamInfo { 1000 IdentifierInfo *Ident; 1001 SourceLocation IdentLoc; 1002 Decl *Param; 1003 1004 /// DefaultArgTokens - When the parameter's default argument 1005 /// cannot be parsed immediately (because it occurs within the 1006 /// declaration of a member function), it will be stored here as a 1007 /// sequence of tokens to be parsed once the class definition is 1008 /// complete. Non-NULL indicates that there is a default argument. 1009 CachedTokens *DefaultArgTokens; 1010 1011 ParamInfo() {} 1012 ParamInfo(IdentifierInfo *ident, SourceLocation iloc, 1013 Decl *param, 1014 CachedTokens *DefArgTokens = 0) 1015 : Ident(ident), IdentLoc(iloc), Param(param), 1016 DefaultArgTokens(DefArgTokens) {} 1017 }; 1018 1019 struct TypeAndRange { 1020 ParsedType Ty; 1021 SourceRange Range; 1022 }; 1023 1024 struct FunctionTypeInfo : TypeInfoCommon { 1025 /// hasPrototype - This is true if the function had at least one typed 1026 /// argument. If the function is () or (a,b,c), then it has no prototype, 1027 /// and is treated as a K&R-style function. 1028 unsigned hasPrototype : 1; 1029 1030 /// isVariadic - If this function has a prototype, and if that 1031 /// proto ends with ',...)', this is true. When true, EllipsisLoc 1032 /// contains the location of the ellipsis. 1033 unsigned isVariadic : 1; 1034 1035 /// \brief Whether the ref-qualifier (if any) is an lvalue reference. 1036 /// Otherwise, it's an rvalue reference. 1037 unsigned RefQualifierIsLValueRef : 1; 1038 1039 /// The type qualifiers: const/volatile/restrict. 1040 /// The qualifier bitmask values are the same as in QualType. 1041 unsigned TypeQuals : 3; 1042 1043 /// ExceptionSpecType - An ExceptionSpecificationType value. 1044 unsigned ExceptionSpecType : 3; 1045 1046 /// DeleteArgInfo - If this is true, we need to delete[] ArgInfo. 1047 unsigned DeleteArgInfo : 1; 1048 1049 /// When isVariadic is true, the location of the ellipsis in the source. 1050 unsigned EllipsisLoc; 1051 1052 /// NumArgs - This is the number of formal arguments provided for the 1053 /// declarator. 1054 unsigned NumArgs; 1055 1056 /// NumExceptions - This is the number of types in the dynamic-exception- 1057 /// decl, if the function has one. 1058 unsigned NumExceptions; 1059 1060 /// \brief The location of the ref-qualifier, if any. 1061 /// 1062 /// If this is an invalid location, there is no ref-qualifier. 1063 unsigned RefQualifierLoc; 1064 1065 /// \brief When ExceptionSpecType isn't EST_None, the location of the 1066 /// keyword introducing the spec. 1067 unsigned ExceptionSpecLoc; 1068 1069 /// ArgInfo - This is a pointer to a new[]'d array of ParamInfo objects that 1070 /// describe the arguments for this function declarator. This is null if 1071 /// there are no arguments specified. 1072 ParamInfo *ArgInfo; 1073 1074 union { 1075 /// \brief Pointer to a new[]'d array of TypeAndRange objects that 1076 /// contain the types in the function's dynamic exception specification 1077 /// and their locations, if there is one. 1078 TypeAndRange *Exceptions; 1079 1080 /// \brief Pointer to the expression in the noexcept-specifier of this 1081 /// function, if it has one. 1082 Expr *NoexceptExpr; 1083 }; 1084 1085 /// TrailingReturnType - If this isn't null, it's the trailing return type 1086 /// specified. This is actually a ParsedType, but stored as void* to 1087 /// allow union storage. 1088 void *TrailingReturnType; 1089 1090 /// freeArgs - reset the argument list to having zero arguments. This is 1091 /// used in various places for error recovery. 1092 void freeArgs() { 1093 if (DeleteArgInfo) { 1094 delete[] ArgInfo; 1095 DeleteArgInfo = false; 1096 } 1097 NumArgs = 0; 1098 } 1099 1100 void destroy() { 1101 if (DeleteArgInfo) 1102 delete[] ArgInfo; 1103 if (getExceptionSpecType() == EST_Dynamic) 1104 delete[] Exceptions; 1105 } 1106 1107 /// isKNRPrototype - Return true if this is a K&R style identifier list, 1108 /// like "void foo(a,b,c)". In a function definition, this will be followed 1109 /// by the argument type definitions. 1110 bool isKNRPrototype() const { 1111 return !hasPrototype && NumArgs != 0; 1112 } 1113 1114 SourceLocation getEllipsisLoc() const { 1115 return SourceLocation::getFromRawEncoding(EllipsisLoc); 1116 } 1117 SourceLocation getExceptionSpecLoc() const { 1118 return SourceLocation::getFromRawEncoding(ExceptionSpecLoc); 1119 } 1120 1121 /// \brief Retrieve the location of the ref-qualifier, if any. 1122 SourceLocation getRefQualifierLoc() const { 1123 return SourceLocation::getFromRawEncoding(RefQualifierLoc); 1124 } 1125 1126 /// \brief Determine whether this function declaration contains a 1127 /// ref-qualifier. 1128 bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); } 1129 1130 /// \brief Get the type of exception specification this function has. 1131 ExceptionSpecificationType getExceptionSpecType() const { 1132 return static_cast<ExceptionSpecificationType>(ExceptionSpecType); 1133 } 1134 }; 1135 1136 struct BlockPointerTypeInfo : TypeInfoCommon { 1137 /// For now, sema will catch these as invalid. 1138 /// The type qualifiers: const/volatile/restrict. 1139 unsigned TypeQuals : 3; 1140 1141 void destroy() { 1142 } 1143 }; 1144 1145 struct MemberPointerTypeInfo : TypeInfoCommon { 1146 /// The type qualifiers: const/volatile/restrict. 1147 unsigned TypeQuals : 3; 1148 // CXXScopeSpec has a constructor, so it can't be a direct member. 1149 // So we need some pointer-aligned storage and a bit of trickery. 1150 union { 1151 void *Aligner; 1152 char Mem[sizeof(CXXScopeSpec)]; 1153 } ScopeMem; 1154 CXXScopeSpec &Scope() { 1155 return *reinterpret_cast<CXXScopeSpec*>(ScopeMem.Mem); 1156 } 1157 const CXXScopeSpec &Scope() const { 1158 return *reinterpret_cast<const CXXScopeSpec*>(ScopeMem.Mem); 1159 } 1160 void destroy() { 1161 Scope().~CXXScopeSpec(); 1162 } 1163 }; 1164 1165 union { 1166 TypeInfoCommon Common; 1167 PointerTypeInfo Ptr; 1168 ReferenceTypeInfo Ref; 1169 ArrayTypeInfo Arr; 1170 FunctionTypeInfo Fun; 1171 BlockPointerTypeInfo Cls; 1172 MemberPointerTypeInfo Mem; 1173 }; 1174 1175 void destroy() { 1176 switch (Kind) { 1177 default: assert(0 && "Unknown decl type!"); 1178 case DeclaratorChunk::Function: return Fun.destroy(); 1179 case DeclaratorChunk::Pointer: return Ptr.destroy(); 1180 case DeclaratorChunk::BlockPointer: return Cls.destroy(); 1181 case DeclaratorChunk::Reference: return Ref.destroy(); 1182 case DeclaratorChunk::Array: return Arr.destroy(); 1183 case DeclaratorChunk::MemberPointer: return Mem.destroy(); 1184 case DeclaratorChunk::Paren: return; 1185 } 1186 } 1187 1188 /// getAttrs - If there are attributes applied to this declaratorchunk, return 1189 /// them. 1190 const AttributeList *getAttrs() const { 1191 return Common.AttrList; 1192 } 1193 1194 AttributeList *&getAttrListRef() { 1195 return Common.AttrList; 1196 } 1197 1198 /// getPointer - Return a DeclaratorChunk for a pointer. 1199 /// 1200 static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc, 1201 SourceLocation ConstQualLoc, 1202 SourceLocation VolatileQualLoc, 1203 SourceLocation RestrictQualLoc) { 1204 DeclaratorChunk I; 1205 I.Kind = Pointer; 1206 I.Loc = Loc; 1207 I.Ptr.TypeQuals = TypeQuals; 1208 I.Ptr.ConstQualLoc = ConstQualLoc.getRawEncoding(); 1209 I.Ptr.VolatileQualLoc = VolatileQualLoc.getRawEncoding(); 1210 I.Ptr.RestrictQualLoc = RestrictQualLoc.getRawEncoding(); 1211 I.Ptr.AttrList = 0; 1212 return I; 1213 } 1214 1215 /// getReference - Return a DeclaratorChunk for a reference. 1216 /// 1217 static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc, 1218 bool lvalue) { 1219 DeclaratorChunk I; 1220 I.Kind = Reference; 1221 I.Loc = Loc; 1222 I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0; 1223 I.Ref.LValueRef = lvalue; 1224 I.Ref.AttrList = 0; 1225 return I; 1226 } 1227 1228 /// getArray - Return a DeclaratorChunk for an array. 1229 /// 1230 static DeclaratorChunk getArray(unsigned TypeQuals, 1231 bool isStatic, bool isStar, Expr *NumElts, 1232 SourceLocation LBLoc, SourceLocation RBLoc) { 1233 DeclaratorChunk I; 1234 I.Kind = Array; 1235 I.Loc = LBLoc; 1236 I.EndLoc = RBLoc; 1237 I.Arr.AttrList = 0; 1238 I.Arr.TypeQuals = TypeQuals; 1239 I.Arr.hasStatic = isStatic; 1240 I.Arr.isStar = isStar; 1241 I.Arr.NumElts = NumElts; 1242 return I; 1243 } 1244 1245 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function. 1246 /// "TheDeclarator" is the declarator that this will be added to. 1247 static DeclaratorChunk getFunction(bool hasProto, bool isVariadic, 1248 SourceLocation EllipsisLoc, 1249 ParamInfo *ArgInfo, unsigned NumArgs, 1250 unsigned TypeQuals, 1251 bool RefQualifierIsLvalueRef, 1252 SourceLocation RefQualifierLoc, 1253 ExceptionSpecificationType ESpecType, 1254 SourceLocation ESpecLoc, 1255 ParsedType *Exceptions, 1256 SourceRange *ExceptionRanges, 1257 unsigned NumExceptions, 1258 Expr *NoexceptExpr, 1259 SourceLocation LocalRangeBegin, 1260 SourceLocation LocalRangeEnd, 1261 Declarator &TheDeclarator, 1262 ParsedType TrailingReturnType = 1263 ParsedType()); 1264 1265 /// getBlockPointer - Return a DeclaratorChunk for a block. 1266 /// 1267 static DeclaratorChunk getBlockPointer(unsigned TypeQuals, 1268 SourceLocation Loc) { 1269 DeclaratorChunk I; 1270 I.Kind = BlockPointer; 1271 I.Loc = Loc; 1272 I.Cls.TypeQuals = TypeQuals; 1273 I.Cls.AttrList = 0; 1274 return I; 1275 } 1276 1277 static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS, 1278 unsigned TypeQuals, 1279 SourceLocation Loc) { 1280 DeclaratorChunk I; 1281 I.Kind = MemberPointer; 1282 I.Loc = Loc; 1283 I.Mem.TypeQuals = TypeQuals; 1284 I.Mem.AttrList = 0; 1285 new (I.Mem.ScopeMem.Mem) CXXScopeSpec(SS); 1286 return I; 1287 } 1288 1289 /// getParen - Return a DeclaratorChunk for a paren. 1290 /// 1291 static DeclaratorChunk getParen(SourceLocation LParenLoc, 1292 SourceLocation RParenLoc) { 1293 DeclaratorChunk I; 1294 I.Kind = Paren; 1295 I.Loc = LParenLoc; 1296 I.EndLoc = RParenLoc; 1297 I.Common.AttrList = 0; 1298 return I; 1299 } 1300 1301}; 1302 1303/// Declarator - Information about one declarator, including the parsed type 1304/// information and the identifier. When the declarator is fully formed, this 1305/// is turned into the appropriate Decl object. 1306/// 1307/// Declarators come in two types: normal declarators and abstract declarators. 1308/// Abstract declarators are used when parsing types, and don't have an 1309/// identifier. Normal declarators do have ID's. 1310/// 1311/// Instances of this class should be a transient object that lives on the 1312/// stack, not objects that are allocated in large quantities on the heap. 1313class Declarator { 1314public: 1315 enum TheContext { 1316 FileContext, // File scope declaration. 1317 PrototypeContext, // Within a function prototype. 1318 ObjCPrototypeContext,// Within a method prototype. 1319 KNRTypeListContext, // K&R type definition list for formals. 1320 TypeNameContext, // Abstract declarator for types. 1321 MemberContext, // Struct/Union field. 1322 BlockContext, // Declaration within a block in a function. 1323 ForContext, // Declaration within first part of a for loop. 1324 ConditionContext, // Condition declaration in a C++ if/switch/while/for. 1325 TemplateParamContext,// Within a template parameter list. 1326 CXXCatchContext, // C++ catch exception-declaration 1327 BlockLiteralContext, // Block literal declarator. 1328 TemplateTypeArgContext, // Template type argument. 1329 AliasDeclContext // C++0x alias-declaration. 1330 }; 1331 1332private: 1333 const DeclSpec &DS; 1334 CXXScopeSpec SS; 1335 UnqualifiedId Name; 1336 SourceRange Range; 1337 1338 /// Context - Where we are parsing this declarator. 1339 /// 1340 TheContext Context; 1341 1342 /// DeclTypeInfo - This holds each type that the declarator includes as it is 1343 /// parsed. This is pushed from the identifier out, which means that element 1344 /// #0 will be the most closely bound to the identifier, and 1345 /// DeclTypeInfo.back() will be the least closely bound. 1346 llvm::SmallVector<DeclaratorChunk, 8> DeclTypeInfo; 1347 1348 /// InvalidType - Set by Sema::GetTypeForDeclarator(). 1349 bool InvalidType : 1; 1350 1351 /// GroupingParens - Set by Parser::ParseParenDeclarator(). 1352 bool GroupingParens : 1; 1353 1354 /// Attrs - Attributes. 1355 ParsedAttributes Attrs; 1356 1357 /// AsmLabel - The asm label, if specified. 1358 Expr *AsmLabel; 1359 1360 /// InlineParams - This is a local array used for the first function decl 1361 /// chunk to avoid going to the heap for the common case when we have one 1362 /// function chunk in the declarator. 1363 DeclaratorChunk::ParamInfo InlineParams[16]; 1364 bool InlineParamsUsed; 1365 1366 /// Extension - true if the declaration is preceded by __extension__. 1367 bool Extension : 1; 1368 1369 /// \brief If provided, the source location of the ellipsis used to describe 1370 /// this declarator as a parameter pack. 1371 SourceLocation EllipsisLoc; 1372 1373 friend struct DeclaratorChunk; 1374 1375public: 1376 Declarator(const DeclSpec &ds, TheContext C) 1377 : DS(ds), Range(ds.getSourceRange()), Context(C), 1378 InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error), 1379 GroupingParens(false), Attrs(ds.getAttributePool().getFactory()), 1380 AsmLabel(0), InlineParamsUsed(false), Extension(false) { 1381 } 1382 1383 ~Declarator() { 1384 clear(); 1385 } 1386 1387 /// getDeclSpec - Return the declaration-specifier that this declarator was 1388 /// declared with. 1389 const DeclSpec &getDeclSpec() const { return DS; } 1390 1391 /// getMutableDeclSpec - Return a non-const version of the DeclSpec. This 1392 /// should be used with extreme care: declspecs can often be shared between 1393 /// multiple declarators, so mutating the DeclSpec affects all of the 1394 /// Declarators. This should only be done when the declspec is known to not 1395 /// be shared or when in error recovery etc. 1396 DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); } 1397 1398 AttributePool &getAttributePool() const { 1399 return Attrs.getPool(); 1400 } 1401 1402 /// getCXXScopeSpec - Return the C++ scope specifier (global scope or 1403 /// nested-name-specifier) that is part of the declarator-id. 1404 const CXXScopeSpec &getCXXScopeSpec() const { return SS; } 1405 CXXScopeSpec &getCXXScopeSpec() { return SS; } 1406 1407 /// \brief Retrieve the name specified by this declarator. 1408 UnqualifiedId &getName() { return Name; } 1409 1410 TheContext getContext() const { return Context; } 1411 1412 bool isPrototypeContext() const { 1413 return (Context == PrototypeContext || Context == ObjCPrototypeContext); 1414 } 1415 1416 /// getSourceRange - Get the source range that spans this declarator. 1417 const SourceRange &getSourceRange() const { return Range; } 1418 1419 void SetSourceRange(SourceRange R) { Range = R; } 1420 /// SetRangeBegin - Set the start of the source range to Loc, unless it's 1421 /// invalid. 1422 void SetRangeBegin(SourceLocation Loc) { 1423 if (!Loc.isInvalid()) 1424 Range.setBegin(Loc); 1425 } 1426 /// SetRangeEnd - Set the end of the source range to Loc, unless it's invalid. 1427 void SetRangeEnd(SourceLocation Loc) { 1428 if (!Loc.isInvalid()) 1429 Range.setEnd(Loc); 1430 } 1431 /// ExtendWithDeclSpec - Extend the declarator source range to include the 1432 /// given declspec, unless its location is invalid. Adopts the range start if 1433 /// the current range start is invalid. 1434 void ExtendWithDeclSpec(const DeclSpec &DS) { 1435 const SourceRange &SR = DS.getSourceRange(); 1436 if (Range.getBegin().isInvalid()) 1437 Range.setBegin(SR.getBegin()); 1438 if (!SR.getEnd().isInvalid()) 1439 Range.setEnd(SR.getEnd()); 1440 } 1441 1442 /// clear - Reset the contents of this Declarator. 1443 void clear() { 1444 SS.clear(); 1445 Name.clear(); 1446 Range = DS.getSourceRange(); 1447 1448 for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i) 1449 DeclTypeInfo[i].destroy(); 1450 DeclTypeInfo.clear(); 1451 Attrs.clear(); 1452 AsmLabel = 0; 1453 InlineParamsUsed = false; 1454 } 1455 1456 /// mayOmitIdentifier - Return true if the identifier is either optional or 1457 /// not allowed. This is true for typenames, prototypes, and template 1458 /// parameter lists. 1459 bool mayOmitIdentifier() const { 1460 switch (Context) { 1461 case FileContext: 1462 case KNRTypeListContext: 1463 case MemberContext: 1464 case BlockContext: 1465 case ForContext: 1466 case ConditionContext: 1467 return false; 1468 1469 case TypeNameContext: 1470 case AliasDeclContext: 1471 case PrototypeContext: 1472 case ObjCPrototypeContext: 1473 case TemplateParamContext: 1474 case CXXCatchContext: 1475 case BlockLiteralContext: 1476 case TemplateTypeArgContext: 1477 return true; 1478 } 1479 llvm_unreachable("unknown context kind!"); 1480 } 1481 1482 /// mayHaveIdentifier - Return true if the identifier is either optional or 1483 /// required. This is true for normal declarators and prototypes, but not 1484 /// typenames. 1485 bool mayHaveIdentifier() const { 1486 switch (Context) { 1487 case FileContext: 1488 case KNRTypeListContext: 1489 case MemberContext: 1490 case BlockContext: 1491 case ForContext: 1492 case ConditionContext: 1493 case PrototypeContext: 1494 case TemplateParamContext: 1495 case CXXCatchContext: 1496 return true; 1497 1498 case TypeNameContext: 1499 case AliasDeclContext: 1500 case ObjCPrototypeContext: 1501 case BlockLiteralContext: 1502 case TemplateTypeArgContext: 1503 return false; 1504 } 1505 llvm_unreachable("unknown context kind!"); 1506 } 1507 1508 /// mayBeFollowedByCXXDirectInit - Return true if the declarator can be 1509 /// followed by a C++ direct initializer, e.g. "int x(1);". 1510 bool mayBeFollowedByCXXDirectInit() const { 1511 if (hasGroupingParens()) return false; 1512 1513 switch (Context) { 1514 case FileContext: 1515 case BlockContext: 1516 case ForContext: 1517 return true; 1518 1519 case KNRTypeListContext: 1520 case MemberContext: 1521 case ConditionContext: 1522 case PrototypeContext: 1523 case ObjCPrototypeContext: 1524 case TemplateParamContext: 1525 case CXXCatchContext: 1526 case TypeNameContext: 1527 case AliasDeclContext: 1528 case BlockLiteralContext: 1529 case TemplateTypeArgContext: 1530 return false; 1531 } 1532 llvm_unreachable("unknown context kind!"); 1533 } 1534 1535 /// isPastIdentifier - Return true if we have parsed beyond the point where 1536 /// the 1537 bool isPastIdentifier() const { return Name.isValid(); } 1538 1539 /// hasName - Whether this declarator has a name, which might be an 1540 /// identifier (accessible via getIdentifier()) or some kind of 1541 /// special C++ name (constructor, destructor, etc.). 1542 bool hasName() const { 1543 return Name.getKind() != UnqualifiedId::IK_Identifier || Name.Identifier; 1544 } 1545 1546 IdentifierInfo *getIdentifier() const { 1547 if (Name.getKind() == UnqualifiedId::IK_Identifier) 1548 return Name.Identifier; 1549 1550 return 0; 1551 } 1552 SourceLocation getIdentifierLoc() const { return Name.StartLocation; } 1553 1554 /// \brief Set the name of this declarator to be the given identifier. 1555 void SetIdentifier(IdentifierInfo *Id, SourceLocation IdLoc) { 1556 Name.setIdentifier(Id, IdLoc); 1557 } 1558 1559 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to 1560 /// EndLoc, which should be the last token of the chunk. 1561 void AddTypeInfo(const DeclaratorChunk &TI, 1562 ParsedAttributes &attrs, 1563 SourceLocation EndLoc) { 1564 DeclTypeInfo.push_back(TI); 1565 DeclTypeInfo.back().getAttrListRef() = attrs.getList(); 1566 getAttributePool().takeAllFrom(attrs.getPool()); 1567 1568 if (!EndLoc.isInvalid()) 1569 SetRangeEnd(EndLoc); 1570 } 1571 1572 /// AddInnermostTypeInfo - Add a new innermost chunk to this declarator. 1573 void AddInnermostTypeInfo(const DeclaratorChunk &TI) { 1574 DeclTypeInfo.insert(DeclTypeInfo.begin(), TI); 1575 } 1576 1577 /// getNumTypeObjects() - Return the number of types applied to this 1578 /// declarator. 1579 unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); } 1580 1581 /// Return the specified TypeInfo from this declarator. TypeInfo #0 is 1582 /// closest to the identifier. 1583 const DeclaratorChunk &getTypeObject(unsigned i) const { 1584 assert(i < DeclTypeInfo.size() && "Invalid type chunk"); 1585 return DeclTypeInfo[i]; 1586 } 1587 DeclaratorChunk &getTypeObject(unsigned i) { 1588 assert(i < DeclTypeInfo.size() && "Invalid type chunk"); 1589 return DeclTypeInfo[i]; 1590 } 1591 1592 void DropFirstTypeObject() 1593 { 1594 assert(!DeclTypeInfo.empty() && "No type chunks to drop."); 1595 DeclTypeInfo.front().destroy(); 1596 DeclTypeInfo.erase(DeclTypeInfo.begin()); 1597 } 1598 1599 /// isFunctionDeclarator - This method returns true if the declarator 1600 /// is a function declarator (looking through parentheses). 1601 /// If true is returned, then the reference type parameter idx is 1602 /// assigned with the index of the declaration chunk. 1603 bool isFunctionDeclarator(unsigned& idx) const { 1604 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) { 1605 switch (DeclTypeInfo[i].Kind) { 1606 case DeclaratorChunk::Function: 1607 idx = i; 1608 return true; 1609 case DeclaratorChunk::Paren: 1610 continue; 1611 case DeclaratorChunk::Pointer: 1612 case DeclaratorChunk::Reference: 1613 case DeclaratorChunk::Array: 1614 case DeclaratorChunk::BlockPointer: 1615 case DeclaratorChunk::MemberPointer: 1616 return false; 1617 } 1618 llvm_unreachable("Invalid type chunk"); 1619 return false; 1620 } 1621 return false; 1622 } 1623 1624 /// isFunctionDeclarator - Once this declarator is fully parsed and formed, 1625 /// this method returns true if the identifier is a function declarator 1626 /// (looking through parentheses). 1627 bool isFunctionDeclarator() const { 1628 unsigned index; 1629 return isFunctionDeclarator(index); 1630 } 1631 1632 /// getFunctionTypeInfo - Retrieves the function type info object 1633 /// (looking through parentheses). 1634 DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() { 1635 assert(isFunctionDeclarator() && "Not a function declarator!"); 1636 unsigned index = 0; 1637 isFunctionDeclarator(index); 1638 return DeclTypeInfo[index].Fun; 1639 } 1640 1641 /// getFunctionTypeInfo - Retrieves the function type info object 1642 /// (looking through parentheses). 1643 const DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() const { 1644 return const_cast<Declarator*>(this)->getFunctionTypeInfo(); 1645 } 1646 1647 /// takeAttributes - Takes attributes from the given parsed-attributes 1648 /// set and add them to this declarator. 1649 /// 1650 /// These examples both add 3 attributes to "var": 1651 /// short int var __attribute__((aligned(16),common,deprecated)); 1652 /// short int x, __attribute__((aligned(16)) var 1653 /// __attribute__((common,deprecated)); 1654 /// 1655 /// Also extends the range of the declarator. 1656 void takeAttributes(ParsedAttributes &attrs, SourceLocation lastLoc) { 1657 Attrs.takeAllFrom(attrs); 1658 1659 if (!lastLoc.isInvalid()) 1660 SetRangeEnd(lastLoc); 1661 } 1662 1663 const AttributeList *getAttributes() const { return Attrs.getList(); } 1664 AttributeList *getAttributes() { return Attrs.getList(); } 1665 1666 AttributeList *&getAttrListRef() { return Attrs.getListRef(); } 1667 1668 /// hasAttributes - do we contain any attributes? 1669 bool hasAttributes() const { 1670 if (getAttributes() || getDeclSpec().hasAttributes()) return true; 1671 for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i) 1672 if (getTypeObject(i).getAttrs()) 1673 return true; 1674 return false; 1675 } 1676 1677 void setAsmLabel(Expr *E) { AsmLabel = E; } 1678 Expr *getAsmLabel() const { return AsmLabel; } 1679 1680 void setExtension(bool Val = true) { Extension = Val; } 1681 bool getExtension() const { return Extension; } 1682 1683 void setInvalidType(bool Val = true) { InvalidType = Val; } 1684 bool isInvalidType() const { 1685 return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error; 1686 } 1687 1688 void setGroupingParens(bool flag) { GroupingParens = flag; } 1689 bool hasGroupingParens() const { return GroupingParens; } 1690 1691 bool hasEllipsis() const { return EllipsisLoc.isValid(); } 1692 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } 1693 void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; } 1694}; 1695 1696/// FieldDeclarator - This little struct is used to capture information about 1697/// structure field declarators, which is basically just a bitfield size. 1698struct FieldDeclarator { 1699 Declarator D; 1700 Expr *BitfieldSize; 1701 explicit FieldDeclarator(DeclSpec &DS) : D(DS, Declarator::MemberContext) { 1702 BitfieldSize = 0; 1703 } 1704}; 1705 1706/// VirtSpecifiers - Represents a C++0x virt-specifier-seq. 1707class VirtSpecifiers { 1708public: 1709 enum Specifier { 1710 VS_None = 0, 1711 VS_Override = 1, 1712 VS_Final = 2 1713 }; 1714 1715 VirtSpecifiers() : Specifiers(0) { } 1716 1717 bool SetSpecifier(Specifier VS, SourceLocation Loc, 1718 const char *&PrevSpec); 1719 1720 bool isOverrideSpecified() const { return Specifiers & VS_Override; } 1721 SourceLocation getOverrideLoc() const { return VS_overrideLoc; } 1722 1723 bool isFinalSpecified() const { return Specifiers & VS_Final; } 1724 SourceLocation getFinalLoc() const { return VS_finalLoc; } 1725 1726 void clear() { Specifiers = 0; } 1727 1728 static const char *getSpecifierName(Specifier VS); 1729 1730 SourceLocation getLastLocation() const { return LastLocation; } 1731 1732private: 1733 unsigned Specifiers; 1734 1735 SourceLocation VS_overrideLoc, VS_finalLoc; 1736 SourceLocation LastLocation; 1737}; 1738 1739} // end namespace clang 1740 1741#endif 1742