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