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