ASTContext.h revision 175c8e2e57befe2db15dd339ab5eb27d0c2b6013
15c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu//===--- ASTContext.h - Context to hold long-lived AST nodes ----*- C++ -*-===// 25c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// 35c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// The LLVM Compiler Infrastructure 45c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// 55c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// This file is distributed under the University of Illinois Open Source 65c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// License. See LICENSE.TXT for details. 75c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// 85c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu//===----------------------------------------------------------------------===// 95c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu/// 105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu/// \file 115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu/// \brief Defines the clang::ASTContext interface. 125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu/// 135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu//===----------------------------------------------------------------------===// 145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#ifndef LLVM_CLANG_AST_ASTCONTEXT_H 165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#define LLVM_CLANG_AST_ASTCONTEXT_H 175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/AST/ASTTypeTraits.h" 195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/AST/CanonicalType.h" 205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/AST/CommentCommandTraits.h" 215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/AST/Decl.h" 225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/AST/LambdaMangleContext.h" 235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/AST/NestedNameSpecifier.h" 245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/AST/PrettyPrinter.h" 255c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/AST/RawCommentList.h" 265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/AST/RecursiveASTVisitor.h" 275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/AST/TemplateName.h" 285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/AST/Type.h" 295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Basic/AddressSpaces.h" 305c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Basic/IdentifierTable.h" 315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Basic/LangOptions.h" 325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Basic/OperatorKinds.h" 335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Basic/PartialDiagnostic.h" 345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "clang/Basic/VersionTuple.h" 355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/ADT/DenseMap.h" 365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/ADT/FoldingSet.h" 375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/ADT/IntrusiveRefCntPtr.h" 385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/ADT/OwningPtr.h" 395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/ADT/SmallPtrSet.h" 405c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/ADT/TinyPtrVector.h" 415c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "llvm/Support/Allocator.h" 425c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include <vector> 435c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 445c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace llvm { 455c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu struct fltSemantics; 465c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu} 475c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liunamespace clang { 495c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class FileManager; 505c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class ASTRecordLayout; 515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class BlockExpr; 525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class CharUnits; 535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class DiagnosticsEngine; 545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class Expr; 555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class ExternalASTSource; 565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class ASTMutationListener; 575c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class IdentifierTable; 585c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class SelectorTable; 595c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class TargetInfo; 605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class CXXABI; 615c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // Decls 625c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class MangleContext; 635c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class ObjCIvarDecl; 645c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class ObjCPropertyDecl; 655c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class UnresolvedSetIterator; 665c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class UsingDecl; 675c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class UsingShadowDecl; 685c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 695c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu namespace Builtin { class Context; } 705c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 715c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu namespace comments { 725c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu class FullComment; 735c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu } 745c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 755c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu/// \brief Holds long-lived AST nodes (such as types and decls) that can be 765c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu/// referred to throughout the semantic analysis of a file. 775c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass ASTContext : public RefCountedBase<ASTContext> { 785c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ASTContext &this_() { return *this; } 795c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 805c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable SmallVector<Type *, 0> Types; 815c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<ExtQuals> ExtQualNodes; 825c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<ComplexType> ComplexTypes; 835c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<PointerType> PointerTypes; 845c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<BlockPointerType> BlockPointerTypes; 855c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<LValueReferenceType> LValueReferenceTypes; 865c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<RValueReferenceType> RValueReferenceTypes; 875c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<MemberPointerType> MemberPointerTypes; 885c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes; 895c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes; 905c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable std::vector<VariableArrayType*> VariableArrayTypes; 915c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<DependentSizedArrayType> DependentSizedArrayTypes; 925c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<DependentSizedExtVectorType> 935c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu DependentSizedExtVectorTypes; 945c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<VectorType> VectorTypes; 955c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<FunctionNoProtoType> FunctionNoProtoTypes; 965c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::ContextualFoldingSet<FunctionProtoType, ASTContext&> 975c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu FunctionProtoTypes; 985c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<DependentTypeOfExprType> DependentTypeOfExprTypes; 995c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<DependentDecltypeType> DependentDecltypeTypes; 1005c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes; 1015c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<SubstTemplateTypeParmType> 1025c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu SubstTemplateTypeParmTypes; 1035c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<SubstTemplateTypeParmPackType> 1045c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu SubstTemplateTypeParmPackTypes; 1055c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::ContextualFoldingSet<TemplateSpecializationType, ASTContext&> 1065c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu TemplateSpecializationTypes; 1075c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<ParenType> ParenTypes; 1085c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<ElaboratedType> ElaboratedTypes; 1095c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<DependentNameType> DependentNameTypes; 1105c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::ContextualFoldingSet<DependentTemplateSpecializationType, 1115c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ASTContext&> 1125c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu DependentTemplateSpecializationTypes; 1135c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::FoldingSet<PackExpansionType> PackExpansionTypes; 1145c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<ObjCObjectTypeImpl> ObjCObjectTypes; 1155c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<ObjCObjectPointerType> ObjCObjectPointerTypes; 1165c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<AutoType> AutoTypes; 1175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<AtomicType> AtomicTypes; 1185c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu llvm::FoldingSet<AttributedType> AttributedTypes; 1195c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<QualifiedTemplateName> QualifiedTemplateNames; 1215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<DependentTemplateName> DependentTemplateNames; 1225c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<SubstTemplateTemplateParmStorage> 1235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu SubstTemplateTemplateParms; 1245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::ContextualFoldingSet<SubstTemplateTemplateParmPackStorage, 1255c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ASTContext&> 1265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu SubstTemplateTemplateParmPacks; 1275c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1285c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief The set of nested name specifiers. 129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// 130116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch /// This set is managed by the NestedNameSpecifier class. 1315c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable llvm::FoldingSet<NestedNameSpecifier> NestedNameSpecifiers; 1325c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu mutable NestedNameSpecifier *GlobalNestedNameSpecifier; 1335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu friend class NestedNameSpecifier; 1345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 1355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu /// \brief A cache mapping from RecordDecls to ASTRecordLayouts. 136 /// 137 /// This is lazily created. This is intentionally not serialized. 138 mutable llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> 139 ASTRecordLayouts; 140 mutable llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*> 141 ObjCLayouts; 142 143 /// \brief A cache from types to size and alignment information. 144 typedef llvm::DenseMap<const Type*, 145 std::pair<uint64_t, unsigned> > TypeInfoMap; 146 mutable TypeInfoMap MemoizedTypeInfo; 147 148 /// \brief A cache mapping from CXXRecordDecls to key functions. 149 llvm::DenseMap<const CXXRecordDecl*, const CXXMethodDecl*> KeyFunctions; 150 151 /// \brief Mapping from ObjCContainers to their ObjCImplementations. 152 llvm::DenseMap<ObjCContainerDecl*, ObjCImplDecl*> ObjCImpls; 153 154 /// \brief Mapping from ObjCMethod to its duplicate declaration in the same 155 /// interface. 156 llvm::DenseMap<const ObjCMethodDecl*,const ObjCMethodDecl*> ObjCMethodRedecls; 157 158 /// \brief Mapping from __block VarDecls to their copy initialization expr. 159 llvm::DenseMap<const VarDecl*, Expr*> BlockVarCopyInits; 160 161 /// \brief Mapping from class scope functions specialization to their 162 /// template patterns. 163 llvm::DenseMap<const FunctionDecl*, FunctionDecl*> 164 ClassScopeSpecializationPattern; 165 166 /// \brief Representation of a "canonical" template template parameter that 167 /// is used in canonical template names. 168 class CanonicalTemplateTemplateParm : public llvm::FoldingSetNode { 169 TemplateTemplateParmDecl *Parm; 170 171 public: 172 CanonicalTemplateTemplateParm(TemplateTemplateParmDecl *Parm) 173 : Parm(Parm) { } 174 175 TemplateTemplateParmDecl *getParam() const { return Parm; } 176 177 void Profile(llvm::FoldingSetNodeID &ID) { Profile(ID, Parm); } 178 179 static void Profile(llvm::FoldingSetNodeID &ID, 180 TemplateTemplateParmDecl *Parm); 181 }; 182 mutable llvm::FoldingSet<CanonicalTemplateTemplateParm> 183 CanonTemplateTemplateParms; 184 185 TemplateTemplateParmDecl * 186 getCanonicalTemplateTemplateParmDecl(TemplateTemplateParmDecl *TTP) const; 187 188 /// \brief The typedef for the __int128_t type. 189 mutable TypedefDecl *Int128Decl; 190 191 /// \brief The typedef for the __uint128_t type. 192 mutable TypedefDecl *UInt128Decl; 193 194 /// \brief The typedef for the target specific predefined 195 /// __builtin_va_list type. 196 mutable TypedefDecl *BuiltinVaListDecl; 197 198 /// \brief The typedef for the predefined \c id type. 199 mutable TypedefDecl *ObjCIdDecl; 200 201 /// \brief The typedef for the predefined \c SEL type. 202 mutable TypedefDecl *ObjCSelDecl; 203 204 /// \brief The typedef for the predefined \c Class type. 205 mutable TypedefDecl *ObjCClassDecl; 206 207 /// \brief The typedef for the predefined \c Protocol class in Objective-C. 208 mutable ObjCInterfaceDecl *ObjCProtocolClassDecl; 209 210 /// \brief The typedef for the predefined 'BOOL' type. 211 mutable TypedefDecl *BOOLDecl; 212 213 // Typedefs which may be provided defining the structure of Objective-C 214 // pseudo-builtins 215 QualType ObjCIdRedefinitionType; 216 QualType ObjCClassRedefinitionType; 217 QualType ObjCSelRedefinitionType; 218 219 QualType ObjCConstantStringType; 220 mutable RecordDecl *CFConstantStringTypeDecl; 221 222 mutable QualType ObjCSuperType; 223 224 QualType ObjCNSStringType; 225 226 /// \brief The typedef declaration for the Objective-C "instancetype" type. 227 TypedefDecl *ObjCInstanceTypeDecl; 228 229 /// \brief The type for the C FILE type. 230 TypeDecl *FILEDecl; 231 232 /// \brief The type for the C jmp_buf type. 233 TypeDecl *jmp_bufDecl; 234 235 /// \brief The type for the C sigjmp_buf type. 236 TypeDecl *sigjmp_bufDecl; 237 238 /// \brief The type for the C ucontext_t type. 239 TypeDecl *ucontext_tDecl; 240 241 /// \brief Type for the Block descriptor for Blocks CodeGen. 242 /// 243 /// Since this is only used for generation of debug info, it is not 244 /// serialized. 245 mutable RecordDecl *BlockDescriptorType; 246 247 /// \brief Type for the Block descriptor for Blocks CodeGen. 248 /// 249 /// Since this is only used for generation of debug info, it is not 250 /// serialized. 251 mutable RecordDecl *BlockDescriptorExtendedType; 252 253 /// \brief Declaration for the CUDA cudaConfigureCall function. 254 FunctionDecl *cudaConfigureCallDecl; 255 256 TypeSourceInfo NullTypeSourceInfo; 257 258 /// \brief Keeps track of all declaration attributes. 259 /// 260 /// Since so few decls have attrs, we keep them in a hash map instead of 261 /// wasting space in the Decl class. 262 llvm::DenseMap<const Decl*, AttrVec*> DeclAttrs; 263 264 /// \brief Keeps track of the static data member templates from which 265 /// static data members of class template specializations were instantiated. 266 /// 267 /// This data structure stores the mapping from instantiations of static 268 /// data members to the static data member representations within the 269 /// class template from which they were instantiated along with the kind 270 /// of instantiation or specialization (a TemplateSpecializationKind - 1). 271 /// 272 /// Given the following example: 273 /// 274 /// \code 275 /// template<typename T> 276 /// struct X { 277 /// static T value; 278 /// }; 279 /// 280 /// template<typename T> 281 /// T X<T>::value = T(17); 282 /// 283 /// int *x = &X<int>::value; 284 /// \endcode 285 /// 286 /// This mapping will contain an entry that maps from the VarDecl for 287 /// X<int>::value to the corresponding VarDecl for X<T>::value (within the 288 /// class template X) and will be marked TSK_ImplicitInstantiation. 289 llvm::DenseMap<const VarDecl *, MemberSpecializationInfo *> 290 InstantiatedFromStaticDataMember; 291 292 /// \brief Keeps track of the declaration from which a UsingDecl was 293 /// created during instantiation. 294 /// 295 /// The source declaration is always a UsingDecl, an UnresolvedUsingValueDecl, 296 /// or an UnresolvedUsingTypenameDecl. 297 /// 298 /// For example: 299 /// \code 300 /// template<typename T> 301 /// struct A { 302 /// void f(); 303 /// }; 304 /// 305 /// template<typename T> 306 /// struct B : A<T> { 307 /// using A<T>::f; 308 /// }; 309 /// 310 /// template struct B<int>; 311 /// \endcode 312 /// 313 /// This mapping will contain an entry that maps from the UsingDecl in 314 /// B<int> to the UnresolvedUsingDecl in B<T>. 315 llvm::DenseMap<UsingDecl *, NamedDecl *> InstantiatedFromUsingDecl; 316 317 llvm::DenseMap<UsingShadowDecl*, UsingShadowDecl*> 318 InstantiatedFromUsingShadowDecl; 319 320 llvm::DenseMap<FieldDecl *, FieldDecl *> InstantiatedFromUnnamedFieldDecl; 321 322 /// \brief Mapping that stores the methods overridden by a given C++ 323 /// member function. 324 /// 325 /// Since most C++ member functions aren't virtual and therefore 326 /// don't override anything, we store the overridden functions in 327 /// this map on the side rather than within the CXXMethodDecl structure. 328 typedef llvm::TinyPtrVector<const CXXMethodDecl*> CXXMethodVector; 329 llvm::DenseMap<const CXXMethodDecl *, CXXMethodVector> OverriddenMethods; 330 331 /// \brief Used to cache results from \c getBaseObjCCategoriesAfterInterface. 332 mutable llvm::DenseMap<const ObjCInterfaceDecl *, 333 llvm::SmallVector<const ObjCCategoryDecl *, 2> > CatsAfterInterface; 334 335 /// \brief Mapping from each declaration context to its corresponding lambda 336 /// mangling context. 337 llvm::DenseMap<const DeclContext *, LambdaMangleContext> LambdaMangleContexts; 338 339 llvm::DenseMap<const DeclContext *, unsigned> UnnamedMangleContexts; 340 llvm::DenseMap<const TagDecl *, unsigned> UnnamedMangleNumbers; 341 342 /// \brief Mapping that stores parameterIndex values for ParmVarDecls when 343 /// that value exceeds the bitfield size of ParmVarDeclBits.ParameterIndex. 344 typedef llvm::DenseMap<const VarDecl *, unsigned> ParameterIndexTable; 345 ParameterIndexTable ParamIndices; 346 347 ImportDecl *FirstLocalImport; 348 ImportDecl *LastLocalImport; 349 350 TranslationUnitDecl *TUDecl; 351 352 /// \brief The associated SourceManager object.a 353 SourceManager &SourceMgr; 354 355 /// \brief The language options used to create the AST associated with 356 /// this ASTContext object. 357 LangOptions &LangOpts; 358 359 /// \brief The allocator used to create AST objects. 360 /// 361 /// AST objects are never destructed; rather, all memory associated with the 362 /// AST objects will be released when the ASTContext itself is destroyed. 363 mutable llvm::BumpPtrAllocator BumpAlloc; 364 365 /// \brief Allocator for partial diagnostics. 366 PartialDiagnostic::StorageAllocator DiagAllocator; 367 368 /// \brief The current C++ ABI. 369 OwningPtr<CXXABI> ABI; 370 CXXABI *createCXXABI(const TargetInfo &T); 371 372 /// \brief The logical -> physical address space map. 373 const LangAS::Map *AddrSpaceMap; 374 375 friend class ASTDeclReader; 376 friend class ASTReader; 377 friend class ASTWriter; 378 friend class CXXRecordDecl; 379 380 const TargetInfo *Target; 381 clang::PrintingPolicy PrintingPolicy; 382 383public: 384 IdentifierTable &Idents; 385 SelectorTable &Selectors; 386 Builtin::Context &BuiltinInfo; 387 mutable DeclarationNameTable DeclarationNames; 388 OwningPtr<ExternalASTSource> ExternalSource; 389 ASTMutationListener *Listener; 390 391 /// \brief Contains parents of a node. 392 typedef llvm::SmallVector<ast_type_traits::DynTypedNode, 1> ParentVector; 393 394 /// \brief Maps from a node to its parents. 395 typedef llvm::DenseMap<const void *, ParentVector> ParentMap; 396 397 /// \brief Returns the parents of the given node. 398 /// 399 /// Note that this will lazily compute the parents of all nodes 400 /// and store them for later retrieval. Thus, the first call is O(n) 401 /// in the number of AST nodes. 402 /// 403 /// Caveats and FIXMEs: 404 /// Calculating the parent map over all AST nodes will need to load the 405 /// full AST. This can be undesirable in the case where the full AST is 406 /// expensive to create (for example, when using precompiled header 407 /// preambles). Thus, there are good opportunities for optimization here. 408 /// One idea is to walk the given node downwards, looking for references 409 /// to declaration contexts - once a declaration context is found, compute 410 /// the parent map for the declaration context; if that can satisfy the 411 /// request, loading the whole AST can be avoided. Note that this is made 412 /// more complex by statements in templates having multiple parents - those 413 /// problems can be solved by building closure over the templated parts of 414 /// the AST, which also avoids touching large parts of the AST. 415 /// Additionally, we will want to add an interface to already give a hint 416 /// where to search for the parents, for example when looking at a statement 417 /// inside a certain function. 418 /// 419 /// 'NodeT' can be one of Decl, Stmt, Type, TypeLoc, 420 /// NestedNameSpecifier or NestedNameSpecifierLoc. 421 template <typename NodeT> 422 ParentVector getParents(const NodeT &Node) { 423 return getParents(ast_type_traits::DynTypedNode::create(Node)); 424 } 425 426 ParentVector getParents(const ast_type_traits::DynTypedNode &Node) { 427 assert(Node.getMemoizationData() && 428 "Invariant broken: only nodes that support memoization may be " 429 "used in the parent map."); 430 if (!AllParents) { 431 // We always need to run over the whole translation unit, as 432 // hasAncestor can escape any subtree. 433 AllParents.reset( 434 ParentMapASTVisitor::buildMap(*getTranslationUnitDecl())); 435 } 436 ParentMap::const_iterator I = AllParents->find(Node.getMemoizationData()); 437 if (I == AllParents->end()) { 438 return ParentVector(); 439 } 440 return I->second; 441 } 442 443 const clang::PrintingPolicy &getPrintingPolicy() const { 444 return PrintingPolicy; 445 } 446 447 void setPrintingPolicy(const clang::PrintingPolicy &Policy) { 448 PrintingPolicy = Policy; 449 } 450 451 SourceManager& getSourceManager() { return SourceMgr; } 452 const SourceManager& getSourceManager() const { return SourceMgr; } 453 454 llvm::BumpPtrAllocator &getAllocator() const { 455 return BumpAlloc; 456 } 457 458 void *Allocate(unsigned Size, unsigned Align = 8) const { 459 return BumpAlloc.Allocate(Size, Align); 460 } 461 void Deallocate(void *Ptr) const { } 462 463 /// Return the total amount of physical memory allocated for representing 464 /// AST nodes and type information. 465 size_t getASTAllocatedMemory() const { 466 return BumpAlloc.getTotalMemory(); 467 } 468 /// Return the total memory used for various side tables. 469 size_t getSideTableAllocatedMemory() const; 470 471 PartialDiagnostic::StorageAllocator &getDiagAllocator() { 472 return DiagAllocator; 473 } 474 475 const TargetInfo &getTargetInfo() const { return *Target; } 476 477 const LangOptions& getLangOpts() const { return LangOpts; } 478 479 DiagnosticsEngine &getDiagnostics() const; 480 481 FullSourceLoc getFullLoc(SourceLocation Loc) const { 482 return FullSourceLoc(Loc,SourceMgr); 483 } 484 485 /// \brief All comments in this translation unit. 486 RawCommentList Comments; 487 488 /// \brief True if comments are already loaded from ExternalASTSource. 489 mutable bool CommentsLoaded; 490 491 class RawCommentAndCacheFlags { 492 public: 493 enum Kind { 494 /// We searched for a comment attached to the particular declaration, but 495 /// didn't find any. 496 /// 497 /// getRaw() == 0. 498 NoCommentInDecl = 0, 499 500 /// We have found a comment attached to this particular declaration. 501 /// 502 /// getRaw() != 0. 503 FromDecl, 504 505 /// This declaration does not have an attached comment, and we have 506 /// searched the redeclaration chain. 507 /// 508 /// If getRaw() == 0, the whole redeclaration chain does not have any 509 /// comments. 510 /// 511 /// If getRaw() != 0, it is a comment propagated from other 512 /// redeclaration. 513 FromRedecl 514 }; 515 516 Kind getKind() const LLVM_READONLY { 517 return Data.getInt(); 518 } 519 520 void setKind(Kind K) { 521 Data.setInt(K); 522 } 523 524 const RawComment *getRaw() const LLVM_READONLY { 525 return Data.getPointer(); 526 } 527 528 void setRaw(const RawComment *RC) { 529 Data.setPointer(RC); 530 } 531 532 const Decl *getOriginalDecl() const LLVM_READONLY { 533 return OriginalDecl; 534 } 535 536 void setOriginalDecl(const Decl *Orig) { 537 OriginalDecl = Orig; 538 } 539 540 private: 541 llvm::PointerIntPair<const RawComment *, 2, Kind> Data; 542 const Decl *OriginalDecl; 543 }; 544 545 /// \brief Mapping from declarations to comments attached to any 546 /// redeclaration. 547 /// 548 /// Raw comments are owned by Comments list. This mapping is populated 549 /// lazily. 550 mutable llvm::DenseMap<const Decl *, RawCommentAndCacheFlags> RedeclComments; 551 552 /// \brief Mapping from declarations to parsed comments attached to any 553 /// redeclaration. 554 mutable llvm::DenseMap<const Decl *, comments::FullComment *> ParsedComments; 555 556 /// \brief Return the documentation comment attached to a given declaration, 557 /// without looking into cache. 558 RawComment *getRawCommentForDeclNoCache(const Decl *D) const; 559 560public: 561 RawCommentList &getRawCommentList() { 562 return Comments; 563 } 564 565 void addComment(const RawComment &RC) { 566 assert(LangOpts.RetainCommentsFromSystemHeaders || 567 !SourceMgr.isInSystemHeader(RC.getSourceRange().getBegin())); 568 Comments.addComment(RC, BumpAlloc); 569 } 570 571 /// \brief Return the documentation comment attached to a given declaration. 572 /// Returns NULL if no comment is attached. 573 /// 574 /// \param OriginalDecl if not NULL, is set to declaration AST node that had 575 /// the comment, if the comment we found comes from a redeclaration. 576 const RawComment *getRawCommentForAnyRedecl( 577 const Decl *D, 578 const Decl **OriginalDecl = NULL) const; 579 580 /// Return parsed documentation comment attached to a given declaration. 581 /// Returns NULL if no comment is attached. 582 /// 583 /// \param PP the Preprocessor used with this TU. Could be NULL if 584 /// preprocessor is not available. 585 comments::FullComment *getCommentForDecl(const Decl *D, 586 const Preprocessor *PP) const; 587 588 comments::FullComment *cloneFullComment(comments::FullComment *FC, 589 const Decl *D) const; 590 591private: 592 mutable comments::CommandTraits CommentCommandTraits; 593 594public: 595 comments::CommandTraits &getCommentCommandTraits() const { 596 return CommentCommandTraits; 597 } 598 599 /// \brief Retrieve the attributes for the given declaration. 600 AttrVec& getDeclAttrs(const Decl *D); 601 602 /// \brief Erase the attributes corresponding to the given declaration. 603 void eraseDeclAttrs(const Decl *D); 604 605 /// \brief If this variable is an instantiated static data member of a 606 /// class template specialization, returns the templated static data member 607 /// from which it was instantiated. 608 MemberSpecializationInfo *getInstantiatedFromStaticDataMember( 609 const VarDecl *Var); 610 611 FunctionDecl *getClassScopeSpecializationPattern(const FunctionDecl *FD); 612 613 void setClassScopeSpecializationPattern(FunctionDecl *FD, 614 FunctionDecl *Pattern); 615 616 /// \brief Note that the static data member \p Inst is an instantiation of 617 /// the static data member template \p Tmpl of a class template. 618 void setInstantiatedFromStaticDataMember(VarDecl *Inst, VarDecl *Tmpl, 619 TemplateSpecializationKind TSK, 620 SourceLocation PointOfInstantiation = SourceLocation()); 621 622 /// \brief If the given using decl \p Inst is an instantiation of a 623 /// (possibly unresolved) using decl from a template instantiation, 624 /// return it. 625 NamedDecl *getInstantiatedFromUsingDecl(UsingDecl *Inst); 626 627 /// \brief Remember that the using decl \p Inst is an instantiation 628 /// of the using decl \p Pattern of a class template. 629 void setInstantiatedFromUsingDecl(UsingDecl *Inst, NamedDecl *Pattern); 630 631 void setInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst, 632 UsingShadowDecl *Pattern); 633 UsingShadowDecl *getInstantiatedFromUsingShadowDecl(UsingShadowDecl *Inst); 634 635 FieldDecl *getInstantiatedFromUnnamedFieldDecl(FieldDecl *Field); 636 637 void setInstantiatedFromUnnamedFieldDecl(FieldDecl *Inst, FieldDecl *Tmpl); 638 639 /// \brief Return \c true if \p FD is a zero-length bitfield which follows 640 /// the non-bitfield \p LastFD. 641 bool ZeroBitfieldFollowsNonBitfield(const FieldDecl *FD, 642 const FieldDecl *LastFD) const; 643 644 /// \brief Return \c true if \p FD is a zero-length bitfield which follows 645 /// the bitfield \p LastFD. 646 bool ZeroBitfieldFollowsBitfield(const FieldDecl *FD, 647 const FieldDecl *LastFD) const; 648 649 /// \brief Return \c true if \p FD is a bitfield which follows the bitfield 650 /// \p LastFD. 651 bool BitfieldFollowsBitfield(const FieldDecl *FD, 652 const FieldDecl *LastFD) const; 653 654 /// \brief Return \c true if \p FD is not a bitfield which follows the 655 /// bitfield \p LastFD. 656 bool NonBitfieldFollowsBitfield(const FieldDecl *FD, 657 const FieldDecl *LastFD) const; 658 659 /// \brief Return \c true if \p FD is a bitfield which follows the 660 /// non-bitfield \p LastFD. 661 bool BitfieldFollowsNonBitfield(const FieldDecl *FD, 662 const FieldDecl *LastFD) const; 663 664 // Access to the set of methods overridden by the given C++ method. 665 typedef CXXMethodVector::const_iterator overridden_cxx_method_iterator; 666 overridden_cxx_method_iterator 667 overridden_methods_begin(const CXXMethodDecl *Method) const; 668 669 overridden_cxx_method_iterator 670 overridden_methods_end(const CXXMethodDecl *Method) const; 671 672 unsigned overridden_methods_size(const CXXMethodDecl *Method) const; 673 674 /// \brief Note that the given C++ \p Method overrides the given \p 675 /// Overridden method. 676 void addOverriddenMethod(const CXXMethodDecl *Method, 677 const CXXMethodDecl *Overridden); 678 679 /// \brief Return C++ or ObjC overridden methods for the given \p Method. 680 /// 681 /// An ObjC method is considered to override any method in the class's 682 /// base classes, its protocols, or its categories' protocols, that has 683 /// the same selector and is of the same kind (class or instance). 684 /// A method in an implementation is not considered as overriding the same 685 /// method in the interface or its categories. 686 void getOverriddenMethods( 687 const NamedDecl *Method, 688 SmallVectorImpl<const NamedDecl *> &Overridden) const; 689 690 /// \brief Returns the ObjC categories of base classes, that were declared 691 /// after the given interface declaration. 692 void getBaseObjCCategoriesAfterInterface(const ObjCInterfaceDecl *D, 693 SmallVectorImpl<const ObjCCategoryDecl *> &Cats) const; 694 695 /// \brief Notify the AST context that a new import declaration has been 696 /// parsed or implicitly created within this translation unit. 697 void addedLocalImportDecl(ImportDecl *Import); 698 699 static ImportDecl *getNextLocalImport(ImportDecl *Import) { 700 return Import->NextLocalImport; 701 } 702 703 /// \brief Iterator that visits import declarations. 704 class import_iterator { 705 ImportDecl *Import; 706 707 public: 708 typedef ImportDecl *value_type; 709 typedef ImportDecl *reference; 710 typedef ImportDecl *pointer; 711 typedef int difference_type; 712 typedef std::forward_iterator_tag iterator_category; 713 714 import_iterator() : Import() { } 715 explicit import_iterator(ImportDecl *Import) : Import(Import) { } 716 717 reference operator*() const { return Import; } 718 pointer operator->() const { return Import; } 719 720 import_iterator &operator++() { 721 Import = ASTContext::getNextLocalImport(Import); 722 return *this; 723 } 724 725 import_iterator operator++(int) { 726 import_iterator Other(*this); 727 ++(*this); 728 return Other; 729 } 730 731 friend bool operator==(import_iterator X, import_iterator Y) { 732 return X.Import == Y.Import; 733 } 734 735 friend bool operator!=(import_iterator X, import_iterator Y) { 736 return X.Import != Y.Import; 737 } 738 }; 739 740 import_iterator local_import_begin() const { 741 return import_iterator(FirstLocalImport); 742 } 743 import_iterator local_import_end() const { return import_iterator(); } 744 745 TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; } 746 747 748 // Builtin Types. 749 CanQualType VoidTy; 750 CanQualType BoolTy; 751 CanQualType CharTy; 752 CanQualType WCharTy; // [C++ 3.9.1p5], integer type in C99. 753 CanQualType WIntTy; // [C99 7.24.1], integer type unchanged by default promotions. 754 CanQualType Char16Ty; // [C++0x 3.9.1p5], integer type in C99. 755 CanQualType Char32Ty; // [C++0x 3.9.1p5], integer type in C99. 756 CanQualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy, Int128Ty; 757 CanQualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 758 CanQualType UnsignedLongLongTy, UnsignedInt128Ty; 759 CanQualType FloatTy, DoubleTy, LongDoubleTy; 760 CanQualType HalfTy; // [OpenCL 6.1.1.1], ARM NEON 761 CanQualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 762 CanQualType VoidPtrTy, NullPtrTy; 763 CanQualType DependentTy, OverloadTy, BoundMemberTy, UnknownAnyTy; 764 CanQualType BuiltinFnTy; 765 CanQualType PseudoObjectTy, ARCUnbridgedCastTy; 766 CanQualType ObjCBuiltinIdTy, ObjCBuiltinClassTy, ObjCBuiltinSelTy; 767 CanQualType ObjCBuiltinBoolTy; 768 CanQualType OCLImage1dTy, OCLImage1dArrayTy, OCLImage1dBufferTy; 769 CanQualType OCLImage2dTy, OCLImage2dArrayTy; 770 CanQualType OCLImage3dTy; 771 CanQualType OCLSamplerTy, OCLEventTy; 772 773 // Types for deductions in C++0x [stmt.ranged]'s desugaring. Built on demand. 774 mutable QualType AutoDeductTy; // Deduction against 'auto'. 775 mutable QualType AutoRRefDeductTy; // Deduction against 'auto &&'. 776 777 // Type used to help define __builtin_va_list for some targets. 778 // The type is built when constructing 'BuiltinVaListDecl'. 779 mutable QualType VaListTagTy; 780 781 ASTContext(LangOptions& LOpts, SourceManager &SM, const TargetInfo *t, 782 IdentifierTable &idents, SelectorTable &sels, 783 Builtin::Context &builtins, 784 unsigned size_reserve, 785 bool DelayInitialization = false); 786 787 ~ASTContext(); 788 789 /// \brief Attach an external AST source to the AST context. 790 /// 791 /// The external AST source provides the ability to load parts of 792 /// the abstract syntax tree as needed from some external storage, 793 /// e.g., a precompiled header. 794 void setExternalSource(OwningPtr<ExternalASTSource> &Source); 795 796 /// \brief Retrieve a pointer to the external AST source associated 797 /// with this AST context, if any. 798 ExternalASTSource *getExternalSource() const { return ExternalSource.get(); } 799 800 /// \brief Attach an AST mutation listener to the AST context. 801 /// 802 /// The AST mutation listener provides the ability to track modifications to 803 /// the abstract syntax tree entities committed after they were initially 804 /// created. 805 void setASTMutationListener(ASTMutationListener *Listener) { 806 this->Listener = Listener; 807 } 808 809 /// \brief Retrieve a pointer to the AST mutation listener associated 810 /// with this AST context, if any. 811 ASTMutationListener *getASTMutationListener() const { return Listener; } 812 813 void PrintStats() const; 814 const SmallVectorImpl<Type *>& getTypes() const { return Types; } 815 816 /// \brief Retrieve the declaration for the 128-bit signed integer type. 817 TypedefDecl *getInt128Decl() const; 818 819 /// \brief Retrieve the declaration for the 128-bit unsigned integer type. 820 TypedefDecl *getUInt128Decl() const; 821 822 //===--------------------------------------------------------------------===// 823 // Type Constructors 824 //===--------------------------------------------------------------------===// 825 826private: 827 /// \brief Return a type with extended qualifiers. 828 QualType getExtQualType(const Type *Base, Qualifiers Quals) const; 829 830 QualType getTypeDeclTypeSlow(const TypeDecl *Decl) const; 831 832public: 833 /// \brief Return the uniqued reference to the type for an address space 834 /// qualified type with the specified type and address space. 835 /// 836 /// The resulting type has a union of the qualifiers from T and the address 837 /// space. If T already has an address space specifier, it is silently 838 /// replaced. 839 QualType getAddrSpaceQualType(QualType T, unsigned AddressSpace) const; 840 841 /// \brief Return the uniqued reference to the type for an Objective-C 842 /// gc-qualified type. 843 /// 844 /// The retulting type has a union of the qualifiers from T and the gc 845 /// attribute. 846 QualType getObjCGCQualType(QualType T, Qualifiers::GC gcAttr) const; 847 848 /// \brief Return the uniqued reference to the type for a \c restrict 849 /// qualified type. 850 /// 851 /// The resulting type has a union of the qualifiers from \p T and 852 /// \c restrict. 853 QualType getRestrictType(QualType T) const { 854 return T.withFastQualifiers(Qualifiers::Restrict); 855 } 856 857 /// \brief Return the uniqued reference to the type for a \c volatile 858 /// qualified type. 859 /// 860 /// The resulting type has a union of the qualifiers from \p T and 861 /// \c volatile. 862 QualType getVolatileType(QualType T) const { 863 return T.withFastQualifiers(Qualifiers::Volatile); 864 } 865 866 /// \brief Return the uniqued reference to the type for a \c const 867 /// qualified type. 868 /// 869 /// The resulting type has a union of the qualifiers from \p T and \c const. 870 /// 871 /// It can be reasonably expected that this will always be equivalent to 872 /// calling T.withConst(). 873 QualType getConstType(QualType T) const { return T.withConst(); } 874 875 /// \brief Change the ExtInfo on a function type. 876 const FunctionType *adjustFunctionType(const FunctionType *Fn, 877 FunctionType::ExtInfo EInfo); 878 879 /// \brief Return the uniqued reference to the type for a complex 880 /// number with the specified element type. 881 QualType getComplexType(QualType T) const; 882 CanQualType getComplexType(CanQualType T) const { 883 return CanQualType::CreateUnsafe(getComplexType((QualType) T)); 884 } 885 886 /// \brief Return the uniqued reference to the type for a pointer to 887 /// the specified type. 888 QualType getPointerType(QualType T) const; 889 CanQualType getPointerType(CanQualType T) const { 890 return CanQualType::CreateUnsafe(getPointerType((QualType) T)); 891 } 892 893 /// \brief Return the uniqued reference to the atomic type for the specified 894 /// type. 895 QualType getAtomicType(QualType T) const; 896 897 /// \brief Return the uniqued reference to the type for a block of the 898 /// specified type. 899 QualType getBlockPointerType(QualType T) const; 900 901 /// Gets the struct used to keep track of the descriptor for pointer to 902 /// blocks. 903 QualType getBlockDescriptorType() const; 904 905 /// Gets the struct used to keep track of the extended descriptor for 906 /// pointer to blocks. 907 QualType getBlockDescriptorExtendedType() const; 908 909 void setcudaConfigureCallDecl(FunctionDecl *FD) { 910 cudaConfigureCallDecl = FD; 911 } 912 FunctionDecl *getcudaConfigureCallDecl() { 913 return cudaConfigureCallDecl; 914 } 915 916 /// Returns true iff we need copy/dispose helpers for the given type. 917 bool BlockRequiresCopying(QualType Ty, const VarDecl *D); 918 919 920 /// Returns true, if given type has a known lifetime. HasByrefExtendedLayout is set 921 /// to false in this case. If HasByrefExtendedLayout returns true, byref variable 922 /// has extended lifetime. 923 bool getByrefLifetime(QualType Ty, 924 Qualifiers::ObjCLifetime &Lifetime, 925 bool &HasByrefExtendedLayout) const; 926 927 /// \brief Return the uniqued reference to the type for an lvalue reference 928 /// to the specified type. 929 QualType getLValueReferenceType(QualType T, bool SpelledAsLValue = true) 930 const; 931 932 /// \brief Return the uniqued reference to the type for an rvalue reference 933 /// to the specified type. 934 QualType getRValueReferenceType(QualType T) const; 935 936 /// \brief Return the uniqued reference to the type for a member pointer to 937 /// the specified type in the specified class. 938 /// 939 /// The class \p Cls is a \c Type because it could be a dependent name. 940 QualType getMemberPointerType(QualType T, const Type *Cls) const; 941 942 /// \brief Return a non-unique reference to the type for a variable array of 943 /// the specified element type. 944 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 945 ArrayType::ArraySizeModifier ASM, 946 unsigned IndexTypeQuals, 947 SourceRange Brackets) const; 948 949 /// \brief Return a non-unique reference to the type for a dependently-sized 950 /// array of the specified element type. 951 /// 952 /// FIXME: We will need these to be uniqued, or at least comparable, at some 953 /// point. 954 QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts, 955 ArrayType::ArraySizeModifier ASM, 956 unsigned IndexTypeQuals, 957 SourceRange Brackets) const; 958 959 /// \brief Return a unique reference to the type for an incomplete array of 960 /// the specified element type. 961 QualType getIncompleteArrayType(QualType EltTy, 962 ArrayType::ArraySizeModifier ASM, 963 unsigned IndexTypeQuals) const; 964 965 /// \brief Return the unique reference to the type for a constant array of 966 /// the specified element type. 967 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 968 ArrayType::ArraySizeModifier ASM, 969 unsigned IndexTypeQuals) const; 970 971 /// \brief Returns a vla type where known sizes are replaced with [*]. 972 QualType getVariableArrayDecayedType(QualType Ty) const; 973 974 /// \brief Return the unique reference to a vector type of the specified 975 /// element type and size. 976 /// 977 /// \pre \p VectorType must be a built-in type. 978 QualType getVectorType(QualType VectorType, unsigned NumElts, 979 VectorType::VectorKind VecKind) const; 980 981 /// \brief Return the unique reference to an extended vector type 982 /// of the specified element type and size. 983 /// 984 /// \pre \p VectorType must be a built-in type. 985 QualType getExtVectorType(QualType VectorType, unsigned NumElts) const; 986 987 /// \pre Return a non-unique reference to the type for a dependently-sized 988 /// vector of the specified element type. 989 /// 990 /// FIXME: We will need these to be uniqued, or at least comparable, at some 991 /// point. 992 QualType getDependentSizedExtVectorType(QualType VectorType, 993 Expr *SizeExpr, 994 SourceLocation AttrLoc) const; 995 996 /// \brief Return a K&R style C function type like 'int()'. 997 QualType getFunctionNoProtoType(QualType ResultTy, 998 const FunctionType::ExtInfo &Info) const; 999 1000 QualType getFunctionNoProtoType(QualType ResultTy) const { 1001 return getFunctionNoProtoType(ResultTy, FunctionType::ExtInfo()); 1002 } 1003 1004 /// \brief Return a normal function type with a typed argument list. 1005 QualType getFunctionType(QualType ResultTy, ArrayRef<QualType> Args, 1006 const FunctionProtoType::ExtProtoInfo &EPI) const; 1007 1008 /// \brief Return the unique reference to the type for the specified type 1009 /// declaration. 1010 QualType getTypeDeclType(const TypeDecl *Decl, 1011 const TypeDecl *PrevDecl = 0) const { 1012 assert(Decl && "Passed null for Decl param"); 1013 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0); 1014 1015 if (PrevDecl) { 1016 assert(PrevDecl->TypeForDecl && "previous decl has no TypeForDecl"); 1017 Decl->TypeForDecl = PrevDecl->TypeForDecl; 1018 return QualType(PrevDecl->TypeForDecl, 0); 1019 } 1020 1021 return getTypeDeclTypeSlow(Decl); 1022 } 1023 1024 /// \brief Return the unique reference to the type for the specified 1025 /// typedef-name decl. 1026 QualType getTypedefType(const TypedefNameDecl *Decl, 1027 QualType Canon = QualType()) const; 1028 1029 QualType getRecordType(const RecordDecl *Decl) const; 1030 1031 QualType getEnumType(const EnumDecl *Decl) const; 1032 1033 QualType getInjectedClassNameType(CXXRecordDecl *Decl, QualType TST) const; 1034 1035 QualType getAttributedType(AttributedType::Kind attrKind, 1036 QualType modifiedType, 1037 QualType equivalentType); 1038 1039 QualType getSubstTemplateTypeParmType(const TemplateTypeParmType *Replaced, 1040 QualType Replacement) const; 1041 QualType getSubstTemplateTypeParmPackType( 1042 const TemplateTypeParmType *Replaced, 1043 const TemplateArgument &ArgPack); 1044 1045 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, 1046 bool ParameterPack, 1047 TemplateTypeParmDecl *ParmDecl = 0) const; 1048 1049 QualType getTemplateSpecializationType(TemplateName T, 1050 const TemplateArgument *Args, 1051 unsigned NumArgs, 1052 QualType Canon = QualType()) const; 1053 1054 QualType getCanonicalTemplateSpecializationType(TemplateName T, 1055 const TemplateArgument *Args, 1056 unsigned NumArgs) const; 1057 1058 QualType getTemplateSpecializationType(TemplateName T, 1059 const TemplateArgumentListInfo &Args, 1060 QualType Canon = QualType()) const; 1061 1062 TypeSourceInfo * 1063 getTemplateSpecializationTypeInfo(TemplateName T, SourceLocation TLoc, 1064 const TemplateArgumentListInfo &Args, 1065 QualType Canon = QualType()) const; 1066 1067 QualType getParenType(QualType NamedType) const; 1068 1069 QualType getElaboratedType(ElaboratedTypeKeyword Keyword, 1070 NestedNameSpecifier *NNS, 1071 QualType NamedType) const; 1072 QualType getDependentNameType(ElaboratedTypeKeyword Keyword, 1073 NestedNameSpecifier *NNS, 1074 const IdentifierInfo *Name, 1075 QualType Canon = QualType()) const; 1076 1077 QualType getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 1078 NestedNameSpecifier *NNS, 1079 const IdentifierInfo *Name, 1080 const TemplateArgumentListInfo &Args) const; 1081 QualType getDependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 1082 NestedNameSpecifier *NNS, 1083 const IdentifierInfo *Name, 1084 unsigned NumArgs, 1085 const TemplateArgument *Args) const; 1086 1087 QualType getPackExpansionType(QualType Pattern, 1088 Optional<unsigned> NumExpansions); 1089 1090 QualType getObjCInterfaceType(const ObjCInterfaceDecl *Decl, 1091 ObjCInterfaceDecl *PrevDecl = 0) const; 1092 1093 QualType getObjCObjectType(QualType Base, 1094 ObjCProtocolDecl * const *Protocols, 1095 unsigned NumProtocols) const; 1096 1097 /// \brief Return a ObjCObjectPointerType type for the given ObjCObjectType. 1098 QualType getObjCObjectPointerType(QualType OIT) const; 1099 1100 /// \brief GCC extension. 1101 QualType getTypeOfExprType(Expr *e) const; 1102 QualType getTypeOfType(QualType t) const; 1103 1104 /// \brief C++11 decltype. 1105 QualType getDecltypeType(Expr *e, QualType UnderlyingType) const; 1106 1107 /// \brief Unary type transforms 1108 QualType getUnaryTransformType(QualType BaseType, QualType UnderlyingType, 1109 UnaryTransformType::UTTKind UKind) const; 1110 1111 /// \brief C++11 deduced auto type. 1112 QualType getAutoType(QualType DeducedType) const; 1113 1114 /// \brief C++11 deduction pattern for 'auto' type. 1115 QualType getAutoDeductType() const; 1116 1117 /// \brief C++11 deduction pattern for 'auto &&' type. 1118 QualType getAutoRRefDeductType() const; 1119 1120 /// \brief Return the unique reference to the type for the specified TagDecl 1121 /// (struct/union/class/enum) decl. 1122 QualType getTagDeclType(const TagDecl *Decl) const; 1123 1124 /// \brief Return the unique type for "size_t" (C99 7.17), defined in 1125 /// <stddef.h>. 1126 /// 1127 /// The sizeof operator requires this (C99 6.5.3.4p4). 1128 CanQualType getSizeType() const; 1129 1130 /// \brief Return the unique type for "intmax_t" (C99 7.18.1.5), defined in 1131 /// <stdint.h>. 1132 CanQualType getIntMaxType() const; 1133 1134 /// \brief Return the unique type for "uintmax_t" (C99 7.18.1.5), defined in 1135 /// <stdint.h>. 1136 CanQualType getUIntMaxType() const; 1137 1138 /// \brief In C++, this returns the unique wchar_t type. In C99, this 1139 /// returns a type compatible with the type defined in <stddef.h> as defined 1140 /// by the target. 1141 QualType getWCharType() const { return WCharTy; } 1142 1143 /// \brief Return the type of "signed wchar_t". 1144 /// 1145 /// Used when in C++, as a GCC extension. 1146 QualType getSignedWCharType() const; 1147 1148 /// \brief Return the type of "unsigned wchar_t". 1149 /// 1150 /// Used when in C++, as a GCC extension. 1151 QualType getUnsignedWCharType() const; 1152 1153 /// \brief In C99, this returns a type compatible with the type 1154 /// defined in <stddef.h> as defined by the target. 1155 QualType getWIntType() const { return WIntTy; } 1156 1157 /// \brief Return a type compatible with "intptr_t" (C99 7.18.1.4), 1158 /// as defined by the target. 1159 QualType getIntPtrType() const; 1160 1161 /// \brief Return a type compatible with "uintptr_t" (C99 7.18.1.4), 1162 /// as defined by the target. 1163 QualType getUIntPtrType() const; 1164 1165 /// \brief Return the unique type for "ptrdiff_t" (C99 7.17) defined in 1166 /// <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 1167 QualType getPointerDiffType() const; 1168 1169 /// \brief Return the unique type for "pid_t" defined in 1170 /// <sys/types.h>. We need this to compute the correct type for vfork(). 1171 QualType getProcessIDType() const; 1172 1173 /// \brief Return the C structure type used to represent constant CFStrings. 1174 QualType getCFConstantStringType() const; 1175 1176 /// \brief Returns the C struct type for objc_super 1177 QualType getObjCSuperType() const; 1178 void setObjCSuperType(QualType ST) { ObjCSuperType = ST; } 1179 1180 /// Get the structure type used to representation CFStrings, or NULL 1181 /// if it hasn't yet been built. 1182 QualType getRawCFConstantStringType() const { 1183 if (CFConstantStringTypeDecl) 1184 return getTagDeclType(CFConstantStringTypeDecl); 1185 return QualType(); 1186 } 1187 void setCFConstantStringType(QualType T); 1188 1189 // This setter/getter represents the ObjC type for an NSConstantString. 1190 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 1191 QualType getObjCConstantStringInterface() const { 1192 return ObjCConstantStringType; 1193 } 1194 1195 QualType getObjCNSStringType() const { 1196 return ObjCNSStringType; 1197 } 1198 1199 void setObjCNSStringType(QualType T) { 1200 ObjCNSStringType = T; 1201 } 1202 1203 /// \brief Retrieve the type that \c id has been defined to, which may be 1204 /// different from the built-in \c id if \c id has been typedef'd. 1205 QualType getObjCIdRedefinitionType() const { 1206 if (ObjCIdRedefinitionType.isNull()) 1207 return getObjCIdType(); 1208 return ObjCIdRedefinitionType; 1209 } 1210 1211 /// \brief Set the user-written type that redefines \c id. 1212 void setObjCIdRedefinitionType(QualType RedefType) { 1213 ObjCIdRedefinitionType = RedefType; 1214 } 1215 1216 /// \brief Retrieve the type that \c Class has been defined to, which may be 1217 /// different from the built-in \c Class if \c Class has been typedef'd. 1218 QualType getObjCClassRedefinitionType() const { 1219 if (ObjCClassRedefinitionType.isNull()) 1220 return getObjCClassType(); 1221 return ObjCClassRedefinitionType; 1222 } 1223 1224 /// \brief Set the user-written type that redefines 'SEL'. 1225 void setObjCClassRedefinitionType(QualType RedefType) { 1226 ObjCClassRedefinitionType = RedefType; 1227 } 1228 1229 /// \brief Retrieve the type that 'SEL' has been defined to, which may be 1230 /// different from the built-in 'SEL' if 'SEL' has been typedef'd. 1231 QualType getObjCSelRedefinitionType() const { 1232 if (ObjCSelRedefinitionType.isNull()) 1233 return getObjCSelType(); 1234 return ObjCSelRedefinitionType; 1235 } 1236 1237 1238 /// \brief Set the user-written type that redefines 'SEL'. 1239 void setObjCSelRedefinitionType(QualType RedefType) { 1240 ObjCSelRedefinitionType = RedefType; 1241 } 1242 1243 /// \brief Retrieve the Objective-C "instancetype" type, if already known; 1244 /// otherwise, returns a NULL type; 1245 QualType getObjCInstanceType() { 1246 return getTypeDeclType(getObjCInstanceTypeDecl()); 1247 } 1248 1249 /// \brief Retrieve the typedef declaration corresponding to the Objective-C 1250 /// "instancetype" type. 1251 TypedefDecl *getObjCInstanceTypeDecl(); 1252 1253 /// \brief Set the type for the C FILE type. 1254 void setFILEDecl(TypeDecl *FILEDecl) { this->FILEDecl = FILEDecl; } 1255 1256 /// \brief Retrieve the C FILE type. 1257 QualType getFILEType() const { 1258 if (FILEDecl) 1259 return getTypeDeclType(FILEDecl); 1260 return QualType(); 1261 } 1262 1263 /// \brief Set the type for the C jmp_buf type. 1264 void setjmp_bufDecl(TypeDecl *jmp_bufDecl) { 1265 this->jmp_bufDecl = jmp_bufDecl; 1266 } 1267 1268 /// \brief Retrieve the C jmp_buf type. 1269 QualType getjmp_bufType() const { 1270 if (jmp_bufDecl) 1271 return getTypeDeclType(jmp_bufDecl); 1272 return QualType(); 1273 } 1274 1275 /// \brief Set the type for the C sigjmp_buf type. 1276 void setsigjmp_bufDecl(TypeDecl *sigjmp_bufDecl) { 1277 this->sigjmp_bufDecl = sigjmp_bufDecl; 1278 } 1279 1280 /// \brief Retrieve the C sigjmp_buf type. 1281 QualType getsigjmp_bufType() const { 1282 if (sigjmp_bufDecl) 1283 return getTypeDeclType(sigjmp_bufDecl); 1284 return QualType(); 1285 } 1286 1287 /// \brief Set the type for the C ucontext_t type. 1288 void setucontext_tDecl(TypeDecl *ucontext_tDecl) { 1289 this->ucontext_tDecl = ucontext_tDecl; 1290 } 1291 1292 /// \brief Retrieve the C ucontext_t type. 1293 QualType getucontext_tType() const { 1294 if (ucontext_tDecl) 1295 return getTypeDeclType(ucontext_tDecl); 1296 return QualType(); 1297 } 1298 1299 /// \brief The result type of logical operations, '<', '>', '!=', etc. 1300 QualType getLogicalOperationType() const { 1301 return getLangOpts().CPlusPlus ? BoolTy : IntTy; 1302 } 1303 1304 /// \brief Emit the Objective-CC type encoding for the given type \p T into 1305 /// \p S. 1306 /// 1307 /// If \p Field is specified then record field names are also encoded. 1308 void getObjCEncodingForType(QualType T, std::string &S, 1309 const FieldDecl *Field=0) const; 1310 1311 void getLegacyIntegralTypeEncoding(QualType &t) const; 1312 1313 /// \brief Put the string version of the type qualifiers \p QT into \p S. 1314 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 1315 std::string &S) const; 1316 1317 /// \brief Emit the encoded type for the function \p Decl into \p S. 1318 /// 1319 /// This is in the same format as Objective-C method encodings. 1320 /// 1321 /// \returns true if an error occurred (e.g., because one of the parameter 1322 /// types is incomplete), false otherwise. 1323 bool getObjCEncodingForFunctionDecl(const FunctionDecl *Decl, std::string& S); 1324 1325 /// \brief Emit the encoded type for the method declaration \p Decl into 1326 /// \p S. 1327 /// 1328 /// \returns true if an error occurred (e.g., because one of the parameter 1329 /// types is incomplete), false otherwise. 1330 bool getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S, 1331 bool Extended = false) 1332 const; 1333 1334 /// \brief Return the encoded type for this block declaration. 1335 std::string getObjCEncodingForBlock(const BlockExpr *blockExpr) const; 1336 1337 /// getObjCEncodingForPropertyDecl - Return the encoded type for 1338 /// this method declaration. If non-NULL, Container must be either 1339 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 1340 /// only be NULL when getting encodings for protocol properties. 1341 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 1342 const Decl *Container, 1343 std::string &S) const; 1344 1345 bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, 1346 ObjCProtocolDecl *rProto) const; 1347 1348 /// \brief Return the size of type \p T for Objective-C encoding purpose, 1349 /// in characters. 1350 CharUnits getObjCEncodingTypeSize(QualType T) const; 1351 1352 /// \brief Retrieve the typedef corresponding to the predefined \c id type 1353 /// in Objective-C. 1354 TypedefDecl *getObjCIdDecl() const; 1355 1356 /// \brief Represents the Objective-CC \c id type. 1357 /// 1358 /// This is set up lazily, by Sema. \c id is always a (typedef for a) 1359 /// pointer type, a pointer to a struct. 1360 QualType getObjCIdType() const { 1361 return getTypeDeclType(getObjCIdDecl()); 1362 } 1363 1364 /// \brief Retrieve the typedef corresponding to the predefined 'SEL' type 1365 /// in Objective-C. 1366 TypedefDecl *getObjCSelDecl() const; 1367 1368 /// \brief Retrieve the type that corresponds to the predefined Objective-C 1369 /// 'SEL' type. 1370 QualType getObjCSelType() const { 1371 return getTypeDeclType(getObjCSelDecl()); 1372 } 1373 1374 /// \brief Retrieve the typedef declaration corresponding to the predefined 1375 /// Objective-C 'Class' type. 1376 TypedefDecl *getObjCClassDecl() const; 1377 1378 /// \brief Represents the Objective-C \c Class type. 1379 /// 1380 /// This is set up lazily, by Sema. \c Class is always a (typedef for a) 1381 /// pointer type, a pointer to a struct. 1382 QualType getObjCClassType() const { 1383 return getTypeDeclType(getObjCClassDecl()); 1384 } 1385 1386 /// \brief Retrieve the Objective-C class declaration corresponding to 1387 /// the predefined \c Protocol class. 1388 ObjCInterfaceDecl *getObjCProtocolDecl() const; 1389 1390 /// \brief Retrieve declaration of 'BOOL' typedef 1391 TypedefDecl *getBOOLDecl() const { 1392 return BOOLDecl; 1393 } 1394 1395 /// \brief Save declaration of 'BOOL' typedef 1396 void setBOOLDecl(TypedefDecl *TD) { 1397 BOOLDecl = TD; 1398 } 1399 1400 /// \brief type of 'BOOL' type. 1401 QualType getBOOLType() const { 1402 return getTypeDeclType(getBOOLDecl()); 1403 } 1404 1405 /// \brief Retrieve the type of the Objective-C \c Protocol class. 1406 QualType getObjCProtoType() const { 1407 return getObjCInterfaceType(getObjCProtocolDecl()); 1408 } 1409 1410 /// \brief Retrieve the C type declaration corresponding to the predefined 1411 /// \c __builtin_va_list type. 1412 TypedefDecl *getBuiltinVaListDecl() const; 1413 1414 /// \brief Retrieve the type of the \c __builtin_va_list type. 1415 QualType getBuiltinVaListType() const { 1416 return getTypeDeclType(getBuiltinVaListDecl()); 1417 } 1418 1419 /// \brief Retrieve the C type declaration corresponding to the predefined 1420 /// \c __va_list_tag type used to help define the \c __builtin_va_list type 1421 /// for some targets. 1422 QualType getVaListTagType() const; 1423 1424 /// \brief Return a type with additional \c const, \c volatile, or 1425 /// \c restrict qualifiers. 1426 QualType getCVRQualifiedType(QualType T, unsigned CVR) const { 1427 return getQualifiedType(T, Qualifiers::fromCVRMask(CVR)); 1428 } 1429 1430 /// \brief Un-split a SplitQualType. 1431 QualType getQualifiedType(SplitQualType split) const { 1432 return getQualifiedType(split.Ty, split.Quals); 1433 } 1434 1435 /// \brief Return a type with additional qualifiers. 1436 QualType getQualifiedType(QualType T, Qualifiers Qs) const { 1437 if (!Qs.hasNonFastQualifiers()) 1438 return T.withFastQualifiers(Qs.getFastQualifiers()); 1439 QualifierCollector Qc(Qs); 1440 const Type *Ptr = Qc.strip(T); 1441 return getExtQualType(Ptr, Qc); 1442 } 1443 1444 /// \brief Return a type with additional qualifiers. 1445 QualType getQualifiedType(const Type *T, Qualifiers Qs) const { 1446 if (!Qs.hasNonFastQualifiers()) 1447 return QualType(T, Qs.getFastQualifiers()); 1448 return getExtQualType(T, Qs); 1449 } 1450 1451 /// \brief Return a type with the given lifetime qualifier. 1452 /// 1453 /// \pre Neither type.ObjCLifetime() nor \p lifetime may be \c OCL_None. 1454 QualType getLifetimeQualifiedType(QualType type, 1455 Qualifiers::ObjCLifetime lifetime) { 1456 assert(type.getObjCLifetime() == Qualifiers::OCL_None); 1457 assert(lifetime != Qualifiers::OCL_None); 1458 1459 Qualifiers qs; 1460 qs.addObjCLifetime(lifetime); 1461 return getQualifiedType(type, qs); 1462 } 1463 1464 DeclarationNameInfo getNameForTemplate(TemplateName Name, 1465 SourceLocation NameLoc) const; 1466 1467 TemplateName getOverloadedTemplateName(UnresolvedSetIterator Begin, 1468 UnresolvedSetIterator End) const; 1469 1470 TemplateName getQualifiedTemplateName(NestedNameSpecifier *NNS, 1471 bool TemplateKeyword, 1472 TemplateDecl *Template) const; 1473 1474 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 1475 const IdentifierInfo *Name) const; 1476 TemplateName getDependentTemplateName(NestedNameSpecifier *NNS, 1477 OverloadedOperatorKind Operator) const; 1478 TemplateName getSubstTemplateTemplateParm(TemplateTemplateParmDecl *param, 1479 TemplateName replacement) const; 1480 TemplateName getSubstTemplateTemplateParmPack(TemplateTemplateParmDecl *Param, 1481 const TemplateArgument &ArgPack) const; 1482 1483 enum GetBuiltinTypeError { 1484 GE_None, ///< No error 1485 GE_Missing_stdio, ///< Missing a type from <stdio.h> 1486 GE_Missing_setjmp, ///< Missing a type from <setjmp.h> 1487 GE_Missing_ucontext ///< Missing a type from <ucontext.h> 1488 }; 1489 1490 /// \brief Return the type for the specified builtin. 1491 /// 1492 /// If \p IntegerConstantArgs is non-null, it is filled in with a bitmask of 1493 /// arguments to the builtin that are required to be integer constant 1494 /// expressions. 1495 QualType GetBuiltinType(unsigned ID, GetBuiltinTypeError &Error, 1496 unsigned *IntegerConstantArgs = 0) const; 1497 1498private: 1499 CanQualType getFromTargetType(unsigned Type) const; 1500 std::pair<uint64_t, unsigned> getTypeInfoImpl(const Type *T) const; 1501 1502 //===--------------------------------------------------------------------===// 1503 // Type Predicates. 1504 //===--------------------------------------------------------------------===// 1505 1506public: 1507 /// \brief Return one of the GCNone, Weak or Strong Objective-C garbage 1508 /// collection attributes. 1509 Qualifiers::GC getObjCGCAttrKind(QualType Ty) const; 1510 1511 /// \brief Return true if the given vector types are of the same unqualified 1512 /// type or if they are equivalent to the same GCC vector type. 1513 /// 1514 /// \note This ignores whether they are target-specific (AltiVec or Neon) 1515 /// types. 1516 bool areCompatibleVectorTypes(QualType FirstVec, QualType SecondVec); 1517 1518 /// \brief Return true if this is an \c NSObject object with its \c NSObject 1519 /// attribute set. 1520 static bool isObjCNSObjectType(QualType Ty) { 1521 return Ty->isObjCNSObjectType(); 1522 } 1523 1524 //===--------------------------------------------------------------------===// 1525 // Type Sizing and Analysis 1526 //===--------------------------------------------------------------------===// 1527 1528 /// \brief Return the APFloat 'semantics' for the specified scalar floating 1529 /// point type. 1530 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 1531 1532 /// \brief Get the size and alignment of the specified complete type in bits. 1533 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T) const; 1534 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) const { 1535 return getTypeInfo(T.getTypePtr()); 1536 } 1537 1538 /// \brief Return the size of the specified (complete) type \p T, in bits. 1539 uint64_t getTypeSize(QualType T) const { 1540 return getTypeInfo(T).first; 1541 } 1542 uint64_t getTypeSize(const Type *T) const { 1543 return getTypeInfo(T).first; 1544 } 1545 1546 /// \brief Return the size of the character type, in bits. 1547 uint64_t getCharWidth() const { 1548 return getTypeSize(CharTy); 1549 } 1550 1551 /// \brief Convert a size in bits to a size in characters. 1552 CharUnits toCharUnitsFromBits(int64_t BitSize) const; 1553 1554 /// \brief Convert a size in characters to a size in bits. 1555 int64_t toBits(CharUnits CharSize) const; 1556 1557 /// \brief Return the size of the specified (complete) type \p T, in 1558 /// characters. 1559 CharUnits getTypeSizeInChars(QualType T) const; 1560 CharUnits getTypeSizeInChars(const Type *T) const; 1561 1562 /// \brief Return the ABI-specified alignment of a (complete) type \p T, in 1563 /// bits. 1564 unsigned getTypeAlign(QualType T) const { 1565 return getTypeInfo(T).second; 1566 } 1567 unsigned getTypeAlign(const Type *T) const { 1568 return getTypeInfo(T).second; 1569 } 1570 1571 /// \brief Return the ABI-specified alignment of a (complete) type \p T, in 1572 /// characters. 1573 CharUnits getTypeAlignInChars(QualType T) const; 1574 CharUnits getTypeAlignInChars(const Type *T) const; 1575 1576 // getTypeInfoDataSizeInChars - Return the size of a type, in chars. If the 1577 // type is a record, its data size is returned. 1578 std::pair<CharUnits, CharUnits> getTypeInfoDataSizeInChars(QualType T) const; 1579 1580 std::pair<CharUnits, CharUnits> getTypeInfoInChars(const Type *T) const; 1581 std::pair<CharUnits, CharUnits> getTypeInfoInChars(QualType T) const; 1582 1583 /// \brief Return the "preferred" alignment of the specified type \p T for 1584 /// the current target, in bits. 1585 /// 1586 /// This can be different than the ABI alignment in cases where it is 1587 /// beneficial for performance to overalign a data type. 1588 unsigned getPreferredTypeAlign(const Type *T) const; 1589 1590 /// \brief Return a conservative estimate of the alignment of the specified 1591 /// decl \p D. 1592 /// 1593 /// \pre \p D must not be a bitfield type, as bitfields do not have a valid 1594 /// alignment. 1595 /// 1596 /// If \p RefAsPointee, references are treated like their underlying type 1597 /// (for alignof), else they're treated like pointers (for CodeGen). 1598 CharUnits getDeclAlign(const Decl *D, bool RefAsPointee = false) const; 1599 1600 /// \brief Get or compute information about the layout of the specified 1601 /// record (struct/union/class) \p D, which indicates its size and field 1602 /// position information. 1603 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D) const; 1604 1605 /// \brief Get or compute information about the layout of the specified 1606 /// Objective-C interface. 1607 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) 1608 const; 1609 1610 void DumpRecordLayout(const RecordDecl *RD, raw_ostream &OS, 1611 bool Simple = false) const; 1612 1613 /// \brief Get or compute information about the layout of the specified 1614 /// Objective-C implementation. 1615 /// 1616 /// This may differ from the interface if synthesized ivars are present. 1617 const ASTRecordLayout & 1618 getASTObjCImplementationLayout(const ObjCImplementationDecl *D) const; 1619 1620 /// \brief Get our current best idea for the key function of the 1621 /// given record decl, or NULL if there isn't one. 1622 /// 1623 /// The key function is, according to the Itanium C++ ABI section 5.2.3: 1624 /// ...the first non-pure virtual function that is not inline at the 1625 /// point of class definition. 1626 /// 1627 /// Other ABIs use the same idea. However, the ARM C++ ABI ignores 1628 /// virtual functions that are defined 'inline', which means that 1629 /// the result of this computation can change. 1630 const CXXMethodDecl *getCurrentKeyFunction(const CXXRecordDecl *RD); 1631 1632 /// \brief Observe that the given method cannot be a key function. 1633 /// Checks the key-function cache for the method's class and clears it 1634 /// if matches the given declaration. 1635 /// 1636 /// This is used in ABIs where out-of-line definitions marked 1637 /// inline are not considered to be key functions. 1638 /// 1639 /// \param method should be the declaration from the class definition 1640 void setNonKeyFunction(const CXXMethodDecl *method); 1641 1642 /// Get the offset of a FieldDecl or IndirectFieldDecl, in bits. 1643 uint64_t getFieldOffset(const ValueDecl *FD) const; 1644 1645 bool isNearlyEmpty(const CXXRecordDecl *RD) const; 1646 1647 MangleContext *createMangleContext(); 1648 1649 void DeepCollectObjCIvars(const ObjCInterfaceDecl *OI, bool leafClass, 1650 SmallVectorImpl<const ObjCIvarDecl*> &Ivars) const; 1651 1652 unsigned CountNonClassIvars(const ObjCInterfaceDecl *OI) const; 1653 void CollectInheritedProtocols(const Decl *CDecl, 1654 llvm::SmallPtrSet<ObjCProtocolDecl*, 8> &Protocols); 1655 1656 //===--------------------------------------------------------------------===// 1657 // Type Operators 1658 //===--------------------------------------------------------------------===// 1659 1660 /// \brief Return the canonical (structural) type corresponding to the 1661 /// specified potentially non-canonical type \p T. 1662 /// 1663 /// The non-canonical version of a type may have many "decorated" versions of 1664 /// types. Decorators can include typedefs, 'typeof' operators, etc. The 1665 /// returned type is guaranteed to be free of any of these, allowing two 1666 /// canonical types to be compared for exact equality with a simple pointer 1667 /// comparison. 1668 CanQualType getCanonicalType(QualType T) const { 1669 return CanQualType::CreateUnsafe(T.getCanonicalType()); 1670 } 1671 1672 const Type *getCanonicalType(const Type *T) const { 1673 return T->getCanonicalTypeInternal().getTypePtr(); 1674 } 1675 1676 /// \brief Return the canonical parameter type corresponding to the specific 1677 /// potentially non-canonical one. 1678 /// 1679 /// Qualifiers are stripped off, functions are turned into function 1680 /// pointers, and arrays decay one level into pointers. 1681 CanQualType getCanonicalParamType(QualType T) const; 1682 1683 /// \brief Determine whether the given types \p T1 and \p T2 are equivalent. 1684 bool hasSameType(QualType T1, QualType T2) const { 1685 return getCanonicalType(T1) == getCanonicalType(T2); 1686 } 1687 1688 /// \brief Return this type as a completely-unqualified array type, 1689 /// capturing the qualifiers in \p Quals. 1690 /// 1691 /// This will remove the minimal amount of sugaring from the types, similar 1692 /// to the behavior of QualType::getUnqualifiedType(). 1693 /// 1694 /// \param T is the qualified type, which may be an ArrayType 1695 /// 1696 /// \param Quals will receive the full set of qualifiers that were 1697 /// applied to the array. 1698 /// 1699 /// \returns if this is an array type, the completely unqualified array type 1700 /// that corresponds to it. Otherwise, returns T.getUnqualifiedType(). 1701 QualType getUnqualifiedArrayType(QualType T, Qualifiers &Quals); 1702 1703 /// \brief Determine whether the given types are equivalent after 1704 /// cvr-qualifiers have been removed. 1705 bool hasSameUnqualifiedType(QualType T1, QualType T2) const { 1706 return getCanonicalType(T1).getTypePtr() == 1707 getCanonicalType(T2).getTypePtr(); 1708 } 1709 1710 bool UnwrapSimilarPointerTypes(QualType &T1, QualType &T2); 1711 1712 /// \brief Retrieves the "canonical" nested name specifier for a 1713 /// given nested name specifier. 1714 /// 1715 /// The canonical nested name specifier is a nested name specifier 1716 /// that uniquely identifies a type or namespace within the type 1717 /// system. For example, given: 1718 /// 1719 /// \code 1720 /// namespace N { 1721 /// struct S { 1722 /// template<typename T> struct X { typename T* type; }; 1723 /// }; 1724 /// } 1725 /// 1726 /// template<typename T> struct Y { 1727 /// typename N::S::X<T>::type member; 1728 /// }; 1729 /// \endcode 1730 /// 1731 /// Here, the nested-name-specifier for N::S::X<T>:: will be 1732 /// S::X<template-param-0-0>, since 'S' and 'X' are uniquely defined 1733 /// by declarations in the type system and the canonical type for 1734 /// the template type parameter 'T' is template-param-0-0. 1735 NestedNameSpecifier * 1736 getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) const; 1737 1738 /// \brief Retrieves the default calling convention to use for 1739 /// C++ instance methods. 1740 CallingConv getDefaultCXXMethodCallConv(bool isVariadic); 1741 1742 /// \brief Retrieves the canonical representation of the given 1743 /// calling convention. 1744 CallingConv getCanonicalCallConv(CallingConv CC) const; 1745 1746 /// \brief Determines whether two calling conventions name the same 1747 /// calling convention. 1748 bool isSameCallConv(CallingConv lcc, CallingConv rcc) { 1749 return (getCanonicalCallConv(lcc) == getCanonicalCallConv(rcc)); 1750 } 1751 1752 /// \brief Retrieves the "canonical" template name that refers to a 1753 /// given template. 1754 /// 1755 /// The canonical template name is the simplest expression that can 1756 /// be used to refer to a given template. For most templates, this 1757 /// expression is just the template declaration itself. For example, 1758 /// the template std::vector can be referred to via a variety of 1759 /// names---std::vector, \::std::vector, vector (if vector is in 1760 /// scope), etc.---but all of these names map down to the same 1761 /// TemplateDecl, which is used to form the canonical template name. 1762 /// 1763 /// Dependent template names are more interesting. Here, the 1764 /// template name could be something like T::template apply or 1765 /// std::allocator<T>::template rebind, where the nested name 1766 /// specifier itself is dependent. In this case, the canonical 1767 /// template name uses the shortest form of the dependent 1768 /// nested-name-specifier, which itself contains all canonical 1769 /// types, values, and templates. 1770 TemplateName getCanonicalTemplateName(TemplateName Name) const; 1771 1772 /// \brief Determine whether the given template names refer to the same 1773 /// template. 1774 bool hasSameTemplateName(TemplateName X, TemplateName Y); 1775 1776 /// \brief Retrieve the "canonical" template argument. 1777 /// 1778 /// The canonical template argument is the simplest template argument 1779 /// (which may be a type, value, expression, or declaration) that 1780 /// expresses the value of the argument. 1781 TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg) 1782 const; 1783 1784 /// Type Query functions. If the type is an instance of the specified class, 1785 /// return the Type pointer for the underlying maximally pretty type. This 1786 /// is a member of ASTContext because this may need to do some amount of 1787 /// canonicalization, e.g. to move type qualifiers into the element type. 1788 const ArrayType *getAsArrayType(QualType T) const; 1789 const ConstantArrayType *getAsConstantArrayType(QualType T) const { 1790 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 1791 } 1792 const VariableArrayType *getAsVariableArrayType(QualType T) const { 1793 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 1794 } 1795 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) const { 1796 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 1797 } 1798 const DependentSizedArrayType *getAsDependentSizedArrayType(QualType T) 1799 const { 1800 return dyn_cast_or_null<DependentSizedArrayType>(getAsArrayType(T)); 1801 } 1802 1803 /// \brief Return the innermost element type of an array type. 1804 /// 1805 /// For example, will return "int" for int[m][n] 1806 QualType getBaseElementType(const ArrayType *VAT) const; 1807 1808 /// \brief Return the innermost element type of a type (which needn't 1809 /// actually be an array type). 1810 QualType getBaseElementType(QualType QT) const; 1811 1812 /// \brief Return number of constant array elements. 1813 uint64_t getConstantArrayElementCount(const ConstantArrayType *CA) const; 1814 1815 /// \brief Perform adjustment on the parameter type of a function. 1816 /// 1817 /// This routine adjusts the given parameter type @p T to the actual 1818 /// parameter type used by semantic analysis (C99 6.7.5.3p[7,8], 1819 /// C++ [dcl.fct]p3). The adjusted parameter type is returned. 1820 QualType getAdjustedParameterType(QualType T) const; 1821 1822 /// \brief Retrieve the parameter type as adjusted for use in the signature 1823 /// of a function, decaying array and function types and removing top-level 1824 /// cv-qualifiers. 1825 QualType getSignatureParameterType(QualType T) const; 1826 1827 /// \brief Return the properly qualified result of decaying the specified 1828 /// array type to a pointer. 1829 /// 1830 /// This operation is non-trivial when handling typedefs etc. The canonical 1831 /// type of \p T must be an array type, this returns a pointer to a properly 1832 /// qualified element of the array. 1833 /// 1834 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 1835 QualType getArrayDecayedType(QualType T) const; 1836 1837 /// \brief Return the type that \p PromotableType will promote to: C99 1838 /// 6.3.1.1p2, assuming that \p PromotableType is a promotable integer type. 1839 QualType getPromotedIntegerType(QualType PromotableType) const; 1840 1841 /// \brief Recurses in pointer/array types until it finds an Objective-C 1842 /// retainable type and returns its ownership. 1843 Qualifiers::ObjCLifetime getInnerObjCOwnership(QualType T) const; 1844 1845 /// \brief Whether this is a promotable bitfield reference according 1846 /// to C99 6.3.1.1p2, bullet 2 (and GCC extensions). 1847 /// 1848 /// \returns the type this bit-field will promote to, or NULL if no 1849 /// promotion occurs. 1850 QualType isPromotableBitField(Expr *E) const; 1851 1852 /// \brief Return the highest ranked integer type, see C99 6.3.1.8p1. 1853 /// 1854 /// If \p LHS > \p RHS, returns 1. If \p LHS == \p RHS, returns 0. If 1855 /// \p LHS < \p RHS, return -1. 1856 int getIntegerTypeOrder(QualType LHS, QualType RHS) const; 1857 1858 /// \brief Compare the rank of the two specified floating point types, 1859 /// ignoring the domain of the type (i.e. 'double' == '_Complex double'). 1860 /// 1861 /// If \p LHS > \p RHS, returns 1. If \p LHS == \p RHS, returns 0. If 1862 /// \p LHS < \p RHS, return -1. 1863 int getFloatingTypeOrder(QualType LHS, QualType RHS) const; 1864 1865 /// \brief Return a real floating point or a complex type (based on 1866 /// \p typeDomain/\p typeSize). 1867 /// 1868 /// \param typeDomain a real floating point or complex type. 1869 /// \param typeSize a real floating point or complex type. 1870 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 1871 QualType typeDomain) const; 1872 1873 unsigned getTargetAddressSpace(QualType T) const { 1874 return getTargetAddressSpace(T.getQualifiers()); 1875 } 1876 1877 unsigned getTargetAddressSpace(Qualifiers Q) const { 1878 return getTargetAddressSpace(Q.getAddressSpace()); 1879 } 1880 1881 unsigned getTargetAddressSpace(unsigned AS) const { 1882 if (AS < LangAS::Offset || AS >= LangAS::Offset + LangAS::Count) 1883 return AS; 1884 else 1885 return (*AddrSpaceMap)[AS - LangAS::Offset]; 1886 } 1887 1888private: 1889 // Helper for integer ordering 1890 unsigned getIntegerRank(const Type *T) const; 1891 1892public: 1893 1894 //===--------------------------------------------------------------------===// 1895 // Type Compatibility Predicates 1896 //===--------------------------------------------------------------------===// 1897 1898 /// Compatibility predicates used to check assignment expressions. 1899 bool typesAreCompatible(QualType T1, QualType T2, 1900 bool CompareUnqualified = false); // C99 6.2.7p1 1901 1902 bool propertyTypesAreCompatible(QualType, QualType); 1903 bool typesAreBlockPointerCompatible(QualType, QualType); 1904 1905 bool isObjCIdType(QualType T) const { 1906 return T == getObjCIdType(); 1907 } 1908 bool isObjCClassType(QualType T) const { 1909 return T == getObjCClassType(); 1910 } 1911 bool isObjCSelType(QualType T) const { 1912 return T == getObjCSelType(); 1913 } 1914 bool QualifiedIdConformsQualifiedId(QualType LHS, QualType RHS); 1915 bool ObjCQualifiedIdTypesAreCompatible(QualType LHS, QualType RHS, 1916 bool ForCompare); 1917 1918 bool ObjCQualifiedClassTypesAreCompatible(QualType LHS, QualType RHS); 1919 1920 // Check the safety of assignment from LHS to RHS 1921 bool canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT, 1922 const ObjCObjectPointerType *RHSOPT); 1923 bool canAssignObjCInterfaces(const ObjCObjectType *LHS, 1924 const ObjCObjectType *RHS); 1925 bool canAssignObjCInterfacesInBlockPointer( 1926 const ObjCObjectPointerType *LHSOPT, 1927 const ObjCObjectPointerType *RHSOPT, 1928 bool BlockReturnType); 1929 bool areComparableObjCPointerTypes(QualType LHS, QualType RHS); 1930 QualType areCommonBaseCompatible(const ObjCObjectPointerType *LHSOPT, 1931 const ObjCObjectPointerType *RHSOPT); 1932 bool canBindObjCObjectType(QualType To, QualType From); 1933 1934 // Functions for calculating composite types 1935 QualType mergeTypes(QualType, QualType, bool OfBlockPointer=false, 1936 bool Unqualified = false, bool BlockReturnType = false); 1937 QualType mergeFunctionTypes(QualType, QualType, bool OfBlockPointer=false, 1938 bool Unqualified = false); 1939 QualType mergeFunctionArgumentTypes(QualType, QualType, 1940 bool OfBlockPointer=false, 1941 bool Unqualified = false); 1942 QualType mergeTransparentUnionType(QualType, QualType, 1943 bool OfBlockPointer=false, 1944 bool Unqualified = false); 1945 1946 QualType mergeObjCGCQualifiers(QualType, QualType); 1947 1948 bool FunctionTypesMatchOnNSConsumedAttrs( 1949 const FunctionProtoType *FromFunctionType, 1950 const FunctionProtoType *ToFunctionType); 1951 1952 void ResetObjCLayout(const ObjCContainerDecl *CD) { 1953 ObjCLayouts[CD] = 0; 1954 } 1955 1956 //===--------------------------------------------------------------------===// 1957 // Integer Predicates 1958 //===--------------------------------------------------------------------===// 1959 1960 // The width of an integer, as defined in C99 6.2.6.2. This is the number 1961 // of bits in an integer type excluding any padding bits. 1962 unsigned getIntWidth(QualType T) const; 1963 1964 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 1965 // unsigned integer type. This method takes a signed type, and returns the 1966 // corresponding unsigned integer type. 1967 QualType getCorrespondingUnsignedType(QualType T) const; 1968 1969 //===--------------------------------------------------------------------===// 1970 // Type Iterators. 1971 //===--------------------------------------------------------------------===// 1972 1973 typedef SmallVectorImpl<Type *>::iterator type_iterator; 1974 typedef SmallVectorImpl<Type *>::const_iterator const_type_iterator; 1975 1976 type_iterator types_begin() { return Types.begin(); } 1977 type_iterator types_end() { return Types.end(); } 1978 const_type_iterator types_begin() const { return Types.begin(); } 1979 const_type_iterator types_end() const { return Types.end(); } 1980 1981 //===--------------------------------------------------------------------===// 1982 // Integer Values 1983 //===--------------------------------------------------------------------===// 1984 1985 /// \brief Make an APSInt of the appropriate width and signedness for the 1986 /// given \p Value and integer \p Type. 1987 llvm::APSInt MakeIntValue(uint64_t Value, QualType Type) const { 1988 llvm::APSInt Res(getIntWidth(Type), 1989 !Type->isSignedIntegerOrEnumerationType()); 1990 Res = Value; 1991 return Res; 1992 } 1993 1994 bool isSentinelNullExpr(const Expr *E); 1995 1996 /// \brief Get the implementation of the ObjCInterfaceDecl \p D, or NULL if 1997 /// none exists. 1998 ObjCImplementationDecl *getObjCImplementation(ObjCInterfaceDecl *D); 1999 /// \brief Get the implementation of the ObjCCategoryDecl \p D, or NULL if 2000 /// none exists. 2001 ObjCCategoryImplDecl *getObjCImplementation(ObjCCategoryDecl *D); 2002 2003 /// \brief Return true if there is at least one \@implementation in the TU. 2004 bool AnyObjCImplementation() { 2005 return !ObjCImpls.empty(); 2006 } 2007 2008 /// \brief Set the implementation of ObjCInterfaceDecl. 2009 void setObjCImplementation(ObjCInterfaceDecl *IFaceD, 2010 ObjCImplementationDecl *ImplD); 2011 /// \brief Set the implementation of ObjCCategoryDecl. 2012 void setObjCImplementation(ObjCCategoryDecl *CatD, 2013 ObjCCategoryImplDecl *ImplD); 2014 2015 /// \brief Get the duplicate declaration of a ObjCMethod in the same 2016 /// interface, or null if none exists. 2017 const ObjCMethodDecl *getObjCMethodRedeclaration( 2018 const ObjCMethodDecl *MD) const { 2019 return ObjCMethodRedecls.lookup(MD); 2020 } 2021 2022 void setObjCMethodRedeclaration(const ObjCMethodDecl *MD, 2023 const ObjCMethodDecl *Redecl) { 2024 assert(!getObjCMethodRedeclaration(MD) && "MD already has a redeclaration"); 2025 ObjCMethodRedecls[MD] = Redecl; 2026 } 2027 2028 /// \brief Returns the Objective-C interface that \p ND belongs to if it is 2029 /// an Objective-C method/property/ivar etc. that is part of an interface, 2030 /// otherwise returns null. 2031 const ObjCInterfaceDecl *getObjContainingInterface(const NamedDecl *ND) const; 2032 2033 /// \brief Set the copy inialization expression of a block var decl. 2034 void setBlockVarCopyInits(VarDecl*VD, Expr* Init); 2035 /// \brief Get the copy initialization expression of the VarDecl \p VD, or 2036 /// NULL if none exists. 2037 Expr *getBlockVarCopyInits(const VarDecl* VD); 2038 2039 /// \brief Allocate an uninitialized TypeSourceInfo. 2040 /// 2041 /// The caller should initialize the memory held by TypeSourceInfo using 2042 /// the TypeLoc wrappers. 2043 /// 2044 /// \param T the type that will be the basis for type source info. This type 2045 /// should refer to how the declarator was written in source code, not to 2046 /// what type semantic analysis resolved the declarator to. 2047 /// 2048 /// \param Size the size of the type info to create, or 0 if the size 2049 /// should be calculated based on the type. 2050 TypeSourceInfo *CreateTypeSourceInfo(QualType T, unsigned Size = 0) const; 2051 2052 /// \brief Allocate a TypeSourceInfo where all locations have been 2053 /// initialized to a given location, which defaults to the empty 2054 /// location. 2055 TypeSourceInfo * 2056 getTrivialTypeSourceInfo(QualType T, 2057 SourceLocation Loc = SourceLocation()) const; 2058 2059 TypeSourceInfo *getNullTypeSourceInfo() { return &NullTypeSourceInfo; } 2060 2061 /// \brief Add a deallocation callback that will be invoked when the 2062 /// ASTContext is destroyed. 2063 /// 2064 /// \param Callback A callback function that will be invoked on destruction. 2065 /// 2066 /// \param Data Pointer data that will be provided to the callback function 2067 /// when it is called. 2068 void AddDeallocation(void (*Callback)(void*), void *Data); 2069 2070 GVALinkage GetGVALinkageForFunction(const FunctionDecl *FD); 2071 GVALinkage GetGVALinkageForVariable(const VarDecl *VD); 2072 2073 /// \brief Determines if the decl can be CodeGen'ed or deserialized from PCH 2074 /// lazily, only when used; this is only relevant for function or file scoped 2075 /// var definitions. 2076 /// 2077 /// \returns true if the function/var must be CodeGen'ed/deserialized even if 2078 /// it is not used. 2079 bool DeclMustBeEmitted(const Decl *D); 2080 2081 void addUnnamedTag(const TagDecl *Tag); 2082 int getUnnamedTagManglingNumber(const TagDecl *Tag) const; 2083 2084 /// \brief Retrieve the lambda mangling number for a lambda expression. 2085 unsigned getLambdaManglingNumber(CXXMethodDecl *CallOperator); 2086 2087 /// \brief Used by ParmVarDecl to store on the side the 2088 /// index of the parameter when it exceeds the size of the normal bitfield. 2089 void setParameterIndex(const ParmVarDecl *D, unsigned index); 2090 2091 /// \brief Used by ParmVarDecl to retrieve on the side the 2092 /// index of the parameter when it exceeds the size of the normal bitfield. 2093 unsigned getParameterIndex(const ParmVarDecl *D) const; 2094 2095 //===--------------------------------------------------------------------===// 2096 // Statistics 2097 //===--------------------------------------------------------------------===// 2098 2099 /// \brief The number of implicitly-declared default constructors. 2100 static unsigned NumImplicitDefaultConstructors; 2101 2102 /// \brief The number of implicitly-declared default constructors for 2103 /// which declarations were built. 2104 static unsigned NumImplicitDefaultConstructorsDeclared; 2105 2106 /// \brief The number of implicitly-declared copy constructors. 2107 static unsigned NumImplicitCopyConstructors; 2108 2109 /// \brief The number of implicitly-declared copy constructors for 2110 /// which declarations were built. 2111 static unsigned NumImplicitCopyConstructorsDeclared; 2112 2113 /// \brief The number of implicitly-declared move constructors. 2114 static unsigned NumImplicitMoveConstructors; 2115 2116 /// \brief The number of implicitly-declared move constructors for 2117 /// which declarations were built. 2118 static unsigned NumImplicitMoveConstructorsDeclared; 2119 2120 /// \brief The number of implicitly-declared copy assignment operators. 2121 static unsigned NumImplicitCopyAssignmentOperators; 2122 2123 /// \brief The number of implicitly-declared copy assignment operators for 2124 /// which declarations were built. 2125 static unsigned NumImplicitCopyAssignmentOperatorsDeclared; 2126 2127 /// \brief The number of implicitly-declared move assignment operators. 2128 static unsigned NumImplicitMoveAssignmentOperators; 2129 2130 /// \brief The number of implicitly-declared move assignment operators for 2131 /// which declarations were built. 2132 static unsigned NumImplicitMoveAssignmentOperatorsDeclared; 2133 2134 /// \brief The number of implicitly-declared destructors. 2135 static unsigned NumImplicitDestructors; 2136 2137 /// \brief The number of implicitly-declared destructors for which 2138 /// declarations were built. 2139 static unsigned NumImplicitDestructorsDeclared; 2140 2141private: 2142 ASTContext(const ASTContext &) LLVM_DELETED_FUNCTION; 2143 void operator=(const ASTContext &) LLVM_DELETED_FUNCTION; 2144 2145public: 2146 /// \brief Initialize built-in types. 2147 /// 2148 /// This routine may only be invoked once for a given ASTContext object. 2149 /// It is normally invoked by the ASTContext constructor. However, the 2150 /// constructor can be asked to delay initialization, which places the burden 2151 /// of calling this function on the user of that object. 2152 /// 2153 /// \param Target The target 2154 void InitBuiltinTypes(const TargetInfo &Target); 2155 2156private: 2157 void InitBuiltinType(CanQualType &R, BuiltinType::Kind K); 2158 2159 // Return the Objective-C type encoding for a given type. 2160 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 2161 bool ExpandPointedToStructures, 2162 bool ExpandStructures, 2163 const FieldDecl *Field, 2164 bool OutermostType = false, 2165 bool EncodingProperty = false, 2166 bool StructField = false, 2167 bool EncodeBlockParameters = false, 2168 bool EncodeClassNames = false, 2169 bool EncodePointerToObjCTypedef = false) const; 2170 2171 // Adds the encoding of the structure's members. 2172 void getObjCEncodingForStructureImpl(RecordDecl *RD, std::string &S, 2173 const FieldDecl *Field, 2174 bool includeVBases = true) const; 2175 2176 // Adds the encoding of a method parameter or return type. 2177 void getObjCEncodingForMethodParameter(Decl::ObjCDeclQualifier QT, 2178 QualType T, std::string& S, 2179 bool Extended) const; 2180 2181 const ASTRecordLayout & 2182 getObjCLayout(const ObjCInterfaceDecl *D, 2183 const ObjCImplementationDecl *Impl) const; 2184 2185private: 2186 /// \brief A set of deallocations that should be performed when the 2187 /// ASTContext is destroyed. 2188 SmallVector<std::pair<void (*)(void*), void *>, 16> Deallocations; 2189 2190 // FIXME: This currently contains the set of StoredDeclMaps used 2191 // by DeclContext objects. This probably should not be in ASTContext, 2192 // but we include it here so that ASTContext can quickly deallocate them. 2193 llvm::PointerIntPair<StoredDeclsMap*,1> LastSDM; 2194 2195 /// \brief A counter used to uniquely identify "blocks". 2196 mutable unsigned int UniqueBlockByRefTypeID; 2197 2198 friend class DeclContext; 2199 friend class DeclarationNameTable; 2200 void ReleaseDeclContextMaps(); 2201 2202 /// \brief A \c RecursiveASTVisitor that builds a map from nodes to their 2203 /// parents as defined by the \c RecursiveASTVisitor. 2204 /// 2205 /// Note that the relationship described here is purely in terms of AST 2206 /// traversal - there are other relationships (for example declaration context) 2207 /// in the AST that are better modeled by special matchers. 2208 /// 2209 /// FIXME: Currently only builds up the map using \c Stmt and \c Decl nodes. 2210 class ParentMapASTVisitor : public RecursiveASTVisitor<ParentMapASTVisitor> { 2211 public: 2212 /// \brief Builds and returns the translation unit's parent map. 2213 /// 2214 /// The caller takes ownership of the returned \c ParentMap. 2215 static ParentMap *buildMap(TranslationUnitDecl &TU) { 2216 ParentMapASTVisitor Visitor(new ParentMap); 2217 Visitor.TraverseDecl(&TU); 2218 return Visitor.Parents; 2219 } 2220 2221 private: 2222 typedef RecursiveASTVisitor<ParentMapASTVisitor> VisitorBase; 2223 2224 ParentMapASTVisitor(ParentMap *Parents) : Parents(Parents) { 2225 } 2226 2227 bool shouldVisitTemplateInstantiations() const { 2228 return true; 2229 } 2230 bool shouldVisitImplicitCode() const { 2231 return true; 2232 } 2233 // Disables data recursion. We intercept Traverse* methods in the RAV, which 2234 // are not triggered during data recursion. 2235 bool shouldUseDataRecursionFor(clang::Stmt *S) const { 2236 return false; 2237 } 2238 2239 template <typename T> 2240 bool TraverseNode(T *Node, bool(VisitorBase:: *traverse) (T *)) { 2241 if (Node == NULL) 2242 return true; 2243 if (ParentStack.size() > 0) 2244 // FIXME: Currently we add the same parent multiple times, for example 2245 // when we visit all subexpressions of template instantiations; this is 2246 // suboptimal, bug benign: the only way to visit those is with 2247 // hasAncestor / hasParent, and those do not create new matches. 2248 // The plan is to enable DynTypedNode to be storable in a map or hash 2249 // map. The main problem there is to implement hash functions / 2250 // comparison operators for all types that DynTypedNode supports that 2251 // do not have pointer identity. 2252 (*Parents)[Node].push_back(ParentStack.back()); 2253 ParentStack.push_back(ast_type_traits::DynTypedNode::create(*Node)); 2254 bool Result = (this ->* traverse) (Node); 2255 ParentStack.pop_back(); 2256 return Result; 2257 } 2258 2259 bool TraverseDecl(Decl *DeclNode) { 2260 return TraverseNode(DeclNode, &VisitorBase::TraverseDecl); 2261 } 2262 2263 bool TraverseStmt(Stmt *StmtNode) { 2264 return TraverseNode(StmtNode, &VisitorBase::TraverseStmt); 2265 } 2266 2267 ParentMap *Parents; 2268 llvm::SmallVector<ast_type_traits::DynTypedNode, 16> ParentStack; 2269 2270 friend class RecursiveASTVisitor<ParentMapASTVisitor>; 2271 }; 2272 2273 llvm::OwningPtr<ParentMap> AllParents; 2274}; 2275 2276/// \brief Utility function for constructing a nullary selector. 2277static inline Selector GetNullarySelector(StringRef name, ASTContext& Ctx) { 2278 IdentifierInfo* II = &Ctx.Idents.get(name); 2279 return Ctx.Selectors.getSelector(0, &II); 2280} 2281 2282/// \brief Utility function for constructing an unary selector. 2283static inline Selector GetUnarySelector(StringRef name, ASTContext& Ctx) { 2284 IdentifierInfo* II = &Ctx.Idents.get(name); 2285 return Ctx.Selectors.getSelector(1, &II); 2286} 2287 2288} // end namespace clang 2289 2290// operator new and delete aren't allowed inside namespaces. 2291 2292/// @brief Placement new for using the ASTContext's allocator. 2293/// 2294/// This placement form of operator new uses the ASTContext's allocator for 2295/// obtaining memory. 2296/// 2297/// IMPORTANT: These are also declared in clang/AST/AttrIterator.h! Any changes 2298/// here need to also be made there. 2299/// 2300/// We intentionally avoid using a nothrow specification here so that the calls 2301/// to this operator will not perform a null check on the result -- the 2302/// underlying allocator never returns null pointers. 2303/// 2304/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 2305/// @code 2306/// // Default alignment (8) 2307/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 2308/// // Specific alignment 2309/// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments); 2310/// @endcode 2311/// Please note that you cannot use delete on the pointer; it must be 2312/// deallocated using an explicit destructor call followed by 2313/// @c Context.Deallocate(Ptr). 2314/// 2315/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 2316/// @param C The ASTContext that provides the allocator. 2317/// @param Alignment The alignment of the allocated memory (if the underlying 2318/// allocator supports it). 2319/// @return The allocated memory. Could be NULL. 2320inline void *operator new(size_t Bytes, const clang::ASTContext &C, 2321 size_t Alignment) { 2322 return C.Allocate(Bytes, Alignment); 2323} 2324/// @brief Placement delete companion to the new above. 2325/// 2326/// This operator is just a companion to the new above. There is no way of 2327/// invoking it directly; see the new operator for more details. This operator 2328/// is called implicitly by the compiler if a placement new expression using 2329/// the ASTContext throws in the object constructor. 2330inline void operator delete(void *Ptr, const clang::ASTContext &C, size_t) { 2331 C.Deallocate(Ptr); 2332} 2333 2334/// This placement form of operator new[] uses the ASTContext's allocator for 2335/// obtaining memory. 2336/// 2337/// We intentionally avoid using a nothrow specification here so that the calls 2338/// to this operator will not perform a null check on the result -- the 2339/// underlying allocator never returns null pointers. 2340/// 2341/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 2342/// @code 2343/// // Default alignment (8) 2344/// char *data = new (Context) char[10]; 2345/// // Specific alignment 2346/// char *data = new (Context, 4) char[10]; 2347/// @endcode 2348/// Please note that you cannot use delete on the pointer; it must be 2349/// deallocated using an explicit destructor call followed by 2350/// @c Context.Deallocate(Ptr). 2351/// 2352/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 2353/// @param C The ASTContext that provides the allocator. 2354/// @param Alignment The alignment of the allocated memory (if the underlying 2355/// allocator supports it). 2356/// @return The allocated memory. Could be NULL. 2357inline void *operator new[](size_t Bytes, const clang::ASTContext& C, 2358 size_t Alignment = 8) { 2359 return C.Allocate(Bytes, Alignment); 2360} 2361 2362/// @brief Placement delete[] companion to the new[] above. 2363/// 2364/// This operator is just a companion to the new[] above. There is no way of 2365/// invoking it directly; see the new[] operator for more details. This operator 2366/// is called implicitly by the compiler if a placement new[] expression using 2367/// the ASTContext throws in the object constructor. 2368inline void operator delete[](void *Ptr, const clang::ASTContext &C, size_t) { 2369 C.Deallocate(Ptr); 2370} 2371 2372#endif 2373