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