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