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