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/Lambda.h" 29 #include "clang/Basic/OperatorKinds.h" 30 #include "clang/Basic/Specifiers.h" 31 #include "llvm/ADT/SmallVector.h" 32 #include "llvm/Support/Compiler.h" 33 #include "llvm/Support/ErrorHandling.h" 34 35 namespace clang { 36 class ASTContext; 37 class TypeLoc; 38 class LangOptions; 39 class DiagnosticsEngine; 40 class IdentifierInfo; 41 class NamespaceAliasDecl; 42 class NamespaceDecl; 43 class NestedNameSpecifier; 44 class NestedNameSpecifierLoc; 45 class ObjCDeclSpec; 46 class Preprocessor; 47 class Sema; 48 class Declarator; 49 struct TemplateIdAnnotation; 50 51 /// CXXScopeSpec - Represents a C++ nested-name-specifier or a global scope 52 /// specifier. These can be in 3 states: 53 /// 1) Not present, identified by isEmpty() 54 /// 2) Present, identified by isNotEmpty() 55 /// 2.a) Valid, idenified by isValid() 56 /// 2.b) Invalid, identified by isInvalid(). 57 /// 58 /// isSet() is deprecated because it mostly corresponded to "valid" but was 59 /// often used as if it meant "present". 60 /// 61 /// The actual scope is described by getScopeRep(). 62 class CXXScopeSpec { 63 SourceRange Range; 64 NestedNameSpecifierLocBuilder Builder; 65 66 public: getRange()67 const SourceRange &getRange() const { return Range; } setRange(const SourceRange & R)68 void setRange(const SourceRange &R) { Range = R; } setBeginLoc(SourceLocation Loc)69 void setBeginLoc(SourceLocation Loc) { Range.setBegin(Loc); } setEndLoc(SourceLocation Loc)70 void setEndLoc(SourceLocation Loc) { Range.setEnd(Loc); } getBeginLoc()71 SourceLocation getBeginLoc() const { return Range.getBegin(); } getEndLoc()72 SourceLocation getEndLoc() const { return Range.getEnd(); } 73 74 /// \brief Retrieve the representation of the nested-name-specifier. getScopeRep()75 NestedNameSpecifier *getScopeRep() const { 76 return Builder.getRepresentation(); 77 } 78 79 /// \brief Extend the current nested-name-specifier by another 80 /// nested-name-specifier component of the form 'type::'. 81 /// 82 /// \param Context The AST context in which this nested-name-specifier 83 /// resides. 84 /// 85 /// \param TemplateKWLoc The location of the 'template' keyword, if present. 86 /// 87 /// \param TL The TypeLoc that describes the type preceding the '::'. 88 /// 89 /// \param ColonColonLoc The location of the trailing '::'. 90 void Extend(ASTContext &Context, SourceLocation TemplateKWLoc, TypeLoc TL, 91 SourceLocation ColonColonLoc); 92 93 /// \brief Extend the current nested-name-specifier by another 94 /// nested-name-specifier component of the form 'identifier::'. 95 /// 96 /// \param Context The AST context in which this nested-name-specifier 97 /// resides. 98 /// 99 /// \param Identifier The identifier. 100 /// 101 /// \param IdentifierLoc The location of the identifier. 102 /// 103 /// \param ColonColonLoc The location of the trailing '::'. 104 void Extend(ASTContext &Context, IdentifierInfo *Identifier, 105 SourceLocation IdentifierLoc, SourceLocation ColonColonLoc); 106 107 /// \brief Extend the current nested-name-specifier by another 108 /// nested-name-specifier component of the form 'namespace::'. 109 /// 110 /// \param Context The AST context in which this nested-name-specifier 111 /// resides. 112 /// 113 /// \param Namespace The namespace. 114 /// 115 /// \param NamespaceLoc The location of the namespace name. 116 /// 117 /// \param ColonColonLoc The location of the trailing '::'. 118 void Extend(ASTContext &Context, NamespaceDecl *Namespace, 119 SourceLocation NamespaceLoc, SourceLocation ColonColonLoc); 120 121 /// \brief Extend the current nested-name-specifier by another 122 /// nested-name-specifier component of the form 'namespace-alias::'. 123 /// 124 /// \param Context The AST context in which this nested-name-specifier 125 /// resides. 126 /// 127 /// \param Alias The namespace alias. 128 /// 129 /// \param AliasLoc The location of the namespace alias 130 /// name. 131 /// 132 /// \param ColonColonLoc The location of the trailing '::'. 133 void Extend(ASTContext &Context, NamespaceAliasDecl *Alias, 134 SourceLocation AliasLoc, SourceLocation ColonColonLoc); 135 136 /// \brief Turn this (empty) nested-name-specifier into the global 137 /// nested-name-specifier '::'. 138 void MakeGlobal(ASTContext &Context, SourceLocation ColonColonLoc); 139 140 /// \brief Make a new nested-name-specifier from incomplete source-location 141 /// information. 142 /// 143 /// FIXME: This routine should be used very, very rarely, in cases where we 144 /// need to synthesize a nested-name-specifier. Most code should instead use 145 /// \c Adopt() with a proper \c NestedNameSpecifierLoc. 146 void MakeTrivial(ASTContext &Context, NestedNameSpecifier *Qualifier, 147 SourceRange R); 148 149 /// \brief Adopt an existing nested-name-specifier (with source-range 150 /// information). 151 void Adopt(NestedNameSpecifierLoc Other); 152 153 /// \brief Retrieve a nested-name-specifier with location information, copied 154 /// into the given AST context. 155 /// 156 /// \param Context The context into which this nested-name-specifier will be 157 /// copied. 158 NestedNameSpecifierLoc getWithLocInContext(ASTContext &Context) const; 159 160 /// \brief Retrieve the location of the name in the last qualifier 161 /// in this nested name specifier. For example: 162 /// ::foo::bar<0>:: 163 /// ^~~ 164 SourceLocation getLastQualifierNameLoc() const; 165 166 /// No scope specifier. isEmpty()167 bool isEmpty() const { return !Range.isValid(); } 168 /// A scope specifier is present, but may be valid or invalid. isNotEmpty()169 bool isNotEmpty() const { return !isEmpty(); } 170 171 /// An error occurred during parsing of the scope specifier. isInvalid()172 bool isInvalid() const { return isNotEmpty() && getScopeRep() == 0; } 173 /// A scope specifier is present, and it refers to a real scope. isValid()174 bool isValid() const { return isNotEmpty() && getScopeRep() != 0; } 175 176 /// \brief Indicate that this nested-name-specifier is invalid. SetInvalid(SourceRange R)177 void SetInvalid(SourceRange R) { 178 assert(R.isValid() && "Must have a valid source range"); 179 if (Range.getBegin().isInvalid()) 180 Range.setBegin(R.getBegin()); 181 Range.setEnd(R.getEnd()); 182 Builder.Clear(); 183 } 184 185 /// Deprecated. Some call sites intend isNotEmpty() while others intend 186 /// isValid(). isSet()187 bool isSet() const { return getScopeRep() != 0; } 188 clear()189 void clear() { 190 Range = SourceRange(); 191 Builder.Clear(); 192 } 193 194 /// \brief Retrieve the data associated with the source-location information. location_data()195 char *location_data() const { return Builder.getBuffer().first; } 196 197 /// \brief Retrieve the size of the data associated with source-location 198 /// information. location_size()199 unsigned location_size() const { return Builder.getBuffer().second; } 200 }; 201 202 /// DeclSpec - This class captures information about "declaration specifiers", 203 /// which encompasses storage-class-specifiers, type-specifiers, 204 /// type-qualifiers, and function-specifiers. 205 class DeclSpec { 206 public: 207 // storage-class-specifier 208 // Note: The order of these enumerators is important for diagnostics. 209 enum SCS { 210 SCS_unspecified = 0, 211 SCS_typedef, 212 SCS_extern, 213 SCS_static, 214 SCS_auto, 215 SCS_register, 216 SCS_private_extern, 217 SCS_mutable 218 }; 219 220 // Import type specifier width enumeration and constants. 221 typedef TypeSpecifierWidth TSW; 222 static const TSW TSW_unspecified = clang::TSW_unspecified; 223 static const TSW TSW_short = clang::TSW_short; 224 static const TSW TSW_long = clang::TSW_long; 225 static const TSW TSW_longlong = clang::TSW_longlong; 226 227 enum TSC { 228 TSC_unspecified, 229 TSC_imaginary, 230 TSC_complex 231 }; 232 233 // Import type specifier sign enumeration and constants. 234 typedef TypeSpecifierSign TSS; 235 static const TSS TSS_unspecified = clang::TSS_unspecified; 236 static const TSS TSS_signed = clang::TSS_signed; 237 static const TSS TSS_unsigned = clang::TSS_unsigned; 238 239 // Import type specifier type enumeration and constants. 240 typedef TypeSpecifierType TST; 241 static const TST TST_unspecified = clang::TST_unspecified; 242 static const TST TST_void = clang::TST_void; 243 static const TST TST_char = clang::TST_char; 244 static const TST TST_wchar = clang::TST_wchar; 245 static const TST TST_char16 = clang::TST_char16; 246 static const TST TST_char32 = clang::TST_char32; 247 static const TST TST_int = clang::TST_int; 248 static const TST TST_int128 = clang::TST_int128; 249 static const TST TST_half = clang::TST_half; 250 static const TST TST_float = clang::TST_float; 251 static const TST TST_double = clang::TST_double; 252 static const TST TST_bool = clang::TST_bool; 253 static const TST TST_decimal32 = clang::TST_decimal32; 254 static const TST TST_decimal64 = clang::TST_decimal64; 255 static const TST TST_decimal128 = clang::TST_decimal128; 256 static const TST TST_enum = clang::TST_enum; 257 static const TST TST_union = clang::TST_union; 258 static const TST TST_struct = clang::TST_struct; 259 static const TST TST_class = clang::TST_class; 260 static const TST TST_typename = clang::TST_typename; 261 static const TST TST_typeofType = clang::TST_typeofType; 262 static const TST TST_typeofExpr = clang::TST_typeofExpr; 263 static const TST TST_decltype = clang::TST_decltype; 264 static const TST TST_underlyingType = clang::TST_underlyingType; 265 static const TST TST_auto = clang::TST_auto; 266 static const TST TST_unknown_anytype = clang::TST_unknown_anytype; 267 static const TST TST_atomic = clang::TST_atomic; 268 static const TST TST_error = clang::TST_error; 269 270 // type-qualifiers 271 enum TQ { // NOTE: These flags must be kept in sync with Qualifiers::TQ. 272 TQ_unspecified = 0, 273 TQ_const = 1, 274 TQ_restrict = 2, 275 TQ_volatile = 4 276 }; 277 278 /// ParsedSpecifiers - Flags to query which specifiers were applied. This is 279 /// returned by getParsedSpecifiers. 280 enum ParsedSpecifiers { 281 PQ_None = 0, 282 PQ_StorageClassSpecifier = 1, 283 PQ_TypeSpecifier = 2, 284 PQ_TypeQualifier = 4, 285 PQ_FunctionSpecifier = 8 286 }; 287 288 private: 289 // storage-class-specifier 290 /*SCS*/unsigned StorageClassSpec : 3; 291 unsigned SCS_thread_specified : 1; 292 unsigned SCS_extern_in_linkage_spec : 1; 293 294 // type-specifier 295 /*TSW*/unsigned TypeSpecWidth : 2; 296 /*TSC*/unsigned TypeSpecComplex : 2; 297 /*TSS*/unsigned TypeSpecSign : 2; 298 /*TST*/unsigned TypeSpecType : 5; 299 unsigned TypeAltiVecVector : 1; 300 unsigned TypeAltiVecPixel : 1; 301 unsigned TypeAltiVecBool : 1; 302 unsigned TypeSpecOwned : 1; 303 304 // type-qualifiers 305 unsigned TypeQualifiers : 3; // Bitwise OR of TQ. 306 307 // function-specifier 308 unsigned FS_inline_specified : 1; 309 unsigned FS_virtual_specified : 1; 310 unsigned FS_explicit_specified : 1; 311 312 // friend-specifier 313 unsigned Friend_specified : 1; 314 315 // constexpr-specifier 316 unsigned Constexpr_specified : 1; 317 318 /*SCS*/unsigned StorageClassSpecAsWritten : 3; 319 320 union { 321 UnionParsedType TypeRep; 322 Decl *DeclRep; 323 Expr *ExprRep; 324 }; 325 326 // attributes. 327 ParsedAttributes Attrs; 328 329 // Scope specifier for the type spec, if applicable. 330 CXXScopeSpec TypeScope; 331 332 // List of protocol qualifiers for objective-c classes. Used for 333 // protocol-qualified interfaces "NString<foo>" and protocol-qualified id 334 // "id<foo>". 335 Decl * const *ProtocolQualifiers; 336 unsigned NumProtocolQualifiers; 337 SourceLocation ProtocolLAngleLoc; 338 SourceLocation *ProtocolLocs; 339 340 // SourceLocation info. These are null if the item wasn't specified or if 341 // the setting was synthesized. 342 SourceRange Range; 343 344 SourceLocation StorageClassSpecLoc, SCS_threadLoc; 345 SourceLocation TSWLoc, TSCLoc, TSSLoc, TSTLoc, AltiVecLoc; 346 /// TSTNameLoc - If TypeSpecType is any of class, enum, struct, union, 347 /// typename, then this is the location of the named type (if present); 348 /// otherwise, it is the same as TSTLoc. Hence, the pair TSTLoc and 349 /// TSTNameLoc provides source range info for tag types. 350 SourceLocation TSTNameLoc; 351 SourceRange TypeofParensRange; 352 SourceLocation TQ_constLoc, TQ_restrictLoc, TQ_volatileLoc; 353 SourceLocation FS_inlineLoc, FS_virtualLoc, FS_explicitLoc; 354 SourceLocation FriendLoc, ModulePrivateLoc, ConstexprLoc; 355 356 WrittenBuiltinSpecs writtenBS; 357 void SaveWrittenBuiltinSpecs(); 358 void SaveStorageSpecifierAsWritten(); 359 360 ObjCDeclSpec *ObjCQualifiers; 361 isTypeRep(TST T)362 static bool isTypeRep(TST T) { 363 return (T == TST_typename || T == TST_typeofType || 364 T == TST_underlyingType || T == TST_atomic); 365 } isExprRep(TST T)366 static bool isExprRep(TST T) { 367 return (T == TST_typeofExpr || T == TST_decltype); 368 } isDeclRep(TST T)369 static bool isDeclRep(TST T) { 370 return (T == TST_enum || T == TST_struct || 371 T == TST_union || T == TST_class); 372 } 373 374 DeclSpec(const DeclSpec&); // DO NOT IMPLEMENT 375 void operator=(const DeclSpec&); // DO NOT IMPLEMENT 376 public: 377 DeclSpec(AttributeFactory & attrFactory)378 DeclSpec(AttributeFactory &attrFactory) 379 : StorageClassSpec(SCS_unspecified), 380 SCS_thread_specified(false), 381 SCS_extern_in_linkage_spec(false), 382 TypeSpecWidth(TSW_unspecified), 383 TypeSpecComplex(TSC_unspecified), 384 TypeSpecSign(TSS_unspecified), 385 TypeSpecType(TST_unspecified), 386 TypeAltiVecVector(false), 387 TypeAltiVecPixel(false), 388 TypeAltiVecBool(false), 389 TypeSpecOwned(false), 390 TypeQualifiers(TQ_unspecified), 391 FS_inline_specified(false), 392 FS_virtual_specified(false), 393 FS_explicit_specified(false), 394 Friend_specified(false), 395 Constexpr_specified(false), 396 StorageClassSpecAsWritten(SCS_unspecified), 397 Attrs(attrFactory), 398 ProtocolQualifiers(0), 399 NumProtocolQualifiers(0), 400 ProtocolLocs(0), 401 writtenBS(), 402 ObjCQualifiers(0) { 403 } ~DeclSpec()404 ~DeclSpec() { 405 delete [] ProtocolQualifiers; 406 delete [] ProtocolLocs; 407 } 408 // storage-class-specifier getStorageClassSpec()409 SCS getStorageClassSpec() const { return (SCS)StorageClassSpec; } isThreadSpecified()410 bool isThreadSpecified() const { return SCS_thread_specified; } isExternInLinkageSpec()411 bool isExternInLinkageSpec() const { return SCS_extern_in_linkage_spec; } setExternInLinkageSpec(bool Value)412 void setExternInLinkageSpec(bool Value) { 413 SCS_extern_in_linkage_spec = Value; 414 } 415 getStorageClassSpecLoc()416 SourceLocation getStorageClassSpecLoc() const { return StorageClassSpecLoc; } getThreadSpecLoc()417 SourceLocation getThreadSpecLoc() const { return SCS_threadLoc; } 418 ClearStorageClassSpecs()419 void ClearStorageClassSpecs() { 420 StorageClassSpec = DeclSpec::SCS_unspecified; 421 SCS_thread_specified = false; 422 SCS_extern_in_linkage_spec = false; 423 StorageClassSpecLoc = SourceLocation(); 424 SCS_threadLoc = SourceLocation(); 425 } 426 427 // type-specifier getTypeSpecWidth()428 TSW getTypeSpecWidth() const { return (TSW)TypeSpecWidth; } getTypeSpecComplex()429 TSC getTypeSpecComplex() const { return (TSC)TypeSpecComplex; } getTypeSpecSign()430 TSS getTypeSpecSign() const { return (TSS)TypeSpecSign; } getTypeSpecType()431 TST getTypeSpecType() const { return (TST)TypeSpecType; } isTypeAltiVecVector()432 bool isTypeAltiVecVector() const { return TypeAltiVecVector; } isTypeAltiVecPixel()433 bool isTypeAltiVecPixel() const { return TypeAltiVecPixel; } isTypeAltiVecBool()434 bool isTypeAltiVecBool() const { return TypeAltiVecBool; } isTypeSpecOwned()435 bool isTypeSpecOwned() const { return TypeSpecOwned; } getRepAsType()436 ParsedType getRepAsType() const { 437 assert(isTypeRep((TST) TypeSpecType) && "DeclSpec does not store a type"); 438 return TypeRep; 439 } getRepAsDecl()440 Decl *getRepAsDecl() const { 441 assert(isDeclRep((TST) TypeSpecType) && "DeclSpec does not store a decl"); 442 return DeclRep; 443 } getRepAsExpr()444 Expr *getRepAsExpr() const { 445 assert(isExprRep((TST) TypeSpecType) && "DeclSpec does not store an expr"); 446 return ExprRep; 447 } getTypeSpecScope()448 CXXScopeSpec &getTypeSpecScope() { return TypeScope; } getTypeSpecScope()449 const CXXScopeSpec &getTypeSpecScope() const { return TypeScope; } 450 getSourceRange()451 const SourceRange &getSourceRange() const LLVM_READONLY { return Range; } getLocStart()452 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); } getLocEnd()453 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); } 454 getTypeSpecWidthLoc()455 SourceLocation getTypeSpecWidthLoc() const { return TSWLoc; } getTypeSpecComplexLoc()456 SourceLocation getTypeSpecComplexLoc() const { return TSCLoc; } getTypeSpecSignLoc()457 SourceLocation getTypeSpecSignLoc() const { return TSSLoc; } getTypeSpecTypeLoc()458 SourceLocation getTypeSpecTypeLoc() const { return TSTLoc; } getAltiVecLoc()459 SourceLocation getAltiVecLoc() const { return AltiVecLoc; } 460 getTypeSpecTypeNameLoc()461 SourceLocation getTypeSpecTypeNameLoc() const { 462 assert(isDeclRep((TST) TypeSpecType) || TypeSpecType == TST_typename); 463 return TSTNameLoc; 464 } 465 getTypeofParensRange()466 SourceRange getTypeofParensRange() const { return TypeofParensRange; } setTypeofParensRange(SourceRange range)467 void setTypeofParensRange(SourceRange range) { TypeofParensRange = range; } 468 469 /// getSpecifierName - Turn a type-specifier-type into a string like "_Bool" 470 /// or "union". 471 static const char *getSpecifierName(DeclSpec::TST T); 472 static const char *getSpecifierName(DeclSpec::TQ Q); 473 static const char *getSpecifierName(DeclSpec::TSS S); 474 static const char *getSpecifierName(DeclSpec::TSC C); 475 static const char *getSpecifierName(DeclSpec::TSW W); 476 static const char *getSpecifierName(DeclSpec::SCS S); 477 478 // type-qualifiers 479 480 /// getTypeQualifiers - Return a set of TQs. getTypeQualifiers()481 unsigned getTypeQualifiers() const { return TypeQualifiers; } getConstSpecLoc()482 SourceLocation getConstSpecLoc() const { return TQ_constLoc; } getRestrictSpecLoc()483 SourceLocation getRestrictSpecLoc() const { return TQ_restrictLoc; } getVolatileSpecLoc()484 SourceLocation getVolatileSpecLoc() const { return TQ_volatileLoc; } 485 486 /// \brief Clear out all of the type qualifiers. ClearTypeQualifiers()487 void ClearTypeQualifiers() { 488 TypeQualifiers = 0; 489 TQ_constLoc = SourceLocation(); 490 TQ_restrictLoc = SourceLocation(); 491 TQ_volatileLoc = SourceLocation(); 492 } 493 494 // function-specifier isInlineSpecified()495 bool isInlineSpecified() const { return FS_inline_specified; } getInlineSpecLoc()496 SourceLocation getInlineSpecLoc() const { return FS_inlineLoc; } 497 isVirtualSpecified()498 bool isVirtualSpecified() const { return FS_virtual_specified; } getVirtualSpecLoc()499 SourceLocation getVirtualSpecLoc() const { return FS_virtualLoc; } 500 isExplicitSpecified()501 bool isExplicitSpecified() const { return FS_explicit_specified; } getExplicitSpecLoc()502 SourceLocation getExplicitSpecLoc() const { return FS_explicitLoc; } 503 ClearFunctionSpecs()504 void ClearFunctionSpecs() { 505 FS_inline_specified = false; 506 FS_inlineLoc = SourceLocation(); 507 FS_virtual_specified = false; 508 FS_virtualLoc = SourceLocation(); 509 FS_explicit_specified = false; 510 FS_explicitLoc = SourceLocation(); 511 } 512 513 /// hasTypeSpecifier - Return true if any type-specifier has been found. hasTypeSpecifier()514 bool hasTypeSpecifier() const { 515 return getTypeSpecType() != DeclSpec::TST_unspecified || 516 getTypeSpecWidth() != DeclSpec::TSW_unspecified || 517 getTypeSpecComplex() != DeclSpec::TSC_unspecified || 518 getTypeSpecSign() != DeclSpec::TSS_unspecified; 519 } 520 521 /// getParsedSpecifiers - Return a bitmask of which flavors of specifiers this 522 /// DeclSpec includes. 523 /// 524 unsigned getParsedSpecifiers() const; 525 getStorageClassSpecAsWritten()526 SCS getStorageClassSpecAsWritten() const { 527 return (SCS)StorageClassSpecAsWritten; 528 } 529 530 /// isEmpty - Return true if this declaration specifier is completely empty: 531 /// no tokens were parsed in the production of it. isEmpty()532 bool isEmpty() const { 533 return getParsedSpecifiers() == DeclSpec::PQ_None; 534 } 535 SetRangeStart(SourceLocation Loc)536 void SetRangeStart(SourceLocation Loc) { Range.setBegin(Loc); } SetRangeEnd(SourceLocation Loc)537 void SetRangeEnd(SourceLocation Loc) { Range.setEnd(Loc); } 538 539 /// These methods set the specified attribute of the DeclSpec and 540 /// return false if there was no error. If an error occurs (for 541 /// example, if we tried to set "auto" on a spec with "extern" 542 /// already set), they return true and set PrevSpec and DiagID 543 /// such that 544 /// Diag(Loc, DiagID) << PrevSpec; 545 /// will yield a useful result. 546 /// 547 /// TODO: use a more general approach that still allows these 548 /// diagnostics to be ignored when desired. 549 bool SetStorageClassSpec(Sema &S, SCS SC, SourceLocation Loc, 550 const char *&PrevSpec, unsigned &DiagID); 551 bool SetStorageClassSpecThread(SourceLocation Loc, const char *&PrevSpec, 552 unsigned &DiagID); 553 bool SetTypeSpecWidth(TSW W, SourceLocation Loc, const char *&PrevSpec, 554 unsigned &DiagID); 555 bool SetTypeSpecComplex(TSC C, SourceLocation Loc, const char *&PrevSpec, 556 unsigned &DiagID); 557 bool SetTypeSpecSign(TSS S, SourceLocation Loc, const char *&PrevSpec, 558 unsigned &DiagID); 559 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, 560 unsigned &DiagID); 561 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, 562 unsigned &DiagID, ParsedType Rep); 563 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, 564 unsigned &DiagID, Decl *Rep, bool Owned); 565 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc, 566 SourceLocation TagNameLoc, const char *&PrevSpec, 567 unsigned &DiagID, ParsedType Rep); 568 bool SetTypeSpecType(TST T, SourceLocation TagKwLoc, 569 SourceLocation TagNameLoc, const char *&PrevSpec, 570 unsigned &DiagID, Decl *Rep, bool Owned); 571 572 bool SetTypeSpecType(TST T, SourceLocation Loc, const char *&PrevSpec, 573 unsigned &DiagID, Expr *Rep); 574 bool SetTypeAltiVecVector(bool isAltiVecVector, SourceLocation Loc, 575 const char *&PrevSpec, unsigned &DiagID); 576 bool SetTypeAltiVecPixel(bool isAltiVecPixel, SourceLocation Loc, 577 const char *&PrevSpec, unsigned &DiagID); 578 bool SetTypeSpecError(); UpdateDeclRep(Decl * Rep)579 void UpdateDeclRep(Decl *Rep) { 580 assert(isDeclRep((TST) TypeSpecType)); 581 DeclRep = Rep; 582 } UpdateTypeRep(ParsedType Rep)583 void UpdateTypeRep(ParsedType Rep) { 584 assert(isTypeRep((TST) TypeSpecType)); 585 TypeRep = Rep; 586 } UpdateExprRep(Expr * Rep)587 void UpdateExprRep(Expr *Rep) { 588 assert(isExprRep((TST) TypeSpecType)); 589 ExprRep = Rep; 590 } 591 592 bool SetTypeQual(TQ T, SourceLocation Loc, const char *&PrevSpec, 593 unsigned &DiagID, const LangOptions &Lang); 594 595 bool SetFunctionSpecInline(SourceLocation Loc, const char *&PrevSpec, 596 unsigned &DiagID); 597 bool SetFunctionSpecVirtual(SourceLocation Loc, const char *&PrevSpec, 598 unsigned &DiagID); 599 bool SetFunctionSpecExplicit(SourceLocation Loc, const char *&PrevSpec, 600 unsigned &DiagID); 601 602 bool SetFriendSpec(SourceLocation Loc, const char *&PrevSpec, 603 unsigned &DiagID); 604 bool setModulePrivateSpec(SourceLocation Loc, const char *&PrevSpec, 605 unsigned &DiagID); 606 bool SetConstexprSpec(SourceLocation Loc, const char *&PrevSpec, 607 unsigned &DiagID); 608 isFriendSpecified()609 bool isFriendSpecified() const { return Friend_specified; } getFriendSpecLoc()610 SourceLocation getFriendSpecLoc() const { return FriendLoc; } 611 isModulePrivateSpecified()612 bool isModulePrivateSpecified() const { return ModulePrivateLoc.isValid(); } getModulePrivateSpecLoc()613 SourceLocation getModulePrivateSpecLoc() const { return ModulePrivateLoc; } 614 isConstexprSpecified()615 bool isConstexprSpecified() const { return Constexpr_specified; } getConstexprSpecLoc()616 SourceLocation getConstexprSpecLoc() const { return ConstexprLoc; } 617 ClearConstexprSpec()618 void ClearConstexprSpec() { 619 Constexpr_specified = false; 620 ConstexprLoc = SourceLocation(); 621 } 622 getAttributePool()623 AttributePool &getAttributePool() const { 624 return Attrs.getPool(); 625 } 626 627 /// AddAttributes - contatenates two attribute lists. 628 /// The GCC attribute syntax allows for the following: 629 /// 630 /// short __attribute__(( unused, deprecated )) 631 /// int __attribute__(( may_alias, aligned(16) )) var; 632 /// 633 /// This declares 4 attributes using 2 lists. The following syntax is 634 /// also allowed and equivalent to the previous declaration. 635 /// 636 /// short __attribute__((unused)) __attribute__((deprecated)) 637 /// int __attribute__((may_alias)) __attribute__((aligned(16))) var; 638 /// addAttributes(AttributeList * AL)639 void addAttributes(AttributeList *AL) { 640 Attrs.addAll(AL); 641 } setAttributes(AttributeList * AL)642 void setAttributes(AttributeList *AL) { 643 Attrs.set(AL); 644 } 645 hasAttributes()646 bool hasAttributes() const { return !Attrs.empty(); } 647 getAttributes()648 ParsedAttributes &getAttributes() { return Attrs; } getAttributes()649 const ParsedAttributes &getAttributes() const { return Attrs; } 650 651 /// TakeAttributes - Return the current attribute list and remove them from 652 /// the DeclSpec so that it doesn't own them. takeAttributes()653 ParsedAttributes takeAttributes() { 654 // The non-const "copy" constructor clears the operand automatically. 655 return Attrs; 656 } 657 takeAttributesFrom(ParsedAttributes & attrs)658 void takeAttributesFrom(ParsedAttributes &attrs) { 659 Attrs.takeAllFrom(attrs); 660 } 661 662 typedef Decl * const *ProtocolQualifierListTy; getProtocolQualifiers()663 ProtocolQualifierListTy getProtocolQualifiers() const { 664 return ProtocolQualifiers; 665 } getProtocolLocs()666 SourceLocation *getProtocolLocs() const { return ProtocolLocs; } getNumProtocolQualifiers()667 unsigned getNumProtocolQualifiers() const { 668 return NumProtocolQualifiers; 669 } getProtocolLAngleLoc()670 SourceLocation getProtocolLAngleLoc() const { return ProtocolLAngleLoc; } 671 void setProtocolQualifiers(Decl * const *Protos, unsigned NP, 672 SourceLocation *ProtoLocs, 673 SourceLocation LAngleLoc); 674 675 /// Finish - This does final analysis of the declspec, issuing diagnostics for 676 /// things like "_Imaginary" (lacking an FP type). After calling this method, 677 /// DeclSpec is guaranteed self-consistent, even if an error occurred. 678 void Finish(DiagnosticsEngine &D, Preprocessor &PP); 679 getWrittenBuiltinSpecs()680 const WrittenBuiltinSpecs& getWrittenBuiltinSpecs() const { 681 return writtenBS; 682 } 683 getObjCQualifiers()684 ObjCDeclSpec *getObjCQualifiers() const { return ObjCQualifiers; } setObjCQualifiers(ObjCDeclSpec * quals)685 void setObjCQualifiers(ObjCDeclSpec *quals) { ObjCQualifiers = quals; } 686 687 /// isMissingDeclaratorOk - This checks if this DeclSpec can stand alone, 688 /// without a Declarator. Only tag declspecs can stand alone. 689 bool isMissingDeclaratorOk(); 690 }; 691 692 /// ObjCDeclSpec - This class captures information about 693 /// "declaration specifiers" specific to objective-c 694 class ObjCDeclSpec { 695 public: 696 /// ObjCDeclQualifier - Qualifier used on types in method 697 /// declarations. Not all combinations are sensible. Parameters 698 /// can be one of { in, out, inout } with one of { bycopy, byref }. 699 /// Returns can either be { oneway } or not. 700 /// 701 /// This should be kept in sync with Decl::ObjCDeclQualifier. 702 enum ObjCDeclQualifier { 703 DQ_None = 0x0, 704 DQ_In = 0x1, 705 DQ_Inout = 0x2, 706 DQ_Out = 0x4, 707 DQ_Bycopy = 0x8, 708 DQ_Byref = 0x10, 709 DQ_Oneway = 0x20 710 }; 711 712 /// PropertyAttributeKind - list of property attributes. 713 enum ObjCPropertyAttributeKind { 714 DQ_PR_noattr = 0x0, 715 DQ_PR_readonly = 0x01, 716 DQ_PR_getter = 0x02, 717 DQ_PR_assign = 0x04, 718 DQ_PR_readwrite = 0x08, 719 DQ_PR_retain = 0x10, 720 DQ_PR_copy = 0x20, 721 DQ_PR_nonatomic = 0x40, 722 DQ_PR_setter = 0x80, 723 DQ_PR_atomic = 0x100, 724 DQ_PR_weak = 0x200, 725 DQ_PR_strong = 0x400, 726 DQ_PR_unsafe_unretained = 0x800 727 }; 728 729 ObjCDeclSpec()730 ObjCDeclSpec() 731 : objcDeclQualifier(DQ_None), PropertyAttributes(DQ_PR_noattr), 732 GetterName(0), SetterName(0) { } getObjCDeclQualifier()733 ObjCDeclQualifier getObjCDeclQualifier() const { return objcDeclQualifier; } setObjCDeclQualifier(ObjCDeclQualifier DQVal)734 void setObjCDeclQualifier(ObjCDeclQualifier DQVal) { 735 objcDeclQualifier = (ObjCDeclQualifier) (objcDeclQualifier | DQVal); 736 } 737 getPropertyAttributes()738 ObjCPropertyAttributeKind getPropertyAttributes() const { 739 return ObjCPropertyAttributeKind(PropertyAttributes); 740 } setPropertyAttributes(ObjCPropertyAttributeKind PRVal)741 void setPropertyAttributes(ObjCPropertyAttributeKind PRVal) { 742 PropertyAttributes = 743 (ObjCPropertyAttributeKind)(PropertyAttributes | PRVal); 744 } 745 getGetterName()746 const IdentifierInfo *getGetterName() const { return GetterName; } getGetterName()747 IdentifierInfo *getGetterName() { return GetterName; } setGetterName(IdentifierInfo * name)748 void setGetterName(IdentifierInfo *name) { GetterName = name; } 749 getSetterName()750 const IdentifierInfo *getSetterName() const { return SetterName; } getSetterName()751 IdentifierInfo *getSetterName() { return SetterName; } setSetterName(IdentifierInfo * name)752 void setSetterName(IdentifierInfo *name) { SetterName = name; } 753 754 private: 755 // FIXME: These two are unrelated and mutially exclusive. So perhaps 756 // we can put them in a union to reflect their mutual exclusiveness 757 // (space saving is negligible). 758 ObjCDeclQualifier objcDeclQualifier : 6; 759 760 // NOTE: VC++ treats enums as signed, avoid using ObjCPropertyAttributeKind 761 unsigned PropertyAttributes : 12; 762 IdentifierInfo *GetterName; // getter name of NULL if no getter 763 IdentifierInfo *SetterName; // setter name of NULL if no setter 764 }; 765 766 /// \brief Represents a C++ unqualified-id that has been parsed. 767 class UnqualifiedId { 768 private: 769 const UnqualifiedId &operator=(const UnqualifiedId &); // DO NOT IMPLEMENT 770 771 public: 772 /// \brief Describes the kind of unqualified-id parsed. 773 enum IdKind { 774 /// \brief An identifier. 775 IK_Identifier, 776 /// \brief An overloaded operator name, e.g., operator+. 777 IK_OperatorFunctionId, 778 /// \brief A conversion function name, e.g., operator int. 779 IK_ConversionFunctionId, 780 /// \brief A user-defined literal name, e.g., operator "" _i. 781 IK_LiteralOperatorId, 782 /// \brief A constructor name. 783 IK_ConstructorName, 784 /// \brief A constructor named via a template-id. 785 IK_ConstructorTemplateId, 786 /// \brief A destructor name. 787 IK_DestructorName, 788 /// \brief A template-id, e.g., f<int>. 789 IK_TemplateId, 790 /// \brief An implicit 'self' parameter 791 IK_ImplicitSelfParam 792 } Kind; 793 794 /// \brief Anonymous union that holds extra data associated with the 795 /// parsed unqualified-id. 796 union { 797 /// \brief When Kind == IK_Identifier, the parsed identifier, or when Kind 798 /// == IK_UserLiteralId, the identifier suffix. 799 IdentifierInfo *Identifier; 800 801 /// \brief When Kind == IK_OperatorFunctionId, the overloaded operator 802 /// that we parsed. 803 struct { 804 /// \brief The kind of overloaded operator. 805 OverloadedOperatorKind Operator; 806 807 /// \brief The source locations of the individual tokens that name 808 /// the operator, e.g., the "new", "[", and "]" tokens in 809 /// operator new []. 810 /// 811 /// Different operators have different numbers of tokens in their name, 812 /// up to three. Any remaining source locations in this array will be 813 /// set to an invalid value for operators with fewer than three tokens. 814 unsigned SymbolLocations[3]; 815 } OperatorFunctionId; 816 817 /// \brief When Kind == IK_ConversionFunctionId, the type that the 818 /// conversion function names. 819 UnionParsedType ConversionFunctionId; 820 821 /// \brief When Kind == IK_ConstructorName, the class-name of the type 822 /// whose constructor is being referenced. 823 UnionParsedType ConstructorName; 824 825 /// \brief When Kind == IK_DestructorName, the type referred to by the 826 /// class-name. 827 UnionParsedType DestructorName; 828 829 /// \brief When Kind == IK_TemplateId or IK_ConstructorTemplateId, 830 /// the template-id annotation that contains the template name and 831 /// template arguments. 832 TemplateIdAnnotation *TemplateId; 833 }; 834 835 /// \brief The location of the first token that describes this unqualified-id, 836 /// which will be the location of the identifier, "operator" keyword, 837 /// tilde (for a destructor), or the template name of a template-id. 838 SourceLocation StartLocation; 839 840 /// \brief The location of the last token that describes this unqualified-id. 841 SourceLocation EndLocation; 842 UnqualifiedId()843 UnqualifiedId() : Kind(IK_Identifier), Identifier(0) { } 844 845 /// \brief Do not use this copy constructor. It is temporary, and only 846 /// exists because we are holding FieldDeclarators in a SmallVector when we 847 /// don't actually need them. 848 /// 849 /// FIXME: Kill this copy constructor. UnqualifiedId(const UnqualifiedId & Other)850 UnqualifiedId(const UnqualifiedId &Other) 851 : Kind(IK_Identifier), Identifier(Other.Identifier), 852 StartLocation(Other.StartLocation), EndLocation(Other.EndLocation) { 853 assert(Other.Kind == IK_Identifier && "Cannot copy non-identifiers"); 854 } 855 856 /// \brief Destroy this unqualified-id. ~UnqualifiedId()857 ~UnqualifiedId() { clear(); } 858 859 /// \brief Clear out this unqualified-id, setting it to default (invalid) 860 /// state. 861 void clear(); 862 863 /// \brief Determine whether this unqualified-id refers to a valid name. isValid()864 bool isValid() const { return StartLocation.isValid(); } 865 866 /// \brief Determine whether this unqualified-id refers to an invalid name. isInvalid()867 bool isInvalid() const { return !isValid(); } 868 869 /// \brief Determine what kind of name we have. getKind()870 IdKind getKind() const { return Kind; } setKind(IdKind kind)871 void setKind(IdKind kind) { Kind = kind; } 872 873 /// \brief Specify that this unqualified-id was parsed as an identifier. 874 /// 875 /// \param Id the parsed identifier. 876 /// \param IdLoc the location of the parsed identifier. setIdentifier(const IdentifierInfo * Id,SourceLocation IdLoc)877 void setIdentifier(const IdentifierInfo *Id, SourceLocation IdLoc) { 878 Kind = IK_Identifier; 879 Identifier = const_cast<IdentifierInfo *>(Id); 880 StartLocation = EndLocation = IdLoc; 881 } 882 883 /// \brief Specify that this unqualified-id was parsed as an 884 /// operator-function-id. 885 /// 886 /// \param OperatorLoc the location of the 'operator' keyword. 887 /// 888 /// \param Op the overloaded operator. 889 /// 890 /// \param SymbolLocations the locations of the individual operator symbols 891 /// in the operator. 892 void setOperatorFunctionId(SourceLocation OperatorLoc, 893 OverloadedOperatorKind Op, 894 SourceLocation SymbolLocations[3]); 895 896 /// \brief Specify that this unqualified-id was parsed as a 897 /// conversion-function-id. 898 /// 899 /// \param OperatorLoc the location of the 'operator' keyword. 900 /// 901 /// \param Ty the type to which this conversion function is converting. 902 /// 903 /// \param EndLoc the location of the last token that makes up the type name. setConversionFunctionId(SourceLocation OperatorLoc,ParsedType Ty,SourceLocation EndLoc)904 void setConversionFunctionId(SourceLocation OperatorLoc, 905 ParsedType Ty, 906 SourceLocation EndLoc) { 907 Kind = IK_ConversionFunctionId; 908 StartLocation = OperatorLoc; 909 EndLocation = EndLoc; 910 ConversionFunctionId = Ty; 911 } 912 913 /// \brief Specific that this unqualified-id was parsed as a 914 /// literal-operator-id. 915 /// 916 /// \param Id the parsed identifier. 917 /// 918 /// \param OpLoc the location of the 'operator' keyword. 919 /// 920 /// \param IdLoc the location of the identifier. setLiteralOperatorId(const IdentifierInfo * Id,SourceLocation OpLoc,SourceLocation IdLoc)921 void setLiteralOperatorId(const IdentifierInfo *Id, SourceLocation OpLoc, 922 SourceLocation IdLoc) { 923 Kind = IK_LiteralOperatorId; 924 Identifier = const_cast<IdentifierInfo *>(Id); 925 StartLocation = OpLoc; 926 EndLocation = IdLoc; 927 } 928 929 /// \brief Specify that this unqualified-id was parsed as a constructor name. 930 /// 931 /// \param ClassType the class type referred to by the constructor name. 932 /// 933 /// \param ClassNameLoc the location of the class name. 934 /// 935 /// \param EndLoc the location of the last token that makes up the type name. setConstructorName(ParsedType ClassType,SourceLocation ClassNameLoc,SourceLocation EndLoc)936 void setConstructorName(ParsedType ClassType, 937 SourceLocation ClassNameLoc, 938 SourceLocation EndLoc) { 939 Kind = IK_ConstructorName; 940 StartLocation = ClassNameLoc; 941 EndLocation = EndLoc; 942 ConstructorName = ClassType; 943 } 944 945 /// \brief Specify that this unqualified-id was parsed as a 946 /// template-id that names a constructor. 947 /// 948 /// \param TemplateId the template-id annotation that describes the parsed 949 /// template-id. This UnqualifiedId instance will take ownership of the 950 /// \p TemplateId and will free it on destruction. 951 void setConstructorTemplateId(TemplateIdAnnotation *TemplateId); 952 953 /// \brief Specify that this unqualified-id was parsed as a destructor name. 954 /// 955 /// \param TildeLoc the location of the '~' that introduces the destructor 956 /// name. 957 /// 958 /// \param ClassType the name of the class referred to by the destructor name. setDestructorName(SourceLocation TildeLoc,ParsedType ClassType,SourceLocation EndLoc)959 void setDestructorName(SourceLocation TildeLoc, 960 ParsedType ClassType, 961 SourceLocation EndLoc) { 962 Kind = IK_DestructorName; 963 StartLocation = TildeLoc; 964 EndLocation = EndLoc; 965 DestructorName = ClassType; 966 } 967 968 /// \brief Specify that this unqualified-id was parsed as a template-id. 969 /// 970 /// \param TemplateId the template-id annotation that describes the parsed 971 /// template-id. This UnqualifiedId instance will take ownership of the 972 /// \p TemplateId and will free it on destruction. 973 void setTemplateId(TemplateIdAnnotation *TemplateId); 974 975 /// \brief Return the source range that covers this unqualified-id. getSourceRange()976 SourceRange getSourceRange() const LLVM_READONLY { 977 return SourceRange(StartLocation, EndLocation); 978 } getLocStart()979 SourceLocation getLocStart() const LLVM_READONLY { return StartLocation; } getLocEnd()980 SourceLocation getLocEnd() const LLVM_READONLY { return EndLocation; } 981 }; 982 983 /// CachedTokens - A set of tokens that has been cached for later 984 /// parsing. 985 typedef SmallVector<Token, 4> CachedTokens; 986 987 /// DeclaratorChunk - One instance of this struct is used for each type in a 988 /// declarator that is parsed. 989 /// 990 /// This is intended to be a small value object. 991 struct DeclaratorChunk { 992 enum { 993 Pointer, Reference, Array, Function, BlockPointer, MemberPointer, Paren 994 } Kind; 995 996 /// Loc - The place where this type was defined. 997 SourceLocation Loc; 998 /// EndLoc - If valid, the place where this chunck ends. 999 SourceLocation EndLoc; 1000 1001 struct TypeInfoCommon { 1002 AttributeList *AttrList; 1003 }; 1004 1005 struct PointerTypeInfo : TypeInfoCommon { 1006 /// The type qualifiers: const/volatile/restrict. 1007 unsigned TypeQuals : 3; 1008 1009 /// The location of the const-qualifier, if any. 1010 unsigned ConstQualLoc; 1011 1012 /// The location of the volatile-qualifier, if any. 1013 unsigned VolatileQualLoc; 1014 1015 /// The location of the restrict-qualifier, if any. 1016 unsigned RestrictQualLoc; 1017 destroyDeclaratorChunk::PointerTypeInfo1018 void destroy() { 1019 } 1020 }; 1021 1022 struct ReferenceTypeInfo : TypeInfoCommon { 1023 /// The type qualifier: restrict. [GNU] C++ extension 1024 bool HasRestrict : 1; 1025 /// True if this is an lvalue reference, false if it's an rvalue reference. 1026 bool LValueRef : 1; destroyDeclaratorChunk::ReferenceTypeInfo1027 void destroy() { 1028 } 1029 }; 1030 1031 struct ArrayTypeInfo : TypeInfoCommon { 1032 /// The type qualifiers for the array: const/volatile/restrict. 1033 unsigned TypeQuals : 3; 1034 1035 /// True if this dimension included the 'static' keyword. 1036 bool hasStatic : 1; 1037 1038 /// True if this dimension was [*]. In this case, NumElts is null. 1039 bool isStar : 1; 1040 1041 /// This is the size of the array, or null if [] or [*] was specified. 1042 /// Since the parser is multi-purpose, and we don't want to impose a root 1043 /// expression class on all clients, NumElts is untyped. 1044 Expr *NumElts; 1045 destroyDeclaratorChunk::ArrayTypeInfo1046 void destroy() {} 1047 }; 1048 1049 /// ParamInfo - An array of paraminfo objects is allocated whenever a function 1050 /// declarator is parsed. There are two interesting styles of arguments here: 1051 /// K&R-style identifier lists and parameter type lists. K&R-style identifier 1052 /// lists will have information about the identifier, but no type information. 1053 /// Parameter type lists will have type info (if the actions module provides 1054 /// it), but may have null identifier info: e.g. for 'void foo(int X, int)'. 1055 struct ParamInfo { 1056 IdentifierInfo *Ident; 1057 SourceLocation IdentLoc; 1058 Decl *Param; 1059 1060 /// DefaultArgTokens - When the parameter's default argument 1061 /// cannot be parsed immediately (because it occurs within the 1062 /// declaration of a member function), it will be stored here as a 1063 /// sequence of tokens to be parsed once the class definition is 1064 /// complete. Non-NULL indicates that there is a default argument. 1065 CachedTokens *DefaultArgTokens; 1066 ParamInfoDeclaratorChunk::ParamInfo1067 ParamInfo() {} 1068 ParamInfo(IdentifierInfo *ident, SourceLocation iloc, 1069 Decl *param, 1070 CachedTokens *DefArgTokens = 0) IdentDeclaratorChunk::ParamInfo1071 : Ident(ident), IdentLoc(iloc), Param(param), 1072 DefaultArgTokens(DefArgTokens) {} 1073 }; 1074 1075 struct TypeAndRange { 1076 ParsedType Ty; 1077 SourceRange Range; 1078 }; 1079 1080 struct FunctionTypeInfo : TypeInfoCommon { 1081 /// hasPrototype - This is true if the function had at least one typed 1082 /// argument. If the function is () or (a,b,c), then it has no prototype, 1083 /// and is treated as a K&R-style function. 1084 unsigned hasPrototype : 1; 1085 1086 /// isVariadic - If this function has a prototype, and if that 1087 /// proto ends with ',...)', this is true. When true, EllipsisLoc 1088 /// contains the location of the ellipsis. 1089 unsigned isVariadic : 1; 1090 1091 /// \brief Whether the ref-qualifier (if any) is an lvalue reference. 1092 /// Otherwise, it's an rvalue reference. 1093 unsigned RefQualifierIsLValueRef : 1; 1094 1095 /// The type qualifiers: const/volatile/restrict. 1096 /// The qualifier bitmask values are the same as in QualType. 1097 unsigned TypeQuals : 3; 1098 1099 /// ExceptionSpecType - An ExceptionSpecificationType value. 1100 unsigned ExceptionSpecType : 3; 1101 1102 /// DeleteArgInfo - If this is true, we need to delete[] ArgInfo. 1103 unsigned DeleteArgInfo : 1; 1104 1105 /// When isVariadic is true, the location of the ellipsis in the source. 1106 unsigned EllipsisLoc; 1107 1108 /// NumArgs - This is the number of formal arguments provided for the 1109 /// declarator. 1110 unsigned NumArgs; 1111 1112 /// NumExceptions - This is the number of types in the dynamic-exception- 1113 /// decl, if the function has one. 1114 unsigned NumExceptions; 1115 1116 /// \brief The location of the ref-qualifier, if any. 1117 /// 1118 /// If this is an invalid location, there is no ref-qualifier. 1119 unsigned RefQualifierLoc; 1120 1121 /// \brief The location of the const-qualifier, if any. 1122 /// 1123 /// If this is an invalid location, there is no const-qualifier. 1124 unsigned ConstQualifierLoc; 1125 1126 /// \brief The location of the volatile-qualifier, if any. 1127 /// 1128 /// If this is an invalid location, there is no volatile-qualifier. 1129 unsigned VolatileQualifierLoc; 1130 1131 /// \brief The location of the 'mutable' qualifer in a lambda-declarator, if 1132 /// any. 1133 unsigned MutableLoc; 1134 1135 /// \brief When ExceptionSpecType isn't EST_None or EST_Delayed, the 1136 /// location of the keyword introducing the spec. 1137 unsigned ExceptionSpecLoc; 1138 1139 /// ArgInfo - This is a pointer to a new[]'d array of ParamInfo objects that 1140 /// describe the arguments for this function declarator. This is null if 1141 /// there are no arguments specified. 1142 ParamInfo *ArgInfo; 1143 1144 union { 1145 /// \brief Pointer to a new[]'d array of TypeAndRange objects that 1146 /// contain the types in the function's dynamic exception specification 1147 /// and their locations, if there is one. 1148 TypeAndRange *Exceptions; 1149 1150 /// \brief Pointer to the expression in the noexcept-specifier of this 1151 /// function, if it has one. 1152 Expr *NoexceptExpr; 1153 1154 /// \brief Pointer to the cached tokens for an exception-specification 1155 /// that has not yet been parsed. 1156 CachedTokens *ExceptionSpecTokens; 1157 }; 1158 1159 /// TrailingReturnType - If this isn't null, it's the trailing return type 1160 /// specified. This is actually a ParsedType, but stored as void* to 1161 /// allow union storage. 1162 void *TrailingReturnType; 1163 1164 /// freeArgs - reset the argument list to having zero arguments. This is 1165 /// used in various places for error recovery. freeArgsDeclaratorChunk::FunctionTypeInfo1166 void freeArgs() { 1167 if (DeleteArgInfo) { 1168 delete[] ArgInfo; 1169 DeleteArgInfo = false; 1170 } 1171 NumArgs = 0; 1172 } 1173 destroyDeclaratorChunk::FunctionTypeInfo1174 void destroy() { 1175 if (DeleteArgInfo) 1176 delete[] ArgInfo; 1177 if (getExceptionSpecType() == EST_Dynamic) 1178 delete[] Exceptions; 1179 else if (getExceptionSpecType() == EST_Delayed) 1180 delete ExceptionSpecTokens; 1181 } 1182 1183 /// isKNRPrototype - Return true if this is a K&R style identifier list, 1184 /// like "void foo(a,b,c)". In a function definition, this will be followed 1185 /// by the argument type definitions. isKNRPrototypeDeclaratorChunk::FunctionTypeInfo1186 bool isKNRPrototype() const { 1187 return !hasPrototype && NumArgs != 0; 1188 } 1189 getEllipsisLocDeclaratorChunk::FunctionTypeInfo1190 SourceLocation getEllipsisLoc() const { 1191 return SourceLocation::getFromRawEncoding(EllipsisLoc); 1192 } getExceptionSpecLocDeclaratorChunk::FunctionTypeInfo1193 SourceLocation getExceptionSpecLoc() const { 1194 return SourceLocation::getFromRawEncoding(ExceptionSpecLoc); 1195 } 1196 1197 /// \brief Retrieve the location of the ref-qualifier, if any. getRefQualifierLocDeclaratorChunk::FunctionTypeInfo1198 SourceLocation getRefQualifierLoc() const { 1199 return SourceLocation::getFromRawEncoding(RefQualifierLoc); 1200 } 1201 1202 /// \brief Retrieve the location of the ref-qualifier, if any. getConstQualifierLocDeclaratorChunk::FunctionTypeInfo1203 SourceLocation getConstQualifierLoc() const { 1204 return SourceLocation::getFromRawEncoding(ConstQualifierLoc); 1205 } 1206 1207 /// \brief Retrieve the location of the ref-qualifier, if any. getVolatileQualifierLocDeclaratorChunk::FunctionTypeInfo1208 SourceLocation getVolatileQualifierLoc() const { 1209 return SourceLocation::getFromRawEncoding(VolatileQualifierLoc); 1210 } 1211 1212 /// \brief Retrieve the location of the 'mutable' qualifier, if any. getMutableLocDeclaratorChunk::FunctionTypeInfo1213 SourceLocation getMutableLoc() const { 1214 return SourceLocation::getFromRawEncoding(MutableLoc); 1215 } 1216 1217 /// \brief Determine whether this function declaration contains a 1218 /// ref-qualifier. hasRefQualifierDeclaratorChunk::FunctionTypeInfo1219 bool hasRefQualifier() const { return getRefQualifierLoc().isValid(); } 1220 1221 /// \brief Determine whether this lambda-declarator contains a 'mutable' 1222 /// qualifier. hasMutableQualifierDeclaratorChunk::FunctionTypeInfo1223 bool hasMutableQualifier() const { return getMutableLoc().isValid(); } 1224 1225 /// \brief Get the type of exception specification this function has. getExceptionSpecTypeDeclaratorChunk::FunctionTypeInfo1226 ExceptionSpecificationType getExceptionSpecType() const { 1227 return static_cast<ExceptionSpecificationType>(ExceptionSpecType); 1228 } 1229 }; 1230 1231 struct BlockPointerTypeInfo : TypeInfoCommon { 1232 /// For now, sema will catch these as invalid. 1233 /// The type qualifiers: const/volatile/restrict. 1234 unsigned TypeQuals : 3; 1235 destroyDeclaratorChunk::BlockPointerTypeInfo1236 void destroy() { 1237 } 1238 }; 1239 1240 struct MemberPointerTypeInfo : TypeInfoCommon { 1241 /// The type qualifiers: const/volatile/restrict. 1242 unsigned TypeQuals : 3; 1243 // CXXScopeSpec has a constructor, so it can't be a direct member. 1244 // So we need some pointer-aligned storage and a bit of trickery. 1245 union { 1246 void *Aligner; 1247 char Mem[sizeof(CXXScopeSpec)]; 1248 } ScopeMem; ScopeDeclaratorChunk::MemberPointerTypeInfo1249 CXXScopeSpec &Scope() { 1250 return *reinterpret_cast<CXXScopeSpec*>(ScopeMem.Mem); 1251 } ScopeDeclaratorChunk::MemberPointerTypeInfo1252 const CXXScopeSpec &Scope() const { 1253 return *reinterpret_cast<const CXXScopeSpec*>(ScopeMem.Mem); 1254 } destroyDeclaratorChunk::MemberPointerTypeInfo1255 void destroy() { 1256 Scope().~CXXScopeSpec(); 1257 } 1258 }; 1259 1260 union { 1261 TypeInfoCommon Common; 1262 PointerTypeInfo Ptr; 1263 ReferenceTypeInfo Ref; 1264 ArrayTypeInfo Arr; 1265 FunctionTypeInfo Fun; 1266 BlockPointerTypeInfo Cls; 1267 MemberPointerTypeInfo Mem; 1268 }; 1269 destroyDeclaratorChunk1270 void destroy() { 1271 switch (Kind) { 1272 case DeclaratorChunk::Function: return Fun.destroy(); 1273 case DeclaratorChunk::Pointer: return Ptr.destroy(); 1274 case DeclaratorChunk::BlockPointer: return Cls.destroy(); 1275 case DeclaratorChunk::Reference: return Ref.destroy(); 1276 case DeclaratorChunk::Array: return Arr.destroy(); 1277 case DeclaratorChunk::MemberPointer: return Mem.destroy(); 1278 case DeclaratorChunk::Paren: return; 1279 } 1280 } 1281 1282 /// getAttrs - If there are attributes applied to this declaratorchunk, return 1283 /// them. getAttrsDeclaratorChunk1284 const AttributeList *getAttrs() const { 1285 return Common.AttrList; 1286 } 1287 getAttrListRefDeclaratorChunk1288 AttributeList *&getAttrListRef() { 1289 return Common.AttrList; 1290 } 1291 1292 /// getPointer - Return a DeclaratorChunk for a pointer. 1293 /// getPointerDeclaratorChunk1294 static DeclaratorChunk getPointer(unsigned TypeQuals, SourceLocation Loc, 1295 SourceLocation ConstQualLoc, 1296 SourceLocation VolatileQualLoc, 1297 SourceLocation RestrictQualLoc) { 1298 DeclaratorChunk I; 1299 I.Kind = Pointer; 1300 I.Loc = Loc; 1301 I.Ptr.TypeQuals = TypeQuals; 1302 I.Ptr.ConstQualLoc = ConstQualLoc.getRawEncoding(); 1303 I.Ptr.VolatileQualLoc = VolatileQualLoc.getRawEncoding(); 1304 I.Ptr.RestrictQualLoc = RestrictQualLoc.getRawEncoding(); 1305 I.Ptr.AttrList = 0; 1306 return I; 1307 } 1308 1309 /// getReference - Return a DeclaratorChunk for a reference. 1310 /// getReferenceDeclaratorChunk1311 static DeclaratorChunk getReference(unsigned TypeQuals, SourceLocation Loc, 1312 bool lvalue) { 1313 DeclaratorChunk I; 1314 I.Kind = Reference; 1315 I.Loc = Loc; 1316 I.Ref.HasRestrict = (TypeQuals & DeclSpec::TQ_restrict) != 0; 1317 I.Ref.LValueRef = lvalue; 1318 I.Ref.AttrList = 0; 1319 return I; 1320 } 1321 1322 /// getArray - Return a DeclaratorChunk for an array. 1323 /// getArrayDeclaratorChunk1324 static DeclaratorChunk getArray(unsigned TypeQuals, 1325 bool isStatic, bool isStar, Expr *NumElts, 1326 SourceLocation LBLoc, SourceLocation RBLoc) { 1327 DeclaratorChunk I; 1328 I.Kind = Array; 1329 I.Loc = LBLoc; 1330 I.EndLoc = RBLoc; 1331 I.Arr.AttrList = 0; 1332 I.Arr.TypeQuals = TypeQuals; 1333 I.Arr.hasStatic = isStatic; 1334 I.Arr.isStar = isStar; 1335 I.Arr.NumElts = NumElts; 1336 return I; 1337 } 1338 1339 /// DeclaratorChunk::getFunction - Return a DeclaratorChunk for a function. 1340 /// "TheDeclarator" is the declarator that this will be added to. 1341 static DeclaratorChunk getFunction(bool hasProto, bool isVariadic, 1342 SourceLocation EllipsisLoc, 1343 ParamInfo *ArgInfo, unsigned NumArgs, 1344 unsigned TypeQuals, 1345 bool RefQualifierIsLvalueRef, 1346 SourceLocation RefQualifierLoc, 1347 SourceLocation ConstQualifierLoc, 1348 SourceLocation VolatileQualifierLoc, 1349 SourceLocation MutableLoc, 1350 ExceptionSpecificationType ESpecType, 1351 SourceLocation ESpecLoc, 1352 ParsedType *Exceptions, 1353 SourceRange *ExceptionRanges, 1354 unsigned NumExceptions, 1355 Expr *NoexceptExpr, 1356 CachedTokens *ExceptionSpecTokens, 1357 SourceLocation LocalRangeBegin, 1358 SourceLocation LocalRangeEnd, 1359 Declarator &TheDeclarator, 1360 ParsedType TrailingReturnType = 1361 ParsedType()); 1362 1363 /// getBlockPointer - Return a DeclaratorChunk for a block. 1364 /// getBlockPointerDeclaratorChunk1365 static DeclaratorChunk getBlockPointer(unsigned TypeQuals, 1366 SourceLocation Loc) { 1367 DeclaratorChunk I; 1368 I.Kind = BlockPointer; 1369 I.Loc = Loc; 1370 I.Cls.TypeQuals = TypeQuals; 1371 I.Cls.AttrList = 0; 1372 return I; 1373 } 1374 getMemberPointerDeclaratorChunk1375 static DeclaratorChunk getMemberPointer(const CXXScopeSpec &SS, 1376 unsigned TypeQuals, 1377 SourceLocation Loc) { 1378 DeclaratorChunk I; 1379 I.Kind = MemberPointer; 1380 I.Loc = Loc; 1381 I.Mem.TypeQuals = TypeQuals; 1382 I.Mem.AttrList = 0; 1383 new (I.Mem.ScopeMem.Mem) CXXScopeSpec(SS); 1384 return I; 1385 } 1386 1387 /// getParen - Return a DeclaratorChunk for a paren. 1388 /// getParenDeclaratorChunk1389 static DeclaratorChunk getParen(SourceLocation LParenLoc, 1390 SourceLocation RParenLoc) { 1391 DeclaratorChunk I; 1392 I.Kind = Paren; 1393 I.Loc = LParenLoc; 1394 I.EndLoc = RParenLoc; 1395 I.Common.AttrList = 0; 1396 return I; 1397 } 1398 1399 }; 1400 1401 /// \brief Described the kind of function definition (if any) provided for 1402 /// a function. 1403 enum FunctionDefinitionKind { 1404 FDK_Declaration, 1405 FDK_Definition, 1406 FDK_Defaulted, 1407 FDK_Deleted 1408 }; 1409 1410 /// Declarator - Information about one declarator, including the parsed type 1411 /// information and the identifier. When the declarator is fully formed, this 1412 /// is turned into the appropriate Decl object. 1413 /// 1414 /// Declarators come in two types: normal declarators and abstract declarators. 1415 /// Abstract declarators are used when parsing types, and don't have an 1416 /// identifier. Normal declarators do have ID's. 1417 /// 1418 /// Instances of this class should be a transient object that lives on the 1419 /// stack, not objects that are allocated in large quantities on the heap. 1420 class Declarator { 1421 public: 1422 enum TheContext { 1423 FileContext, // File scope declaration. 1424 PrototypeContext, // Within a function prototype. 1425 ObjCResultContext, // An ObjC method result type. 1426 ObjCParameterContext,// An ObjC method parameter type. 1427 KNRTypeListContext, // K&R type definition list for formals. 1428 TypeNameContext, // Abstract declarator for types. 1429 MemberContext, // Struct/Union field. 1430 BlockContext, // Declaration within a block in a function. 1431 ForContext, // Declaration within first part of a for loop. 1432 ConditionContext, // Condition declaration in a C++ if/switch/while/for. 1433 TemplateParamContext,// Within a template parameter list. 1434 CXXNewContext, // C++ new-expression. 1435 CXXCatchContext, // C++ catch exception-declaration 1436 ObjCCatchContext, // Objective-C catch exception-declaration 1437 BlockLiteralContext, // Block literal declarator. 1438 LambdaExprContext, // Lambda-expression declarator. 1439 TrailingReturnContext, // C++11 trailing-type-specifier. 1440 TemplateTypeArgContext, // Template type argument. 1441 AliasDeclContext, // C++11 alias-declaration. 1442 AliasTemplateContext // C++11 alias-declaration template. 1443 }; 1444 1445 private: 1446 const DeclSpec &DS; 1447 CXXScopeSpec SS; 1448 UnqualifiedId Name; 1449 SourceRange Range; 1450 1451 /// Context - Where we are parsing this declarator. 1452 /// 1453 TheContext Context; 1454 1455 /// DeclTypeInfo - This holds each type that the declarator includes as it is 1456 /// parsed. This is pushed from the identifier out, which means that element 1457 /// #0 will be the most closely bound to the identifier, and 1458 /// DeclTypeInfo.back() will be the least closely bound. 1459 SmallVector<DeclaratorChunk, 8> DeclTypeInfo; 1460 1461 /// InvalidType - Set by Sema::GetTypeForDeclarator(). 1462 bool InvalidType : 1; 1463 1464 /// GroupingParens - Set by Parser::ParseParenDeclarator(). 1465 bool GroupingParens : 1; 1466 1467 /// FunctionDefinition - Is this Declarator for a function or member 1468 /// definition and, if so, what kind? 1469 /// 1470 /// Actually a FunctionDefinitionKind. 1471 unsigned FunctionDefinition : 2; 1472 1473 // Redeclaration - Is this Declarator is a redeclaration. 1474 bool Redeclaration : 1; 1475 1476 /// Attrs - Attributes. 1477 ParsedAttributes Attrs; 1478 1479 /// AsmLabel - The asm label, if specified. 1480 Expr *AsmLabel; 1481 1482 /// InlineParams - This is a local array used for the first function decl 1483 /// chunk to avoid going to the heap for the common case when we have one 1484 /// function chunk in the declarator. 1485 DeclaratorChunk::ParamInfo InlineParams[16]; 1486 bool InlineParamsUsed; 1487 1488 /// Extension - true if the declaration is preceded by __extension__. 1489 bool Extension : 1; 1490 1491 /// \brief If this is the second or subsequent declarator in this declaration, 1492 /// the location of the comma before this declarator. 1493 SourceLocation CommaLoc; 1494 1495 /// \brief If provided, the source location of the ellipsis used to describe 1496 /// this declarator as a parameter pack. 1497 SourceLocation EllipsisLoc; 1498 1499 friend struct DeclaratorChunk; 1500 1501 public: Declarator(const DeclSpec & ds,TheContext C)1502 Declarator(const DeclSpec &ds, TheContext C) 1503 : DS(ds), Range(ds.getSourceRange()), Context(C), 1504 InvalidType(DS.getTypeSpecType() == DeclSpec::TST_error), 1505 GroupingParens(false), FunctionDefinition(FDK_Declaration), 1506 Redeclaration(false), 1507 Attrs(ds.getAttributePool().getFactory()), AsmLabel(0), 1508 InlineParamsUsed(false), Extension(false) { 1509 } 1510 ~Declarator()1511 ~Declarator() { 1512 clear(); 1513 } 1514 1515 /// getDeclSpec - Return the declaration-specifier that this declarator was 1516 /// declared with. getDeclSpec()1517 const DeclSpec &getDeclSpec() const { return DS; } 1518 1519 /// getMutableDeclSpec - Return a non-const version of the DeclSpec. This 1520 /// should be used with extreme care: declspecs can often be shared between 1521 /// multiple declarators, so mutating the DeclSpec affects all of the 1522 /// Declarators. This should only be done when the declspec is known to not 1523 /// be shared or when in error recovery etc. getMutableDeclSpec()1524 DeclSpec &getMutableDeclSpec() { return const_cast<DeclSpec &>(DS); } 1525 getAttributePool()1526 AttributePool &getAttributePool() const { 1527 return Attrs.getPool(); 1528 } 1529 1530 /// getCXXScopeSpec - Return the C++ scope specifier (global scope or 1531 /// nested-name-specifier) that is part of the declarator-id. getCXXScopeSpec()1532 const CXXScopeSpec &getCXXScopeSpec() const { return SS; } getCXXScopeSpec()1533 CXXScopeSpec &getCXXScopeSpec() { return SS; } 1534 1535 /// \brief Retrieve the name specified by this declarator. getName()1536 UnqualifiedId &getName() { return Name; } 1537 getContext()1538 TheContext getContext() const { return Context; } 1539 isPrototypeContext()1540 bool isPrototypeContext() const { 1541 return (Context == PrototypeContext || 1542 Context == ObjCParameterContext || 1543 Context == ObjCResultContext); 1544 } 1545 1546 /// getSourceRange - Get the source range that spans this declarator. getSourceRange()1547 const SourceRange &getSourceRange() const LLVM_READONLY { return Range; } getLocStart()1548 SourceLocation getLocStart() const LLVM_READONLY { return Range.getBegin(); } getLocEnd()1549 SourceLocation getLocEnd() const LLVM_READONLY { return Range.getEnd(); } 1550 SetSourceRange(SourceRange R)1551 void SetSourceRange(SourceRange R) { Range = R; } 1552 /// SetRangeBegin - Set the start of the source range to Loc, unless it's 1553 /// invalid. SetRangeBegin(SourceLocation Loc)1554 void SetRangeBegin(SourceLocation Loc) { 1555 if (!Loc.isInvalid()) 1556 Range.setBegin(Loc); 1557 } 1558 /// SetRangeEnd - Set the end of the source range to Loc, unless it's invalid. SetRangeEnd(SourceLocation Loc)1559 void SetRangeEnd(SourceLocation Loc) { 1560 if (!Loc.isInvalid()) 1561 Range.setEnd(Loc); 1562 } 1563 /// ExtendWithDeclSpec - Extend the declarator source range to include the 1564 /// given declspec, unless its location is invalid. Adopts the range start if 1565 /// the current range start is invalid. ExtendWithDeclSpec(const DeclSpec & DS)1566 void ExtendWithDeclSpec(const DeclSpec &DS) { 1567 const SourceRange &SR = DS.getSourceRange(); 1568 if (Range.getBegin().isInvalid()) 1569 Range.setBegin(SR.getBegin()); 1570 if (!SR.getEnd().isInvalid()) 1571 Range.setEnd(SR.getEnd()); 1572 } 1573 1574 /// clear - Reset the contents of this Declarator. clear()1575 void clear() { 1576 SS.clear(); 1577 Name.clear(); 1578 Range = DS.getSourceRange(); 1579 1580 for (unsigned i = 0, e = DeclTypeInfo.size(); i != e; ++i) 1581 DeclTypeInfo[i].destroy(); 1582 DeclTypeInfo.clear(); 1583 Attrs.clear(); 1584 AsmLabel = 0; 1585 InlineParamsUsed = false; 1586 CommaLoc = SourceLocation(); 1587 EllipsisLoc = SourceLocation(); 1588 } 1589 1590 /// mayOmitIdentifier - Return true if the identifier is either optional or 1591 /// not allowed. This is true for typenames, prototypes, and template 1592 /// parameter lists. mayOmitIdentifier()1593 bool mayOmitIdentifier() const { 1594 switch (Context) { 1595 case FileContext: 1596 case KNRTypeListContext: 1597 case MemberContext: 1598 case BlockContext: 1599 case ForContext: 1600 case ConditionContext: 1601 return false; 1602 1603 case TypeNameContext: 1604 case AliasDeclContext: 1605 case AliasTemplateContext: 1606 case PrototypeContext: 1607 case ObjCParameterContext: 1608 case ObjCResultContext: 1609 case TemplateParamContext: 1610 case CXXNewContext: 1611 case CXXCatchContext: 1612 case ObjCCatchContext: 1613 case BlockLiteralContext: 1614 case LambdaExprContext: 1615 case TemplateTypeArgContext: 1616 case TrailingReturnContext: 1617 return true; 1618 } 1619 llvm_unreachable("unknown context kind!"); 1620 } 1621 1622 /// mayHaveIdentifier - Return true if the identifier is either optional or 1623 /// required. This is true for normal declarators and prototypes, but not 1624 /// typenames. mayHaveIdentifier()1625 bool mayHaveIdentifier() const { 1626 switch (Context) { 1627 case FileContext: 1628 case KNRTypeListContext: 1629 case MemberContext: 1630 case BlockContext: 1631 case ForContext: 1632 case ConditionContext: 1633 case PrototypeContext: 1634 case TemplateParamContext: 1635 case CXXCatchContext: 1636 case ObjCCatchContext: 1637 return true; 1638 1639 case TypeNameContext: 1640 case CXXNewContext: 1641 case AliasDeclContext: 1642 case AliasTemplateContext: 1643 case ObjCParameterContext: 1644 case ObjCResultContext: 1645 case BlockLiteralContext: 1646 case LambdaExprContext: 1647 case TemplateTypeArgContext: 1648 case TrailingReturnContext: 1649 return false; 1650 } 1651 llvm_unreachable("unknown context kind!"); 1652 } 1653 1654 /// mayBeFollowedByCXXDirectInit - Return true if the declarator can be 1655 /// followed by a C++ direct initializer, e.g. "int x(1);". mayBeFollowedByCXXDirectInit()1656 bool mayBeFollowedByCXXDirectInit() const { 1657 if (hasGroupingParens()) return false; 1658 1659 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_typedef) 1660 return false; 1661 1662 if (getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_extern && 1663 Context != FileContext) 1664 return false; 1665 1666 // Special names can't have direct initializers. 1667 if (Name.getKind() != UnqualifiedId::IK_Identifier) 1668 return false; 1669 1670 switch (Context) { 1671 case FileContext: 1672 case BlockContext: 1673 case ForContext: 1674 return true; 1675 1676 case ConditionContext: 1677 // This may not be followed by a direct initializer, but it can't be a 1678 // function declaration either, and we'd prefer to perform a tentative 1679 // parse in order to produce the right diagnostic. 1680 return true; 1681 1682 case KNRTypeListContext: 1683 case MemberContext: 1684 case PrototypeContext: 1685 case ObjCParameterContext: 1686 case ObjCResultContext: 1687 case TemplateParamContext: 1688 case CXXCatchContext: 1689 case ObjCCatchContext: 1690 case TypeNameContext: 1691 case CXXNewContext: 1692 case AliasDeclContext: 1693 case AliasTemplateContext: 1694 case BlockLiteralContext: 1695 case LambdaExprContext: 1696 case TemplateTypeArgContext: 1697 case TrailingReturnContext: 1698 return false; 1699 } 1700 llvm_unreachable("unknown context kind!"); 1701 } 1702 1703 /// isPastIdentifier - Return true if we have parsed beyond the point where 1704 /// the isPastIdentifier()1705 bool isPastIdentifier() const { return Name.isValid(); } 1706 1707 /// hasName - Whether this declarator has a name, which might be an 1708 /// identifier (accessible via getIdentifier()) or some kind of 1709 /// special C++ name (constructor, destructor, etc.). hasName()1710 bool hasName() const { 1711 return Name.getKind() != UnqualifiedId::IK_Identifier || Name.Identifier; 1712 } 1713 getIdentifier()1714 IdentifierInfo *getIdentifier() const { 1715 if (Name.getKind() == UnqualifiedId::IK_Identifier) 1716 return Name.Identifier; 1717 1718 return 0; 1719 } getIdentifierLoc()1720 SourceLocation getIdentifierLoc() const { return Name.StartLocation; } 1721 1722 /// \brief Set the name of this declarator to be the given identifier. SetIdentifier(IdentifierInfo * Id,SourceLocation IdLoc)1723 void SetIdentifier(IdentifierInfo *Id, SourceLocation IdLoc) { 1724 Name.setIdentifier(Id, IdLoc); 1725 } 1726 1727 /// AddTypeInfo - Add a chunk to this declarator. Also extend the range to 1728 /// EndLoc, which should be the last token of the chunk. AddTypeInfo(const DeclaratorChunk & TI,ParsedAttributes & attrs,SourceLocation EndLoc)1729 void AddTypeInfo(const DeclaratorChunk &TI, 1730 ParsedAttributes &attrs, 1731 SourceLocation EndLoc) { 1732 DeclTypeInfo.push_back(TI); 1733 DeclTypeInfo.back().getAttrListRef() = attrs.getList(); 1734 getAttributePool().takeAllFrom(attrs.getPool()); 1735 1736 if (!EndLoc.isInvalid()) 1737 SetRangeEnd(EndLoc); 1738 } 1739 1740 /// AddInnermostTypeInfo - Add a new innermost chunk to this declarator. AddInnermostTypeInfo(const DeclaratorChunk & TI)1741 void AddInnermostTypeInfo(const DeclaratorChunk &TI) { 1742 DeclTypeInfo.insert(DeclTypeInfo.begin(), TI); 1743 } 1744 1745 /// getNumTypeObjects() - Return the number of types applied to this 1746 /// declarator. getNumTypeObjects()1747 unsigned getNumTypeObjects() const { return DeclTypeInfo.size(); } 1748 1749 /// Return the specified TypeInfo from this declarator. TypeInfo #0 is 1750 /// closest to the identifier. getTypeObject(unsigned i)1751 const DeclaratorChunk &getTypeObject(unsigned i) const { 1752 assert(i < DeclTypeInfo.size() && "Invalid type chunk"); 1753 return DeclTypeInfo[i]; 1754 } getTypeObject(unsigned i)1755 DeclaratorChunk &getTypeObject(unsigned i) { 1756 assert(i < DeclTypeInfo.size() && "Invalid type chunk"); 1757 return DeclTypeInfo[i]; 1758 } 1759 DropFirstTypeObject()1760 void DropFirstTypeObject() 1761 { 1762 assert(!DeclTypeInfo.empty() && "No type chunks to drop."); 1763 DeclTypeInfo.front().destroy(); 1764 DeclTypeInfo.erase(DeclTypeInfo.begin()); 1765 } 1766 1767 /// isArrayOfUnknownBound - This method returns true if the declarator 1768 /// is a declarator for an array of unknown bound (looking through 1769 /// parentheses). isArrayOfUnknownBound()1770 bool isArrayOfUnknownBound() const { 1771 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) { 1772 switch (DeclTypeInfo[i].Kind) { 1773 case DeclaratorChunk::Paren: 1774 continue; 1775 case DeclaratorChunk::Function: 1776 case DeclaratorChunk::Pointer: 1777 case DeclaratorChunk::Reference: 1778 case DeclaratorChunk::BlockPointer: 1779 case DeclaratorChunk::MemberPointer: 1780 return false; 1781 case DeclaratorChunk::Array: 1782 return !DeclTypeInfo[i].Arr.NumElts; 1783 } 1784 llvm_unreachable("Invalid type chunk"); 1785 } 1786 return false; 1787 } 1788 1789 /// isFunctionDeclarator - This method returns true if the declarator 1790 /// is a function declarator (looking through parentheses). 1791 /// If true is returned, then the reference type parameter idx is 1792 /// assigned with the index of the declaration chunk. isFunctionDeclarator(unsigned & idx)1793 bool isFunctionDeclarator(unsigned& idx) const { 1794 for (unsigned i = 0, i_end = DeclTypeInfo.size(); i < i_end; ++i) { 1795 switch (DeclTypeInfo[i].Kind) { 1796 case DeclaratorChunk::Function: 1797 idx = i; 1798 return true; 1799 case DeclaratorChunk::Paren: 1800 continue; 1801 case DeclaratorChunk::Pointer: 1802 case DeclaratorChunk::Reference: 1803 case DeclaratorChunk::Array: 1804 case DeclaratorChunk::BlockPointer: 1805 case DeclaratorChunk::MemberPointer: 1806 return false; 1807 } 1808 llvm_unreachable("Invalid type chunk"); 1809 } 1810 return false; 1811 } 1812 1813 /// isFunctionDeclarator - Once this declarator is fully parsed and formed, 1814 /// this method returns true if the identifier is a function declarator 1815 /// (looking through parentheses). isFunctionDeclarator()1816 bool isFunctionDeclarator() const { 1817 unsigned index; 1818 return isFunctionDeclarator(index); 1819 } 1820 1821 /// getFunctionTypeInfo - Retrieves the function type info object 1822 /// (looking through parentheses). getFunctionTypeInfo()1823 DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() { 1824 assert(isFunctionDeclarator() && "Not a function declarator!"); 1825 unsigned index = 0; 1826 isFunctionDeclarator(index); 1827 return DeclTypeInfo[index].Fun; 1828 } 1829 1830 /// getFunctionTypeInfo - Retrieves the function type info object 1831 /// (looking through parentheses). getFunctionTypeInfo()1832 const DeclaratorChunk::FunctionTypeInfo &getFunctionTypeInfo() const { 1833 return const_cast<Declarator*>(this)->getFunctionTypeInfo(); 1834 } 1835 1836 /// \brief Determine whether the declaration that will be produced from 1837 /// this declaration will be a function. 1838 /// 1839 /// A declaration can declare a function even if the declarator itself 1840 /// isn't a function declarator, if the type specifier refers to a function 1841 /// type. This routine checks for both cases. 1842 bool isDeclarationOfFunction() const; 1843 1844 /// takeAttributes - Takes attributes from the given parsed-attributes 1845 /// set and add them to this declarator. 1846 /// 1847 /// These examples both add 3 attributes to "var": 1848 /// short int var __attribute__((aligned(16),common,deprecated)); 1849 /// short int x, __attribute__((aligned(16)) var 1850 /// __attribute__((common,deprecated)); 1851 /// 1852 /// Also extends the range of the declarator. takeAttributes(ParsedAttributes & attrs,SourceLocation lastLoc)1853 void takeAttributes(ParsedAttributes &attrs, SourceLocation lastLoc) { 1854 Attrs.takeAllFrom(attrs); 1855 1856 if (!lastLoc.isInvalid()) 1857 SetRangeEnd(lastLoc); 1858 } 1859 getAttributes()1860 const AttributeList *getAttributes() const { return Attrs.getList(); } getAttributes()1861 AttributeList *getAttributes() { return Attrs.getList(); } 1862 getAttrListRef()1863 AttributeList *&getAttrListRef() { return Attrs.getListRef(); } 1864 1865 /// hasAttributes - do we contain any attributes? hasAttributes()1866 bool hasAttributes() const { 1867 if (getAttributes() || getDeclSpec().hasAttributes()) return true; 1868 for (unsigned i = 0, e = getNumTypeObjects(); i != e; ++i) 1869 if (getTypeObject(i).getAttrs()) 1870 return true; 1871 return false; 1872 } 1873 setAsmLabel(Expr * E)1874 void setAsmLabel(Expr *E) { AsmLabel = E; } getAsmLabel()1875 Expr *getAsmLabel() const { return AsmLabel; } 1876 1877 void setExtension(bool Val = true) { Extension = Val; } getExtension()1878 bool getExtension() const { return Extension; } 1879 1880 void setInvalidType(bool Val = true) { InvalidType = Val; } isInvalidType()1881 bool isInvalidType() const { 1882 return InvalidType || DS.getTypeSpecType() == DeclSpec::TST_error; 1883 } 1884 setGroupingParens(bool flag)1885 void setGroupingParens(bool flag) { GroupingParens = flag; } hasGroupingParens()1886 bool hasGroupingParens() const { return GroupingParens; } 1887 isFirstDeclarator()1888 bool isFirstDeclarator() const { return !CommaLoc.isValid(); } getCommaLoc()1889 SourceLocation getCommaLoc() const { return CommaLoc; } setCommaLoc(SourceLocation CL)1890 void setCommaLoc(SourceLocation CL) { CommaLoc = CL; } 1891 hasEllipsis()1892 bool hasEllipsis() const { return EllipsisLoc.isValid(); } getEllipsisLoc()1893 SourceLocation getEllipsisLoc() const { return EllipsisLoc; } setEllipsisLoc(SourceLocation EL)1894 void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; } 1895 setFunctionDefinitionKind(FunctionDefinitionKind Val)1896 void setFunctionDefinitionKind(FunctionDefinitionKind Val) { 1897 FunctionDefinition = Val; 1898 } 1899 isFunctionDefinition()1900 bool isFunctionDefinition() const { 1901 return getFunctionDefinitionKind() != FDK_Declaration; 1902 } 1903 getFunctionDefinitionKind()1904 FunctionDefinitionKind getFunctionDefinitionKind() const { 1905 return (FunctionDefinitionKind)FunctionDefinition; 1906 } 1907 setRedeclaration(bool Val)1908 void setRedeclaration(bool Val) { Redeclaration = Val; } isRedeclaration()1909 bool isRedeclaration() const { return Redeclaration; } 1910 }; 1911 1912 /// FieldDeclarator - This little struct is used to capture information about 1913 /// structure field declarators, which is basically just a bitfield size. 1914 struct FieldDeclarator { 1915 Declarator D; 1916 Expr *BitfieldSize; FieldDeclaratorFieldDeclarator1917 explicit FieldDeclarator(DeclSpec &DS) : D(DS, Declarator::MemberContext) { 1918 BitfieldSize = 0; 1919 } 1920 }; 1921 1922 /// VirtSpecifiers - Represents a C++0x virt-specifier-seq. 1923 class VirtSpecifiers { 1924 public: 1925 enum Specifier { 1926 VS_None = 0, 1927 VS_Override = 1, 1928 VS_Final = 2 1929 }; 1930 VirtSpecifiers()1931 VirtSpecifiers() : Specifiers(0) { } 1932 1933 bool SetSpecifier(Specifier VS, SourceLocation Loc, 1934 const char *&PrevSpec); 1935 isOverrideSpecified()1936 bool isOverrideSpecified() const { return Specifiers & VS_Override; } getOverrideLoc()1937 SourceLocation getOverrideLoc() const { return VS_overrideLoc; } 1938 isFinalSpecified()1939 bool isFinalSpecified() const { return Specifiers & VS_Final; } getFinalLoc()1940 SourceLocation getFinalLoc() const { return VS_finalLoc; } 1941 clear()1942 void clear() { Specifiers = 0; } 1943 1944 static const char *getSpecifierName(Specifier VS); 1945 getLastLocation()1946 SourceLocation getLastLocation() const { return LastLocation; } 1947 1948 private: 1949 unsigned Specifiers; 1950 1951 SourceLocation VS_overrideLoc, VS_finalLoc; 1952 SourceLocation LastLocation; 1953 }; 1954 1955 /// LambdaCapture - An individual capture in a lambda introducer. 1956 struct LambdaCapture { 1957 LambdaCaptureKind Kind; 1958 SourceLocation Loc; 1959 IdentifierInfo* Id; 1960 SourceLocation EllipsisLoc; 1961 1962 LambdaCapture(LambdaCaptureKind Kind, SourceLocation Loc, 1963 IdentifierInfo* Id = 0, 1964 SourceLocation EllipsisLoc = SourceLocation()) KindLambdaCapture1965 : Kind(Kind), Loc(Loc), Id(Id), EllipsisLoc(EllipsisLoc) 1966 {} 1967 }; 1968 1969 /// LambdaIntroducer - Represents a complete lambda introducer. 1970 struct LambdaIntroducer { 1971 SourceRange Range; 1972 SourceLocation DefaultLoc; 1973 LambdaCaptureDefault Default; 1974 llvm::SmallVector<LambdaCapture, 4> Captures; 1975 LambdaIntroducerLambdaIntroducer1976 LambdaIntroducer() 1977 : Default(LCD_None) {} 1978 1979 /// addCapture - Append a capture in a lambda introducer. 1980 void addCapture(LambdaCaptureKind Kind, 1981 SourceLocation Loc, 1982 IdentifierInfo* Id = 0, 1983 SourceLocation EllipsisLoc = SourceLocation()) { 1984 Captures.push_back(LambdaCapture(Kind, Loc, Id, EllipsisLoc)); 1985 } 1986 1987 }; 1988 1989 } // end namespace clang 1990 1991 #endif 1992