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