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