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