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