Decl.h revision 9852f58f50b4fc20914fbce5b4454135a42343f4
1//===--- Decl.h - Classes for representing declarations ---------*- 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 Decl subclasses. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_DECL_H 15#define LLVM_CLANG_AST_DECL_H 16 17#include "clang/AST/APValue.h" 18#include "clang/AST/DeclBase.h" 19#include "clang/AST/DeclarationName.h" 20#include "clang/AST/ExternalASTSource.h" 21#include "clang/AST/Redeclarable.h" 22#include "clang/AST/Type.h" 23#include "clang/Basic/Linkage.h" 24#include "llvm/ADT/ArrayRef.h" 25#include "llvm/ADT/Optional.h" 26#include "llvm/Support/Compiler.h" 27 28namespace clang { 29struct ASTTemplateArgumentListInfo; 30class CXXTemporary; 31class CompoundStmt; 32class DependentFunctionTemplateSpecializationInfo; 33class Expr; 34class FunctionTemplateDecl; 35class FunctionTemplateSpecializationInfo; 36class LabelStmt; 37class MemberSpecializationInfo; 38class Module; 39class NestedNameSpecifier; 40class Stmt; 41class StringLiteral; 42class TemplateArgumentList; 43class TemplateParameterList; 44class TypeLoc; 45class UnresolvedSetImpl; 46 47/// \brief A container of type source information. 48/// 49/// A client can read the relevant info using TypeLoc wrappers, e.g: 50/// @code 51/// TypeLoc TL = TypeSourceInfo->getTypeLoc(); 52/// if (PointerLoc *PL = dyn_cast<PointerLoc>(&TL)) 53/// PL->getStarLoc().print(OS, SrcMgr); 54/// @endcode 55/// 56class TypeSourceInfo { 57 QualType Ty; 58 // Contains a memory block after the class, used for type source information, 59 // allocated by ASTContext. 60 friend class ASTContext; 61 TypeSourceInfo(QualType ty) : Ty(ty) { } 62public: 63 /// \brief Return the type wrapped by this type source info. 64 QualType getType() const { return Ty; } 65 66 /// \brief Return the TypeLoc wrapper for the type source info. 67 TypeLoc getTypeLoc() const; // implemented in TypeLoc.h 68}; 69 70/// TranslationUnitDecl - The top declaration context. 71class TranslationUnitDecl : public Decl, public DeclContext { 72 virtual void anchor(); 73 ASTContext &Ctx; 74 75 /// The (most recently entered) anonymous namespace for this 76 /// translation unit, if one has been created. 77 NamespaceDecl *AnonymousNamespace; 78 79 explicit TranslationUnitDecl(ASTContext &ctx) 80 : Decl(TranslationUnit, 0, SourceLocation()), 81 DeclContext(TranslationUnit), 82 Ctx(ctx), AnonymousNamespace(0) {} 83public: 84 ASTContext &getASTContext() const { return Ctx; } 85 86 NamespaceDecl *getAnonymousNamespace() const { return AnonymousNamespace; } 87 void setAnonymousNamespace(NamespaceDecl *D) { AnonymousNamespace = D; } 88 89 static TranslationUnitDecl *Create(ASTContext &C); 90 // Implement isa/cast/dyncast/etc. 91 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 92 static bool classofKind(Kind K) { return K == TranslationUnit; } 93 static DeclContext *castToDeclContext(const TranslationUnitDecl *D) { 94 return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D)); 95 } 96 static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) { 97 return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC)); 98 } 99}; 100 101/// NamedDecl - This represents a decl with a name. Many decls have names such 102/// as ObjCMethodDecl, but not \@class, etc. 103class NamedDecl : public Decl { 104 virtual void anchor(); 105 /// Name - The name of this declaration, which is typically a normal 106 /// identifier but may also be a special kind of name (C++ 107 /// constructor, Objective-C selector, etc.) 108 DeclarationName Name; 109 110private: 111 NamedDecl *getUnderlyingDeclImpl(); 112 113protected: 114 NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N) 115 : Decl(DK, DC, L), Name(N) { } 116 117public: 118 /// getIdentifier - Get the identifier that names this declaration, 119 /// if there is one. This will return NULL if this declaration has 120 /// no name (e.g., for an unnamed class) or if the name is a special 121 /// name (C++ constructor, Objective-C selector, etc.). 122 IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); } 123 124 /// getName - Get the name of identifier for this declaration as a StringRef. 125 /// This requires that the declaration have a name and that it be a simple 126 /// identifier. 127 StringRef getName() const { 128 assert(Name.isIdentifier() && "Name is not a simple identifier"); 129 return getIdentifier() ? getIdentifier()->getName() : ""; 130 } 131 132 /// getNameAsString - Get a human-readable name for the declaration, even if 133 /// it is one of the special kinds of names (C++ constructor, Objective-C 134 /// selector, etc). Creating this name requires expensive string 135 /// manipulation, so it should be called only when performance doesn't matter. 136 /// For simple declarations, getNameAsCString() should suffice. 137 // 138 // FIXME: This function should be renamed to indicate that it is not just an 139 // alternate form of getName(), and clients should move as appropriate. 140 // 141 // FIXME: Deprecated, move clients to getName(). 142 std::string getNameAsString() const { return Name.getAsString(); } 143 144 void printName(raw_ostream &os) const { return Name.printName(os); } 145 146 /// getDeclName - Get the actual, stored name of the declaration, 147 /// which may be a special name. 148 DeclarationName getDeclName() const { return Name; } 149 150 /// \brief Set the name of this declaration. 151 void setDeclName(DeclarationName N) { Name = N; } 152 153 /// getQualifiedNameAsString - Returns human-readable qualified name for 154 /// declaration, like A::B::i, for i being member of namespace A::B. 155 /// If declaration is not member of context which can be named (record, 156 /// namespace), it will return same result as getNameAsString(). 157 /// Creating this name is expensive, so it should be called only when 158 /// performance doesn't matter. 159 std::string getQualifiedNameAsString() const; 160 std::string getQualifiedNameAsString(const PrintingPolicy &Policy) const; 161 162 /// getNameForDiagnostic - Appends a human-readable name for this 163 /// declaration into the given string. 164 /// 165 /// This is the method invoked by Sema when displaying a NamedDecl 166 /// in a diagnostic. It does not necessarily produce the same 167 /// result as getNameAsString(); for example, class template 168 /// specializations are printed with their template arguments. 169 /// 170 /// TODO: use an API that doesn't require so many temporary strings 171 virtual void getNameForDiagnostic(std::string &S, 172 const PrintingPolicy &Policy, 173 bool Qualified) const { 174 if (Qualified) 175 S += getQualifiedNameAsString(Policy); 176 else 177 S += getNameAsString(); 178 } 179 180 /// declarationReplaces - Determine whether this declaration, if 181 /// known to be well-formed within its context, will replace the 182 /// declaration OldD if introduced into scope. A declaration will 183 /// replace another declaration if, for example, it is a 184 /// redeclaration of the same variable or function, but not if it is 185 /// a declaration of a different kind (function vs. class) or an 186 /// overloaded function. 187 bool declarationReplaces(NamedDecl *OldD) const; 188 189 /// \brief Determine whether this declaration has linkage. 190 bool hasLinkage() const; 191 192 using Decl::isModulePrivate; 193 using Decl::setModulePrivate; 194 195 /// \brief Determine whether this declaration is hidden from name lookup. 196 bool isHidden() const { return Hidden; } 197 198 /// \brief Determine whether this declaration is a C++ class member. 199 bool isCXXClassMember() const { 200 const DeclContext *DC = getDeclContext(); 201 202 // C++0x [class.mem]p1: 203 // The enumerators of an unscoped enumeration defined in 204 // the class are members of the class. 205 // FIXME: support C++0x scoped enumerations. 206 if (isa<EnumDecl>(DC)) 207 DC = DC->getParent(); 208 209 return DC->isRecord(); 210 } 211 212 /// \brief Determine whether the given declaration is an instance member of 213 /// a C++ class. 214 bool isCXXInstanceMember() const; 215 216 class LinkageInfo { 217 uint8_t linkage_ : 2; 218 uint8_t visibility_ : 2; 219 uint8_t explicit_ : 1; 220 221 void setVisibility(Visibility V, bool E) { visibility_ = V; explicit_ = E; } 222 public: 223 LinkageInfo() : linkage_(ExternalLinkage), visibility_(DefaultVisibility), 224 explicit_(false) {} 225 LinkageInfo(Linkage L, Visibility V, bool E) 226 : linkage_(L), visibility_(V), explicit_(E) { 227 assert(linkage() == L && visibility() == V && visibilityExplicit() == E && 228 "Enum truncated!"); 229 } 230 231 static LinkageInfo external() { 232 return LinkageInfo(); 233 } 234 static LinkageInfo internal() { 235 return LinkageInfo(InternalLinkage, DefaultVisibility, false); 236 } 237 static LinkageInfo uniqueExternal() { 238 return LinkageInfo(UniqueExternalLinkage, DefaultVisibility, false); 239 } 240 static LinkageInfo none() { 241 return LinkageInfo(NoLinkage, DefaultVisibility, false); 242 } 243 244 Linkage linkage() const { return (Linkage)linkage_; } 245 Visibility visibility() const { return (Visibility)visibility_; } 246 bool visibilityExplicit() const { return explicit_; } 247 248 void setLinkage(Linkage L) { linkage_ = L; } 249 void mergeLinkage(Linkage L) { 250 setLinkage(minLinkage(linkage(), L)); 251 } 252 void mergeLinkage(LinkageInfo Other) { 253 mergeLinkage(Other.linkage()); 254 } 255 256 // Merge the visibility V giving preference to explicit ones. 257 // This is used, for example, when merging the visibility of a class 258 // down to one of its members. If the member has no explicit visibility, 259 // the class visibility wins. 260 void mergeVisibility(Visibility V, bool E = false) { 261 // Never increase the visibility 262 if (visibility() < V) 263 return; 264 265 // If we have an explicit visibility, keep it 266 if (visibilityExplicit()) 267 return; 268 269 setVisibility(V, E); 270 } 271 // Merge the visibility V, keeping the most restrictive one. 272 // This is used for cases like merging the visibility of a template 273 // argument to an instantiation. If we already have a hidden class, 274 // no argument should give it default visibility. 275 void mergeVisibilityWithMin(Visibility V, bool E = false) { 276 // Never increase the visibility 277 if (visibility() < V) 278 return; 279 280 // FIXME: this 281 // If this visibility is explicit, keep it. 282 if (visibilityExplicit() && !E) 283 return; 284 285 // should be replaced with this 286 // Don't lose the explicit bit for nothing 287 // if (visibility() == V && visibilityExplicit()) 288 // return; 289 290 setVisibility(V, E); 291 } 292 void mergeVisibility(LinkageInfo Other) { 293 mergeVisibility(Other.visibility(), Other.visibilityExplicit()); 294 } 295 void mergeVisibilityWithMin(LinkageInfo Other) { 296 mergeVisibilityWithMin(Other.visibility(), Other.visibilityExplicit()); 297 } 298 299 void merge(LinkageInfo Other) { 300 mergeLinkage(Other); 301 mergeVisibility(Other); 302 } 303 void mergeWithMin(LinkageInfo Other) { 304 mergeLinkage(Other); 305 mergeVisibilityWithMin(Other); 306 } 307 }; 308 309 /// \brief Determine what kind of linkage this entity has. 310 Linkage getLinkage() const; 311 312 /// \brief Determines the visibility of this entity. 313 Visibility getVisibility() const { 314 return getLinkageAndVisibility().visibility(); 315 } 316 317 /// \brief Determines the linkage and visibility of this entity. 318 LinkageInfo getLinkageAndVisibility() const; 319 320 /// \brief If visibility was explicitly specified for this 321 /// declaration, return that visibility. 322 llvm::Optional<Visibility> getExplicitVisibility() const; 323 324 /// \brief Clear the linkage cache in response to a change 325 /// to the declaration. 326 void ClearLinkageCache(); 327 328 /// \brief Looks through UsingDecls and ObjCCompatibleAliasDecls for 329 /// the underlying named decl. 330 NamedDecl *getUnderlyingDecl() { 331 // Fast-path the common case. 332 if (this->getKind() != UsingShadow && 333 this->getKind() != ObjCCompatibleAlias) 334 return this; 335 336 return getUnderlyingDeclImpl(); 337 } 338 const NamedDecl *getUnderlyingDecl() const { 339 return const_cast<NamedDecl*>(this)->getUnderlyingDecl(); 340 } 341 342 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 343 static bool classofKind(Kind K) { return K >= firstNamed && K <= lastNamed; } 344}; 345 346inline raw_ostream &operator<<(raw_ostream &OS, const NamedDecl &ND) { 347 ND.printName(OS); 348 return OS; 349} 350 351/// LabelDecl - Represents the declaration of a label. Labels also have a 352/// corresponding LabelStmt, which indicates the position that the label was 353/// defined at. For normal labels, the location of the decl is the same as the 354/// location of the statement. For GNU local labels (__label__), the decl 355/// location is where the __label__ is. 356class LabelDecl : public NamedDecl { 357 virtual void anchor(); 358 LabelStmt *TheStmt; 359 /// LocStart - For normal labels, this is the same as the main declaration 360 /// label, i.e., the location of the identifier; for GNU local labels, 361 /// this is the location of the __label__ keyword. 362 SourceLocation LocStart; 363 364 LabelDecl(DeclContext *DC, SourceLocation IdentL, IdentifierInfo *II, 365 LabelStmt *S, SourceLocation StartL) 366 : NamedDecl(Label, DC, IdentL, II), TheStmt(S), LocStart(StartL) {} 367 368public: 369 static LabelDecl *Create(ASTContext &C, DeclContext *DC, 370 SourceLocation IdentL, IdentifierInfo *II); 371 static LabelDecl *Create(ASTContext &C, DeclContext *DC, 372 SourceLocation IdentL, IdentifierInfo *II, 373 SourceLocation GnuLabelL); 374 static LabelDecl *CreateDeserialized(ASTContext &C, unsigned ID); 375 376 LabelStmt *getStmt() const { return TheStmt; } 377 void setStmt(LabelStmt *T) { TheStmt = T; } 378 379 bool isGnuLocal() const { return LocStart != getLocation(); } 380 void setLocStart(SourceLocation L) { LocStart = L; } 381 382 SourceRange getSourceRange() const LLVM_READONLY { 383 return SourceRange(LocStart, getLocation()); 384 } 385 386 // Implement isa/cast/dyncast/etc. 387 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 388 static bool classofKind(Kind K) { return K == Label; } 389}; 390 391/// NamespaceDecl - Represent a C++ namespace. 392class NamespaceDecl : public NamedDecl, public DeclContext, 393 public Redeclarable<NamespaceDecl> 394{ 395 virtual void anchor(); 396 397 /// LocStart - The starting location of the source range, pointing 398 /// to either the namespace or the inline keyword. 399 SourceLocation LocStart; 400 /// RBraceLoc - The ending location of the source range. 401 SourceLocation RBraceLoc; 402 403 /// \brief A pointer to either the anonymous namespace that lives just inside 404 /// this namespace or to the first namespace in the chain (the latter case 405 /// only when this is not the first in the chain), along with a 406 /// boolean value indicating whether this is an inline namespace. 407 llvm::PointerIntPair<NamespaceDecl *, 1, bool> AnonOrFirstNamespaceAndInline; 408 409 NamespaceDecl(DeclContext *DC, bool Inline, SourceLocation StartLoc, 410 SourceLocation IdLoc, IdentifierInfo *Id, 411 NamespaceDecl *PrevDecl); 412 413 typedef Redeclarable<NamespaceDecl> redeclarable_base; 414 virtual NamespaceDecl *getNextRedeclaration() { 415 return RedeclLink.getNext(); 416 } 417 virtual NamespaceDecl *getPreviousDeclImpl() { 418 return getPreviousDecl(); 419 } 420 virtual NamespaceDecl *getMostRecentDeclImpl() { 421 return getMostRecentDecl(); 422 } 423 424public: 425 static NamespaceDecl *Create(ASTContext &C, DeclContext *DC, 426 bool Inline, SourceLocation StartLoc, 427 SourceLocation IdLoc, IdentifierInfo *Id, 428 NamespaceDecl *PrevDecl); 429 430 static NamespaceDecl *CreateDeserialized(ASTContext &C, unsigned ID); 431 432 typedef redeclarable_base::redecl_iterator redecl_iterator; 433 using redeclarable_base::redecls_begin; 434 using redeclarable_base::redecls_end; 435 using redeclarable_base::getPreviousDecl; 436 using redeclarable_base::getMostRecentDecl; 437 438 /// \brief Returns true if this is an anonymous namespace declaration. 439 /// 440 /// For example: 441 /// \code 442 /// namespace { 443 /// ... 444 /// }; 445 /// \endcode 446 /// q.v. C++ [namespace.unnamed] 447 bool isAnonymousNamespace() const { 448 return !getIdentifier(); 449 } 450 451 /// \brief Returns true if this is an inline namespace declaration. 452 bool isInline() const { 453 return AnonOrFirstNamespaceAndInline.getInt(); 454 } 455 456 /// \brief Set whether this is an inline namespace declaration. 457 void setInline(bool Inline) { 458 AnonOrFirstNamespaceAndInline.setInt(Inline); 459 } 460 461 /// \brief Get the original (first) namespace declaration. 462 NamespaceDecl *getOriginalNamespace() { 463 if (isFirstDeclaration()) 464 return this; 465 466 return AnonOrFirstNamespaceAndInline.getPointer(); 467 } 468 469 /// \brief Get the original (first) namespace declaration. 470 const NamespaceDecl *getOriginalNamespace() const { 471 if (isFirstDeclaration()) 472 return this; 473 474 return AnonOrFirstNamespaceAndInline.getPointer(); 475 } 476 477 /// \brief Return true if this declaration is an original (first) declaration 478 /// of the namespace. This is false for non-original (subsequent) namespace 479 /// declarations and anonymous namespaces. 480 bool isOriginalNamespace() const { 481 return isFirstDeclaration(); 482 } 483 484 /// \brief Retrieve the anonymous namespace nested inside this namespace, 485 /// if any. 486 NamespaceDecl *getAnonymousNamespace() const { 487 return getOriginalNamespace()->AnonOrFirstNamespaceAndInline.getPointer(); 488 } 489 490 void setAnonymousNamespace(NamespaceDecl *D) { 491 getOriginalNamespace()->AnonOrFirstNamespaceAndInline.setPointer(D); 492 } 493 494 /// Retrieves the canonical declaration of this namespace. 495 NamespaceDecl *getCanonicalDecl() { 496 return getOriginalNamespace(); 497 } 498 const NamespaceDecl *getCanonicalDecl() const { 499 return getOriginalNamespace(); 500 } 501 502 virtual SourceRange getSourceRange() const LLVM_READONLY { 503 return SourceRange(LocStart, RBraceLoc); 504 } 505 506 SourceLocation getLocStart() const LLVM_READONLY { return LocStart; } 507 SourceLocation getRBraceLoc() const { return RBraceLoc; } 508 void setLocStart(SourceLocation L) { LocStart = L; } 509 void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } 510 511 // Implement isa/cast/dyncast/etc. 512 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 513 static bool classofKind(Kind K) { return K == Namespace; } 514 static DeclContext *castToDeclContext(const NamespaceDecl *D) { 515 return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D)); 516 } 517 static NamespaceDecl *castFromDeclContext(const DeclContext *DC) { 518 return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC)); 519 } 520 521 friend class ASTDeclReader; 522 friend class ASTDeclWriter; 523}; 524 525/// ValueDecl - Represent the declaration of a variable (in which case it is 526/// an lvalue) a function (in which case it is a function designator) or 527/// an enum constant. 528class ValueDecl : public NamedDecl { 529 virtual void anchor(); 530 QualType DeclType; 531 532protected: 533 ValueDecl(Kind DK, DeclContext *DC, SourceLocation L, 534 DeclarationName N, QualType T) 535 : NamedDecl(DK, DC, L, N), DeclType(T) {} 536public: 537 QualType getType() const { return DeclType; } 538 void setType(QualType newType) { DeclType = newType; } 539 540 /// \brief Determine whether this symbol is weakly-imported, 541 /// or declared with the weak or weak-ref attr. 542 bool isWeak() const; 543 544 // Implement isa/cast/dyncast/etc. 545 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 546 static bool classofKind(Kind K) { return K >= firstValue && K <= lastValue; } 547}; 548 549/// QualifierInfo - A struct with extended info about a syntactic 550/// name qualifier, to be used for the case of out-of-line declarations. 551struct QualifierInfo { 552 NestedNameSpecifierLoc QualifierLoc; 553 554 /// NumTemplParamLists - The number of "outer" template parameter lists. 555 /// The count includes all of the template parameter lists that were matched 556 /// against the template-ids occurring into the NNS and possibly (in the 557 /// case of an explicit specialization) a final "template <>". 558 unsigned NumTemplParamLists; 559 560 /// TemplParamLists - A new-allocated array of size NumTemplParamLists, 561 /// containing pointers to the "outer" template parameter lists. 562 /// It includes all of the template parameter lists that were matched 563 /// against the template-ids occurring into the NNS and possibly (in the 564 /// case of an explicit specialization) a final "template <>". 565 TemplateParameterList** TemplParamLists; 566 567 /// Default constructor. 568 QualifierInfo() : QualifierLoc(), NumTemplParamLists(0), TemplParamLists(0) {} 569 570 /// setTemplateParameterListsInfo - Sets info about "outer" template 571 /// parameter lists. 572 void setTemplateParameterListsInfo(ASTContext &Context, 573 unsigned NumTPLists, 574 TemplateParameterList **TPLists); 575 576private: 577 // Copy constructor and copy assignment are disabled. 578 QualifierInfo(const QualifierInfo&) LLVM_DELETED_FUNCTION; 579 QualifierInfo& operator=(const QualifierInfo&) LLVM_DELETED_FUNCTION; 580}; 581 582/// \brief Represents a ValueDecl that came out of a declarator. 583/// Contains type source information through TypeSourceInfo. 584class DeclaratorDecl : public ValueDecl { 585 // A struct representing both a TInfo and a syntactic qualifier, 586 // to be used for the (uncommon) case of out-of-line declarations. 587 struct ExtInfo : public QualifierInfo { 588 TypeSourceInfo *TInfo; 589 }; 590 591 llvm::PointerUnion<TypeSourceInfo*, ExtInfo*> DeclInfo; 592 593 /// InnerLocStart - The start of the source range for this declaration, 594 /// ignoring outer template declarations. 595 SourceLocation InnerLocStart; 596 597 bool hasExtInfo() const { return DeclInfo.is<ExtInfo*>(); } 598 ExtInfo *getExtInfo() { return DeclInfo.get<ExtInfo*>(); } 599 const ExtInfo *getExtInfo() const { return DeclInfo.get<ExtInfo*>(); } 600 601protected: 602 DeclaratorDecl(Kind DK, DeclContext *DC, SourceLocation L, 603 DeclarationName N, QualType T, TypeSourceInfo *TInfo, 604 SourceLocation StartL) 605 : ValueDecl(DK, DC, L, N, T), DeclInfo(TInfo), InnerLocStart(StartL) { 606 } 607 608public: 609 TypeSourceInfo *getTypeSourceInfo() const { 610 return hasExtInfo() 611 ? getExtInfo()->TInfo 612 : DeclInfo.get<TypeSourceInfo*>(); 613 } 614 void setTypeSourceInfo(TypeSourceInfo *TI) { 615 if (hasExtInfo()) 616 getExtInfo()->TInfo = TI; 617 else 618 DeclInfo = TI; 619 } 620 621 /// getInnerLocStart - Return SourceLocation representing start of source 622 /// range ignoring outer template declarations. 623 SourceLocation getInnerLocStart() const { return InnerLocStart; } 624 void setInnerLocStart(SourceLocation L) { InnerLocStart = L; } 625 626 /// getOuterLocStart - Return SourceLocation representing start of source 627 /// range taking into account any outer template declarations. 628 SourceLocation getOuterLocStart() const; 629 630 virtual SourceRange getSourceRange() const LLVM_READONLY; 631 SourceLocation getLocStart() const LLVM_READONLY { 632 return getOuterLocStart(); 633 } 634 635 /// \brief Retrieve the nested-name-specifier that qualifies the name of this 636 /// declaration, if it was present in the source. 637 NestedNameSpecifier *getQualifier() const { 638 return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() 639 : 0; 640 } 641 642 /// \brief Retrieve the nested-name-specifier (with source-location 643 /// information) that qualifies the name of this declaration, if it was 644 /// present in the source. 645 NestedNameSpecifierLoc getQualifierLoc() const { 646 return hasExtInfo() ? getExtInfo()->QualifierLoc 647 : NestedNameSpecifierLoc(); 648 } 649 650 void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); 651 652 unsigned getNumTemplateParameterLists() const { 653 return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; 654 } 655 TemplateParameterList *getTemplateParameterList(unsigned index) const { 656 assert(index < getNumTemplateParameterLists()); 657 return getExtInfo()->TemplParamLists[index]; 658 } 659 void setTemplateParameterListsInfo(ASTContext &Context, unsigned NumTPLists, 660 TemplateParameterList **TPLists); 661 662 SourceLocation getTypeSpecStartLoc() const; 663 664 // Implement isa/cast/dyncast/etc. 665 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 666 static bool classofKind(Kind K) { 667 return K >= firstDeclarator && K <= lastDeclarator; 668 } 669 670 friend class ASTDeclReader; 671 friend class ASTDeclWriter; 672}; 673 674/// \brief Structure used to store a statement, the constant value to 675/// which it was evaluated (if any), and whether or not the statement 676/// is an integral constant expression (if known). 677struct EvaluatedStmt { 678 EvaluatedStmt() : WasEvaluated(false), IsEvaluating(false), CheckedICE(false), 679 CheckingICE(false), IsICE(false) { } 680 681 /// \brief Whether this statement was already evaluated. 682 bool WasEvaluated : 1; 683 684 /// \brief Whether this statement is being evaluated. 685 bool IsEvaluating : 1; 686 687 /// \brief Whether we already checked whether this statement was an 688 /// integral constant expression. 689 bool CheckedICE : 1; 690 691 /// \brief Whether we are checking whether this statement is an 692 /// integral constant expression. 693 bool CheckingICE : 1; 694 695 /// \brief Whether this statement is an integral constant expression, 696 /// or in C++11, whether the statement is a constant expression. Only 697 /// valid if CheckedICE is true. 698 bool IsICE : 1; 699 700 Stmt *Value; 701 APValue Evaluated; 702}; 703 704/// VarDecl - An instance of this class is created to represent a variable 705/// declaration or definition. 706class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> { 707public: 708 typedef clang::StorageClass StorageClass; 709 710 /// getStorageClassSpecifierString - Return the string used to 711 /// specify the storage class \p SC. 712 /// 713 /// It is illegal to call this function with SC == None. 714 static const char *getStorageClassSpecifierString(StorageClass SC); 715 716 /// \brief Initialization styles. 717 enum InitializationStyle { 718 CInit, ///< C-style initialization with assignment 719 CallInit, ///< Call-style initialization (C++98) 720 ListInit ///< Direct list-initialization (C++11) 721 }; 722 723protected: 724 /// \brief Placeholder type used in Init to denote an unparsed C++ default 725 /// argument. 726 struct UnparsedDefaultArgument; 727 728 /// \brief Placeholder type used in Init to denote an uninstantiated C++ 729 /// default argument. 730 struct UninstantiatedDefaultArgument; 731 732 typedef llvm::PointerUnion4<Stmt *, EvaluatedStmt *, 733 UnparsedDefaultArgument *, 734 UninstantiatedDefaultArgument *> InitType; 735 736 /// \brief The initializer for this variable or, for a ParmVarDecl, the 737 /// C++ default argument. 738 mutable InitType Init; 739 740private: 741 class VarDeclBitfields { 742 friend class VarDecl; 743 friend class ASTDeclReader; 744 745 unsigned SClass : 3; 746 unsigned SClassAsWritten : 3; 747 unsigned ThreadSpecified : 1; 748 unsigned InitStyle : 2; 749 750 /// \brief Whether this variable is the exception variable in a C++ catch 751 /// or an Objective-C @catch statement. 752 unsigned ExceptionVar : 1; 753 754 /// \brief Whether this local variable could be allocated in the return 755 /// slot of its function, enabling the named return value optimization 756 /// (NRVO). 757 unsigned NRVOVariable : 1; 758 759 /// \brief Whether this variable is the for-range-declaration in a C++0x 760 /// for-range statement. 761 unsigned CXXForRangeDecl : 1; 762 763 /// \brief Whether this variable is an ARC pseudo-__strong 764 /// variable; see isARCPseudoStrong() for details. 765 unsigned ARCPseudoStrong : 1; 766 767 /// \brief Whether this variable is (C++0x) constexpr. 768 unsigned IsConstexpr : 1; 769 }; 770 enum { NumVarDeclBits = 14 }; 771 772 friend class ASTDeclReader; 773 friend class StmtIteratorBase; 774 775protected: 776 enum { NumParameterIndexBits = 8 }; 777 778 class ParmVarDeclBitfields { 779 friend class ParmVarDecl; 780 friend class ASTDeclReader; 781 782 unsigned : NumVarDeclBits; 783 784 /// Whether this parameter inherits a default argument from a 785 /// prior declaration. 786 unsigned HasInheritedDefaultArg : 1; 787 788 /// Whether this parameter undergoes K&R argument promotion. 789 unsigned IsKNRPromoted : 1; 790 791 /// Whether this parameter is an ObjC method parameter or not. 792 unsigned IsObjCMethodParam : 1; 793 794 /// If IsObjCMethodParam, a Decl::ObjCDeclQualifier. 795 /// Otherwise, the number of function parameter scopes enclosing 796 /// the function parameter scope in which this parameter was 797 /// declared. 798 unsigned ScopeDepthOrObjCQuals : 7; 799 800 /// The number of parameters preceding this parameter in the 801 /// function parameter scope in which it was declared. 802 unsigned ParameterIndex : NumParameterIndexBits; 803 }; 804 805 union { 806 unsigned AllBits; 807 VarDeclBitfields VarDeclBits; 808 ParmVarDeclBitfields ParmVarDeclBits; 809 }; 810 811 VarDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc, 812 SourceLocation IdLoc, IdentifierInfo *Id, 813 QualType T, TypeSourceInfo *TInfo, StorageClass SC, 814 StorageClass SCAsWritten) 815 : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), Init() { 816 assert(sizeof(VarDeclBitfields) <= sizeof(unsigned)); 817 assert(sizeof(ParmVarDeclBitfields) <= sizeof(unsigned)); 818 AllBits = 0; 819 VarDeclBits.SClass = SC; 820 VarDeclBits.SClassAsWritten = SCAsWritten; 821 // Everything else is implicitly initialized to false. 822 } 823 824 typedef Redeclarable<VarDecl> redeclarable_base; 825 virtual VarDecl *getNextRedeclaration() { return RedeclLink.getNext(); } 826 virtual VarDecl *getPreviousDeclImpl() { 827 return getPreviousDecl(); 828 } 829 virtual VarDecl *getMostRecentDeclImpl() { 830 return getMostRecentDecl(); 831 } 832 833public: 834 typedef redeclarable_base::redecl_iterator redecl_iterator; 835 using redeclarable_base::redecls_begin; 836 using redeclarable_base::redecls_end; 837 using redeclarable_base::getPreviousDecl; 838 using redeclarable_base::getMostRecentDecl; 839 840 static VarDecl *Create(ASTContext &C, DeclContext *DC, 841 SourceLocation StartLoc, SourceLocation IdLoc, 842 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, 843 StorageClass S, StorageClass SCAsWritten); 844 845 static VarDecl *CreateDeserialized(ASTContext &C, unsigned ID); 846 847 virtual SourceRange getSourceRange() const LLVM_READONLY; 848 849 StorageClass getStorageClass() const { 850 return (StorageClass) VarDeclBits.SClass; 851 } 852 StorageClass getStorageClassAsWritten() const { 853 return (StorageClass) VarDeclBits.SClassAsWritten; 854 } 855 void setStorageClass(StorageClass SC); 856 void setStorageClassAsWritten(StorageClass SC) { 857 assert(isLegalForVariable(SC)); 858 VarDeclBits.SClassAsWritten = SC; 859 } 860 861 void setThreadSpecified(bool T) { VarDeclBits.ThreadSpecified = T; } 862 bool isThreadSpecified() const { 863 return VarDeclBits.ThreadSpecified; 864 } 865 866 /// hasLocalStorage - Returns true if a variable with function scope 867 /// is a non-static local variable. 868 bool hasLocalStorage() const { 869 if (getStorageClass() == SC_None) 870 return !isFileVarDecl(); 871 872 // Return true for: Auto, Register. 873 // Return false for: Extern, Static, PrivateExtern, OpenCLWorkGroupLocal. 874 875 return getStorageClass() >= SC_Auto; 876 } 877 878 /// isStaticLocal - Returns true if a variable with function scope is a 879 /// static local variable. 880 bool isStaticLocal() const { 881 return getStorageClass() == SC_Static && !isFileVarDecl(); 882 } 883 884 /// hasExternStorage - Returns true if a variable has extern or 885 /// __private_extern__ storage. 886 bool hasExternalStorage() const { 887 return getStorageClass() == SC_Extern || 888 getStorageClass() == SC_PrivateExtern; 889 } 890 891 /// hasGlobalStorage - Returns true for all variables that do not 892 /// have local storage. This includs all global variables as well 893 /// as static variables declared within a function. 894 bool hasGlobalStorage() const { return !hasLocalStorage(); } 895 896 /// \brief Determines whether this variable is a variable with 897 /// external, C linkage. 898 bool isExternC() const; 899 900 /// isLocalVarDecl - Returns true for local variable declarations 901 /// other than parameters. Note that this includes static variables 902 /// inside of functions. It also includes variables inside blocks. 903 /// 904 /// void foo() { int x; static int y; extern int z; } 905 /// 906 bool isLocalVarDecl() const { 907 if (getKind() != Decl::Var) 908 return false; 909 if (const DeclContext *DC = getDeclContext()) 910 return DC->getRedeclContext()->isFunctionOrMethod(); 911 return false; 912 } 913 914 /// isFunctionOrMethodVarDecl - Similar to isLocalVarDecl, but 915 /// excludes variables declared in blocks. 916 bool isFunctionOrMethodVarDecl() const { 917 if (getKind() != Decl::Var) 918 return false; 919 const DeclContext *DC = getDeclContext()->getRedeclContext(); 920 return DC->isFunctionOrMethod() && DC->getDeclKind() != Decl::Block; 921 } 922 923 /// \brief Determines whether this is a static data member. 924 /// 925 /// This will only be true in C++, and applies to, e.g., the 926 /// variable 'x' in: 927 /// \code 928 /// struct S { 929 /// static int x; 930 /// }; 931 /// \endcode 932 bool isStaticDataMember() const { 933 // If it wasn't static, it would be a FieldDecl. 934 return getKind() != Decl::ParmVar && getDeclContext()->isRecord(); 935 } 936 937 virtual VarDecl *getCanonicalDecl(); 938 const VarDecl *getCanonicalDecl() const { 939 return const_cast<VarDecl*>(this)->getCanonicalDecl(); 940 } 941 942 enum DefinitionKind { 943 DeclarationOnly, ///< This declaration is only a declaration. 944 TentativeDefinition, ///< This declaration is a tentative definition. 945 Definition ///< This declaration is definitely a definition. 946 }; 947 948 /// \brief Check whether this declaration is a definition. If this could be 949 /// a tentative definition (in C), don't check whether there's an overriding 950 /// definition. 951 DefinitionKind isThisDeclarationADefinition(ASTContext &) const; 952 DefinitionKind isThisDeclarationADefinition() const { 953 return isThisDeclarationADefinition(getASTContext()); 954 } 955 956 /// \brief Check whether this variable is defined in this 957 /// translation unit. 958 DefinitionKind hasDefinition(ASTContext &) const; 959 DefinitionKind hasDefinition() const { 960 return hasDefinition(getASTContext()); 961 } 962 963 /// \brief Get the tentative definition that acts as the real definition in 964 /// a TU. Returns null if there is a proper definition available. 965 VarDecl *getActingDefinition(); 966 const VarDecl *getActingDefinition() const { 967 return const_cast<VarDecl*>(this)->getActingDefinition(); 968 } 969 970 /// \brief Determine whether this is a tentative definition of a 971 /// variable in C. 972 bool isTentativeDefinitionNow() const; 973 974 /// \brief Get the real (not just tentative) definition for this declaration. 975 VarDecl *getDefinition(ASTContext &); 976 const VarDecl *getDefinition(ASTContext &C) const { 977 return const_cast<VarDecl*>(this)->getDefinition(C); 978 } 979 VarDecl *getDefinition() { 980 return getDefinition(getASTContext()); 981 } 982 const VarDecl *getDefinition() const { 983 return const_cast<VarDecl*>(this)->getDefinition(); 984 } 985 986 /// \brief Determine whether this is or was instantiated from an out-of-line 987 /// definition of a static data member. 988 virtual bool isOutOfLine() const; 989 990 /// \brief If this is a static data member, find its out-of-line definition. 991 VarDecl *getOutOfLineDefinition(); 992 993 /// isFileVarDecl - Returns true for file scoped variable declaration. 994 bool isFileVarDecl() const { 995 if (getKind() != Decl::Var) 996 return false; 997 998 if (getDeclContext()->getRedeclContext()->isFileContext()) 999 return true; 1000 1001 if (isStaticDataMember()) 1002 return true; 1003 1004 return false; 1005 } 1006 1007 /// getAnyInitializer - Get the initializer for this variable, no matter which 1008 /// declaration it is attached to. 1009 const Expr *getAnyInitializer() const { 1010 const VarDecl *D; 1011 return getAnyInitializer(D); 1012 } 1013 1014 /// getAnyInitializer - Get the initializer for this variable, no matter which 1015 /// declaration it is attached to. Also get that declaration. 1016 const Expr *getAnyInitializer(const VarDecl *&D) const; 1017 1018 bool hasInit() const { 1019 return !Init.isNull() && (Init.is<Stmt *>() || Init.is<EvaluatedStmt *>()); 1020 } 1021 const Expr *getInit() const { 1022 if (Init.isNull()) 1023 return 0; 1024 1025 const Stmt *S = Init.dyn_cast<Stmt *>(); 1026 if (!S) { 1027 if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>()) 1028 S = ES->Value; 1029 } 1030 return (const Expr*) S; 1031 } 1032 Expr *getInit() { 1033 if (Init.isNull()) 1034 return 0; 1035 1036 Stmt *S = Init.dyn_cast<Stmt *>(); 1037 if (!S) { 1038 if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>()) 1039 S = ES->Value; 1040 } 1041 1042 return (Expr*) S; 1043 } 1044 1045 /// \brief Retrieve the address of the initializer expression. 1046 Stmt **getInitAddress() { 1047 if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>()) 1048 return &ES->Value; 1049 1050 // This union hack tip-toes around strict-aliasing rules. 1051 union { 1052 InitType *InitPtr; 1053 Stmt **StmtPtr; 1054 }; 1055 1056 InitPtr = &Init; 1057 return StmtPtr; 1058 } 1059 1060 void setInit(Expr *I); 1061 1062 /// \brief Determine whether this variable is a reference that 1063 /// extends the lifetime of its temporary initializer. 1064 /// 1065 /// A reference extends the lifetime of its temporary initializer if 1066 /// it's initializer is an rvalue that would normally go out of scope 1067 /// at the end of the initializer (a full expression). In such cases, 1068 /// the reference itself takes ownership of the temporary, which will 1069 /// be destroyed when the reference goes out of scope. For example: 1070 /// 1071 /// \code 1072 /// const int &r = 1.0; // creates a temporary of type 'int' 1073 /// \endcode 1074 bool extendsLifetimeOfTemporary() const; 1075 1076 /// \brief Determine whether this variable's value can be used in a 1077 /// constant expression, according to the relevant language standard. 1078 /// This only checks properties of the declaration, and does not check 1079 /// whether the initializer is in fact a constant expression. 1080 bool isUsableInConstantExpressions(ASTContext &C) const; 1081 1082 EvaluatedStmt *ensureEvaluatedStmt() const; 1083 1084 /// \brief Attempt to evaluate the value of the initializer attached to this 1085 /// declaration, and produce notes explaining why it cannot be evaluated or is 1086 /// not a constant expression. Returns a pointer to the value if evaluation 1087 /// succeeded, 0 otherwise. 1088 APValue *evaluateValue() const; 1089 APValue *evaluateValue( 1090 llvm::SmallVectorImpl<PartialDiagnosticAt> &Notes) const; 1091 1092 /// \brief Return the already-evaluated value of this variable's 1093 /// initializer, or NULL if the value is not yet known. Returns pointer 1094 /// to untyped APValue if the value could not be evaluated. 1095 APValue *getEvaluatedValue() const { 1096 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) 1097 if (Eval->WasEvaluated) 1098 return &Eval->Evaluated; 1099 1100 return 0; 1101 } 1102 1103 /// \brief Determines whether it is already known whether the 1104 /// initializer is an integral constant expression or not. 1105 bool isInitKnownICE() const { 1106 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) 1107 return Eval->CheckedICE; 1108 1109 return false; 1110 } 1111 1112 /// \brief Determines whether the initializer is an integral constant 1113 /// expression, or in C++11, whether the initializer is a constant 1114 /// expression. 1115 /// 1116 /// \pre isInitKnownICE() 1117 bool isInitICE() const { 1118 assert(isInitKnownICE() && 1119 "Check whether we already know that the initializer is an ICE"); 1120 return Init.get<EvaluatedStmt *>()->IsICE; 1121 } 1122 1123 /// \brief Determine whether the value of the initializer attached to this 1124 /// declaration is an integral constant expression. 1125 bool checkInitIsICE() const; 1126 1127 void setInitStyle(InitializationStyle Style) { 1128 VarDeclBits.InitStyle = Style; 1129 } 1130 1131 /// \brief The style of initialization for this declaration. 1132 /// 1133 /// C-style initialization is "int x = 1;". Call-style initialization is 1134 /// a C++98 direct-initializer, e.g. "int x(1);". The Init expression will be 1135 /// the expression inside the parens or a "ClassType(a,b,c)" class constructor 1136 /// expression for class types. List-style initialization is C++11 syntax, 1137 /// e.g. "int x{1};". Clients can distinguish between different forms of 1138 /// initialization by checking this value. In particular, "int x = {1};" is 1139 /// C-style, "int x({1})" is call-style, and "int x{1};" is list-style; the 1140 /// Init expression in all three cases is an InitListExpr. 1141 InitializationStyle getInitStyle() const { 1142 return static_cast<InitializationStyle>(VarDeclBits.InitStyle); 1143 } 1144 1145 /// \brief Whether the initializer is a direct-initializer (list or call). 1146 bool isDirectInit() const { 1147 return getInitStyle() != CInit; 1148 } 1149 1150 /// \brief Determine whether this variable is the exception variable in a 1151 /// C++ catch statememt or an Objective-C \@catch statement. 1152 bool isExceptionVariable() const { 1153 return VarDeclBits.ExceptionVar; 1154 } 1155 void setExceptionVariable(bool EV) { VarDeclBits.ExceptionVar = EV; } 1156 1157 /// \brief Determine whether this local variable can be used with the named 1158 /// return value optimization (NRVO). 1159 /// 1160 /// The named return value optimization (NRVO) works by marking certain 1161 /// non-volatile local variables of class type as NRVO objects. These 1162 /// locals can be allocated within the return slot of their containing 1163 /// function, in which case there is no need to copy the object to the 1164 /// return slot when returning from the function. Within the function body, 1165 /// each return that returns the NRVO object will have this variable as its 1166 /// NRVO candidate. 1167 bool isNRVOVariable() const { return VarDeclBits.NRVOVariable; } 1168 void setNRVOVariable(bool NRVO) { VarDeclBits.NRVOVariable = NRVO; } 1169 1170 /// \brief Determine whether this variable is the for-range-declaration in 1171 /// a C++0x for-range statement. 1172 bool isCXXForRangeDecl() const { return VarDeclBits.CXXForRangeDecl; } 1173 void setCXXForRangeDecl(bool FRD) { VarDeclBits.CXXForRangeDecl = FRD; } 1174 1175 /// \brief Determine whether this variable is an ARC pseudo-__strong 1176 /// variable. A pseudo-__strong variable has a __strong-qualified 1177 /// type but does not actually retain the object written into it. 1178 /// Generally such variables are also 'const' for safety. 1179 bool isARCPseudoStrong() const { return VarDeclBits.ARCPseudoStrong; } 1180 void setARCPseudoStrong(bool ps) { VarDeclBits.ARCPseudoStrong = ps; } 1181 1182 /// Whether this variable is (C++11) constexpr. 1183 bool isConstexpr() const { return VarDeclBits.IsConstexpr; } 1184 void setConstexpr(bool IC) { VarDeclBits.IsConstexpr = IC; } 1185 1186 /// \brief If this variable is an instantiated static data member of a 1187 /// class template specialization, returns the templated static data member 1188 /// from which it was instantiated. 1189 VarDecl *getInstantiatedFromStaticDataMember() const; 1190 1191 /// \brief If this variable is a static data member, determine what kind of 1192 /// template specialization or instantiation this is. 1193 TemplateSpecializationKind getTemplateSpecializationKind() const; 1194 1195 /// \brief If this variable is an instantiation of a static data member of a 1196 /// class template specialization, retrieves the member specialization 1197 /// information. 1198 MemberSpecializationInfo *getMemberSpecializationInfo() const; 1199 1200 /// \brief For a static data member that was instantiated from a static 1201 /// data member of a class template, set the template specialiation kind. 1202 void setTemplateSpecializationKind(TemplateSpecializationKind TSK, 1203 SourceLocation PointOfInstantiation = SourceLocation()); 1204 1205 // Implement isa/cast/dyncast/etc. 1206 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1207 static bool classofKind(Kind K) { return K >= firstVar && K <= lastVar; } 1208}; 1209 1210class ImplicitParamDecl : public VarDecl { 1211 virtual void anchor(); 1212public: 1213 static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC, 1214 SourceLocation IdLoc, IdentifierInfo *Id, 1215 QualType T); 1216 1217 static ImplicitParamDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1218 1219 ImplicitParamDecl(DeclContext *DC, SourceLocation IdLoc, 1220 IdentifierInfo *Id, QualType Type) 1221 : VarDecl(ImplicitParam, DC, IdLoc, IdLoc, Id, Type, 1222 /*tinfo*/ 0, SC_None, SC_None) { 1223 setImplicit(); 1224 } 1225 1226 // Implement isa/cast/dyncast/etc. 1227 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1228 static bool classofKind(Kind K) { return K == ImplicitParam; } 1229}; 1230 1231/// ParmVarDecl - Represents a parameter to a function. 1232class ParmVarDecl : public VarDecl { 1233public: 1234 enum { MaxFunctionScopeDepth = 255 }; 1235 enum { MaxFunctionScopeIndex = 255 }; 1236 1237protected: 1238 ParmVarDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc, 1239 SourceLocation IdLoc, IdentifierInfo *Id, 1240 QualType T, TypeSourceInfo *TInfo, 1241 StorageClass S, StorageClass SCAsWritten, Expr *DefArg) 1242 : VarDecl(DK, DC, StartLoc, IdLoc, Id, T, TInfo, S, SCAsWritten) { 1243 assert(ParmVarDeclBits.HasInheritedDefaultArg == false); 1244 assert(ParmVarDeclBits.IsKNRPromoted == false); 1245 assert(ParmVarDeclBits.IsObjCMethodParam == false); 1246 setDefaultArg(DefArg); 1247 } 1248 1249public: 1250 static ParmVarDecl *Create(ASTContext &C, DeclContext *DC, 1251 SourceLocation StartLoc, 1252 SourceLocation IdLoc, IdentifierInfo *Id, 1253 QualType T, TypeSourceInfo *TInfo, 1254 StorageClass S, StorageClass SCAsWritten, 1255 Expr *DefArg); 1256 1257 static ParmVarDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1258 1259 virtual SourceRange getSourceRange() const LLVM_READONLY; 1260 1261 void setObjCMethodScopeInfo(unsigned parameterIndex) { 1262 ParmVarDeclBits.IsObjCMethodParam = true; 1263 setParameterIndex(parameterIndex); 1264 } 1265 1266 void setScopeInfo(unsigned scopeDepth, unsigned parameterIndex) { 1267 assert(!ParmVarDeclBits.IsObjCMethodParam); 1268 1269 ParmVarDeclBits.ScopeDepthOrObjCQuals = scopeDepth; 1270 assert(ParmVarDeclBits.ScopeDepthOrObjCQuals == scopeDepth 1271 && "truncation!"); 1272 1273 setParameterIndex(parameterIndex); 1274 } 1275 1276 bool isObjCMethodParameter() const { 1277 return ParmVarDeclBits.IsObjCMethodParam; 1278 } 1279 1280 unsigned getFunctionScopeDepth() const { 1281 if (ParmVarDeclBits.IsObjCMethodParam) return 0; 1282 return ParmVarDeclBits.ScopeDepthOrObjCQuals; 1283 } 1284 1285 /// Returns the index of this parameter in its prototype or method scope. 1286 unsigned getFunctionScopeIndex() const { 1287 return getParameterIndex(); 1288 } 1289 1290 ObjCDeclQualifier getObjCDeclQualifier() const { 1291 if (!ParmVarDeclBits.IsObjCMethodParam) return OBJC_TQ_None; 1292 return ObjCDeclQualifier(ParmVarDeclBits.ScopeDepthOrObjCQuals); 1293 } 1294 void setObjCDeclQualifier(ObjCDeclQualifier QTVal) { 1295 assert(ParmVarDeclBits.IsObjCMethodParam); 1296 ParmVarDeclBits.ScopeDepthOrObjCQuals = QTVal; 1297 } 1298 1299 /// True if the value passed to this parameter must undergo 1300 /// K&R-style default argument promotion: 1301 /// 1302 /// C99 6.5.2.2. 1303 /// If the expression that denotes the called function has a type 1304 /// that does not include a prototype, the integer promotions are 1305 /// performed on each argument, and arguments that have type float 1306 /// are promoted to double. 1307 bool isKNRPromoted() const { 1308 return ParmVarDeclBits.IsKNRPromoted; 1309 } 1310 void setKNRPromoted(bool promoted) { 1311 ParmVarDeclBits.IsKNRPromoted = promoted; 1312 } 1313 1314 Expr *getDefaultArg(); 1315 const Expr *getDefaultArg() const { 1316 return const_cast<ParmVarDecl *>(this)->getDefaultArg(); 1317 } 1318 1319 void setDefaultArg(Expr *defarg) { 1320 Init = reinterpret_cast<Stmt *>(defarg); 1321 } 1322 1323 /// \brief Retrieve the source range that covers the entire default 1324 /// argument. 1325 SourceRange getDefaultArgRange() const; 1326 void setUninstantiatedDefaultArg(Expr *arg) { 1327 Init = reinterpret_cast<UninstantiatedDefaultArgument *>(arg); 1328 } 1329 Expr *getUninstantiatedDefaultArg() { 1330 return (Expr *)Init.get<UninstantiatedDefaultArgument *>(); 1331 } 1332 const Expr *getUninstantiatedDefaultArg() const { 1333 return (const Expr *)Init.get<UninstantiatedDefaultArgument *>(); 1334 } 1335 1336 /// hasDefaultArg - Determines whether this parameter has a default argument, 1337 /// either parsed or not. 1338 bool hasDefaultArg() const { 1339 return getInit() || hasUnparsedDefaultArg() || 1340 hasUninstantiatedDefaultArg(); 1341 } 1342 1343 /// hasUnparsedDefaultArg - Determines whether this parameter has a 1344 /// default argument that has not yet been parsed. This will occur 1345 /// during the processing of a C++ class whose member functions have 1346 /// default arguments, e.g., 1347 /// @code 1348 /// class X { 1349 /// public: 1350 /// void f(int x = 17); // x has an unparsed default argument now 1351 /// }; // x has a regular default argument now 1352 /// @endcode 1353 bool hasUnparsedDefaultArg() const { 1354 return Init.is<UnparsedDefaultArgument*>(); 1355 } 1356 1357 bool hasUninstantiatedDefaultArg() const { 1358 return Init.is<UninstantiatedDefaultArgument*>(); 1359 } 1360 1361 /// setUnparsedDefaultArg - Specify that this parameter has an 1362 /// unparsed default argument. The argument will be replaced with a 1363 /// real default argument via setDefaultArg when the class 1364 /// definition enclosing the function declaration that owns this 1365 /// default argument is completed. 1366 void setUnparsedDefaultArg() { 1367 Init = (UnparsedDefaultArgument *)0; 1368 } 1369 1370 bool hasInheritedDefaultArg() const { 1371 return ParmVarDeclBits.HasInheritedDefaultArg; 1372 } 1373 1374 void setHasInheritedDefaultArg(bool I = true) { 1375 ParmVarDeclBits.HasInheritedDefaultArg = I; 1376 } 1377 1378 QualType getOriginalType() const { 1379 if (getTypeSourceInfo()) 1380 return getTypeSourceInfo()->getType(); 1381 return getType(); 1382 } 1383 1384 /// \brief Determine whether this parameter is actually a function 1385 /// parameter pack. 1386 bool isParameterPack() const; 1387 1388 /// setOwningFunction - Sets the function declaration that owns this 1389 /// ParmVarDecl. Since ParmVarDecls are often created before the 1390 /// FunctionDecls that own them, this routine is required to update 1391 /// the DeclContext appropriately. 1392 void setOwningFunction(DeclContext *FD) { setDeclContext(FD); } 1393 1394 // Implement isa/cast/dyncast/etc. 1395 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 1396 static bool classofKind(Kind K) { return K == ParmVar; } 1397 1398private: 1399 enum { ParameterIndexSentinel = (1 << NumParameterIndexBits) - 1 }; 1400 1401 void setParameterIndex(unsigned parameterIndex) { 1402 if (parameterIndex >= ParameterIndexSentinel) { 1403 setParameterIndexLarge(parameterIndex); 1404 return; 1405 } 1406 1407 ParmVarDeclBits.ParameterIndex = parameterIndex; 1408 assert(ParmVarDeclBits.ParameterIndex == parameterIndex && "truncation!"); 1409 } 1410 unsigned getParameterIndex() const { 1411 unsigned d = ParmVarDeclBits.ParameterIndex; 1412 return d == ParameterIndexSentinel ? getParameterIndexLarge() : d; 1413 } 1414 1415 void setParameterIndexLarge(unsigned parameterIndex); 1416 unsigned getParameterIndexLarge() const; 1417}; 1418 1419/// FunctionDecl - An instance of this class is created to represent a 1420/// function declaration or definition. 1421/// 1422/// Since a given function can be declared several times in a program, 1423/// there may be several FunctionDecls that correspond to that 1424/// function. Only one of those FunctionDecls will be found when 1425/// traversing the list of declarations in the context of the 1426/// FunctionDecl (e.g., the translation unit); this FunctionDecl 1427/// contains all of the information known about the function. Other, 1428/// previous declarations of the function are available via the 1429/// getPreviousDecl() chain. 1430class FunctionDecl : public DeclaratorDecl, public DeclContext, 1431 public Redeclarable<FunctionDecl> { 1432public: 1433 typedef clang::StorageClass StorageClass; 1434 1435 /// \brief The kind of templated function a FunctionDecl can be. 1436 enum TemplatedKind { 1437 TK_NonTemplate, 1438 TK_FunctionTemplate, 1439 TK_MemberSpecialization, 1440 TK_FunctionTemplateSpecialization, 1441 TK_DependentFunctionTemplateSpecialization 1442 }; 1443 1444private: 1445 /// ParamInfo - new[]'d array of pointers to VarDecls for the formal 1446 /// parameters of this function. This is null if a prototype or if there are 1447 /// no formals. 1448 ParmVarDecl **ParamInfo; 1449 1450 /// DeclsInPrototypeScope - Array of pointers to NamedDecls for 1451 /// decls defined in the function prototype that are not parameters. E.g. 1452 /// 'enum Y' in 'void f(enum Y {AA} x) {}'. 1453 llvm::ArrayRef<NamedDecl*> DeclsInPrototypeScope; 1454 1455 LazyDeclStmtPtr Body; 1456 1457 // FIXME: This can be packed into the bitfields in Decl. 1458 // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum 1459 unsigned SClass : 2; 1460 unsigned SClassAsWritten : 2; 1461 bool IsInline : 1; 1462 bool IsInlineSpecified : 1; 1463 bool IsVirtualAsWritten : 1; 1464 bool IsPure : 1; 1465 bool HasInheritedPrototype : 1; 1466 bool HasWrittenPrototype : 1; 1467 bool IsDeleted : 1; 1468 bool IsTrivial : 1; // sunk from CXXMethodDecl 1469 bool IsDefaulted : 1; // sunk from CXXMethoDecl 1470 bool IsExplicitlyDefaulted : 1; //sunk from CXXMethodDecl 1471 bool HasImplicitReturnZero : 1; 1472 bool IsLateTemplateParsed : 1; 1473 bool IsConstexpr : 1; 1474 1475 /// \brief End part of this FunctionDecl's source range. 1476 /// 1477 /// We could compute the full range in getSourceRange(). However, when we're 1478 /// dealing with a function definition deserialized from a PCH/AST file, 1479 /// we can only compute the full range once the function body has been 1480 /// de-serialized, so it's far better to have the (sometimes-redundant) 1481 /// EndRangeLoc. 1482 SourceLocation EndRangeLoc; 1483 1484 /// \brief The template or declaration that this declaration 1485 /// describes or was instantiated from, respectively. 1486 /// 1487 /// For non-templates, this value will be NULL. For function 1488 /// declarations that describe a function template, this will be a 1489 /// pointer to a FunctionTemplateDecl. For member functions 1490 /// of class template specializations, this will be a MemberSpecializationInfo 1491 /// pointer containing information about the specialization. 1492 /// For function template specializations, this will be a 1493 /// FunctionTemplateSpecializationInfo, which contains information about 1494 /// the template being specialized and the template arguments involved in 1495 /// that specialization. 1496 llvm::PointerUnion4<FunctionTemplateDecl *, 1497 MemberSpecializationInfo *, 1498 FunctionTemplateSpecializationInfo *, 1499 DependentFunctionTemplateSpecializationInfo *> 1500 TemplateOrSpecialization; 1501 1502 /// DNLoc - Provides source/type location info for the 1503 /// declaration name embedded in the DeclaratorDecl base class. 1504 DeclarationNameLoc DNLoc; 1505 1506 /// \brief Specify that this function declaration is actually a function 1507 /// template specialization. 1508 /// 1509 /// \param C the ASTContext. 1510 /// 1511 /// \param Template the function template that this function template 1512 /// specialization specializes. 1513 /// 1514 /// \param TemplateArgs the template arguments that produced this 1515 /// function template specialization from the template. 1516 /// 1517 /// \param InsertPos If non-NULL, the position in the function template 1518 /// specialization set where the function template specialization data will 1519 /// be inserted. 1520 /// 1521 /// \param TSK the kind of template specialization this is. 1522 /// 1523 /// \param TemplateArgsAsWritten location info of template arguments. 1524 /// 1525 /// \param PointOfInstantiation point at which the function template 1526 /// specialization was first instantiated. 1527 void setFunctionTemplateSpecialization(ASTContext &C, 1528 FunctionTemplateDecl *Template, 1529 const TemplateArgumentList *TemplateArgs, 1530 void *InsertPos, 1531 TemplateSpecializationKind TSK, 1532 const TemplateArgumentListInfo *TemplateArgsAsWritten, 1533 SourceLocation PointOfInstantiation); 1534 1535 /// \brief Specify that this record is an instantiation of the 1536 /// member function FD. 1537 void setInstantiationOfMemberFunction(ASTContext &C, FunctionDecl *FD, 1538 TemplateSpecializationKind TSK); 1539 1540 void setParams(ASTContext &C, llvm::ArrayRef<ParmVarDecl *> NewParamInfo); 1541 1542protected: 1543 FunctionDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc, 1544 const DeclarationNameInfo &NameInfo, 1545 QualType T, TypeSourceInfo *TInfo, 1546 StorageClass S, StorageClass SCAsWritten, bool isInlineSpecified, 1547 bool isConstexprSpecified) 1548 : DeclaratorDecl(DK, DC, NameInfo.getLoc(), NameInfo.getName(), T, TInfo, 1549 StartLoc), 1550 DeclContext(DK), 1551 ParamInfo(0), Body(), 1552 SClass(S), SClassAsWritten(SCAsWritten), 1553 IsInline(isInlineSpecified), IsInlineSpecified(isInlineSpecified), 1554 IsVirtualAsWritten(false), IsPure(false), HasInheritedPrototype(false), 1555 HasWrittenPrototype(true), IsDeleted(false), IsTrivial(false), 1556 IsDefaulted(false), IsExplicitlyDefaulted(false), 1557 HasImplicitReturnZero(false), IsLateTemplateParsed(false), 1558 IsConstexpr(isConstexprSpecified), EndRangeLoc(NameInfo.getEndLoc()), 1559 TemplateOrSpecialization(), 1560 DNLoc(NameInfo.getInfo()) {} 1561 1562 typedef Redeclarable<FunctionDecl> redeclarable_base; 1563 virtual FunctionDecl *getNextRedeclaration() { return RedeclLink.getNext(); } 1564 virtual FunctionDecl *getPreviousDeclImpl() { 1565 return getPreviousDecl(); 1566 } 1567 virtual FunctionDecl *getMostRecentDeclImpl() { 1568 return getMostRecentDecl(); 1569 } 1570 1571public: 1572 typedef redeclarable_base::redecl_iterator redecl_iterator; 1573 using redeclarable_base::redecls_begin; 1574 using redeclarable_base::redecls_end; 1575 using redeclarable_base::getPreviousDecl; 1576 using redeclarable_base::getMostRecentDecl; 1577 1578 static FunctionDecl *Create(ASTContext &C, DeclContext *DC, 1579 SourceLocation StartLoc, SourceLocation NLoc, 1580 DeclarationName N, QualType T, 1581 TypeSourceInfo *TInfo, 1582 StorageClass SC = SC_None, 1583 StorageClass SCAsWritten = SC_None, 1584 bool isInlineSpecified = false, 1585 bool hasWrittenPrototype = true, 1586 bool isConstexprSpecified = false) { 1587 DeclarationNameInfo NameInfo(N, NLoc); 1588 return FunctionDecl::Create(C, DC, StartLoc, NameInfo, T, TInfo, 1589 SC, SCAsWritten, 1590 isInlineSpecified, hasWrittenPrototype, 1591 isConstexprSpecified); 1592 } 1593 1594 static FunctionDecl *Create(ASTContext &C, DeclContext *DC, 1595 SourceLocation StartLoc, 1596 const DeclarationNameInfo &NameInfo, 1597 QualType T, TypeSourceInfo *TInfo, 1598 StorageClass SC = SC_None, 1599 StorageClass SCAsWritten = SC_None, 1600 bool isInlineSpecified = false, 1601 bool hasWrittenPrototype = true, 1602 bool isConstexprSpecified = false); 1603 1604 static FunctionDecl *CreateDeserialized(ASTContext &C, unsigned ID); 1605 1606 DeclarationNameInfo getNameInfo() const { 1607 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc); 1608 } 1609 1610 virtual void getNameForDiagnostic(std::string &S, 1611 const PrintingPolicy &Policy, 1612 bool Qualified) const; 1613 1614 void setRangeEnd(SourceLocation E) { EndRangeLoc = E; } 1615 1616 virtual SourceRange getSourceRange() const LLVM_READONLY; 1617 1618 /// \brief Returns true if the function has a body (definition). The 1619 /// function body might be in any of the (re-)declarations of this 1620 /// function. The variant that accepts a FunctionDecl pointer will 1621 /// set that function declaration to the actual declaration 1622 /// containing the body (if there is one). 1623 bool hasBody(const FunctionDecl *&Definition) const; 1624 1625 virtual bool hasBody() const { 1626 const FunctionDecl* Definition; 1627 return hasBody(Definition); 1628 } 1629 1630 /// hasTrivialBody - Returns whether the function has a trivial body that does 1631 /// not require any specific codegen. 1632 bool hasTrivialBody() const; 1633 1634 /// isDefined - Returns true if the function is defined at all, including 1635 /// a deleted definition. Except for the behavior when the function is 1636 /// deleted, behaves like hasBody. 1637 bool isDefined(const FunctionDecl *&Definition) const; 1638 1639 virtual bool isDefined() const { 1640 const FunctionDecl* Definition; 1641 return isDefined(Definition); 1642 } 1643 1644 /// getBody - Retrieve the body (definition) of the function. The 1645 /// function body might be in any of the (re-)declarations of this 1646 /// function. The variant that accepts a FunctionDecl pointer will 1647 /// set that function declaration to the actual declaration 1648 /// containing the body (if there is one). 1649 /// NOTE: For checking if there is a body, use hasBody() instead, to avoid 1650 /// unnecessary AST de-serialization of the body. 1651 Stmt *getBody(const FunctionDecl *&Definition) const; 1652 1653 virtual Stmt *getBody() const { 1654 const FunctionDecl* Definition; 1655 return getBody(Definition); 1656 } 1657 1658 /// isThisDeclarationADefinition - Returns whether this specific 1659 /// declaration of the function is also a definition. This does not 1660 /// determine whether the function has been defined (e.g., in a 1661 /// previous definition); for that information, use isDefined. Note 1662 /// that this returns false for a defaulted function unless that function 1663 /// has been implicitly defined (possibly as deleted). 1664 bool isThisDeclarationADefinition() const { 1665 return IsDeleted || Body || IsLateTemplateParsed; 1666 } 1667 1668 /// doesThisDeclarationHaveABody - Returns whether this specific 1669 /// declaration of the function has a body - that is, if it is a non- 1670 /// deleted definition. 1671 bool doesThisDeclarationHaveABody() const { 1672 return Body || IsLateTemplateParsed; 1673 } 1674 1675 void setBody(Stmt *B); 1676 void setLazyBody(uint64_t Offset) { Body = Offset; } 1677 1678 /// Whether this function is variadic. 1679 bool isVariadic() const; 1680 1681 /// Whether this function is marked as virtual explicitly. 1682 bool isVirtualAsWritten() const { return IsVirtualAsWritten; } 1683 void setVirtualAsWritten(bool V) { IsVirtualAsWritten = V; } 1684 1685 /// Whether this virtual function is pure, i.e. makes the containing class 1686 /// abstract. 1687 bool isPure() const { return IsPure; } 1688 void setPure(bool P = true); 1689 1690 /// Whether this templated function will be late parsed. 1691 bool isLateTemplateParsed() const { return IsLateTemplateParsed; } 1692 void setLateTemplateParsed(bool ILT = true) { IsLateTemplateParsed = ILT; } 1693 1694 /// Whether this function is "trivial" in some specialized C++ senses. 1695 /// Can only be true for default constructors, copy constructors, 1696 /// copy assignment operators, and destructors. Not meaningful until 1697 /// the class has been fully built by Sema. 1698 bool isTrivial() const { return IsTrivial; } 1699 void setTrivial(bool IT) { IsTrivial = IT; } 1700 1701 /// Whether this function is defaulted per C++0x. Only valid for 1702 /// special member functions. 1703 bool isDefaulted() const { return IsDefaulted; } 1704 void setDefaulted(bool D = true) { IsDefaulted = D; } 1705 1706 /// Whether this function is explicitly defaulted per C++0x. Only valid 1707 /// for special member functions. 1708 bool isExplicitlyDefaulted() const { return IsExplicitlyDefaulted; } 1709 void setExplicitlyDefaulted(bool ED = true) { IsExplicitlyDefaulted = ED; } 1710 1711 /// Whether falling off this function implicitly returns null/zero. 1712 /// If a more specific implicit return value is required, front-ends 1713 /// should synthesize the appropriate return statements. 1714 bool hasImplicitReturnZero() const { return HasImplicitReturnZero; } 1715 void setHasImplicitReturnZero(bool IRZ) { HasImplicitReturnZero = IRZ; } 1716 1717 /// \brief Whether this function has a prototype, either because one 1718 /// was explicitly written or because it was "inherited" by merging 1719 /// a declaration without a prototype with a declaration that has a 1720 /// prototype. 1721 bool hasPrototype() const { 1722 return HasWrittenPrototype || HasInheritedPrototype; 1723 } 1724 1725 bool hasWrittenPrototype() const { return HasWrittenPrototype; } 1726 1727 /// \brief Whether this function inherited its prototype from a 1728 /// previous declaration. 1729 bool hasInheritedPrototype() const { return HasInheritedPrototype; } 1730 void setHasInheritedPrototype(bool P = true) { HasInheritedPrototype = P; } 1731 1732 /// Whether this is a (C++11) constexpr function or constexpr constructor. 1733 bool isConstexpr() const { return IsConstexpr; } 1734 void setConstexpr(bool IC); 1735 1736 /// \brief Whether this function has been deleted. 1737 /// 1738 /// A function that is "deleted" (via the C++0x "= delete" syntax) 1739 /// acts like a normal function, except that it cannot actually be 1740 /// called or have its address taken. Deleted functions are 1741 /// typically used in C++ overload resolution to attract arguments 1742 /// whose type or lvalue/rvalue-ness would permit the use of a 1743 /// different overload that would behave incorrectly. For example, 1744 /// one might use deleted functions to ban implicit conversion from 1745 /// a floating-point number to an Integer type: 1746 /// 1747 /// @code 1748 /// struct Integer { 1749 /// Integer(long); // construct from a long 1750 /// Integer(double) = delete; // no construction from float or double 1751 /// Integer(long double) = delete; // no construction from long double 1752 /// }; 1753 /// @endcode 1754 // If a function is deleted, its first declaration must be. 1755 bool isDeleted() const { return getCanonicalDecl()->IsDeleted; } 1756 bool isDeletedAsWritten() const { return IsDeleted && !IsDefaulted; } 1757 void setDeletedAsWritten(bool D = true) { IsDeleted = D; } 1758 1759 /// \brief Determines whether this function is "main", which is the 1760 /// entry point into an executable program. 1761 bool isMain() const; 1762 1763 /// \brief Determines whether this operator new or delete is one 1764 /// of the reserved global placement operators: 1765 /// void *operator new(size_t, void *); 1766 /// void *operator new[](size_t, void *); 1767 /// void operator delete(void *, void *); 1768 /// void operator delete[](void *, void *); 1769 /// These functions have special behavior under [new.delete.placement]: 1770 /// These functions are reserved, a C++ program may not define 1771 /// functions that displace the versions in the Standard C++ library. 1772 /// The provisions of [basic.stc.dynamic] do not apply to these 1773 /// reserved placement forms of operator new and operator delete. 1774 /// 1775 /// This function must be an allocation or deallocation function. 1776 bool isReservedGlobalPlacementOperator() const; 1777 1778 /// \brief Determines whether this function is a function with 1779 /// external, C linkage. 1780 bool isExternC() const; 1781 1782 /// \brief Determines whether this is a global function. 1783 bool isGlobal() const; 1784 1785 void setPreviousDeclaration(FunctionDecl * PrevDecl); 1786 1787 virtual const FunctionDecl *getCanonicalDecl() const; 1788 virtual FunctionDecl *getCanonicalDecl(); 1789 1790 unsigned getBuiltinID() const; 1791 1792 // Iterator access to formal parameters. 1793 unsigned param_size() const { return getNumParams(); } 1794 typedef ParmVarDecl **param_iterator; 1795 typedef ParmVarDecl * const *param_const_iterator; 1796 1797 param_iterator param_begin() { return ParamInfo; } 1798 param_iterator param_end() { return ParamInfo+param_size(); } 1799 1800 param_const_iterator param_begin() const { return ParamInfo; } 1801 param_const_iterator param_end() const { return ParamInfo+param_size(); } 1802 1803 /// getNumParams - Return the number of parameters this function must have 1804 /// based on its FunctionType. This is the length of the ParamInfo array 1805 /// after it has been created. 1806 unsigned getNumParams() const; 1807 1808 const ParmVarDecl *getParamDecl(unsigned i) const { 1809 assert(i < getNumParams() && "Illegal param #"); 1810 return ParamInfo[i]; 1811 } 1812 ParmVarDecl *getParamDecl(unsigned i) { 1813 assert(i < getNumParams() && "Illegal param #"); 1814 return ParamInfo[i]; 1815 } 1816 void setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo) { 1817 setParams(getASTContext(), NewParamInfo); 1818 } 1819 1820 const llvm::ArrayRef<NamedDecl*> &getDeclsInPrototypeScope() const { 1821 return DeclsInPrototypeScope; 1822 } 1823 void setDeclsInPrototypeScope(llvm::ArrayRef<NamedDecl *> NewDecls); 1824 1825 /// getMinRequiredArguments - Returns the minimum number of arguments 1826 /// needed to call this function. This may be fewer than the number of 1827 /// function parameters, if some of the parameters have default 1828 /// arguments (in C++). 1829 unsigned getMinRequiredArguments() const; 1830 1831 QualType getResultType() const { 1832 return getType()->getAs<FunctionType>()->getResultType(); 1833 } 1834 1835 /// \brief Determine the type of an expression that calls this function. 1836 QualType getCallResultType() const { 1837 return getType()->getAs<FunctionType>()->getCallResultType(getASTContext()); 1838 } 1839 1840 StorageClass getStorageClass() const { return StorageClass(SClass); } 1841 void setStorageClass(StorageClass SC); 1842 1843 StorageClass getStorageClassAsWritten() const { 1844 return StorageClass(SClassAsWritten); 1845 } 1846 1847 /// \brief Determine whether the "inline" keyword was specified for this 1848 /// function. 1849 bool isInlineSpecified() const { return IsInlineSpecified; } 1850 1851 /// Set whether the "inline" keyword was specified for this function. 1852 void setInlineSpecified(bool I) { 1853 IsInlineSpecified = I; 1854 IsInline = I; 1855 } 1856 1857 /// Flag that this function is implicitly inline. 1858 void setImplicitlyInline() { 1859 IsInline = true; 1860 } 1861 1862 /// \brief Determine whether this function should be inlined, because it is 1863 /// either marked "inline" or "constexpr" or is a member function of a class 1864 /// that was defined in the class body. 1865 bool isInlined() const; 1866 1867 bool isInlineDefinitionExternallyVisible() const; 1868 1869 bool doesDeclarationForceExternallyVisibleDefinition() const; 1870 1871 /// isOverloadedOperator - Whether this function declaration 1872 /// represents an C++ overloaded operator, e.g., "operator+". 1873 bool isOverloadedOperator() const { 1874 return getOverloadedOperator() != OO_None; 1875 } 1876 1877 OverloadedOperatorKind getOverloadedOperator() const; 1878 1879 const IdentifierInfo *getLiteralIdentifier() const; 1880 1881 /// \brief If this function is an instantiation of a member function 1882 /// of a class template specialization, retrieves the function from 1883 /// which it was instantiated. 1884 /// 1885 /// This routine will return non-NULL for (non-templated) member 1886 /// functions of class templates and for instantiations of function 1887 /// templates. For example, given: 1888 /// 1889 /// \code 1890 /// template<typename T> 1891 /// struct X { 1892 /// void f(T); 1893 /// }; 1894 /// \endcode 1895 /// 1896 /// The declaration for X<int>::f is a (non-templated) FunctionDecl 1897 /// whose parent is the class template specialization X<int>. For 1898 /// this declaration, getInstantiatedFromFunction() will return 1899 /// the FunctionDecl X<T>::A. When a complete definition of 1900 /// X<int>::A is required, it will be instantiated from the 1901 /// declaration returned by getInstantiatedFromMemberFunction(). 1902 FunctionDecl *getInstantiatedFromMemberFunction() const; 1903 1904 /// \brief What kind of templated function this is. 1905 TemplatedKind getTemplatedKind() const; 1906 1907 /// \brief If this function is an instantiation of a member function of a 1908 /// class template specialization, retrieves the member specialization 1909 /// information. 1910 MemberSpecializationInfo *getMemberSpecializationInfo() const; 1911 1912 /// \brief Specify that this record is an instantiation of the 1913 /// member function FD. 1914 void setInstantiationOfMemberFunction(FunctionDecl *FD, 1915 TemplateSpecializationKind TSK) { 1916 setInstantiationOfMemberFunction(getASTContext(), FD, TSK); 1917 } 1918 1919 /// \brief Retrieves the function template that is described by this 1920 /// function declaration. 1921 /// 1922 /// Every function template is represented as a FunctionTemplateDecl 1923 /// and a FunctionDecl (or something derived from FunctionDecl). The 1924 /// former contains template properties (such as the template 1925 /// parameter lists) while the latter contains the actual 1926 /// description of the template's 1927 /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the 1928 /// FunctionDecl that describes the function template, 1929 /// getDescribedFunctionTemplate() retrieves the 1930 /// FunctionTemplateDecl from a FunctionDecl. 1931 FunctionTemplateDecl *getDescribedFunctionTemplate() const { 1932 return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl*>(); 1933 } 1934 1935 void setDescribedFunctionTemplate(FunctionTemplateDecl *Template) { 1936 TemplateOrSpecialization = Template; 1937 } 1938 1939 /// \brief Determine whether this function is a function template 1940 /// specialization. 1941 bool isFunctionTemplateSpecialization() const { 1942 return getPrimaryTemplate() != 0; 1943 } 1944 1945 /// \brief Retrieve the class scope template pattern that this function 1946 /// template specialization is instantiated from. 1947 FunctionDecl *getClassScopeSpecializationPattern() const; 1948 1949 /// \brief If this function is actually a function template specialization, 1950 /// retrieve information about this function template specialization. 1951 /// Otherwise, returns NULL. 1952 FunctionTemplateSpecializationInfo *getTemplateSpecializationInfo() const { 1953 return TemplateOrSpecialization. 1954 dyn_cast<FunctionTemplateSpecializationInfo*>(); 1955 } 1956 1957 /// \brief Determines whether this function is a function template 1958 /// specialization or a member of a class template specialization that can 1959 /// be implicitly instantiated. 1960 bool isImplicitlyInstantiable() const; 1961 1962 /// \brief Determines if the given function was instantiated from a 1963 /// function template. 1964 bool isTemplateInstantiation() const; 1965 1966 /// \brief Retrieve the function declaration from which this function could 1967 /// be instantiated, if it is an instantiation (rather than a non-template 1968 /// or a specialization, for example). 1969 FunctionDecl *getTemplateInstantiationPattern() const; 1970 1971 /// \brief Retrieve the primary template that this function template 1972 /// specialization either specializes or was instantiated from. 1973 /// 1974 /// If this function declaration is not a function template specialization, 1975 /// returns NULL. 1976 FunctionTemplateDecl *getPrimaryTemplate() const; 1977 1978 /// \brief Retrieve the template arguments used to produce this function 1979 /// template specialization from the primary template. 1980 /// 1981 /// If this function declaration is not a function template specialization, 1982 /// returns NULL. 1983 const TemplateArgumentList *getTemplateSpecializationArgs() const; 1984 1985 /// \brief Retrieve the template argument list as written in the sources, 1986 /// if any. 1987 /// 1988 /// If this function declaration is not a function template specialization 1989 /// or if it had no explicit template argument list, returns NULL. 1990 /// Note that it an explicit template argument list may be written empty, 1991 /// e.g., template<> void foo<>(char* s); 1992 const ASTTemplateArgumentListInfo* 1993 getTemplateSpecializationArgsAsWritten() const; 1994 1995 /// \brief Specify that this function declaration is actually a function 1996 /// template specialization. 1997 /// 1998 /// \param Template the function template that this function template 1999 /// specialization specializes. 2000 /// 2001 /// \param TemplateArgs the template arguments that produced this 2002 /// function template specialization from the template. 2003 /// 2004 /// \param InsertPos If non-NULL, the position in the function template 2005 /// specialization set where the function template specialization data will 2006 /// be inserted. 2007 /// 2008 /// \param TSK the kind of template specialization this is. 2009 /// 2010 /// \param TemplateArgsAsWritten location info of template arguments. 2011 /// 2012 /// \param PointOfInstantiation point at which the function template 2013 /// specialization was first instantiated. 2014 void setFunctionTemplateSpecialization(FunctionTemplateDecl *Template, 2015 const TemplateArgumentList *TemplateArgs, 2016 void *InsertPos, 2017 TemplateSpecializationKind TSK = TSK_ImplicitInstantiation, 2018 const TemplateArgumentListInfo *TemplateArgsAsWritten = 0, 2019 SourceLocation PointOfInstantiation = SourceLocation()) { 2020 setFunctionTemplateSpecialization(getASTContext(), Template, TemplateArgs, 2021 InsertPos, TSK, TemplateArgsAsWritten, 2022 PointOfInstantiation); 2023 } 2024 2025 /// \brief Specifies that this function declaration is actually a 2026 /// dependent function template specialization. 2027 void setDependentTemplateSpecialization(ASTContext &Context, 2028 const UnresolvedSetImpl &Templates, 2029 const TemplateArgumentListInfo &TemplateArgs); 2030 2031 DependentFunctionTemplateSpecializationInfo * 2032 getDependentSpecializationInfo() const { 2033 return TemplateOrSpecialization. 2034 dyn_cast<DependentFunctionTemplateSpecializationInfo*>(); 2035 } 2036 2037 /// \brief Determine what kind of template instantiation this function 2038 /// represents. 2039 TemplateSpecializationKind getTemplateSpecializationKind() const; 2040 2041 /// \brief Determine what kind of template instantiation this function 2042 /// represents. 2043 void setTemplateSpecializationKind(TemplateSpecializationKind TSK, 2044 SourceLocation PointOfInstantiation = SourceLocation()); 2045 2046 /// \brief Retrieve the (first) point of instantiation of a function template 2047 /// specialization or a member of a class template specialization. 2048 /// 2049 /// \returns the first point of instantiation, if this function was 2050 /// instantiated from a template; otherwise, returns an invalid source 2051 /// location. 2052 SourceLocation getPointOfInstantiation() const; 2053 2054 /// \brief Determine whether this is or was instantiated from an out-of-line 2055 /// definition of a member function. 2056 virtual bool isOutOfLine() const; 2057 2058 /// \brief Identify a memory copying or setting function. 2059 /// If the given function is a memory copy or setting function, returns 2060 /// the corresponding Builtin ID. If the function is not a memory function, 2061 /// returns 0. 2062 unsigned getMemoryFunctionKind() const; 2063 2064 // Implement isa/cast/dyncast/etc. 2065 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2066 static bool classofKind(Kind K) { 2067 return K >= firstFunction && K <= lastFunction; 2068 } 2069 static DeclContext *castToDeclContext(const FunctionDecl *D) { 2070 return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D)); 2071 } 2072 static FunctionDecl *castFromDeclContext(const DeclContext *DC) { 2073 return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC)); 2074 } 2075 2076 friend class ASTDeclReader; 2077 friend class ASTDeclWriter; 2078}; 2079 2080 2081/// FieldDecl - An instance of this class is created by Sema::ActOnField to 2082/// represent a member of a struct/union/class. 2083class FieldDecl : public DeclaratorDecl { 2084 // FIXME: This can be packed into the bitfields in Decl. 2085 bool Mutable : 1; 2086 mutable unsigned CachedFieldIndex : 31; 2087 2088 /// \brief An InClassInitStyle value, and either a bit width expression (if 2089 /// the InClassInitStyle value is ICIS_NoInit), or a pointer to the in-class 2090 /// initializer for this field (otherwise). 2091 /// 2092 /// We can safely combine these two because in-class initializers are not 2093 /// permitted for bit-fields. 2094 /// 2095 /// If the InClassInitStyle is not ICIS_NoInit and the initializer is null, 2096 /// then this field has an in-class initializer which has not yet been parsed 2097 /// and attached. 2098 llvm::PointerIntPair<Expr *, 2, unsigned> InitializerOrBitWidth; 2099protected: 2100 FieldDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc, 2101 SourceLocation IdLoc, IdentifierInfo *Id, 2102 QualType T, TypeSourceInfo *TInfo, Expr *BW, bool Mutable, 2103 InClassInitStyle InitStyle) 2104 : DeclaratorDecl(DK, DC, IdLoc, Id, T, TInfo, StartLoc), 2105 Mutable(Mutable), CachedFieldIndex(0), 2106 InitializerOrBitWidth(BW, InitStyle) { 2107 assert((!BW || InitStyle == ICIS_NoInit) && "got initializer for bitfield"); 2108 } 2109 2110public: 2111 static FieldDecl *Create(const ASTContext &C, DeclContext *DC, 2112 SourceLocation StartLoc, SourceLocation IdLoc, 2113 IdentifierInfo *Id, QualType T, 2114 TypeSourceInfo *TInfo, Expr *BW, bool Mutable, 2115 InClassInitStyle InitStyle); 2116 2117 static FieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2118 2119 /// getFieldIndex - Returns the index of this field within its record, 2120 /// as appropriate for passing to ASTRecordLayout::getFieldOffset. 2121 unsigned getFieldIndex() const; 2122 2123 /// isMutable - Determines whether this field is mutable (C++ only). 2124 bool isMutable() const { return Mutable; } 2125 2126 /// isBitfield - Determines whether this field is a bitfield. 2127 bool isBitField() const { 2128 return getInClassInitStyle() == ICIS_NoInit && 2129 InitializerOrBitWidth.getPointer(); 2130 } 2131 2132 /// @brief Determines whether this is an unnamed bitfield. 2133 bool isUnnamedBitfield() const { return isBitField() && !getDeclName(); } 2134 2135 /// isAnonymousStructOrUnion - Determines whether this field is a 2136 /// representative for an anonymous struct or union. Such fields are 2137 /// unnamed and are implicitly generated by the implementation to 2138 /// store the data for the anonymous union or struct. 2139 bool isAnonymousStructOrUnion() const; 2140 2141 Expr *getBitWidth() const { 2142 return isBitField() ? InitializerOrBitWidth.getPointer() : 0; 2143 } 2144 unsigned getBitWidthValue(const ASTContext &Ctx) const; 2145 2146 /// setBitWidth - Set the bit-field width for this member. 2147 // Note: used by some clients (i.e., do not remove it). 2148 void setBitWidth(Expr *Width); 2149 /// removeBitWidth - Remove the bit-field width from this member. 2150 // Note: used by some clients (i.e., do not remove it). 2151 void removeBitWidth() { 2152 assert(isBitField() && "no bitfield width to remove"); 2153 InitializerOrBitWidth.setPointer(0); 2154 } 2155 2156 /// getInClassInitStyle - Get the kind of (C++11) in-class initializer which 2157 /// this field has. 2158 InClassInitStyle getInClassInitStyle() const { 2159 return static_cast<InClassInitStyle>(InitializerOrBitWidth.getInt()); 2160 } 2161 2162 /// hasInClassInitializer - Determine whether this member has a C++11 in-class 2163 /// initializer. 2164 bool hasInClassInitializer() const { 2165 return getInClassInitStyle() != ICIS_NoInit; 2166 } 2167 /// getInClassInitializer - Get the C++11 in-class initializer for this 2168 /// member, or null if one has not been set. If a valid declaration has an 2169 /// in-class initializer, but this returns null, then we have not parsed and 2170 /// attached it yet. 2171 Expr *getInClassInitializer() const { 2172 return hasInClassInitializer() ? InitializerOrBitWidth.getPointer() : 0; 2173 } 2174 /// setInClassInitializer - Set the C++11 in-class initializer for this 2175 /// member. 2176 void setInClassInitializer(Expr *Init); 2177 /// removeInClassInitializer - Remove the C++11 in-class initializer from this 2178 /// member. 2179 void removeInClassInitializer() { 2180 assert(hasInClassInitializer() && "no initializer to remove"); 2181 InitializerOrBitWidth.setPointer(0); 2182 InitializerOrBitWidth.setInt(ICIS_NoInit); 2183 } 2184 2185 /// getParent - Returns the parent of this field declaration, which 2186 /// is the struct in which this method is defined. 2187 const RecordDecl *getParent() const { 2188 return cast<RecordDecl>(getDeclContext()); 2189 } 2190 2191 RecordDecl *getParent() { 2192 return cast<RecordDecl>(getDeclContext()); 2193 } 2194 2195 SourceRange getSourceRange() const LLVM_READONLY; 2196 2197 // Implement isa/cast/dyncast/etc. 2198 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2199 static bool classofKind(Kind K) { return K >= firstField && K <= lastField; } 2200 2201 friend class ASTDeclReader; 2202 friend class ASTDeclWriter; 2203}; 2204 2205/// EnumConstantDecl - An instance of this object exists for each enum constant 2206/// that is defined. For example, in "enum X {a,b}", each of a/b are 2207/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a 2208/// TagType for the X EnumDecl. 2209class EnumConstantDecl : public ValueDecl { 2210 Stmt *Init; // an integer constant expression 2211 llvm::APSInt Val; // The value. 2212protected: 2213 EnumConstantDecl(DeclContext *DC, SourceLocation L, 2214 IdentifierInfo *Id, QualType T, Expr *E, 2215 const llvm::APSInt &V) 2216 : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {} 2217 2218public: 2219 2220 static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC, 2221 SourceLocation L, IdentifierInfo *Id, 2222 QualType T, Expr *E, 2223 const llvm::APSInt &V); 2224 static EnumConstantDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2225 2226 const Expr *getInitExpr() const { return (const Expr*) Init; } 2227 Expr *getInitExpr() { return (Expr*) Init; } 2228 const llvm::APSInt &getInitVal() const { return Val; } 2229 2230 void setInitExpr(Expr *E) { Init = (Stmt*) E; } 2231 void setInitVal(const llvm::APSInt &V) { Val = V; } 2232 2233 SourceRange getSourceRange() const LLVM_READONLY; 2234 2235 // Implement isa/cast/dyncast/etc. 2236 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2237 static bool classofKind(Kind K) { return K == EnumConstant; } 2238 2239 friend class StmtIteratorBase; 2240}; 2241 2242/// IndirectFieldDecl - An instance of this class is created to represent a 2243/// field injected from an anonymous union/struct into the parent scope. 2244/// IndirectFieldDecl are always implicit. 2245class IndirectFieldDecl : public ValueDecl { 2246 virtual void anchor(); 2247 NamedDecl **Chaining; 2248 unsigned ChainingSize; 2249 2250 IndirectFieldDecl(DeclContext *DC, SourceLocation L, 2251 DeclarationName N, QualType T, 2252 NamedDecl **CH, unsigned CHS) 2253 : ValueDecl(IndirectField, DC, L, N, T), Chaining(CH), ChainingSize(CHS) {} 2254 2255public: 2256 static IndirectFieldDecl *Create(ASTContext &C, DeclContext *DC, 2257 SourceLocation L, IdentifierInfo *Id, 2258 QualType T, NamedDecl **CH, unsigned CHS); 2259 2260 static IndirectFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2261 2262 typedef NamedDecl * const *chain_iterator; 2263 chain_iterator chain_begin() const { return Chaining; } 2264 chain_iterator chain_end() const { return Chaining+ChainingSize; } 2265 2266 unsigned getChainingSize() const { return ChainingSize; } 2267 2268 FieldDecl *getAnonField() const { 2269 assert(ChainingSize >= 2); 2270 return cast<FieldDecl>(Chaining[ChainingSize - 1]); 2271 } 2272 2273 VarDecl *getVarDecl() const { 2274 assert(ChainingSize >= 2); 2275 return dyn_cast<VarDecl>(*chain_begin()); 2276 } 2277 2278 // Implement isa/cast/dyncast/etc. 2279 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2280 static bool classofKind(Kind K) { return K == IndirectField; } 2281 friend class ASTDeclReader; 2282}; 2283 2284/// TypeDecl - Represents a declaration of a type. 2285/// 2286class TypeDecl : public NamedDecl { 2287 virtual void anchor(); 2288 /// TypeForDecl - This indicates the Type object that represents 2289 /// this TypeDecl. It is a cache maintained by 2290 /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and 2291 /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl. 2292 mutable const Type *TypeForDecl; 2293 /// LocStart - The start of the source range for this declaration. 2294 SourceLocation LocStart; 2295 friend class ASTContext; 2296 friend class DeclContext; 2297 friend class TagDecl; 2298 friend class TemplateTypeParmDecl; 2299 friend class TagType; 2300 friend class ASTReader; 2301 2302protected: 2303 TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 2304 SourceLocation StartL = SourceLocation()) 2305 : NamedDecl(DK, DC, L, Id), TypeForDecl(0), LocStart(StartL) {} 2306 2307public: 2308 // Low-level accessor. If you just want the type defined by this node, 2309 // check out ASTContext::getTypeDeclType or one of 2310 // ASTContext::getTypedefType, ASTContext::getRecordType, etc. if you 2311 // already know the specific kind of node this is. 2312 const Type *getTypeForDecl() const { return TypeForDecl; } 2313 void setTypeForDecl(const Type *TD) { TypeForDecl = TD; } 2314 2315 SourceLocation getLocStart() const LLVM_READONLY { return LocStart; } 2316 void setLocStart(SourceLocation L) { LocStart = L; } 2317 virtual SourceRange getSourceRange() const LLVM_READONLY { 2318 if (LocStart.isValid()) 2319 return SourceRange(LocStart, getLocation()); 2320 else 2321 return SourceRange(getLocation()); 2322 } 2323 2324 // Implement isa/cast/dyncast/etc. 2325 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2326 static bool classofKind(Kind K) { return K >= firstType && K <= lastType; } 2327}; 2328 2329 2330/// Base class for declarations which introduce a typedef-name. 2331class TypedefNameDecl : public TypeDecl, public Redeclarable<TypedefNameDecl> { 2332 virtual void anchor(); 2333 /// UnderlyingType - This is the type the typedef is set to. 2334 TypeSourceInfo *TInfo; 2335 2336protected: 2337 TypedefNameDecl(Kind DK, DeclContext *DC, SourceLocation StartLoc, 2338 SourceLocation IdLoc, IdentifierInfo *Id, 2339 TypeSourceInfo *TInfo) 2340 : TypeDecl(DK, DC, IdLoc, Id, StartLoc), TInfo(TInfo) {} 2341 2342 typedef Redeclarable<TypedefNameDecl> redeclarable_base; 2343 virtual TypedefNameDecl *getNextRedeclaration() { 2344 return RedeclLink.getNext(); 2345 } 2346 virtual TypedefNameDecl *getPreviousDeclImpl() { 2347 return getPreviousDecl(); 2348 } 2349 virtual TypedefNameDecl *getMostRecentDeclImpl() { 2350 return getMostRecentDecl(); 2351 } 2352 2353public: 2354 typedef redeclarable_base::redecl_iterator redecl_iterator; 2355 using redeclarable_base::redecls_begin; 2356 using redeclarable_base::redecls_end; 2357 using redeclarable_base::getPreviousDecl; 2358 using redeclarable_base::getMostRecentDecl; 2359 2360 TypeSourceInfo *getTypeSourceInfo() const { 2361 return TInfo; 2362 } 2363 2364 /// Retrieves the canonical declaration of this typedef-name. 2365 TypedefNameDecl *getCanonicalDecl() { 2366 return getFirstDeclaration(); 2367 } 2368 const TypedefNameDecl *getCanonicalDecl() const { 2369 return getFirstDeclaration(); 2370 } 2371 2372 QualType getUnderlyingType() const { 2373 return TInfo->getType(); 2374 } 2375 void setTypeSourceInfo(TypeSourceInfo *newType) { 2376 TInfo = newType; 2377 } 2378 2379 // Implement isa/cast/dyncast/etc. 2380 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2381 static bool classofKind(Kind K) { 2382 return K >= firstTypedefName && K <= lastTypedefName; 2383 } 2384}; 2385 2386/// TypedefDecl - Represents the declaration of a typedef-name via the 'typedef' 2387/// type specifier. 2388class TypedefDecl : public TypedefNameDecl { 2389 TypedefDecl(DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, 2390 IdentifierInfo *Id, TypeSourceInfo *TInfo) 2391 : TypedefNameDecl(Typedef, DC, StartLoc, IdLoc, Id, TInfo) {} 2392 2393public: 2394 static TypedefDecl *Create(ASTContext &C, DeclContext *DC, 2395 SourceLocation StartLoc, SourceLocation IdLoc, 2396 IdentifierInfo *Id, TypeSourceInfo *TInfo); 2397 static TypedefDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2398 2399 SourceRange getSourceRange() const LLVM_READONLY; 2400 2401 // Implement isa/cast/dyncast/etc. 2402 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2403 static bool classofKind(Kind K) { return K == Typedef; } 2404}; 2405 2406/// TypeAliasDecl - Represents the declaration of a typedef-name via a C++0x 2407/// alias-declaration. 2408class TypeAliasDecl : public TypedefNameDecl { 2409 TypeAliasDecl(DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, 2410 IdentifierInfo *Id, TypeSourceInfo *TInfo) 2411 : TypedefNameDecl(TypeAlias, DC, StartLoc, IdLoc, Id, TInfo) {} 2412 2413public: 2414 static TypeAliasDecl *Create(ASTContext &C, DeclContext *DC, 2415 SourceLocation StartLoc, SourceLocation IdLoc, 2416 IdentifierInfo *Id, TypeSourceInfo *TInfo); 2417 static TypeAliasDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2418 2419 SourceRange getSourceRange() const LLVM_READONLY; 2420 2421 // Implement isa/cast/dyncast/etc. 2422 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2423 static bool classofKind(Kind K) { return K == TypeAlias; } 2424}; 2425 2426/// TagDecl - Represents the declaration of a struct/union/class/enum. 2427class TagDecl 2428 : public TypeDecl, public DeclContext, public Redeclarable<TagDecl> { 2429public: 2430 // This is really ugly. 2431 typedef TagTypeKind TagKind; 2432 2433private: 2434 // FIXME: This can be packed into the bitfields in Decl. 2435 /// TagDeclKind - The TagKind enum. 2436 unsigned TagDeclKind : 3; 2437 2438 /// IsCompleteDefinition - True if this is a definition ("struct foo 2439 /// {};"), false if it is a declaration ("struct foo;"). It is not 2440 /// a definition until the definition has been fully processed. 2441 bool IsCompleteDefinition : 1; 2442 2443protected: 2444 /// IsBeingDefined - True if this is currently being defined. 2445 bool IsBeingDefined : 1; 2446 2447private: 2448 /// IsEmbeddedInDeclarator - True if this tag declaration is 2449 /// "embedded" (i.e., defined or declared for the very first time) 2450 /// in the syntax of a declarator. 2451 bool IsEmbeddedInDeclarator : 1; 2452 2453 /// \brief True if this tag is free standing, e.g. "struct foo;". 2454 bool IsFreeStanding : 1; 2455 2456protected: 2457 // These are used by (and only defined for) EnumDecl. 2458 unsigned NumPositiveBits : 8; 2459 unsigned NumNegativeBits : 8; 2460 2461 /// IsScoped - True if this tag declaration is a scoped enumeration. Only 2462 /// possible in C++11 mode. 2463 bool IsScoped : 1; 2464 /// IsScopedUsingClassTag - If this tag declaration is a scoped enum, 2465 /// then this is true if the scoped enum was declared using the class 2466 /// tag, false if it was declared with the struct tag. No meaning is 2467 /// associated if this tag declaration is not a scoped enum. 2468 bool IsScopedUsingClassTag : 1; 2469 2470 /// IsFixed - True if this is an enumeration with fixed underlying type. Only 2471 /// possible in C++11 or Microsoft extensions mode. 2472 bool IsFixed : 1; 2473 2474private: 2475 SourceLocation RBraceLoc; 2476 2477 // A struct representing syntactic qualifier info, 2478 // to be used for the (uncommon) case of out-of-line declarations. 2479 typedef QualifierInfo ExtInfo; 2480 2481 /// TypedefNameDeclOrQualifier - If the (out-of-line) tag declaration name 2482 /// is qualified, it points to the qualifier info (nns and range); 2483 /// otherwise, if the tag declaration is anonymous and it is part of 2484 /// a typedef or alias, it points to the TypedefNameDecl (used for mangling); 2485 /// otherwise, it is a null (TypedefNameDecl) pointer. 2486 llvm::PointerUnion<TypedefNameDecl*, ExtInfo*> TypedefNameDeclOrQualifier; 2487 2488 bool hasExtInfo() const { return TypedefNameDeclOrQualifier.is<ExtInfo*>(); } 2489 ExtInfo *getExtInfo() { return TypedefNameDeclOrQualifier.get<ExtInfo*>(); } 2490 const ExtInfo *getExtInfo() const { 2491 return TypedefNameDeclOrQualifier.get<ExtInfo*>(); 2492 } 2493 2494protected: 2495 TagDecl(Kind DK, TagKind TK, DeclContext *DC, 2496 SourceLocation L, IdentifierInfo *Id, 2497 TagDecl *PrevDecl, SourceLocation StartL) 2498 : TypeDecl(DK, DC, L, Id, StartL), DeclContext(DK), 2499 TypedefNameDeclOrQualifier((TypedefNameDecl*) 0) { 2500 assert((DK != Enum || TK == TTK_Enum) && 2501 "EnumDecl not matched with TTK_Enum"); 2502 TagDeclKind = TK; 2503 IsCompleteDefinition = false; 2504 IsBeingDefined = false; 2505 IsEmbeddedInDeclarator = false; 2506 IsFreeStanding = false; 2507 setPreviousDeclaration(PrevDecl); 2508 } 2509 2510 typedef Redeclarable<TagDecl> redeclarable_base; 2511 virtual TagDecl *getNextRedeclaration() { return RedeclLink.getNext(); } 2512 virtual TagDecl *getPreviousDeclImpl() { 2513 return getPreviousDecl(); 2514 } 2515 virtual TagDecl *getMostRecentDeclImpl() { 2516 return getMostRecentDecl(); 2517 } 2518 2519 /// @brief Completes the definition of this tag declaration. 2520 /// 2521 /// This is a helper function for derived classes. 2522 void completeDefinition(); 2523 2524public: 2525 typedef redeclarable_base::redecl_iterator redecl_iterator; 2526 using redeclarable_base::redecls_begin; 2527 using redeclarable_base::redecls_end; 2528 using redeclarable_base::getPreviousDecl; 2529 using redeclarable_base::getMostRecentDecl; 2530 2531 SourceLocation getRBraceLoc() const { return RBraceLoc; } 2532 void setRBraceLoc(SourceLocation L) { RBraceLoc = L; } 2533 2534 /// getInnerLocStart - Return SourceLocation representing start of source 2535 /// range ignoring outer template declarations. 2536 SourceLocation getInnerLocStart() const { return getLocStart(); } 2537 2538 /// getOuterLocStart - Return SourceLocation representing start of source 2539 /// range taking into account any outer template declarations. 2540 SourceLocation getOuterLocStart() const; 2541 virtual SourceRange getSourceRange() const LLVM_READONLY; 2542 2543 virtual TagDecl* getCanonicalDecl(); 2544 const TagDecl* getCanonicalDecl() const { 2545 return const_cast<TagDecl*>(this)->getCanonicalDecl(); 2546 } 2547 2548 /// isThisDeclarationADefinition() - Return true if this declaration 2549 /// is a completion definintion of the type. Provided for consistency. 2550 bool isThisDeclarationADefinition() const { 2551 return isCompleteDefinition(); 2552 } 2553 2554 /// isCompleteDefinition - Return true if this decl has its body 2555 /// fully specified. 2556 bool isCompleteDefinition() const { 2557 return IsCompleteDefinition; 2558 } 2559 2560 /// isBeingDefined - Return true if this decl is currently being defined. 2561 bool isBeingDefined() const { 2562 return IsBeingDefined; 2563 } 2564 2565 bool isEmbeddedInDeclarator() const { 2566 return IsEmbeddedInDeclarator; 2567 } 2568 void setEmbeddedInDeclarator(bool isInDeclarator) { 2569 IsEmbeddedInDeclarator = isInDeclarator; 2570 } 2571 2572 bool isFreeStanding() const { return IsFreeStanding; } 2573 void setFreeStanding(bool isFreeStanding = true) { 2574 IsFreeStanding = isFreeStanding; 2575 } 2576 2577 /// \brief Whether this declaration declares a type that is 2578 /// dependent, i.e., a type that somehow depends on template 2579 /// parameters. 2580 bool isDependentType() const { return isDependentContext(); } 2581 2582 /// @brief Starts the definition of this tag declaration. 2583 /// 2584 /// This method should be invoked at the beginning of the definition 2585 /// of this tag declaration. It will set the tag type into a state 2586 /// where it is in the process of being defined. 2587 void startDefinition(); 2588 2589 /// getDefinition - Returns the TagDecl that actually defines this 2590 /// struct/union/class/enum. When determining whether or not a 2591 /// struct/union/class/enum has a definition, one should use this 2592 /// method as opposed to 'isDefinition'. 'isDefinition' indicates 2593 /// whether or not a specific TagDecl is defining declaration, not 2594 /// whether or not the struct/union/class/enum type is defined. 2595 /// This method returns NULL if there is no TagDecl that defines 2596 /// the struct/union/class/enum. 2597 TagDecl *getDefinition() const; 2598 2599 void setCompleteDefinition(bool V) { IsCompleteDefinition = V; } 2600 2601 // FIXME: Return StringRef; 2602 const char *getKindName() const { 2603 return TypeWithKeyword::getTagTypeKindName(getTagKind()); 2604 } 2605 2606 TagKind getTagKind() const { 2607 return TagKind(TagDeclKind); 2608 } 2609 2610 void setTagKind(TagKind TK) { TagDeclKind = TK; } 2611 2612 bool isStruct() const { return getTagKind() == TTK_Struct; } 2613 bool isInterface() const { return getTagKind() == TTK_Interface; } 2614 bool isClass() const { return getTagKind() == TTK_Class; } 2615 bool isUnion() const { return getTagKind() == TTK_Union; } 2616 bool isEnum() const { return getTagKind() == TTK_Enum; } 2617 2618 TypedefNameDecl *getTypedefNameForAnonDecl() const { 2619 return hasExtInfo() ? 0 : 2620 TypedefNameDeclOrQualifier.get<TypedefNameDecl*>(); 2621 } 2622 2623 void setTypedefNameForAnonDecl(TypedefNameDecl *TDD); 2624 2625 /// \brief Retrieve the nested-name-specifier that qualifies the name of this 2626 /// declaration, if it was present in the source. 2627 NestedNameSpecifier *getQualifier() const { 2628 return hasExtInfo() ? getExtInfo()->QualifierLoc.getNestedNameSpecifier() 2629 : 0; 2630 } 2631 2632 /// \brief Retrieve the nested-name-specifier (with source-location 2633 /// information) that qualifies the name of this declaration, if it was 2634 /// present in the source. 2635 NestedNameSpecifierLoc getQualifierLoc() const { 2636 return hasExtInfo() ? getExtInfo()->QualifierLoc 2637 : NestedNameSpecifierLoc(); 2638 } 2639 2640 void setQualifierInfo(NestedNameSpecifierLoc QualifierLoc); 2641 2642 unsigned getNumTemplateParameterLists() const { 2643 return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0; 2644 } 2645 TemplateParameterList *getTemplateParameterList(unsigned i) const { 2646 assert(i < getNumTemplateParameterLists()); 2647 return getExtInfo()->TemplParamLists[i]; 2648 } 2649 void setTemplateParameterListsInfo(ASTContext &Context, unsigned NumTPLists, 2650 TemplateParameterList **TPLists); 2651 2652 // Implement isa/cast/dyncast/etc. 2653 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2654 static bool classofKind(Kind K) { return K >= firstTag && K <= lastTag; } 2655 2656 static DeclContext *castToDeclContext(const TagDecl *D) { 2657 return static_cast<DeclContext *>(const_cast<TagDecl*>(D)); 2658 } 2659 static TagDecl *castFromDeclContext(const DeclContext *DC) { 2660 return static_cast<TagDecl *>(const_cast<DeclContext*>(DC)); 2661 } 2662 2663 friend class ASTDeclReader; 2664 friend class ASTDeclWriter; 2665}; 2666 2667/// EnumDecl - Represents an enum. In C++11, enums can be forward-declared 2668/// with a fixed underlying type, and in C we allow them to be forward-declared 2669/// with no underlying type as an extension. 2670class EnumDecl : public TagDecl { 2671 virtual void anchor(); 2672 /// IntegerType - This represent the integer type that the enum corresponds 2673 /// to for code generation purposes. Note that the enumerator constants may 2674 /// have a different type than this does. 2675 /// 2676 /// If the underlying integer type was explicitly stated in the source 2677 /// code, this is a TypeSourceInfo* for that type. Otherwise this type 2678 /// was automatically deduced somehow, and this is a Type*. 2679 /// 2680 /// Normally if IsFixed(), this would contain a TypeSourceInfo*, but in 2681 /// some cases it won't. 2682 /// 2683 /// The underlying type of an enumeration never has any qualifiers, so 2684 /// we can get away with just storing a raw Type*, and thus save an 2685 /// extra pointer when TypeSourceInfo is needed. 2686 2687 llvm::PointerUnion<const Type*, TypeSourceInfo*> IntegerType; 2688 2689 /// PromotionType - The integer type that values of this type should 2690 /// promote to. In C, enumerators are generally of an integer type 2691 /// directly, but gcc-style large enumerators (and all enumerators 2692 /// in C++) are of the enum type instead. 2693 QualType PromotionType; 2694 2695 /// \brief If this enumeration is an instantiation of a member enumeration 2696 /// of a class template specialization, this is the member specialization 2697 /// information. 2698 MemberSpecializationInfo *SpecializationInfo; 2699 2700 EnumDecl(DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, 2701 IdentifierInfo *Id, EnumDecl *PrevDecl, 2702 bool Scoped, bool ScopedUsingClassTag, bool Fixed) 2703 : TagDecl(Enum, TTK_Enum, DC, IdLoc, Id, PrevDecl, StartLoc), 2704 SpecializationInfo(0) { 2705 assert(Scoped || !ScopedUsingClassTag); 2706 IntegerType = (const Type*)0; 2707 NumNegativeBits = 0; 2708 NumPositiveBits = 0; 2709 IsScoped = Scoped; 2710 IsScopedUsingClassTag = ScopedUsingClassTag; 2711 IsFixed = Fixed; 2712 } 2713 2714 void setInstantiationOfMemberEnum(ASTContext &C, EnumDecl *ED, 2715 TemplateSpecializationKind TSK); 2716public: 2717 EnumDecl *getCanonicalDecl() { 2718 return cast<EnumDecl>(TagDecl::getCanonicalDecl()); 2719 } 2720 const EnumDecl *getCanonicalDecl() const { 2721 return cast<EnumDecl>(TagDecl::getCanonicalDecl()); 2722 } 2723 2724 const EnumDecl *getPreviousDecl() const { 2725 return cast_or_null<EnumDecl>(TagDecl::getPreviousDecl()); 2726 } 2727 EnumDecl *getPreviousDecl() { 2728 return cast_or_null<EnumDecl>(TagDecl::getPreviousDecl()); 2729 } 2730 2731 const EnumDecl *getMostRecentDecl() const { 2732 return cast<EnumDecl>(TagDecl::getMostRecentDecl()); 2733 } 2734 EnumDecl *getMostRecentDecl() { 2735 return cast<EnumDecl>(TagDecl::getMostRecentDecl()); 2736 } 2737 2738 EnumDecl *getDefinition() const { 2739 return cast_or_null<EnumDecl>(TagDecl::getDefinition()); 2740 } 2741 2742 static EnumDecl *Create(ASTContext &C, DeclContext *DC, 2743 SourceLocation StartLoc, SourceLocation IdLoc, 2744 IdentifierInfo *Id, EnumDecl *PrevDecl, 2745 bool IsScoped, bool IsScopedUsingClassTag, 2746 bool IsFixed); 2747 static EnumDecl *CreateDeserialized(ASTContext &C, unsigned ID); 2748 2749 /// completeDefinition - When created, the EnumDecl corresponds to a 2750 /// forward-declared enum. This method is used to mark the 2751 /// declaration as being defined; it's enumerators have already been 2752 /// added (via DeclContext::addDecl). NewType is the new underlying 2753 /// type of the enumeration type. 2754 void completeDefinition(QualType NewType, 2755 QualType PromotionType, 2756 unsigned NumPositiveBits, 2757 unsigned NumNegativeBits); 2758 2759 // enumerator_iterator - Iterates through the enumerators of this 2760 // enumeration. 2761 typedef specific_decl_iterator<EnumConstantDecl> enumerator_iterator; 2762 2763 enumerator_iterator enumerator_begin() const { 2764 const EnumDecl *E = getDefinition(); 2765 if (!E) 2766 E = this; 2767 return enumerator_iterator(E->decls_begin()); 2768 } 2769 2770 enumerator_iterator enumerator_end() const { 2771 const EnumDecl *E = getDefinition(); 2772 if (!E) 2773 E = this; 2774 return enumerator_iterator(E->decls_end()); 2775 } 2776 2777 /// getPromotionType - Return the integer type that enumerators 2778 /// should promote to. 2779 QualType getPromotionType() const { return PromotionType; } 2780 2781 /// \brief Set the promotion type. 2782 void setPromotionType(QualType T) { PromotionType = T; } 2783 2784 /// getIntegerType - Return the integer type this enum decl corresponds to. 2785 /// This returns a null qualtype for an enum forward definition. 2786 QualType getIntegerType() const { 2787 if (!IntegerType) 2788 return QualType(); 2789 if (const Type* T = IntegerType.dyn_cast<const Type*>()) 2790 return QualType(T, 0); 2791 return IntegerType.get<TypeSourceInfo*>()->getType(); 2792 } 2793 2794 /// \brief Set the underlying integer type. 2795 void setIntegerType(QualType T) { IntegerType = T.getTypePtrOrNull(); } 2796 2797 /// \brief Set the underlying integer type source info. 2798 void setIntegerTypeSourceInfo(TypeSourceInfo* TInfo) { IntegerType = TInfo; } 2799 2800 /// \brief Return the type source info for the underlying integer type, 2801 /// if no type source info exists, return 0. 2802 TypeSourceInfo* getIntegerTypeSourceInfo() const { 2803 return IntegerType.dyn_cast<TypeSourceInfo*>(); 2804 } 2805 2806 /// \brief Returns the width in bits required to store all the 2807 /// non-negative enumerators of this enum. 2808 unsigned getNumPositiveBits() const { 2809 return NumPositiveBits; 2810 } 2811 void setNumPositiveBits(unsigned Num) { 2812 NumPositiveBits = Num; 2813 assert(NumPositiveBits == Num && "can't store this bitcount"); 2814 } 2815 2816 /// \brief Returns the width in bits required to store all the 2817 /// negative enumerators of this enum. These widths include 2818 /// the rightmost leading 1; that is: 2819 /// 2820 /// MOST NEGATIVE ENUMERATOR PATTERN NUM NEGATIVE BITS 2821 /// ------------------------ ------- ----------------- 2822 /// -1 1111111 1 2823 /// -10 1110110 5 2824 /// -101 1001011 8 2825 unsigned getNumNegativeBits() const { 2826 return NumNegativeBits; 2827 } 2828 void setNumNegativeBits(unsigned Num) { 2829 NumNegativeBits = Num; 2830 } 2831 2832 /// \brief Returns true if this is a C++0x scoped enumeration. 2833 bool isScoped() const { 2834 return IsScoped; 2835 } 2836 2837 /// \brief Returns true if this is a C++0x scoped enumeration. 2838 bool isScopedUsingClassTag() const { 2839 return IsScopedUsingClassTag; 2840 } 2841 2842 /// \brief Returns true if this is a C++0x enumeration with fixed underlying 2843 /// type. 2844 bool isFixed() const { 2845 return IsFixed; 2846 } 2847 2848 /// \brief Returns true if this can be considered a complete type. 2849 bool isComplete() const { 2850 return isCompleteDefinition() || isFixed(); 2851 } 2852 2853 /// \brief Returns the enumeration (declared within the template) 2854 /// from which this enumeration type was instantiated, or NULL if 2855 /// this enumeration was not instantiated from any template. 2856 EnumDecl *getInstantiatedFromMemberEnum() const; 2857 2858 /// \brief If this enumeration is a member of a specialization of a 2859 /// templated class, determine what kind of template specialization 2860 /// or instantiation this is. 2861 TemplateSpecializationKind getTemplateSpecializationKind() const; 2862 2863 /// \brief For an enumeration member that was instantiated from a member 2864 /// enumeration of a templated class, set the template specialiation kind. 2865 void setTemplateSpecializationKind(TemplateSpecializationKind TSK, 2866 SourceLocation PointOfInstantiation = SourceLocation()); 2867 2868 /// \brief If this enumeration is an instantiation of a member enumeration of 2869 /// a class template specialization, retrieves the member specialization 2870 /// information. 2871 MemberSpecializationInfo *getMemberSpecializationInfo() const { 2872 return SpecializationInfo; 2873 } 2874 2875 /// \brief Specify that this enumeration is an instantiation of the 2876 /// member enumeration ED. 2877 void setInstantiationOfMemberEnum(EnumDecl *ED, 2878 TemplateSpecializationKind TSK) { 2879 setInstantiationOfMemberEnum(getASTContext(), ED, TSK); 2880 } 2881 2882 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 2883 static bool classofKind(Kind K) { return K == Enum; } 2884 2885 friend class ASTDeclReader; 2886}; 2887 2888 2889/// RecordDecl - Represents a struct/union/class. For example: 2890/// struct X; // Forward declaration, no "body". 2891/// union Y { int A, B; }; // Has body with members A and B (FieldDecls). 2892/// This decl will be marked invalid if *any* members are invalid. 2893/// 2894class RecordDecl : public TagDecl { 2895 // FIXME: This can be packed into the bitfields in Decl. 2896 /// HasFlexibleArrayMember - This is true if this struct ends with a flexible 2897 /// array member (e.g. int X[]) or if this union contains a struct that does. 2898 /// If so, this cannot be contained in arrays or other structs as a member. 2899 bool HasFlexibleArrayMember : 1; 2900 2901 /// AnonymousStructOrUnion - Whether this is the type of an anonymous struct 2902 /// or union. 2903 bool AnonymousStructOrUnion : 1; 2904 2905 /// HasObjectMember - This is true if this struct has at least one member 2906 /// containing an Objective-C object pointer type. 2907 bool HasObjectMember : 1; 2908 2909 /// \brief Whether the field declarations of this record have been loaded 2910 /// from external storage. To avoid unnecessary deserialization of 2911 /// methods/nested types we allow deserialization of just the fields 2912 /// when needed. 2913 mutable bool LoadedFieldsFromExternalStorage : 1; 2914 friend class DeclContext; 2915 2916protected: 2917 RecordDecl(Kind DK, TagKind TK, DeclContext *DC, 2918 SourceLocation StartLoc, SourceLocation IdLoc, 2919 IdentifierInfo *Id, RecordDecl *PrevDecl); 2920 2921public: 2922 static RecordDecl *Create(const ASTContext &C, TagKind TK, DeclContext *DC, 2923 SourceLocation StartLoc, SourceLocation IdLoc, 2924 IdentifierInfo *Id, RecordDecl* PrevDecl = 0); 2925 static RecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID); 2926 2927 const RecordDecl *getPreviousDecl() const { 2928 return cast_or_null<RecordDecl>(TagDecl::getPreviousDecl()); 2929 } 2930 RecordDecl *getPreviousDecl() { 2931 return cast_or_null<RecordDecl>(TagDecl::getPreviousDecl()); 2932 } 2933 2934 const RecordDecl *getMostRecentDecl() const { 2935 return cast<RecordDecl>(TagDecl::getMostRecentDecl()); 2936 } 2937 RecordDecl *getMostRecentDecl() { 2938 return cast<RecordDecl>(TagDecl::getMostRecentDecl()); 2939 } 2940 2941 bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; } 2942 void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; } 2943 2944 /// isAnonymousStructOrUnion - Whether this is an anonymous struct 2945 /// or union. To be an anonymous struct or union, it must have been 2946 /// declared without a name and there must be no objects of this 2947 /// type declared, e.g., 2948 /// @code 2949 /// union { int i; float f; }; 2950 /// @endcode 2951 /// is an anonymous union but neither of the following are: 2952 /// @code 2953 /// union X { int i; float f; }; 2954 /// union { int i; float f; } obj; 2955 /// @endcode 2956 bool isAnonymousStructOrUnion() const { return AnonymousStructOrUnion; } 2957 void setAnonymousStructOrUnion(bool Anon) { 2958 AnonymousStructOrUnion = Anon; 2959 } 2960 2961 bool hasObjectMember() const { return HasObjectMember; } 2962 void setHasObjectMember (bool val) { HasObjectMember = val; } 2963 2964 /// \brief Determines whether this declaration represents the 2965 /// injected class name. 2966 /// 2967 /// The injected class name in C++ is the name of the class that 2968 /// appears inside the class itself. For example: 2969 /// 2970 /// \code 2971 /// struct C { 2972 /// // C is implicitly declared here as a synonym for the class name. 2973 /// }; 2974 /// 2975 /// C::C c; // same as "C c;" 2976 /// \endcode 2977 bool isInjectedClassName() const; 2978 2979 /// getDefinition - Returns the RecordDecl that actually defines 2980 /// this struct/union/class. When determining whether or not a 2981 /// struct/union/class is completely defined, one should use this 2982 /// method as opposed to 'isCompleteDefinition'. 2983 /// 'isCompleteDefinition' indicates whether or not a specific 2984 /// RecordDecl is a completed definition, not whether or not the 2985 /// record type is defined. This method returns NULL if there is 2986 /// no RecordDecl that defines the struct/union/tag. 2987 RecordDecl *getDefinition() const { 2988 return cast_or_null<RecordDecl>(TagDecl::getDefinition()); 2989 } 2990 2991 // Iterator access to field members. The field iterator only visits 2992 // the non-static data members of this class, ignoring any static 2993 // data members, functions, constructors, destructors, etc. 2994 typedef specific_decl_iterator<FieldDecl> field_iterator; 2995 2996 field_iterator field_begin() const; 2997 2998 field_iterator field_end() const { 2999 return field_iterator(decl_iterator()); 3000 } 3001 3002 // field_empty - Whether there are any fields (non-static data 3003 // members) in this record. 3004 bool field_empty() const { 3005 return field_begin() == field_end(); 3006 } 3007 3008 /// completeDefinition - Notes that the definition of this type is 3009 /// now complete. 3010 virtual void completeDefinition(); 3011 3012 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 3013 static bool classofKind(Kind K) { 3014 return K >= firstRecord && K <= lastRecord; 3015 } 3016 3017 /// isMsStrust - Get whether or not this is an ms_struct which can 3018 /// be turned on with an attribute, pragma, or -mms-bitfields 3019 /// commandline option. 3020 bool isMsStruct(const ASTContext &C) const; 3021 3022private: 3023 /// \brief Deserialize just the fields. 3024 void LoadFieldsFromExternalStorage() const; 3025}; 3026 3027class FileScopeAsmDecl : public Decl { 3028 virtual void anchor(); 3029 StringLiteral *AsmString; 3030 SourceLocation RParenLoc; 3031 FileScopeAsmDecl(DeclContext *DC, StringLiteral *asmstring, 3032 SourceLocation StartL, SourceLocation EndL) 3033 : Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {} 3034public: 3035 static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC, 3036 StringLiteral *Str, SourceLocation AsmLoc, 3037 SourceLocation RParenLoc); 3038 3039 static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, unsigned ID); 3040 3041 SourceLocation getAsmLoc() const { return getLocation(); } 3042 SourceLocation getRParenLoc() const { return RParenLoc; } 3043 void setRParenLoc(SourceLocation L) { RParenLoc = L; } 3044 SourceRange getSourceRange() const LLVM_READONLY { 3045 return SourceRange(getAsmLoc(), getRParenLoc()); 3046 } 3047 3048 const StringLiteral *getAsmString() const { return AsmString; } 3049 StringLiteral *getAsmString() { return AsmString; } 3050 void setAsmString(StringLiteral *Asm) { AsmString = Asm; } 3051 3052 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 3053 static bool classofKind(Kind K) { return K == FileScopeAsm; } 3054}; 3055 3056/// BlockDecl - This represents a block literal declaration, which is like an 3057/// unnamed FunctionDecl. For example: 3058/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 3059/// 3060class BlockDecl : public Decl, public DeclContext { 3061public: 3062 /// A class which contains all the information about a particular 3063 /// captured value. 3064 class Capture { 3065 enum { 3066 flag_isByRef = 0x1, 3067 flag_isNested = 0x2 3068 }; 3069 3070 /// The variable being captured. 3071 llvm::PointerIntPair<VarDecl*, 2> VariableAndFlags; 3072 3073 /// The copy expression, expressed in terms of a DeclRef (or 3074 /// BlockDeclRef) to the captured variable. Only required if the 3075 /// variable has a C++ class type. 3076 Expr *CopyExpr; 3077 3078 public: 3079 Capture(VarDecl *variable, bool byRef, bool nested, Expr *copy) 3080 : VariableAndFlags(variable, 3081 (byRef ? flag_isByRef : 0) | (nested ? flag_isNested : 0)), 3082 CopyExpr(copy) {} 3083 3084 /// The variable being captured. 3085 VarDecl *getVariable() const { return VariableAndFlags.getPointer(); } 3086 3087 /// Whether this is a "by ref" capture, i.e. a capture of a __block 3088 /// variable. 3089 bool isByRef() const { return VariableAndFlags.getInt() & flag_isByRef; } 3090 3091 /// Whether this is a nested capture, i.e. the variable captured 3092 /// is not from outside the immediately enclosing function/block. 3093 bool isNested() const { return VariableAndFlags.getInt() & flag_isNested; } 3094 3095 bool hasCopyExpr() const { return CopyExpr != 0; } 3096 Expr *getCopyExpr() const { return CopyExpr; } 3097 void setCopyExpr(Expr *e) { CopyExpr = e; } 3098 }; 3099 3100private: 3101 // FIXME: This can be packed into the bitfields in Decl. 3102 bool IsVariadic : 1; 3103 bool CapturesCXXThis : 1; 3104 bool BlockMissingReturnType : 1; 3105 bool IsConversionFromLambda : 1; 3106 /// ParamInfo - new[]'d array of pointers to ParmVarDecls for the formal 3107 /// parameters of this function. This is null if a prototype or if there are 3108 /// no formals. 3109 ParmVarDecl **ParamInfo; 3110 unsigned NumParams; 3111 3112 Stmt *Body; 3113 TypeSourceInfo *SignatureAsWritten; 3114 3115 Capture *Captures; 3116 unsigned NumCaptures; 3117 3118protected: 3119 BlockDecl(DeclContext *DC, SourceLocation CaretLoc) 3120 : Decl(Block, DC, CaretLoc), DeclContext(Block), 3121 IsVariadic(false), CapturesCXXThis(false), 3122 BlockMissingReturnType(true), IsConversionFromLambda(false), 3123 ParamInfo(0), NumParams(0), Body(0), 3124 SignatureAsWritten(0), Captures(0), NumCaptures(0) {} 3125 3126public: 3127 static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L); 3128 static BlockDecl *CreateDeserialized(ASTContext &C, unsigned ID); 3129 3130 SourceLocation getCaretLocation() const { return getLocation(); } 3131 3132 bool isVariadic() const { return IsVariadic; } 3133 void setIsVariadic(bool value) { IsVariadic = value; } 3134 3135 CompoundStmt *getCompoundBody() const { return (CompoundStmt*) Body; } 3136 Stmt *getBody() const { return (Stmt*) Body; } 3137 void setBody(CompoundStmt *B) { Body = (Stmt*) B; } 3138 3139 void setSignatureAsWritten(TypeSourceInfo *Sig) { SignatureAsWritten = Sig; } 3140 TypeSourceInfo *getSignatureAsWritten() const { return SignatureAsWritten; } 3141 3142 // Iterator access to formal parameters. 3143 unsigned param_size() const { return getNumParams(); } 3144 typedef ParmVarDecl **param_iterator; 3145 typedef ParmVarDecl * const *param_const_iterator; 3146 3147 bool param_empty() const { return NumParams == 0; } 3148 param_iterator param_begin() { return ParamInfo; } 3149 param_iterator param_end() { return ParamInfo+param_size(); } 3150 3151 param_const_iterator param_begin() const { return ParamInfo; } 3152 param_const_iterator param_end() const { return ParamInfo+param_size(); } 3153 3154 unsigned getNumParams() const { return NumParams; } 3155 const ParmVarDecl *getParamDecl(unsigned i) const { 3156 assert(i < getNumParams() && "Illegal param #"); 3157 return ParamInfo[i]; 3158 } 3159 ParmVarDecl *getParamDecl(unsigned i) { 3160 assert(i < getNumParams() && "Illegal param #"); 3161 return ParamInfo[i]; 3162 } 3163 void setParams(llvm::ArrayRef<ParmVarDecl *> NewParamInfo); 3164 3165 /// hasCaptures - True if this block (or its nested blocks) captures 3166 /// anything of local storage from its enclosing scopes. 3167 bool hasCaptures() const { return NumCaptures != 0 || CapturesCXXThis; } 3168 3169 /// getNumCaptures - Returns the number of captured variables. 3170 /// Does not include an entry for 'this'. 3171 unsigned getNumCaptures() const { return NumCaptures; } 3172 3173 typedef const Capture *capture_iterator; 3174 typedef const Capture *capture_const_iterator; 3175 capture_iterator capture_begin() { return Captures; } 3176 capture_iterator capture_end() { return Captures + NumCaptures; } 3177 capture_const_iterator capture_begin() const { return Captures; } 3178 capture_const_iterator capture_end() const { return Captures + NumCaptures; } 3179 3180 bool capturesCXXThis() const { return CapturesCXXThis; } 3181 bool blockMissingReturnType() const { return BlockMissingReturnType; } 3182 void setBlockMissingReturnType(bool val) { BlockMissingReturnType = val; } 3183 3184 bool isConversionFromLambda() const { return IsConversionFromLambda; } 3185 void setIsConversionFromLambda(bool val) { IsConversionFromLambda = val; } 3186 3187 bool capturesVariable(const VarDecl *var) const; 3188 3189 void setCaptures(ASTContext &Context, 3190 const Capture *begin, 3191 const Capture *end, 3192 bool capturesCXXThis); 3193 3194 virtual SourceRange getSourceRange() const LLVM_READONLY; 3195 3196 // Implement isa/cast/dyncast/etc. 3197 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 3198 static bool classofKind(Kind K) { return K == Block; } 3199 static DeclContext *castToDeclContext(const BlockDecl *D) { 3200 return static_cast<DeclContext *>(const_cast<BlockDecl*>(D)); 3201 } 3202 static BlockDecl *castFromDeclContext(const DeclContext *DC) { 3203 return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC)); 3204 } 3205}; 3206 3207/// \brief Describes a module import declaration, which makes the contents 3208/// of the named module visible in the current translation unit. 3209/// 3210/// An import declaration imports the named module (or submodule). For example: 3211/// \code 3212/// @__experimental_modules_import std.vector; 3213/// \endcode 3214/// 3215/// Import declarations can also be implicitly generated from 3216/// \#include/\#import directives. 3217class ImportDecl : public Decl { 3218 /// \brief The imported module, along with a bit that indicates whether 3219 /// we have source-location information for each identifier in the module 3220 /// name. 3221 /// 3222 /// When the bit is false, we only have a single source location for the 3223 /// end of the import declaration. 3224 llvm::PointerIntPair<Module *, 1, bool> ImportedAndComplete; 3225 3226 /// \brief The next import in the list of imports local to the translation 3227 /// unit being parsed (not loaded from an AST file). 3228 ImportDecl *NextLocalImport; 3229 3230 friend class ASTReader; 3231 friend class ASTDeclReader; 3232 friend class ASTContext; 3233 3234 ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, 3235 ArrayRef<SourceLocation> IdentifierLocs); 3236 3237 ImportDecl(DeclContext *DC, SourceLocation StartLoc, Module *Imported, 3238 SourceLocation EndLoc); 3239 3240 ImportDecl(EmptyShell Empty) : Decl(Import, Empty), NextLocalImport() { } 3241 3242public: 3243 /// \brief Create a new module import declaration. 3244 static ImportDecl *Create(ASTContext &C, DeclContext *DC, 3245 SourceLocation StartLoc, Module *Imported, 3246 ArrayRef<SourceLocation> IdentifierLocs); 3247 3248 /// \brief Create a new module import declaration for an implicitly-generated 3249 /// import. 3250 static ImportDecl *CreateImplicit(ASTContext &C, DeclContext *DC, 3251 SourceLocation StartLoc, Module *Imported, 3252 SourceLocation EndLoc); 3253 3254 /// \brief Create a new, deserialized module import declaration. 3255 static ImportDecl *CreateDeserialized(ASTContext &C, unsigned ID, 3256 unsigned NumLocations); 3257 3258 /// \brief Retrieve the module that was imported by the import declaration. 3259 Module *getImportedModule() const { return ImportedAndComplete.getPointer(); } 3260 3261 /// \brief Retrieves the locations of each of the identifiers that make up 3262 /// the complete module name in the import declaration. 3263 /// 3264 /// This will return an empty array if the locations of the individual 3265 /// identifiers aren't available. 3266 ArrayRef<SourceLocation> getIdentifierLocs() const; 3267 3268 virtual SourceRange getSourceRange() const LLVM_READONLY; 3269 3270 static bool classof(const Decl *D) { return classofKind(D->getKind()); } 3271 static bool classofKind(Kind K) { return K == Import; } 3272}; 3273 3274 3275/// Insertion operator for diagnostics. This allows sending NamedDecl's 3276/// into a diagnostic with <<. 3277inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 3278 const NamedDecl* ND) { 3279 DB.AddTaggedVal(reinterpret_cast<intptr_t>(ND), 3280 DiagnosticsEngine::ak_nameddecl); 3281 return DB; 3282} 3283inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, 3284 const NamedDecl* ND) { 3285 PD.AddTaggedVal(reinterpret_cast<intptr_t>(ND), 3286 DiagnosticsEngine::ak_nameddecl); 3287 return PD; 3288} 3289 3290template<typename decl_type> 3291void Redeclarable<decl_type>::setPreviousDeclaration(decl_type *PrevDecl) { 3292 // Note: This routine is implemented here because we need both NamedDecl 3293 // and Redeclarable to be defined. 3294 3295 decl_type *First; 3296 3297 if (PrevDecl) { 3298 // Point to previous. Make sure that this is actually the most recent 3299 // redeclaration, or we can build invalid chains. If the most recent 3300 // redeclaration is invalid, it won't be PrevDecl, but we want it anyway. 3301 First = PrevDecl->getFirstDeclaration(); 3302 assert(First->RedeclLink.NextIsLatest() && "Expected first"); 3303 decl_type *MostRecent = First->RedeclLink.getNext(); 3304 RedeclLink = PreviousDeclLink(llvm::cast<decl_type>(MostRecent)); 3305 } else { 3306 // Make this first. 3307 First = static_cast<decl_type*>(this); 3308 } 3309 3310 // First one will point to this one as latest. 3311 First->RedeclLink = LatestDeclLink(static_cast<decl_type*>(this)); 3312 if (NamedDecl *ND = dyn_cast<NamedDecl>(static_cast<decl_type*>(this))) 3313 ND->ClearLinkageCache(); 3314} 3315 3316// Inline function definitions. 3317 3318/// \brief Check if the given decl is complete. 3319/// 3320/// We use this function to break a cycle between the inline definitions in 3321/// Type.h and Decl.h. 3322inline bool IsEnumDeclComplete(EnumDecl *ED) { 3323 return ED->isComplete(); 3324} 3325 3326/// \brief Check if the given decl is scoped. 3327/// 3328/// We use this function to break a cycle between the inline definitions in 3329/// Type.h and Decl.h. 3330inline bool IsEnumDeclScoped(EnumDecl *ED) { 3331 return ED->isScoped(); 3332} 3333 3334} // end namespace clang 3335 3336#endif 3337