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