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