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