ASTContext.h revision 55f6b14230c94272efbbcdd89a92224c8db9f225
1356b8defb51adf939e0e8c114c189d95d923f754Steve Block//===--- ASTContext.h - Context to hold long-lived AST nodes ----*- C++ -*-===// 2356b8defb51adf939e0e8c114c189d95d923f754Steve Block// 3356b8defb51adf939e0e8c114c189d95d923f754Steve Block// The LLVM Compiler Infrastructure 4356b8defb51adf939e0e8c114c189d95d923f754Steve Block// 5356b8defb51adf939e0e8c114c189d95d923f754Steve Block// This file is distributed under the University of Illinois Open Source 6356b8defb51adf939e0e8c114c189d95d923f754Steve Block// License. See LICENSE.TXT for details. 7356b8defb51adf939e0e8c114c189d95d923f754Steve Block// 8356b8defb51adf939e0e8c114c189d95d923f754Steve Block//===----------------------------------------------------------------------===// 9356b8defb51adf939e0e8c114c189d95d923f754Steve Block// 10356b8defb51adf939e0e8c114c189d95d923f754Steve Block// This file defines the ASTContext interface. 11356b8defb51adf939e0e8c114c189d95d923f754Steve Block// 12356b8defb51adf939e0e8c114c189d95d923f754Steve Block//===----------------------------------------------------------------------===// 13356b8defb51adf939e0e8c114c189d95d923f754Steve Block 14356b8defb51adf939e0e8c114c189d95d923f754Steve Block#ifndef LLVM_CLANG_AST_ASTCONTEXT_H 15356b8defb51adf939e0e8c114c189d95d923f754Steve Block#define LLVM_CLANG_AST_ASTCONTEXT_H 16356b8defb51adf939e0e8c114c189d95d923f754Steve Block 17356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include "clang/Basic/IdentifierTable.h" 18356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include "clang/Basic/LangOptions.h" 19356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include "clang/AST/Builtins.h" 20356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include "clang/AST/DeclarationName.h" 21356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include "clang/AST/DeclBase.h" 22356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include "clang/AST/Type.h" 23356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include "clang/Basic/SourceLocation.h" 24356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include "llvm/ADT/DenseMap.h" 25356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include "llvm/ADT/FoldingSet.h" 26356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include "llvm/Bitcode/SerializationFwd.h" 27356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include "llvm/Support/Allocator.h" 28356b8defb51adf939e0e8c114c189d95d923f754Steve Block#include <vector> 29356b8defb51adf939e0e8c114c189d95d923f754Steve Block 30356b8defb51adf939e0e8c114c189d95d923f754Steve Blocknamespace llvm { 31356b8defb51adf939e0e8c114c189d95d923f754Steve Block struct fltSemantics; 32356b8defb51adf939e0e8c114c189d95d923f754Steve Block} 33356b8defb51adf939e0e8c114c189d95d923f754Steve Block 34356b8defb51adf939e0e8c114c189d95d923f754Steve Blocknamespace clang { 35356b8defb51adf939e0e8c114c189d95d923f754Steve Block class ASTRecordLayout; 36356b8defb51adf939e0e8c114c189d95d923f754Steve Block class Expr; 37356b8defb51adf939e0e8c114c189d95d923f754Steve Block class IdentifierTable; 38356b8defb51adf939e0e8c114c189d95d923f754Steve Block class SelectorTable; 39356b8defb51adf939e0e8c114c189d95d923f754Steve Block class SourceManager; 40356b8defb51adf939e0e8c114c189d95d923f754Steve Block class TargetInfo; 41356b8defb51adf939e0e8c114c189d95d923f754Steve Block // Decls 42356b8defb51adf939e0e8c114c189d95d923f754Steve Block class Decl; 43356b8defb51adf939e0e8c114c189d95d923f754Steve Block class ObjCPropertyDecl; 44356b8defb51adf939e0e8c114c189d95d923f754Steve Block class RecordDecl; 45356b8defb51adf939e0e8c114c189d95d923f754Steve Block class TagDecl; 46356b8defb51adf939e0e8c114c189d95d923f754Steve Block class TranslationUnitDecl; 47356b8defb51adf939e0e8c114c189d95d923f754Steve Block class TypeDecl; 48356b8defb51adf939e0e8c114c189d95d923f754Steve Block class TypedefDecl; 49356b8defb51adf939e0e8c114c189d95d923f754Steve Block class TemplateTypeParmDecl; 50356b8defb51adf939e0e8c114c189d95d923f754Steve Block class FieldDecl; 51356b8defb51adf939e0e8c114c189d95d923f754Steve Block class ObjCIvarRefExpr; 52356b8defb51adf939e0e8c114c189d95d923f754Steve Block class ObjCIvarDecl; 53356b8defb51adf939e0e8c114c189d95d923f754Steve Block 54356b8defb51adf939e0e8c114c189d95d923f754Steve Block/// ASTContext - This class holds long-lived AST nodes (such as types and 55356b8defb51adf939e0e8c114c189d95d923f754Steve Block/// decls) that can be referred to throughout the semantic analysis of a file. 56356b8defb51adf939e0e8c114c189d95d923f754Steve Blockclass ASTContext { 57356b8defb51adf939e0e8c114c189d95d923f754Steve Block std::vector<Type*> Types; 58356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<ASQualType> ASQualTypes; 59356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<ComplexType> ComplexTypes; 60356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<PointerType> PointerTypes; 61356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<BlockPointerType> BlockPointerTypes; 62356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<ReferenceType> ReferenceTypes; 63356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<MemberPointerType> MemberPointerTypes; 64356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<ConstantArrayType> ConstantArrayTypes; 65356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<IncompleteArrayType> IncompleteArrayTypes; 66356b8defb51adf939e0e8c114c189d95d923f754Steve Block std::vector<VariableArrayType*> VariableArrayTypes; 67356b8defb51adf939e0e8c114c189d95d923f754Steve Block std::vector<DependentSizedArrayType*> DependentSizedArrayTypes; 68356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<VectorType> VectorTypes; 69356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<FunctionTypeNoProto> FunctionTypeNoProtos; 70356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<FunctionTypeProto> FunctionTypeProtos; 71356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes; 72356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<ClassTemplateSpecializationType> 73356b8defb51adf939e0e8c114c189d95d923f754Steve Block ClassTemplateSpecializationTypes; 74356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<ObjCQualifiedInterfaceType> ObjCQualifiedInterfaceTypes; 75356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::FoldingSet<ObjCQualifiedIdType> ObjCQualifiedIdTypes; 76356b8defb51adf939e0e8c114c189d95d923f754Steve Block /// ASTRecordLayouts - A cache mapping from RecordDecls to ASTRecordLayouts. 77356b8defb51adf939e0e8c114c189d95d923f754Steve Block /// This is lazily created. This is intentionally not serialized. 78356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*> ASTRecordLayouts; 79356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::DenseMap<const ObjCInterfaceDecl*, 80356b8defb51adf939e0e8c114c189d95d923f754Steve Block const ASTRecordLayout*> ASTObjCInterfaces; 81356b8defb51adf939e0e8c114c189d95d923f754Steve Block 82356b8defb51adf939e0e8c114c189d95d923f754Steve Block // FIXME: Shouldn't ASTRecordForInterface/ASTFieldForIvarRef and 83356b8defb51adf939e0e8c114c189d95d923f754Steve Block // addRecordToClass/getFieldDecl be part of the backend (i.e. CodeGenTypes and 84356b8defb51adf939e0e8c114c189d95d923f754Steve Block // CodeGenFunction)? 85356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::DenseMap<const ObjCInterfaceDecl*, 86356b8defb51adf939e0e8c114c189d95d923f754Steve Block const RecordDecl*> ASTRecordForInterface; 87356b8defb51adf939e0e8c114c189d95d923f754Steve Block llvm::DenseMap<const ObjCIvarRefExpr*, const FieldDecl*> ASTFieldForIvarRef; 88356b8defb51adf939e0e8c114c189d95d923f754Steve Block 89356b8defb51adf939e0e8c114c189d95d923f754Steve Block /// BuiltinVaListType - built-in va list type. 90356b8defb51adf939e0e8c114c189d95d923f754Steve Block /// This is initially null and set by Sema::LazilyCreateBuiltin when 91356b8defb51adf939e0e8c114c189d95d923f754Steve Block /// a builtin that takes a valist is encountered. 92356b8defb51adf939e0e8c114c189d95d923f754Steve Block QualType BuiltinVaListType; 93356b8defb51adf939e0e8c114c189d95d923f754Steve Block 94356b8defb51adf939e0e8c114c189d95d923f754Steve Block /// ObjCIdType - a pseudo built-in typedef type (set by Sema). 95356b8defb51adf939e0e8c114c189d95d923f754Steve Block QualType ObjCIdType; 96356b8defb51adf939e0e8c114c189d95d923f754Steve Block const RecordType *IdStructType; 97356b8defb51adf939e0e8c114c189d95d923f754Steve Block 98356b8defb51adf939e0e8c114c189d95d923f754Steve Block /// ObjCSelType - another pseudo built-in typedef type (set by Sema). 99356b8defb51adf939e0e8c114c189d95d923f754Steve Block QualType ObjCSelType; 100356b8defb51adf939e0e8c114c189d95d923f754Steve Block const RecordType *SelStructType; 101356b8defb51adf939e0e8c114c189d95d923f754Steve Block 102356b8defb51adf939e0e8c114c189d95d923f754Steve Block /// ObjCProtoType - another pseudo built-in typedef type (set by Sema). 103356b8defb51adf939e0e8c114c189d95d923f754Steve Block QualType ObjCProtoType; 104356b8defb51adf939e0e8c114c189d95d923f754Steve Block const RecordType *ProtoStructType; 105356b8defb51adf939e0e8c114c189d95d923f754Steve Block 106356b8defb51adf939e0e8c114c189d95d923f754Steve Block /// ObjCClassType - another pseudo built-in typedef type (set by Sema). 107356b8defb51adf939e0e8c114c189d95d923f754Steve Block QualType ObjCClassType; 108356b8defb51adf939e0e8c114c189d95d923f754Steve Block const RecordType *ClassStructType; 109356b8defb51adf939e0e8c114c189d95d923f754Steve Block 110356b8defb51adf939e0e8c114c189d95d923f754Steve Block QualType ObjCConstantStringType; 111356b8defb51adf939e0e8c114c189d95d923f754Steve Block RecordDecl *CFConstantStringTypeDecl; 112356b8defb51adf939e0e8c114c189d95d923f754Steve Block 113356b8defb51adf939e0e8c114c189d95d923f754Steve Block RecordDecl *ObjCFastEnumerationStateTypeDecl; 114 115 TranslationUnitDecl *TUDecl; 116 117 /// SourceMgr - The associated SourceManager object. 118 SourceManager &SourceMgr; 119 120 /// LangOpts - The language options used to create the AST associated with 121 /// this ASTContext object. 122 LangOptions LangOpts; 123 124 /// MallocAlloc/BumpAlloc - The allocator objects used to create AST objects. 125 bool FreeMemory; 126 llvm::MallocAllocator MallocAlloc; 127 llvm::BumpPtrAllocator BumpAlloc; 128public: 129 TargetInfo &Target; 130 IdentifierTable &Idents; 131 SelectorTable &Selectors; 132 DeclarationNameTable DeclarationNames; 133 134 SourceManager& getSourceManager() { return SourceMgr; } 135 void *Allocate(unsigned Size, unsigned Align = 8) { 136 return FreeMemory ? MallocAlloc.Allocate(Size, Align) : 137 BumpAlloc.Allocate(Size, Align); 138 } 139 void Deallocate(void *Ptr) { 140 if (FreeMemory) 141 MallocAlloc.Deallocate(Ptr); 142 } 143 const LangOptions& getLangOptions() const { return LangOpts; } 144 145 FullSourceLoc getFullLoc(SourceLocation Loc) const { 146 return FullSourceLoc(Loc,SourceMgr); 147 } 148 149 TranslationUnitDecl *getTranslationUnitDecl() const { return TUDecl; } 150 151 /// This is intentionally not serialized. It is populated by the 152 /// ASTContext ctor, and there are no external pointers/references to 153 /// internal variables of BuiltinInfo. 154 Builtin::Context BuiltinInfo; 155 156 // Builtin Types. 157 QualType VoidTy; 158 QualType BoolTy; 159 QualType CharTy; 160 QualType WCharTy; // [C++ 3.9.1p5] 161 QualType SignedCharTy, ShortTy, IntTy, LongTy, LongLongTy; 162 QualType UnsignedCharTy, UnsignedShortTy, UnsignedIntTy, UnsignedLongTy; 163 QualType UnsignedLongLongTy; 164 QualType FloatTy, DoubleTy, LongDoubleTy; 165 QualType FloatComplexTy, DoubleComplexTy, LongDoubleComplexTy; 166 QualType VoidPtrTy; 167 QualType OverloadTy; 168 QualType DependentTy; 169 170 ASTContext(const LangOptions& LOpts, SourceManager &SM, TargetInfo &t, 171 IdentifierTable &idents, SelectorTable &sels, 172 bool FreeMemory = true, unsigned size_reserve=0); 173 174 ~ASTContext(); 175 176 void PrintStats() const; 177 const std::vector<Type*>& getTypes() const { return Types; } 178 179 //===--------------------------------------------------------------------===// 180 // Type Constructors 181 //===--------------------------------------------------------------------===// 182 183 /// getASQualType - Return the uniqued reference to the type for an address 184 /// space qualified type with the specified type and address space. The 185 /// resulting type has a union of the qualifiers from T and the address space. 186 // If T already has an address space specifier, it is silently replaced. 187 QualType getASQualType(QualType T, unsigned AddressSpace); 188 189 /// getComplexType - Return the uniqued reference to the type for a complex 190 /// number with the specified element type. 191 QualType getComplexType(QualType T); 192 193 /// getPointerType - Return the uniqued reference to the type for a pointer to 194 /// the specified type. 195 QualType getPointerType(QualType T); 196 197 /// getBlockPointerType - Return the uniqued reference to the type for a block 198 /// of the specified type. 199 QualType getBlockPointerType(QualType T); 200 201 /// getReferenceType - Return the uniqued reference to the type for a 202 /// reference to the specified type. 203 QualType getReferenceType(QualType T); 204 205 /// getMemberPointerType - Return the uniqued reference to the type for a 206 /// member pointer to the specified type in the specified class. The class 207 /// is a Type because it could be a dependent name. 208 QualType getMemberPointerType(QualType T, const Type *Cls); 209 210 /// getVariableArrayType - Returns a non-unique reference to the type for a 211 /// variable array of the specified element type. 212 QualType getVariableArrayType(QualType EltTy, Expr *NumElts, 213 ArrayType::ArraySizeModifier ASM, 214 unsigned EltTypeQuals); 215 216 /// getDependentSizedArrayType - Returns a non-unique reference to 217 /// the type for a dependently-sized array of the specified element 218 /// type. FIXME: We will need these to be uniqued, or at least 219 /// comparable, at some point. 220 QualType getDependentSizedArrayType(QualType EltTy, Expr *NumElts, 221 ArrayType::ArraySizeModifier ASM, 222 unsigned EltTypeQuals); 223 224 /// getIncompleteArrayType - Returns a unique reference to the type for a 225 /// incomplete array of the specified element type. 226 QualType getIncompleteArrayType(QualType EltTy, 227 ArrayType::ArraySizeModifier ASM, 228 unsigned EltTypeQuals); 229 230 /// getConstantArrayType - Return the unique reference to the type for a 231 /// constant array of the specified element type. 232 QualType getConstantArrayType(QualType EltTy, const llvm::APInt &ArySize, 233 ArrayType::ArraySizeModifier ASM, 234 unsigned EltTypeQuals); 235 236 /// getVectorType - Return the unique reference to a vector type of 237 /// the specified element type and size. VectorType must be a built-in type. 238 QualType getVectorType(QualType VectorType, unsigned NumElts); 239 240 /// getExtVectorType - Return the unique reference to an extended vector type 241 /// of the specified element type and size. VectorType must be a built-in 242 /// type. 243 QualType getExtVectorType(QualType VectorType, unsigned NumElts); 244 245 /// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'. 246 /// 247 QualType getFunctionTypeNoProto(QualType ResultTy); 248 249 /// getFunctionType - Return a normal function type with a typed argument 250 /// list. isVariadic indicates whether the argument list includes '...'. 251 QualType getFunctionType(QualType ResultTy, const QualType *ArgArray, 252 unsigned NumArgs, bool isVariadic, 253 unsigned TypeQuals); 254 255 /// getTypeDeclType - Return the unique reference to the type for 256 /// the specified type declaration. 257 QualType getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl=0); 258 259 /// getTypedefType - Return the unique reference to the type for the 260 /// specified typename decl. 261 QualType getTypedefType(TypedefDecl *Decl); 262 QualType getObjCInterfaceType(ObjCInterfaceDecl *Decl); 263 264 QualType getTemplateTypeParmType(unsigned Depth, unsigned Index, 265 IdentifierInfo *Name = 0); 266 267 QualType getClassTemplateSpecializationType(TemplateDecl *Template, 268 unsigned NumArgs, 269 uintptr_t *Args, bool *ArgIsType, 270 QualType Canon); 271 272 /// getObjCQualifiedInterfaceType - Return a 273 /// ObjCQualifiedInterfaceType type for the given interface decl and 274 /// the conforming protocol list. 275 QualType getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl, 276 ObjCProtocolDecl **ProtocolList, unsigned NumProtocols); 277 278 /// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for a 279 /// given 'id' and conforming protocol list. 280 QualType getObjCQualifiedIdType(ObjCProtocolDecl **ProtocolList, 281 unsigned NumProtocols); 282 283 284 /// getTypeOfType - GCC extension. 285 QualType getTypeOfExpr(Expr *e); 286 QualType getTypeOfType(QualType t); 287 288 /// getTagDeclType - Return the unique reference to the type for the 289 /// specified TagDecl (struct/union/class/enum) decl. 290 QualType getTagDeclType(TagDecl *Decl); 291 292 /// getSizeType - Return the unique type for "size_t" (C99 7.17), defined 293 /// in <stddef.h>. The sizeof operator requires this (C99 6.5.3.4p4). 294 QualType getSizeType() const; 295 296 /// getWCharType - Return the unique type for "wchar_t" (C99 7.17), defined 297 /// in <stddef.h>. Wide strings require this (C99 6.4.5p5). 298 QualType getWCharType() const; 299 300 /// getSignedWCharType - Return the type of "signed wchar_t". 301 /// Used when in C++, as a GCC extension. 302 QualType getSignedWCharType() const; 303 304 /// getUnsignedWCharType - Return the type of "unsigned wchar_t". 305 /// Used when in C++, as a GCC extension. 306 QualType getUnsignedWCharType() const; 307 308 /// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?) 309 /// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9). 310 QualType getPointerDiffType() const; 311 312 // getCFConstantStringType - Return the C structure type used to represent 313 // constant CFStrings. 314 QualType getCFConstantStringType(); 315 316 // This setter/getter represents the ObjC type for an NSConstantString. 317 void setObjCConstantStringInterface(ObjCInterfaceDecl *Decl); 318 QualType getObjCConstantStringInterface() const { 319 return ObjCConstantStringType; 320 } 321 322 //// This gets the struct used to keep track of fast enumerations. 323 QualType getObjCFastEnumerationStateType(); 324 325 /// getObjCEncodingForType - Emit the ObjC type encoding for the 326 /// given type into \arg S. If \arg NameFields is specified then 327 /// record field names are also encoded. 328 void getObjCEncodingForType(QualType t, std::string &S, 329 FieldDecl *Field=NULL) const; 330 331 void getLegacyIntegralTypeEncoding(QualType &t) const; 332 333 // Put the string version of type qualifiers into S. 334 void getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT, 335 std::string &S) const; 336 337 /// getObjCEncodingForMethodDecl - Return the encoded type for this method 338 /// declaration. 339 void getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl, std::string &S); 340 341 /// getObjCEncodingForPropertyDecl - Return the encoded type for 342 /// this method declaration. If non-NULL, Container must be either 343 /// an ObjCCategoryImplDecl or ObjCImplementationDecl; it should 344 /// only be NULL when getting encodings for protocol properties. 345 void getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD, 346 const Decl *Container, 347 std::string &S); 348 349 /// getObjCEncodingTypeSize returns size of type for objective-c encoding 350 /// purpose. 351 int getObjCEncodingTypeSize(QualType t); 352 353 /// This setter/getter represents the ObjC 'id' type. It is setup lazily, by 354 /// Sema. id is always a (typedef for a) pointer type, a pointer to a struct. 355 QualType getObjCIdType() const { return ObjCIdType; } 356 void setObjCIdType(TypedefDecl *Decl); 357 358 void setObjCSelType(TypedefDecl *Decl); 359 QualType getObjCSelType() const { return ObjCSelType; } 360 361 void setObjCProtoType(QualType QT); 362 QualType getObjCProtoType() const { return ObjCProtoType; } 363 364 /// This setter/getter repreents the ObjC 'Class' type. It is setup lazily, by 365 /// Sema. 'Class' is always a (typedef for a) pointer type, a pointer to a 366 /// struct. 367 QualType getObjCClassType() const { return ObjCClassType; } 368 void setObjCClassType(TypedefDecl *Decl); 369 370 void setBuiltinVaListType(QualType T); 371 QualType getBuiltinVaListType() const { return BuiltinVaListType; } 372 373private: 374 QualType getFromTargetType(unsigned Type) const; 375 376 //===--------------------------------------------------------------------===// 377 // Type Predicates. 378 //===--------------------------------------------------------------------===// 379 380public: 381 /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer 382 /// to an object type. This includes "id" and "Class" (two 'special' pointers 383 /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified 384 /// ID type). 385 bool isObjCObjectPointerType(QualType Ty) const; 386 387 /// isObjCNSObjectType - Return true if this is an NSObject object with 388 /// its NSObject attribute set. 389 bool isObjCNSObjectType(QualType Ty) const; 390 391 //===--------------------------------------------------------------------===// 392 // Type Sizing and Analysis 393 //===--------------------------------------------------------------------===// 394 395 /// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified 396 /// scalar floating point type. 397 const llvm::fltSemantics &getFloatTypeSemantics(QualType T) const; 398 399 /// getTypeInfo - Get the size and alignment of the specified complete type in 400 /// bits. 401 std::pair<uint64_t, unsigned> getTypeInfo(const Type *T); 402 std::pair<uint64_t, unsigned> getTypeInfo(QualType T) { 403 return getTypeInfo(T.getTypePtr()); 404 } 405 406 /// getTypeSize - Return the size of the specified type, in bits. This method 407 /// does not work on incomplete types. 408 uint64_t getTypeSize(QualType T) { 409 return getTypeInfo(T).first; 410 } 411 uint64_t getTypeSize(const Type *T) { 412 return getTypeInfo(T).first; 413 } 414 415 /// getTypeAlign - Return the ABI-specified alignment of a type, in bits. 416 /// This method does not work on incomplete types. 417 unsigned getTypeAlign(QualType T) { 418 return getTypeInfo(T).second; 419 } 420 unsigned getTypeAlign(const Type *T) { 421 return getTypeInfo(T).second; 422 } 423 424 /// getPreferredTypeAlign - Return the "preferred" alignment of the specified 425 /// type for the current target in bits. This can be different than the ABI 426 /// alignment in cases where it is beneficial for performance to overalign 427 /// a data type. 428 unsigned getPreferredTypeAlign(const Type *T); 429 430 /// getDeclAlign - Return the alignment of the specified decl that should be 431 /// returned by __alignof(). Note that bitfields do not have a valid 432 /// alignment, so this method will assert on them. 433 unsigned getDeclAlign(const Decl *D); 434 435 /// getASTRecordLayout - Get or compute information about the layout of the 436 /// specified record (struct/union/class), which indicates its size and field 437 /// position information. 438 const ASTRecordLayout &getASTRecordLayout(const RecordDecl *D); 439 440 const ASTRecordLayout &getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D); 441 const RecordDecl *addRecordToClass(const ObjCInterfaceDecl *D); 442 const FieldDecl *getFieldDecl(const ObjCIvarRefExpr *MRef) { 443 llvm::DenseMap<const ObjCIvarRefExpr *, const FieldDecl*>::iterator I 444 = ASTFieldForIvarRef.find(MRef); 445 assert (I != ASTFieldForIvarRef.end() && "Unable to find field_decl"); 446 return I->second; 447 } 448 void setFieldDecl(const ObjCInterfaceDecl *OI, 449 const ObjCIvarDecl *Ivar, 450 const ObjCIvarRefExpr *MRef); 451 //===--------------------------------------------------------------------===// 452 // Type Operators 453 //===--------------------------------------------------------------------===// 454 455 /// getCanonicalType - Return the canonical (structural) type corresponding to 456 /// the specified potentially non-canonical type. The non-canonical version 457 /// of a type may have many "decorated" versions of types. Decorators can 458 /// include typedefs, 'typeof' operators, etc. The returned type is guaranteed 459 /// to be free of any of these, allowing two canonical types to be compared 460 /// for exact equality with a simple pointer comparison. 461 QualType getCanonicalType(QualType T); 462 const Type *getCanonicalType(const Type *T) { 463 return T->getCanonicalTypeInternal().getTypePtr(); 464 } 465 466 /// \brief Retrieves the "canonical" declaration of the given tag 467 /// declaration. 468 /// 469 /// The canonical declaration for the given tag declaration is 470 /// either the definition of the tag (if it is a complete type) or 471 /// the first declaration of that tag. 472 TagDecl *getCanonicalDecl(TagDecl *Tag) { 473 QualType T = getTagDeclType(Tag); 474 return cast<TagDecl>(cast<TagType>(T)->getDecl()); 475 } 476 477 /// Type Query functions. If the type is an instance of the specified class, 478 /// return the Type pointer for the underlying maximally pretty type. This 479 /// is a member of ASTContext because this may need to do some amount of 480 /// canonicalization, e.g. to move type qualifiers into the element type. 481 const ArrayType *getAsArrayType(QualType T); 482 const ConstantArrayType *getAsConstantArrayType(QualType T) { 483 return dyn_cast_or_null<ConstantArrayType>(getAsArrayType(T)); 484 } 485 const VariableArrayType *getAsVariableArrayType(QualType T) { 486 return dyn_cast_or_null<VariableArrayType>(getAsArrayType(T)); 487 } 488 const IncompleteArrayType *getAsIncompleteArrayType(QualType T) { 489 return dyn_cast_or_null<IncompleteArrayType>(getAsArrayType(T)); 490 } 491 492 /// getBaseElementType - Returns the innermost element type of a variable 493 /// length array type. For example, will return "int" for int[m][n] 494 QualType getBaseElementType(const VariableArrayType *VAT); 495 496 /// getArrayDecayedType - Return the properly qualified result of decaying the 497 /// specified array type to a pointer. This operation is non-trivial when 498 /// handling typedefs etc. The canonical type of "T" must be an array type, 499 /// this returns a pointer to a properly qualified element of the array. 500 /// 501 /// See C99 6.7.5.3p7 and C99 6.3.2.1p3. 502 QualType getArrayDecayedType(QualType T); 503 504 /// getIntegerTypeOrder - Returns the highest ranked integer type: 505 /// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If 506 /// LHS < RHS, return -1. 507 int getIntegerTypeOrder(QualType LHS, QualType RHS); 508 509 /// getFloatingTypeOrder - Compare the rank of the two specified floating 510 /// point types, ignoring the domain of the type (i.e. 'double' == 511 /// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If 512 /// LHS < RHS, return -1. 513 int getFloatingTypeOrder(QualType LHS, QualType RHS); 514 515 /// getFloatingTypeOfSizeWithinDomain - Returns a real floating 516 /// point or a complex type (based on typeDomain/typeSize). 517 /// 'typeDomain' is a real floating point or complex type. 518 /// 'typeSize' is a real floating point or complex type. 519 QualType getFloatingTypeOfSizeWithinDomain(QualType typeSize, 520 QualType typeDomain) const; 521 522 //===--------------------------------------------------------------------===// 523 // Type Compatibility Predicates 524 //===--------------------------------------------------------------------===// 525 526 /// Compatibility predicates used to check assignment expressions. 527 bool typesAreCompatible(QualType, QualType); // C99 6.2.7p1 528 bool typesAreBlockCompatible(QualType lhs, QualType rhs); 529 530 bool isObjCIdType(QualType T) const { 531 if (!IdStructType) // ObjC isn't enabled 532 return false; 533 return T->getAsStructureType() == IdStructType; 534 } 535 bool isObjCClassType(QualType T) const { 536 if (!ClassStructType) // ObjC isn't enabled 537 return false; 538 return T->getAsStructureType() == ClassStructType; 539 } 540 bool isObjCSelType(QualType T) const { 541 assert(SelStructType && "isObjCSelType used before 'SEL' type is built"); 542 return T->getAsStructureType() == SelStructType; 543 } 544 545 // Check the safety of assignment from LHS to RHS 546 bool canAssignObjCInterfaces(const ObjCInterfaceType *LHS, 547 const ObjCInterfaceType *RHS); 548 549 // Functions for calculating composite types 550 QualType mergeTypes(QualType, QualType); 551 QualType mergeFunctionTypes(QualType, QualType); 552 553 //===--------------------------------------------------------------------===// 554 // Integer Predicates 555 //===--------------------------------------------------------------------===// 556 557 // The width of an integer, as defined in C99 6.2.6.2. This is the number 558 // of bits in an integer type excluding any padding bits. 559 unsigned getIntWidth(QualType T); 560 561 // Per C99 6.2.5p6, for every signed integer type, there is a corresponding 562 // unsigned integer type. This method takes a signed type, and returns the 563 // corresponding unsigned integer type. 564 QualType getCorrespondingUnsignedType(QualType T); 565 566 //===--------------------------------------------------------------------===// 567 // Type Iterators. 568 //===--------------------------------------------------------------------===// 569 570 typedef std::vector<Type*>::iterator type_iterator; 571 typedef std::vector<Type*>::const_iterator const_type_iterator; 572 573 type_iterator types_begin() { return Types.begin(); } 574 type_iterator types_end() { return Types.end(); } 575 const_type_iterator types_begin() const { return Types.begin(); } 576 const_type_iterator types_end() const { return Types.end(); } 577 578 //===--------------------------------------------------------------------===// 579 // Serialization 580 //===--------------------------------------------------------------------===// 581 582 void Emit(llvm::Serializer& S) const; 583 static ASTContext* Create(llvm::Deserializer& D); 584 585private: 586 ASTContext(const ASTContext&); // DO NOT IMPLEMENT 587 void operator=(const ASTContext&); // DO NOT IMPLEMENT 588 589 void InitBuiltinTypes(); 590 void InitBuiltinType(QualType &R, BuiltinType::Kind K); 591 592 // Return the ObjC type encoding for a given type. 593 void getObjCEncodingForTypeImpl(QualType t, std::string &S, 594 bool ExpandPointedToStructures, 595 bool ExpandStructures, 596 FieldDecl *Field, 597 bool OutermostType = false, 598 bool EncodingProperty = false) const; 599 600}; 601 602} // end namespace clang 603 604// operator new and delete aren't allowed inside namespaces. 605// The throw specifications are mandated by the standard. 606/// @brief Placement new for using the ASTContext's allocator. 607/// 608/// This placement form of operator new uses the ASTContext's allocator for 609/// obtaining memory. It is a non-throwing new, which means that it returns 610/// null on error. (If that is what the allocator does. The current does, so if 611/// this ever changes, this operator will have to be changed, too.) 612/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 613/// @code 614/// // Default alignment (16) 615/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 616/// // Specific alignment 617/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 618/// @endcode 619/// Please note that you cannot use delete on the pointer; it must be 620/// deallocated using an explicit destructor call followed by 621/// @c Context.Deallocate(Ptr). 622/// 623/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 624/// @param C The ASTContext that provides the allocator. 625/// @param Alignment The alignment of the allocated memory (if the underlying 626/// allocator supports it). 627/// @return The allocated memory. Could be NULL. 628inline void *operator new(size_t Bytes, clang::ASTContext &C, 629 size_t Alignment = 16) throw () { 630 return C.Allocate(Bytes, Alignment); 631} 632/// @brief Placement delete companion to the new above. 633/// 634/// This operator is just a companion to the new above. There is no way of 635/// invoking it directly; see the new operator for more details. This operator 636/// is called implicitly by the compiler if a placement new expression using 637/// the ASTContext throws in the object constructor. 638inline void operator delete(void *Ptr, clang::ASTContext &C) 639 throw () { 640 C.Deallocate(Ptr); 641} 642 643/// This placement form of operator new[] uses the ASTContext's allocator for 644/// obtaining memory. It is a non-throwing new[], which means that it returns 645/// null on error. 646/// Usage looks like this (assuming there's an ASTContext 'Context' in scope): 647/// @code 648/// // Default alignment (16) 649/// char *data = new (Context) char[10]; 650/// // Specific alignment 651/// char *data = new (Context, 8) char[10]; 652/// @endcode 653/// Please note that you cannot use delete on the pointer; it must be 654/// deallocated using an explicit destructor call followed by 655/// @c Context.Deallocate(Ptr). 656/// 657/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 658/// @param C The ASTContext that provides the allocator. 659/// @param Alignment The alignment of the allocated memory (if the underlying 660/// allocator supports it). 661/// @return The allocated memory. Could be NULL. 662inline void *operator new[](size_t Bytes, clang::ASTContext& C, 663 size_t Alignment = 16) throw () { 664 return C.Allocate(Bytes, Alignment); 665} 666 667/// @brief Placement delete[] companion to the new[] above. 668/// 669/// This operator is just a companion to the new[] above. There is no way of 670/// invoking it directly; see the new[] operator for more details. This operator 671/// is called implicitly by the compiler if a placement new[] expression using 672/// the ASTContext throws in the object constructor. 673inline void operator delete[](void *Ptr, clang::ASTContext &C) throw () { 674 C.Deallocate(Ptr); 675} 676 677#endif 678