Type.h revision 053ec969e3023e4b4a666546476ff74e6bfdfd9a
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===--- Type.h - C Language Family Type Representation ---------*- C++ -*-===// 22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The LLVM Compiler Infrastructure 4a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch// 5b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source 62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// License. See LICENSE.TXT for details. 72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)//===----------------------------------------------------------------------===// 9ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch// 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file defines the Type interface and subclasses. 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// 122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===// 132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef LLVM_CLANG_AST_TYPE_H 157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#define LLVM_CLANG_AST_TYPE_H 167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/Diagnostic.h" 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/IdentifierTable.h" 192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/Linkage.h" 20a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch#include "clang/Basic/PartialDiagnostic.h" 212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/Visibility.h" 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/AST/NestedNameSpecifier.h" 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/AST/TemplateName.h" 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/Casting.h" 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/Support/type_traits.h" 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/APSInt.h" 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/FoldingSet.h" 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/PointerIntPair.h" 297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "llvm/ADT/PointerUnion.h" 307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)using llvm::isa; 32a3f7b4e666c476898878fa745f637129375cd889Ben Murdochusing llvm::cast; 33ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochusing llvm::cast_or_null; 347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)using llvm::dyn_cast; 357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)using llvm::dyn_cast_or_null; 367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace clang { 377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) enum { 387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) TypeAlignmentInBits = 3, 397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) TypeAlignment = 1 << TypeAlignmentInBits 407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) }; 417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class Type; 427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class ExtQuals; 437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class QualType; 447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace llvm { 477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) template <typename T> 487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class PointerLikeTypeTraits; 497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) template<> 507dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch class PointerLikeTypeTraits< ::clang::Type*> { 517dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch public: 527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static inline void *getAsVoidPointer(::clang::Type *P) { return P; } 537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static inline ::clang::Type *getFromVoidPointer(void *P) { 547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return static_cast< ::clang::Type*>(P); 557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch } 567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch enum { NumLowBitsAvailable = clang::TypeAlignmentInBits }; 577dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch }; 587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch template<> 597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch class PointerLikeTypeTraits< ::clang::ExtQuals*> { 607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public: 617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; } 627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static inline ::clang::ExtQuals *getFromVoidPointer(void *P) { 637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return static_cast< ::clang::ExtQuals*>(P); 647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) enum { NumLowBitsAvailable = clang::TypeAlignmentInBits }; 667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) }; 677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) template <> 697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) struct isPodLike<clang::QualType> { static const bool value = true; }; 707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace clang { 737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class ASTContext; 74a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch class TypedefDecl; 75a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch class TemplateDecl; 767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class TemplateTypeParmDecl; 777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class NonTypeTemplateParmDecl; 782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) class TemplateTemplateParmDecl; 792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) class TagDecl; 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) class RecordDecl; 812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) class CXXRecordDecl; 822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) class EnumDecl; 832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) class FieldDecl; 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) class ObjCInterfaceDecl; 852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) class ObjCProtocolDecl; 867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class ObjCMethodDecl; 877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class UnresolvedUsingTypenameDecl; 887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class Expr; 897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class Stmt; 902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) class SourceLocation; 917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class StmtIteratorBase; 92ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch class TemplateArgument; 93ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch class TemplateArgumentLoc; 94ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch class TemplateArgumentListInfo; 95a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch class Type; 967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) class ElaboratedType; 97a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch struct PrintingPolicy; 982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) template <typename> class CanQual; 1007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) typedef CanQual<Type> CanQualType; 1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 10290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Provide forward declarations for all of the *Type classes 1037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#define TYPE(Class, Base) class Class##Type; 1047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "clang/AST/TypeNodes.def" 1057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// Qualifiers - The collection of all-type qualifiers we support. 107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch/// Clang supports five independent qualifiers: 1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// * C99: const, volatile, and restrict 1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// * Embedded C (TR18037): address spaces 1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// * Objective C: the GC attributes (none, weak, or strong) 1117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class Qualifiers { 1127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)public: 1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ. 1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Const = 0x1, 1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Restrict = 0x2, 1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) Volatile = 0x4, 1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) CVRMask = Const | Volatile | Restrict 11890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) }; 11990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) enum GC { 1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) GCNone = 0, 1227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Weak, 123eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch Strong 124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch }; 1257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 126eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch enum { 127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// The maximum supported address space number. 1287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) /// 24 bits should be enough for anyone. 129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) MaxAddressSpace = 0xffffffu, 130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// The width of the "fast" qualifier mask. 1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FastWidth = 2, 1337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) /// The fast qualifier mask. 1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FastMask = (1 << FastWidth) - 1 1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) }; 1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Qualifiers() : Mask(0) {} 1397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static Qualifiers fromFastMask(unsigned Mask) { 1417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Qualifiers Qs; 142ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch Qs.addFastQualifiers(Mask); 143ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch return Qs; 144ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch } 145ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch 146ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch static Qualifiers fromCVRMask(unsigned CVR) { 147ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch Qualifiers Qs; 148ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch Qs.addCVRQualifiers(CVR); 149ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch return Qs; 150ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch } 151a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch 152a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch // Deserialize qualifiers from an opaque representation. 153a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch static Qualifiers fromOpaqueValue(unsigned opaque) { 154a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Qualifiers Qs; 155a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Qs.Mask = opaque; 156a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch return Qs; 157a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch } 158a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch 159a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch // Serialize these qualifiers into an opaque representation. 160a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch unsigned getAsOpaqueValue() const { 161a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch return Mask; 162a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch } 163a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch 164a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch bool hasConst() const { return Mask & Const; } 165a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void setConst(bool flag) { 166a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Mask = (Mask & ~Const) | (flag ? Const : 0); 167a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch } 168a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void removeConst() { Mask &= ~Const; } 169a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void addConst() { Mask |= Const; } 170a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch 171a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch bool hasVolatile() const { return Mask & Volatile; } 172a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void setVolatile(bool flag) { 173a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Mask = (Mask & ~Volatile) | (flag ? Volatile : 0); 174a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch } 175a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void removeVolatile() { Mask &= ~Volatile; } 176a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void addVolatile() { Mask |= Volatile; } 177a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch 178a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch bool hasRestrict() const { return Mask & Restrict; } 179a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void setRestrict(bool flag) { 180a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Mask = (Mask & ~Restrict) | (flag ? Restrict : 0); 181a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch } 182a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void removeRestrict() { Mask &= ~Restrict; } 183a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void addRestrict() { Mask |= Restrict; } 184a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch 185a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch bool hasCVRQualifiers() const { return getCVRQualifiers(); } 186a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch unsigned getCVRQualifiers() const { return Mask & CVRMask; } 187a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void setCVRQualifiers(unsigned mask) { 188a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 189a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Mask = (Mask & ~CVRMask) | mask; 190a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch } 191a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void removeCVRQualifiers(unsigned mask) { 192a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 193a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Mask &= ~mask; 194a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch } 195a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void removeCVRQualifiers() { 196a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch removeCVRQualifiers(CVRMask); 197a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch } 198a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void addCVRQualifiers(unsigned mask) { 199a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits"); 200a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Mask |= mask; 201a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch } 202a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch 203ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch bool hasObjCGCAttr() const { return Mask & GCAttrMask; } 2047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); } 2057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void setObjCGCAttr(GC type) { 2067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift); 2077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void removeObjCGCAttr() { setObjCGCAttr(GCNone); } 209bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch void addObjCGCAttr(GC type) { 210bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch assert(type); 211bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch setObjCGCAttr(type); 212bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch } 213bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch 2147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) bool hasAddressSpace() const { return Mask & AddressSpaceMask; } 215bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch unsigned getAddressSpace() const { return Mask >> AddressSpaceShift; } 216bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch void setAddressSpace(unsigned space) { 217bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch assert(space <= MaxAddressSpace); 218bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch Mask = (Mask & ~AddressSpaceMask) 2197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) | (((uint32_t) space) << AddressSpaceShift); 2207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void removeAddressSpace() { setAddressSpace(0); } 2227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void addAddressSpace(unsigned space) { 2237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) assert(space); 2247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) setAddressSpace(space); 2257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Fast qualifiers are those that can be allocated directly 2287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // on a QualType object. 2297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) bool hasFastQualifiers() const { return getFastQualifiers(); } 2307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) unsigned getFastQualifiers() const { return Mask & FastMask; } 2317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void setFastQualifiers(unsigned mask) { 2327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 2337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Mask = (Mask & ~FastMask) | mask; 2347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void removeFastQualifiers(unsigned mask) { 2367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 2377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Mask &= ~mask; 2387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void removeFastQualifiers() { 2407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) removeFastQualifiers(FastMask); 2417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 242a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void addFastQualifiers(unsigned mask) { 243a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits"); 244a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Mask |= mask; 245a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch } 246a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch 247a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch /// hasNonFastQualifiers - Return true if the set contains any 248a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch /// qualifiers which require an ExtQuals node to be allocated. 249a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch bool hasNonFastQualifiers() const { return Mask & ~FastMask; } 250a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Qualifiers getNonFastQualifiers() const { 2517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Qualifiers Quals = *this; 2527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Quals.setFastQualifiers(0); 2537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return Quals; 2547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 255a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch 256a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch /// hasQualifiers - Return true if the set contains any qualifiers. 257a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch bool hasQualifiers() const { return Mask; } 258a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch bool empty() const { return !Mask; } 259a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch 260a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch /// \brief Add the qualifiers from the given set to this set. 261a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch void addQualifiers(Qualifiers Q) { 262a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch // If the other set doesn't have any non-boolean qualifiers, just 263a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch // bit-or it in. 264a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch if (!(Q.Mask & ~CVRMask)) 265a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Mask |= Q.Mask; 266a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch else { 267a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch Mask |= (Q.Mask & CVRMask); 268a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch if (Q.hasAddressSpace()) 269a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch addAddressSpace(Q.getAddressSpace()); 270a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch if (Q.hasObjCGCAttr()) 2717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) addObjCGCAttr(Q.getObjCGCAttr()); 2727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) bool isSupersetOf(Qualifiers Other) const; 2767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) bool operator==(Qualifiers Other) const { return Mask == Other.Mask; } 2787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; } 2797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) operator bool() const { return hasQualifiers(); } 2817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Qualifiers &operator+=(Qualifiers R) { 2837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) addQualifiers(R); 2847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return *this; 2857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Union two qualifier sets. If an enumerated qualifier appears 2887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // in both sets, use the one from the right. 2897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) friend Qualifiers operator+(Qualifiers L, Qualifiers R) { 290a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch L += R; 291a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch return L; 2927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Qualifiers &operator-=(Qualifiers R) { 2957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Mask = Mask & ~(R.Mask); 2967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return *this; 2977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) /// \brief Compute the difference between two qualifier sets. 3007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) friend Qualifiers operator-(Qualifiers L, Qualifiers R) { 3017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) L -= R; 3027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return L; 3037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 3047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) std::string getAsString() const; 3067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) std::string getAsString(const PrintingPolicy &Policy) const { 3077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) std::string Buffer; 3087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) getAsStringInternal(Buffer, Policy); 3097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return Buffer; 3107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 3117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void getAsStringInternal(std::string &S, const PrintingPolicy &Policy) const; 3127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void Profile(llvm::FoldingSetNodeID &ID) const { 3147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ID.AddInteger(Mask); 3157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 3167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)private: 3187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // bits: |0 1 2|3 .. 4|5 .. 31| 3207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // |C R V|GCAttr|AddrSpace| 3217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) uint32_t Mask; 3227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static const uint32_t GCAttrMask = 0x18; 3247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static const uint32_t GCAttrShift = 3; 3257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static const uint32_t AddressSpaceMask = ~(CVRMask | GCAttrMask); 3267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static const uint32_t AddressSpaceShift = 5; 3277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}; 328eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 3297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// ExtQuals - We can encode up to three bits in the low bits of a 3317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// type pointer, but there are many more type qualifiers that we want 3327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// to be able to apply to an arbitrary type. Therefore we have this 3337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// struct, intended to be heap-allocated and used by QualType to 3347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// store qualifiers. 3357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// 3367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// The current design tags the 'const' and 'restrict' qualifiers in 3377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// two low bits on the QualType pointer; a third bit records whether 3387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// the pointer is an ExtQuals node. 'const' was chosen because it is 3397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// orders of magnitude more common than the other two qualifiers, in 3407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// both library and user code. It's relatively rare to see 3417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// 'restrict' in user code, but many standard C headers are saturated 3427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// with 'restrict' declarations, so that representing them efficiently 3437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// is a critical goal of this representation. 3447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class ExtQuals : public llvm::FoldingSetNode { 3457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // NOTE: changing the fast qualifiers should be straightforward as 3467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // long as you don't make 'const' non-fast. 3477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // 1. Qualifiers: 3487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ). 3497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Fast qualifiers must occupy the low-order bits. 3507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // b) Update Qualifiers::FastWidth and FastMask. 3517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // 2. QualType: 352ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch // a) Update is{Volatile,Restrict}Qualified(), defined inline. 3537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // b) Update remove{Volatile,Restrict}, defined near the end of 3547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // this header. 3557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // 3. ASTContext: 3567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // a) Update get{Volatile,Restrict}Type. 3577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) /// Context - the context to which this set belongs. We save this 3597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) /// here so that QualifierCollector can use it to reapply extended 3607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) /// qualifiers to an arbitrary type without requiring a context to 3617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) /// be pushed through every single API dealing with qualifiers. 3627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ASTContext& Context; 363ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch 364ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch /// BaseType - the underlying type that this qualifies 365ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch const Type *BaseType; 366ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch 367ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch /// Quals - the immutable set of qualifiers applied by this 368558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch /// node; always contains extended qualifiers. 369558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch Qualifiers Quals; 3707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)public: 3727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ExtQuals(ASTContext& Context, const Type *Base, Qualifiers Quals) 3737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) : Context(Context), BaseType(Base), Quals(Quals) 3747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) { 3757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) assert(Quals.hasNonFastQualifiers() 3767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) && "ExtQuals created with no fast qualifiers"); 3777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) assert(!Quals.hasFastQualifiers() 3787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) && "ExtQuals created with fast qualifiers"); 3797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 380ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch 381ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch Qualifiers getQualifiers() const { return Quals; } 3827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) bool hasVolatile() const { return Quals.hasVolatile(); } 3847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); } 3867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); } 3877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) bool hasAddressSpace() const { return Quals.hasAddressSpace(); } 3897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) unsigned getAddressSpace() const { return Quals.getAddressSpace(); } 3907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const Type *getBaseType() const { return BaseType; } 3927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ASTContext &getContext() const { return Context; } 3947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)public: 3967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) void Profile(llvm::FoldingSetNodeID &ID) const { 3977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Profile(ID, getBaseType(), Quals); 3987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 3997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) static void Profile(llvm::FoldingSetNodeID &ID, 4007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const Type *BaseType, 4017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Qualifiers Quals) { 4027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!"); 4037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ID.AddPointer(BaseType); 4047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) Quals.Profile(ID); 4057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 4067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}; 4077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 4087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// CallingConv - Specifies the calling convention that a function uses. 4097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)enum CallingConv { 4107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch CC_Default, 4117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CC_C, // __attribute__((cdecl)) 4127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CC_X86StdCall, // __attribute__((stdcall)) 4137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CC_X86FastCall, // __attribute__((fastcall)) 4147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CC_X86ThisCall, // __attribute__((thiscall)) 4157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CC_X86Pascal // __attribute__((pascal)) 4167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}; 4177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 4187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 4197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// QualType - For efficiency, we don't store CV-qualified types as nodes on 4207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)/// their own: instead each reference to a type stores the qualifiers. This 421/// greatly reduces the number of nodes we need to allocate for types (for 422/// example we only need one for 'int', 'const int', 'volatile int', 423/// 'const volatile int', etc). 424/// 425/// As an added efficiency bonus, instead of making this a pair, we 426/// just store the two bits we care about in the low bits of the 427/// pointer. To handle the packing/unpacking, we make QualType be a 428/// simple wrapper class that acts like a smart pointer. A third bit 429/// indicates whether there are extended qualifiers present, in which 430/// case the pointer points to a special structure. 431class QualType { 432 // Thankfully, these are efficiently composable. 433 llvm::PointerIntPair<llvm::PointerUnion<const Type*,const ExtQuals*>, 434 Qualifiers::FastWidth> Value; 435 436 const ExtQuals *getExtQualsUnsafe() const { 437 return Value.getPointer().get<const ExtQuals*>(); 438 } 439 440 const Type *getTypePtrUnsafe() const { 441 return Value.getPointer().get<const Type*>(); 442 } 443 444 QualType getUnqualifiedTypeSlow() const; 445 446 friend class QualifierCollector; 447public: 448 QualType() {} 449 450 QualType(const Type *Ptr, unsigned Quals) 451 : Value(Ptr, Quals) {} 452 QualType(const ExtQuals *Ptr, unsigned Quals) 453 : Value(Ptr, Quals) {} 454 455 unsigned getLocalFastQualifiers() const { return Value.getInt(); } 456 void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); } 457 458 /// Retrieves a pointer to the underlying (unqualified) type. 459 /// This should really return a const Type, but it's not worth 460 /// changing all the users right now. 461 Type *getTypePtr() const { 462 if (hasLocalNonFastQualifiers()) 463 return const_cast<Type*>(getExtQualsUnsafe()->getBaseType()); 464 return const_cast<Type*>(getTypePtrUnsafe()); 465 } 466 467 void *getAsOpaquePtr() const { return Value.getOpaqueValue(); } 468 static QualType getFromOpaquePtr(void *Ptr) { 469 QualType T; 470 T.Value.setFromOpaqueValue(Ptr); 471 return T; 472 } 473 474 Type &operator*() const { 475 return *getTypePtr(); 476 } 477 478 Type *operator->() const { 479 return getTypePtr(); 480 } 481 482 bool isCanonical() const; 483 bool isCanonicalAsParam() const; 484 485 /// isNull - Return true if this QualType doesn't point to a type yet. 486 bool isNull() const { 487 return Value.getPointer().isNull(); 488 } 489 490 /// \brief Determine whether this particular QualType instance has the 491 /// "const" qualifier set, without looking through typedefs that may have 492 /// added "const" at a different level. 493 bool isLocalConstQualified() const { 494 return (getLocalFastQualifiers() & Qualifiers::Const); 495 } 496 497 /// \brief Determine whether this type is const-qualified. 498 bool isConstQualified() const; 499 500 /// \brief Determine whether this particular QualType instance has the 501 /// "restrict" qualifier set, without looking through typedefs that may have 502 /// added "restrict" at a different level. 503 bool isLocalRestrictQualified() const { 504 return (getLocalFastQualifiers() & Qualifiers::Restrict); 505 } 506 507 /// \brief Determine whether this type is restrict-qualified. 508 bool isRestrictQualified() const; 509 510 /// \brief Determine whether this particular QualType instance has the 511 /// "volatile" qualifier set, without looking through typedefs that may have 512 /// added "volatile" at a different level. 513 bool isLocalVolatileQualified() const { 514 return (hasLocalNonFastQualifiers() && getExtQualsUnsafe()->hasVolatile()); 515 } 516 517 /// \brief Determine whether this type is volatile-qualified. 518 bool isVolatileQualified() const; 519 520 /// \brief Determine whether this particular QualType instance has any 521 /// qualifiers, without looking through any typedefs that might add 522 /// qualifiers at a different level. 523 bool hasLocalQualifiers() const { 524 return getLocalFastQualifiers() || hasLocalNonFastQualifiers(); 525 } 526 527 /// \brief Determine whether this type has any qualifiers. 528 bool hasQualifiers() const; 529 530 /// \brief Determine whether this particular QualType instance has any 531 /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType 532 /// instance. 533 bool hasLocalNonFastQualifiers() const { 534 return Value.getPointer().is<const ExtQuals*>(); 535 } 536 537 /// \brief Retrieve the set of qualifiers local to this particular QualType 538 /// instance, not including any qualifiers acquired through typedefs or 539 /// other sugar. 540 Qualifiers getLocalQualifiers() const { 541 Qualifiers Quals; 542 if (hasLocalNonFastQualifiers()) 543 Quals = getExtQualsUnsafe()->getQualifiers(); 544 Quals.addFastQualifiers(getLocalFastQualifiers()); 545 return Quals; 546 } 547 548 /// \brief Retrieve the set of qualifiers applied to this type. 549 Qualifiers getQualifiers() const; 550 551 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 552 /// local to this particular QualType instance, not including any qualifiers 553 /// acquired through typedefs or other sugar. 554 unsigned getLocalCVRQualifiers() const { 555 unsigned CVR = getLocalFastQualifiers(); 556 if (isLocalVolatileQualified()) 557 CVR |= Qualifiers::Volatile; 558 return CVR; 559 } 560 561 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 562 /// applied to this type. 563 unsigned getCVRQualifiers() const; 564 565 /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers 566 /// applied to this type, looking through any number of unqualified array 567 /// types to their element types' qualifiers. 568 unsigned getCVRQualifiersThroughArrayTypes() const; 569 570 bool isConstant(ASTContext& Ctx) const { 571 return QualType::isConstant(*this, Ctx); 572 } 573 574 // Don't promise in the API that anything besides 'const' can be 575 // easily added. 576 577 /// addConst - add the specified type qualifier to this QualType. 578 void addConst() { 579 addFastQualifiers(Qualifiers::Const); 580 } 581 QualType withConst() const { 582 return withFastQualifiers(Qualifiers::Const); 583 } 584 585 void addFastQualifiers(unsigned TQs) { 586 assert(!(TQs & ~Qualifiers::FastMask) 587 && "non-fast qualifier bits set in mask!"); 588 Value.setInt(Value.getInt() | TQs); 589 } 590 591 // FIXME: The remove* functions are semantically broken, because they might 592 // not remove a qualifier stored on a typedef. Most of the with* functions 593 // have the same problem. 594 void removeConst(); 595 void removeVolatile(); 596 void removeRestrict(); 597 void removeCVRQualifiers(unsigned Mask); 598 599 void removeFastQualifiers() { Value.setInt(0); } 600 void removeFastQualifiers(unsigned Mask) { 601 assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers"); 602 Value.setInt(Value.getInt() & ~Mask); 603 } 604 605 // Creates a type with the given qualifiers in addition to any 606 // qualifiers already on this type. 607 QualType withFastQualifiers(unsigned TQs) const { 608 QualType T = *this; 609 T.addFastQualifiers(TQs); 610 return T; 611 } 612 613 // Creates a type with exactly the given fast qualifiers, removing 614 // any existing fast qualifiers. 615 QualType withExactFastQualifiers(unsigned TQs) const { 616 return withoutFastQualifiers().withFastQualifiers(TQs); 617 } 618 619 // Removes fast qualifiers, but leaves any extended qualifiers in place. 620 QualType withoutFastQualifiers() const { 621 QualType T = *this; 622 T.removeFastQualifiers(); 623 return T; 624 } 625 626 /// \brief Return this type with all of the instance-specific qualifiers 627 /// removed, but without removing any qualifiers that may have been applied 628 /// through typedefs. 629 QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); } 630 631 /// \brief Return the unqualified form of the given type, which might be 632 /// desugared to eliminate qualifiers introduced via typedefs. 633 QualType getUnqualifiedType() const { 634 QualType T = getLocalUnqualifiedType(); 635 if (!T.hasQualifiers()) 636 return T; 637 638 return getUnqualifiedTypeSlow(); 639 } 640 641 bool isMoreQualifiedThan(QualType Other) const; 642 bool isAtLeastAsQualifiedAs(QualType Other) const; 643 QualType getNonReferenceType() const; 644 645 /// \brief Determine the type of a (typically non-lvalue) expression with the 646 /// specified result type. 647 /// 648 /// This routine should be used for expressions for which the return type is 649 /// explicitly specified (e.g., in a cast or call) and isn't necessarily 650 /// an lvalue. It removes a top-level reference (since there are no 651 /// expressions of reference type) and deletes top-level cvr-qualifiers 652 /// from non-class types (in C++) or all types (in C). 653 QualType getNonLValueExprType(ASTContext &Context) const; 654 655 /// getDesugaredType - Return the specified type with any "sugar" removed from 656 /// the type. This takes off typedefs, typeof's etc. If the outer level of 657 /// the type is already concrete, it returns it unmodified. This is similar 658 /// to getting the canonical type, but it doesn't remove *all* typedefs. For 659 /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is 660 /// concrete. 661 /// 662 /// Qualifiers are left in place. 663 QualType getDesugaredType() const { 664 return QualType::getDesugaredType(*this); 665 } 666 667 /// operator==/!= - Indicate whether the specified types and qualifiers are 668 /// identical. 669 friend bool operator==(const QualType &LHS, const QualType &RHS) { 670 return LHS.Value == RHS.Value; 671 } 672 friend bool operator!=(const QualType &LHS, const QualType &RHS) { 673 return LHS.Value != RHS.Value; 674 } 675 std::string getAsString() const; 676 677 std::string getAsString(const PrintingPolicy &Policy) const { 678 std::string S; 679 getAsStringInternal(S, Policy); 680 return S; 681 } 682 void getAsStringInternal(std::string &Str, 683 const PrintingPolicy &Policy) const; 684 685 void dump(const char *s) const; 686 void dump() const; 687 688 void Profile(llvm::FoldingSetNodeID &ID) const { 689 ID.AddPointer(getAsOpaquePtr()); 690 } 691 692 /// getAddressSpace - Return the address space of this type. 693 inline unsigned getAddressSpace() const; 694 695 /// GCAttrTypesAttr - Returns gc attribute of this type. 696 inline Qualifiers::GC getObjCGCAttr() const; 697 698 /// isObjCGCWeak true when Type is objc's weak. 699 bool isObjCGCWeak() const { 700 return getObjCGCAttr() == Qualifiers::Weak; 701 } 702 703 /// isObjCGCStrong true when Type is objc's strong. 704 bool isObjCGCStrong() const { 705 return getObjCGCAttr() == Qualifiers::Strong; 706 } 707 708private: 709 // These methods are implemented in a separate translation unit; 710 // "static"-ize them to avoid creating temporary QualTypes in the 711 // caller. 712 static bool isConstant(QualType T, ASTContext& Ctx); 713 static QualType getDesugaredType(QualType T); 714}; 715 716} // end clang. 717 718namespace llvm { 719/// Implement simplify_type for QualType, so that we can dyn_cast from QualType 720/// to a specific Type class. 721template<> struct simplify_type<const ::clang::QualType> { 722 typedef ::clang::Type* SimpleType; 723 static SimpleType getSimplifiedValue(const ::clang::QualType &Val) { 724 return Val.getTypePtr(); 725 } 726}; 727template<> struct simplify_type< ::clang::QualType> 728 : public simplify_type<const ::clang::QualType> {}; 729 730// Teach SmallPtrSet that QualType is "basically a pointer". 731template<> 732class PointerLikeTypeTraits<clang::QualType> { 733public: 734 static inline void *getAsVoidPointer(clang::QualType P) { 735 return P.getAsOpaquePtr(); 736 } 737 static inline clang::QualType getFromVoidPointer(void *P) { 738 return clang::QualType::getFromOpaquePtr(P); 739 } 740 // Various qualifiers go in low bits. 741 enum { NumLowBitsAvailable = 0 }; 742}; 743 744} // end namespace llvm 745 746namespace clang { 747 748/// Type - This is the base class of the type hierarchy. A central concept 749/// with types is that each type always has a canonical type. A canonical type 750/// is the type with any typedef names stripped out of it or the types it 751/// references. For example, consider: 752/// 753/// typedef int foo; 754/// typedef foo* bar; 755/// 'int *' 'foo *' 'bar' 756/// 757/// There will be a Type object created for 'int'. Since int is canonical, its 758/// canonicaltype pointer points to itself. There is also a Type for 'foo' (a 759/// TypedefType). Its CanonicalType pointer points to the 'int' Type. Next 760/// there is a PointerType that represents 'int*', which, like 'int', is 761/// canonical. Finally, there is a PointerType type for 'foo*' whose canonical 762/// type is 'int*', and there is a TypedefType for 'bar', whose canonical type 763/// is also 'int*'. 764/// 765/// Non-canonical types are useful for emitting diagnostics, without losing 766/// information about typedefs being used. Canonical types are useful for type 767/// comparisons (they allow by-pointer equality tests) and useful for reasoning 768/// about whether something has a particular form (e.g. is a function type), 769/// because they implicitly, recursively, strip all typedefs out of a type. 770/// 771/// Types, once created, are immutable. 772/// 773class Type { 774public: 775 enum TypeClass { 776#define TYPE(Class, Base) Class, 777#define LAST_TYPE(Class) TypeLast = Class, 778#define ABSTRACT_TYPE(Class, Base) 779#include "clang/AST/TypeNodes.def" 780 TagFirst = Record, TagLast = Enum 781 }; 782 783private: 784 Type(const Type&); // DO NOT IMPLEMENT. 785 void operator=(const Type&); // DO NOT IMPLEMENT. 786 787 QualType CanonicalType; 788 789 /// Bitfields required by the Type class. 790 class TypeBitfields { 791 friend class Type; 792 793 /// TypeClass bitfield - Enum that specifies what subclass this belongs to. 794 unsigned TC : 8; 795 796 /// Dependent - Whether this type is a dependent type (C++ [temp.dep.type]). 797 /// Note that this should stay at the end of the ivars for Type so that 798 /// subclasses can pack their bitfields into the same word. 799 unsigned Dependent : 1; 800 801 /// \brief Whether this type is a variably-modified type (C99 6.7.5). 802 unsigned VariablyModified : 1; 803 804 /// \brief Nonzero if the cache (i.e. the bitfields here starting 805 /// with 'Cache') is valid. If so, then this is a 806 /// LangOptions::VisibilityMode+1. 807 mutable unsigned CacheValidAndVisibility : 2; 808 809 /// \brief Linkage of this type. 810 mutable unsigned CachedLinkage : 2; 811 812 /// \brief Whether this type involves and local or unnamed types. 813 mutable unsigned CachedLocalOrUnnamed : 1; 814 815 /// \brief FromAST - Whether this type comes from an AST file. 816 mutable unsigned FromAST : 1; 817 818 bool isCacheValid() const { 819 return (CacheValidAndVisibility != 0); 820 } 821 Visibility getVisibility() const { 822 assert(isCacheValid() && "getting linkage from invalid cache"); 823 return static_cast<Visibility>(CacheValidAndVisibility-1); 824 } 825 Linkage getLinkage() const { 826 assert(isCacheValid() && "getting linkage from invalid cache"); 827 return static_cast<Linkage>(CachedLinkage); 828 } 829 bool hasLocalOrUnnamedType() const { 830 assert(isCacheValid() && "getting linkage from invalid cache"); 831 return CachedLocalOrUnnamed; 832 } 833 }; 834 enum { NumTypeBits = 16 }; 835 836protected: 837 // These classes allow subclasses to somewhat cleanly pack bitfields 838 // into Type. 839 840 class ArrayTypeBitfields { 841 friend class ArrayType; 842 843 unsigned : NumTypeBits; 844 845 /// IndexTypeQuals - CVR qualifiers from declarations like 846 /// 'int X[static restrict 4]'. For function parameters only. 847 unsigned IndexTypeQuals : 3; 848 849 /// SizeModifier - storage class qualifiers from declarations like 850 /// 'int X[static restrict 4]'. For function parameters only. 851 /// Actually an ArrayType::ArraySizeModifier. 852 unsigned SizeModifier : 3; 853 }; 854 855 class BuiltinTypeBitfields { 856 friend class BuiltinType; 857 858 unsigned : NumTypeBits; 859 860 /// The kind (BuiltinType::Kind) of builtin type this is. 861 unsigned Kind : 8; 862 }; 863 864 class FunctionTypeBitfields { 865 friend class FunctionType; 866 867 unsigned : NumTypeBits; 868 869 /// Extra information which affects how the function is called, like 870 /// regparm and the calling convention. 871 unsigned ExtInfo : 8; 872 873 /// A bit to be used by the subclass. 874 unsigned SubclassInfo : 1; 875 876 /// TypeQuals - Used only by FunctionProtoType, put here to pack with the 877 /// other bitfields. 878 /// The qualifiers are part of FunctionProtoType because... 879 /// 880 /// C++ 8.3.5p4: The return type, the parameter type list and the 881 /// cv-qualifier-seq, [...], are part of the function type. 882 unsigned TypeQuals : 3; 883 }; 884 885 class ObjCObjectTypeBitfields { 886 friend class ObjCObjectType; 887 888 unsigned : NumTypeBits; 889 890 /// NumProtocols - The number of protocols stored directly on this 891 /// object type. 892 unsigned NumProtocols : 32 - NumTypeBits; 893 }; 894 895 class ReferenceTypeBitfields { 896 friend class ReferenceType; 897 898 unsigned : NumTypeBits; 899 900 /// True if the type was originally spelled with an lvalue sigil. 901 /// This is never true of rvalue references but can also be false 902 /// on lvalue references because of C++0x [dcl.typedef]p9, 903 /// as follows: 904 /// 905 /// typedef int &ref; // lvalue, spelled lvalue 906 /// typedef int &&rvref; // rvalue 907 /// ref &a; // lvalue, inner ref, spelled lvalue 908 /// ref &&a; // lvalue, inner ref 909 /// rvref &a; // lvalue, inner ref, spelled lvalue 910 /// rvref &&a; // rvalue, inner ref 911 unsigned SpelledAsLValue : 1; 912 913 /// True if the inner type is a reference type. This only happens 914 /// in non-canonical forms. 915 unsigned InnerRef : 1; 916 }; 917 918 class TypeWithKeywordBitfields { 919 friend class TypeWithKeyword; 920 921 unsigned : NumTypeBits; 922 923 /// An ElaboratedTypeKeyword. 8 bits for efficient access. 924 unsigned Keyword : 8; 925 }; 926 927 class VectorTypeBitfields { 928 friend class VectorType; 929 930 unsigned : NumTypeBits; 931 932 /// AltiVecSpec - AltiVec-specific vector information, used 933 /// to differentiate things like 'pixel'. 934 unsigned AltiVecSpec : 2; 935 936 /// NumElements - The number of elements in the vector. 937 unsigned NumElements : 30 - NumTypeBits; 938 }; 939 940 union { 941 TypeBitfields TypeBits; 942 ArrayTypeBitfields ArrayTypeBits; 943 BuiltinTypeBitfields BuiltinTypeBits; 944 FunctionTypeBitfields FunctionTypeBits; 945 ObjCObjectTypeBitfields ObjCObjectTypeBits; 946 ReferenceTypeBitfields ReferenceTypeBits; 947 TypeWithKeywordBitfields TypeWithKeywordBits; 948 VectorTypeBitfields VectorTypeBits; 949 }; 950 951private: 952 /// \brief Set whether this type comes from an AST file. 953 void setFromAST(bool V = true) const { 954 TypeBits.FromAST = V; 955 } 956 957 void ensureCachedProperties() const; 958 959protected: 960 /// \brief Compute the cached properties of this type. 961 class CachedProperties { 962 char linkage; 963 char visibility; 964 bool local; 965 966 public: 967 CachedProperties(Linkage linkage, Visibility visibility, bool local) 968 : linkage(linkage), visibility(visibility), local(local) {} 969 970 Linkage getLinkage() const { return (Linkage) linkage; } 971 Visibility getVisibility() const { return (Visibility) visibility; } 972 bool hasLocalOrUnnamedType() const { return local; } 973 974 friend CachedProperties merge(CachedProperties L, CachedProperties R) { 975 return CachedProperties(minLinkage(L.getLinkage(), R.getLinkage()), 976 minVisibility(L.getVisibility(), R.getVisibility()), 977 L.hasLocalOrUnnamedType() | R.hasLocalOrUnnamedType()); 978 } 979 }; 980 981 virtual CachedProperties getCachedProperties() const; 982 static CachedProperties getCachedProperties(QualType T) { 983 return getCachedProperties(T.getTypePtr()); 984 } 985 static CachedProperties getCachedProperties(const Type *T); 986 987 // silence VC++ warning C4355: 'this' : used in base member initializer list 988 Type *this_() { return this; } 989 Type(TypeClass tc, QualType Canonical, bool Dependent, bool VariablyModified) 990 : CanonicalType(Canonical.isNull() ? QualType(this_(), 0) : Canonical) { 991 TypeBits.TC = tc; 992 TypeBits.Dependent = Dependent; 993 TypeBits.VariablyModified = VariablyModified; 994 TypeBits.CacheValidAndVisibility = 0; 995 TypeBits.CachedLocalOrUnnamed = false; 996 TypeBits.CachedLinkage = NoLinkage; 997 TypeBits.FromAST = false; 998 } 999 virtual ~Type(); 1000 friend class ASTContext; 1001 1002 void setDependent(bool D = true) { TypeBits.Dependent = D; } 1003 void setVariablyModified(bool VM = true) { TypeBits.VariablyModified = VM; } 1004 1005public: 1006 TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); } 1007 1008 /// \brief Whether this type comes from an AST file. 1009 bool isFromAST() const { return TypeBits.FromAST; } 1010 1011 bool isCanonicalUnqualified() const { 1012 return CanonicalType.getTypePtr() == this; 1013 } 1014 1015 /// Types are partitioned into 3 broad categories (C99 6.2.5p1): 1016 /// object types, function types, and incomplete types. 1017 1018 /// isIncompleteType - Return true if this is an incomplete type. 1019 /// A type that can describe objects, but which lacks information needed to 1020 /// determine its size (e.g. void, or a fwd declared struct). Clients of this 1021 /// routine will need to determine if the size is actually required. 1022 bool isIncompleteType() const; 1023 1024 /// isIncompleteOrObjectType - Return true if this is an incomplete or object 1025 /// type, in other words, not a function type. 1026 bool isIncompleteOrObjectType() const { 1027 return !isFunctionType(); 1028 } 1029 1030 /// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10). 1031 bool isPODType() const; 1032 1033 /// isLiteralType - Return true if this is a literal type 1034 /// (C++0x [basic.types]p10) 1035 bool isLiteralType() const; 1036 1037 /// Helper methods to distinguish type categories. All type predicates 1038 /// operate on the canonical type, ignoring typedefs and qualifiers. 1039 1040 /// isBuiltinType - returns true if the type is a builtin type. 1041 bool isBuiltinType() const; 1042 1043 /// isSpecificBuiltinType - Test for a particular builtin type. 1044 bool isSpecificBuiltinType(unsigned K) const; 1045 1046 /// isPlaceholderType - Test for a type which does not represent an 1047 /// actual type-system type but is instead used as a placeholder for 1048 /// various convenient purposes within Clang. All such types are 1049 /// BuiltinTypes. 1050 bool isPlaceholderType() const; 1051 1052 /// isIntegerType() does *not* include complex integers (a GCC extension). 1053 /// isComplexIntegerType() can be used to test for complex integers. 1054 bool isIntegerType() const; // C99 6.2.5p17 (int, char, bool, enum) 1055 bool isEnumeralType() const; 1056 bool isBooleanType() const; 1057 bool isCharType() const; 1058 bool isWideCharType() const; 1059 bool isAnyCharacterType() const; 1060 bool isIntegralType(ASTContext &Ctx) const; 1061 1062 /// \brief Determine whether this type is an integral or enumeration type. 1063 bool isIntegralOrEnumerationType() const; 1064 /// \brief Determine whether this type is an integral or unscoped enumeration 1065 /// type. 1066 bool isIntegralOrUnscopedEnumerationType() const; 1067 1068 /// Floating point categories. 1069 bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double) 1070 /// isComplexType() does *not* include complex integers (a GCC extension). 1071 /// isComplexIntegerType() can be used to test for complex integers. 1072 bool isComplexType() const; // C99 6.2.5p11 (complex) 1073 bool isAnyComplexType() const; // C99 6.2.5p11 (complex) + Complex Int. 1074 bool isFloatingType() const; // C99 6.2.5p11 (real floating + complex) 1075 bool isRealType() const; // C99 6.2.5p17 (real floating + integer) 1076 bool isArithmeticType() const; // C99 6.2.5p18 (integer + floating) 1077 bool isVoidType() const; // C99 6.2.5p19 1078 bool isDerivedType() const; // C99 6.2.5p20 1079 bool isScalarType() const; // C99 6.2.5p21 (arithmetic + pointers) 1080 bool isAggregateType() const; 1081 1082 // Type Predicates: Check to see if this type is structurally the specified 1083 // type, ignoring typedefs and qualifiers. 1084 bool isFunctionType() const; 1085 bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); } 1086 bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); } 1087 bool isPointerType() const; 1088 bool isAnyPointerType() const; // Any C pointer or ObjC object pointer 1089 bool isBlockPointerType() const; 1090 bool isVoidPointerType() const; 1091 bool isReferenceType() const; 1092 bool isLValueReferenceType() const; 1093 bool isRValueReferenceType() const; 1094 bool isFunctionPointerType() const; 1095 bool isMemberPointerType() const; 1096 bool isMemberFunctionPointerType() const; 1097 bool isMemberDataPointerType() const; 1098 bool isArrayType() const; 1099 bool isConstantArrayType() const; 1100 bool isIncompleteArrayType() const; 1101 bool isVariableArrayType() const; 1102 bool isDependentSizedArrayType() const; 1103 bool isRecordType() const; 1104 bool isClassType() const; 1105 bool isStructureType() const; 1106 bool isStructureOrClassType() const; 1107 bool isUnionType() const; 1108 bool isComplexIntegerType() const; // GCC _Complex integer type. 1109 bool isVectorType() const; // GCC vector type. 1110 bool isExtVectorType() const; // Extended vector type. 1111 bool isObjCObjectPointerType() const; // Pointer to *any* ObjC object. 1112 // FIXME: change this to 'raw' interface type, so we can used 'interface' type 1113 // for the common case. 1114 bool isObjCObjectType() const; // NSString or typeof(*(id)0) 1115 bool isObjCQualifiedInterfaceType() const; // NSString<foo> 1116 bool isObjCQualifiedIdType() const; // id<foo> 1117 bool isObjCQualifiedClassType() const; // Class<foo> 1118 bool isObjCObjectOrInterfaceType() const; 1119 bool isObjCIdType() const; // id 1120 bool isObjCClassType() const; // Class 1121 bool isObjCSelType() const; // Class 1122 bool isObjCBuiltinType() const; // 'id' or 'Class' 1123 bool isTemplateTypeParmType() const; // C++ template type parameter 1124 bool isNullPtrType() const; // C++0x nullptr_t 1125 1126 /// isDependentType - Whether this type is a dependent type, meaning 1127 /// that its definition somehow depends on a template parameter 1128 /// (C++ [temp.dep.type]). 1129 bool isDependentType() const { return TypeBits.Dependent; } 1130 1131 /// \brief Whether this type is a variably-modified type (C99 6.7.5). 1132 bool isVariablyModifiedType() const { return TypeBits.VariablyModified; } 1133 1134 /// \brief Whether this type is or contains a local or unnamed type. 1135 bool hasUnnamedOrLocalType() const; 1136 1137 bool isOverloadableType() const; 1138 1139 /// \brief Determine wither this type is a C++ elaborated-type-specifier. 1140 bool isElaboratedTypeSpecifier() const; 1141 1142 /// hasPointerRepresentation - Whether this type is represented 1143 /// natively as a pointer; this includes pointers, references, block 1144 /// pointers, and Objective-C interface, qualified id, and qualified 1145 /// interface types, as well as nullptr_t. 1146 bool hasPointerRepresentation() const; 1147 1148 /// hasObjCPointerRepresentation - Whether this type can represent 1149 /// an objective pointer type for the purpose of GC'ability 1150 bool hasObjCPointerRepresentation() const; 1151 1152 /// \brief Determine whether this type has an integer representation 1153 /// of some sort, e.g., it is an integer type or a vector. 1154 bool hasIntegerRepresentation() const; 1155 1156 /// \brief Determine whether this type has an signed integer representation 1157 /// of some sort, e.g., it is an signed integer type or a vector. 1158 bool hasSignedIntegerRepresentation() const; 1159 1160 /// \brief Determine whether this type has an unsigned integer representation 1161 /// of some sort, e.g., it is an unsigned integer type or a vector. 1162 bool hasUnsignedIntegerRepresentation() const; 1163 1164 /// \brief Determine whether this type has a floating-point representation 1165 /// of some sort, e.g., it is a floating-point type or a vector thereof. 1166 bool hasFloatingRepresentation() const; 1167 1168 // Type Checking Functions: Check to see if this type is structurally the 1169 // specified type, ignoring typedefs and qualifiers, and return a pointer to 1170 // the best type we can. 1171 const RecordType *getAsStructureType() const; 1172 /// NOTE: getAs*ArrayType are methods on ASTContext. 1173 const RecordType *getAsUnionType() const; 1174 const ComplexType *getAsComplexIntegerType() const; // GCC complex int type. 1175 // The following is a convenience method that returns an ObjCObjectPointerType 1176 // for object declared using an interface. 1177 const ObjCObjectPointerType *getAsObjCInterfacePointerType() const; 1178 const ObjCObjectPointerType *getAsObjCQualifiedIdType() const; 1179 const ObjCObjectType *getAsObjCQualifiedInterfaceType() const; 1180 const CXXRecordDecl *getCXXRecordDeclForPointerType() const; 1181 1182 /// \brief Retrieves the CXXRecordDecl that this type refers to, either 1183 /// because the type is a RecordType or because it is the injected-class-name 1184 /// type of a class template or class template partial specialization. 1185 CXXRecordDecl *getAsCXXRecordDecl() const; 1186 1187 // Member-template getAs<specific type>'. Look through sugar for 1188 // an instance of <specific type>. This scheme will eventually 1189 // replace the specific getAsXXXX methods above. 1190 // 1191 // There are some specializations of this member template listed 1192 // immediately following this class. 1193 template <typename T> const T *getAs() const; 1194 1195 /// getArrayElementTypeNoTypeQual - If this is an array type, return the 1196 /// element type of the array, potentially with type qualifiers missing. 1197 /// This method should never be used when type qualifiers are meaningful. 1198 const Type *getArrayElementTypeNoTypeQual() const; 1199 1200 /// getPointeeType - If this is a pointer, ObjC object pointer, or block 1201 /// pointer, this returns the respective pointee. 1202 QualType getPointeeType() const; 1203 1204 /// getUnqualifiedDesugaredType() - Return the specified type with 1205 /// any "sugar" removed from the type, removing any typedefs, 1206 /// typeofs, etc., as well as any qualifiers. 1207 const Type *getUnqualifiedDesugaredType() const; 1208 1209 /// More type predicates useful for type checking/promotion 1210 bool isPromotableIntegerType() const; // C99 6.3.1.1p2 1211 1212 /// isSignedIntegerType - Return true if this is an integer type that is 1213 /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..], 1214 /// an enum decl which has a signed representation, or a vector of signed 1215 /// integer element type. 1216 bool isSignedIntegerType() const; 1217 1218 /// isUnsignedIntegerType - Return true if this is an integer type that is 1219 /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum 1220 /// decl which has an unsigned representation, or a vector of unsigned integer 1221 /// element type. 1222 bool isUnsignedIntegerType() const; 1223 1224 /// isConstantSizeType - Return true if this is not a variable sized type, 1225 /// according to the rules of C99 6.7.5p3. It is not legal to call this on 1226 /// incomplete types. 1227 bool isConstantSizeType() const; 1228 1229 /// isSpecifierType - Returns true if this type can be represented by some 1230 /// set of type specifiers. 1231 bool isSpecifierType() const; 1232 1233 /// \brief Determine the linkage of this type. 1234 Linkage getLinkage() const; 1235 1236 /// \brief Determine the visibility of this type. 1237 Visibility getVisibility() const; 1238 1239 /// \brief Determine the linkage and visibility of this type. 1240 std::pair<Linkage,Visibility> getLinkageAndVisibility() const; 1241 1242 /// \brief Note that the linkage is no longer known. 1243 void ClearLinkageCache(); 1244 1245 const char *getTypeClassName() const; 1246 1247 QualType getCanonicalTypeInternal() const { 1248 return CanonicalType; 1249 } 1250 CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h 1251 void dump() const; 1252 static bool classof(const Type *) { return true; } 1253 1254 friend class ASTReader; 1255 friend class ASTWriter; 1256}; 1257 1258template <> inline const TypedefType *Type::getAs() const { 1259 return dyn_cast<TypedefType>(this); 1260} 1261 1262// We can do canonical leaf types faster, because we don't have to 1263// worry about preserving child type decoration. 1264#define TYPE(Class, Base) 1265#define LEAF_TYPE(Class) \ 1266template <> inline const Class##Type *Type::getAs() const { \ 1267 return dyn_cast<Class##Type>(CanonicalType); \ 1268} 1269#include "clang/AST/TypeNodes.def" 1270 1271 1272/// BuiltinType - This class is used for builtin types like 'int'. Builtin 1273/// types are always canonical and have a literal name field. 1274class BuiltinType : public Type { 1275public: 1276 enum Kind { 1277 Void, 1278 1279 Bool, // This is bool and/or _Bool. 1280 Char_U, // This is 'char' for targets where char is unsigned. 1281 UChar, // This is explicitly qualified unsigned char. 1282 Char16, // This is 'char16_t' for C++. 1283 Char32, // This is 'char32_t' for C++. 1284 UShort, 1285 UInt, 1286 ULong, 1287 ULongLong, 1288 UInt128, // __uint128_t 1289 1290 Char_S, // This is 'char' for targets where char is signed. 1291 SChar, // This is explicitly qualified signed char. 1292 WChar, // This is 'wchar_t' for C++. 1293 Short, 1294 Int, 1295 Long, 1296 LongLong, 1297 Int128, // __int128_t 1298 1299 Float, Double, LongDouble, 1300 1301 NullPtr, // This is the type of C++0x 'nullptr'. 1302 1303 /// This represents the type of an expression whose type is 1304 /// totally unknown, e.g. 'T::foo'. It is permitted for this to 1305 /// appear in situations where the structure of the type is 1306 /// theoretically deducible. 1307 Dependent, 1308 1309 Overload, // This represents the type of an overloaded function declaration. 1310 1311 UndeducedAuto, // In C++0x, this represents the type of an auto variable 1312 // that has not been deduced yet. 1313 1314 /// The primitive Objective C 'id' type. The type pointed to by the 1315 /// user-visible 'id' type. Only ever shows up in an AST as the base 1316 /// type of an ObjCObjectType. 1317 ObjCId, 1318 1319 /// The primitive Objective C 'Class' type. The type pointed to by the 1320 /// user-visible 'Class' type. Only ever shows up in an AST as the 1321 /// base type of an ObjCObjectType. 1322 ObjCClass, 1323 1324 ObjCSel // This represents the ObjC 'SEL' type. 1325 }; 1326 1327protected: 1328 virtual CachedProperties getCachedProperties() const; 1329 1330public: 1331 BuiltinType(Kind K) 1332 : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent), 1333 /*VariablyModified=*/false) { 1334 BuiltinTypeBits.Kind = K; 1335 } 1336 1337 Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); } 1338 const char *getName(const LangOptions &LO) const; 1339 1340 bool isSugared() const { return false; } 1341 QualType desugar() const { return QualType(this, 0); } 1342 1343 bool isInteger() const { 1344 return getKind() >= Bool && getKind() <= Int128; 1345 } 1346 1347 bool isSignedInteger() const { 1348 return getKind() >= Char_S && getKind() <= Int128; 1349 } 1350 1351 bool isUnsignedInteger() const { 1352 return getKind() >= Bool && getKind() <= UInt128; 1353 } 1354 1355 bool isFloatingPoint() const { 1356 return getKind() >= Float && getKind() <= LongDouble; 1357 } 1358 1359 /// Determines whether this type is a "forbidden" placeholder type, 1360 /// i.e. a type which cannot appear in arbitrary positions in a 1361 /// fully-formed expression. 1362 bool isPlaceholderType() const { 1363 return getKind() == Overload || 1364 getKind() == UndeducedAuto; 1365 } 1366 1367 static bool classof(const Type *T) { return T->getTypeClass() == Builtin; } 1368 static bool classof(const BuiltinType *) { return true; } 1369}; 1370 1371/// ComplexType - C99 6.2.5p11 - Complex values. This supports the C99 complex 1372/// types (_Complex float etc) as well as the GCC integer complex extensions. 1373/// 1374class ComplexType : public Type, public llvm::FoldingSetNode { 1375 QualType ElementType; 1376 ComplexType(QualType Element, QualType CanonicalPtr) : 1377 Type(Complex, CanonicalPtr, Element->isDependentType(), 1378 Element->isVariablyModifiedType()), 1379 ElementType(Element) { 1380 } 1381 friend class ASTContext; // ASTContext creates these. 1382 1383protected: 1384 virtual CachedProperties getCachedProperties() const; 1385 1386public: 1387 QualType getElementType() const { return ElementType; } 1388 1389 bool isSugared() const { return false; } 1390 QualType desugar() const { return QualType(this, 0); } 1391 1392 void Profile(llvm::FoldingSetNodeID &ID) { 1393 Profile(ID, getElementType()); 1394 } 1395 static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) { 1396 ID.AddPointer(Element.getAsOpaquePtr()); 1397 } 1398 1399 static bool classof(const Type *T) { return T->getTypeClass() == Complex; } 1400 static bool classof(const ComplexType *) { return true; } 1401}; 1402 1403/// PointerType - C99 6.7.5.1 - Pointer Declarators. 1404/// 1405class PointerType : public Type, public llvm::FoldingSetNode { 1406 QualType PointeeType; 1407 1408 PointerType(QualType Pointee, QualType CanonicalPtr) : 1409 Type(Pointer, CanonicalPtr, Pointee->isDependentType(), 1410 Pointee->isVariablyModifiedType()), 1411 PointeeType(Pointee) { 1412 } 1413 friend class ASTContext; // ASTContext creates these. 1414 1415protected: 1416 virtual CachedProperties getCachedProperties() const; 1417 1418public: 1419 1420 QualType getPointeeType() const { return PointeeType; } 1421 1422 bool isSugared() const { return false; } 1423 QualType desugar() const { return QualType(this, 0); } 1424 1425 void Profile(llvm::FoldingSetNodeID &ID) { 1426 Profile(ID, getPointeeType()); 1427 } 1428 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1429 ID.AddPointer(Pointee.getAsOpaquePtr()); 1430 } 1431 1432 static bool classof(const Type *T) { return T->getTypeClass() == Pointer; } 1433 static bool classof(const PointerType *) { return true; } 1434}; 1435 1436/// BlockPointerType - pointer to a block type. 1437/// This type is to represent types syntactically represented as 1438/// "void (^)(int)", etc. Pointee is required to always be a function type. 1439/// 1440class BlockPointerType : public Type, public llvm::FoldingSetNode { 1441 QualType PointeeType; // Block is some kind of pointer type 1442 BlockPointerType(QualType Pointee, QualType CanonicalCls) : 1443 Type(BlockPointer, CanonicalCls, Pointee->isDependentType(), 1444 Pointee->isVariablyModifiedType()), 1445 PointeeType(Pointee) { 1446 } 1447 friend class ASTContext; // ASTContext creates these. 1448 1449protected: 1450 virtual CachedProperties getCachedProperties() const; 1451 1452public: 1453 1454 // Get the pointee type. Pointee is required to always be a function type. 1455 QualType getPointeeType() const { return PointeeType; } 1456 1457 bool isSugared() const { return false; } 1458 QualType desugar() const { return QualType(this, 0); } 1459 1460 void Profile(llvm::FoldingSetNodeID &ID) { 1461 Profile(ID, getPointeeType()); 1462 } 1463 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) { 1464 ID.AddPointer(Pointee.getAsOpaquePtr()); 1465 } 1466 1467 static bool classof(const Type *T) { 1468 return T->getTypeClass() == BlockPointer; 1469 } 1470 static bool classof(const BlockPointerType *) { return true; } 1471}; 1472 1473/// ReferenceType - Base for LValueReferenceType and RValueReferenceType 1474/// 1475class ReferenceType : public Type, public llvm::FoldingSetNode { 1476 QualType PointeeType; 1477 1478protected: 1479 ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef, 1480 bool SpelledAsLValue) : 1481 Type(tc, CanonicalRef, Referencee->isDependentType(), 1482 Referencee->isVariablyModifiedType()), PointeeType(Referencee) { 1483 ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue; 1484 ReferenceTypeBits.InnerRef = Referencee->isReferenceType(); 1485 } 1486 1487 virtual CachedProperties getCachedProperties() const; 1488 1489public: 1490 bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; } 1491 bool isInnerRef() const { return ReferenceTypeBits.InnerRef; } 1492 1493 QualType getPointeeTypeAsWritten() const { return PointeeType; } 1494 QualType getPointeeType() const { 1495 // FIXME: this might strip inner qualifiers; okay? 1496 const ReferenceType *T = this; 1497 while (T->isInnerRef()) 1498 T = T->PointeeType->getAs<ReferenceType>(); 1499 return T->PointeeType; 1500 } 1501 1502 void Profile(llvm::FoldingSetNodeID &ID) { 1503 Profile(ID, PointeeType, isSpelledAsLValue()); 1504 } 1505 static void Profile(llvm::FoldingSetNodeID &ID, 1506 QualType Referencee, 1507 bool SpelledAsLValue) { 1508 ID.AddPointer(Referencee.getAsOpaquePtr()); 1509 ID.AddBoolean(SpelledAsLValue); 1510 } 1511 1512 static bool classof(const Type *T) { 1513 return T->getTypeClass() == LValueReference || 1514 T->getTypeClass() == RValueReference; 1515 } 1516 static bool classof(const ReferenceType *) { return true; } 1517}; 1518 1519/// LValueReferenceType - C++ [dcl.ref] - Lvalue reference 1520/// 1521class LValueReferenceType : public ReferenceType { 1522 LValueReferenceType(QualType Referencee, QualType CanonicalRef, 1523 bool SpelledAsLValue) : 1524 ReferenceType(LValueReference, Referencee, CanonicalRef, SpelledAsLValue) 1525 {} 1526 friend class ASTContext; // ASTContext creates these 1527public: 1528 bool isSugared() const { return false; } 1529 QualType desugar() const { return QualType(this, 0); } 1530 1531 static bool classof(const Type *T) { 1532 return T->getTypeClass() == LValueReference; 1533 } 1534 static bool classof(const LValueReferenceType *) { return true; } 1535}; 1536 1537/// RValueReferenceType - C++0x [dcl.ref] - Rvalue reference 1538/// 1539class RValueReferenceType : public ReferenceType { 1540 RValueReferenceType(QualType Referencee, QualType CanonicalRef) : 1541 ReferenceType(RValueReference, Referencee, CanonicalRef, false) { 1542 } 1543 friend class ASTContext; // ASTContext creates these 1544public: 1545 bool isSugared() const { return false; } 1546 QualType desugar() const { return QualType(this, 0); } 1547 1548 static bool classof(const Type *T) { 1549 return T->getTypeClass() == RValueReference; 1550 } 1551 static bool classof(const RValueReferenceType *) { return true; } 1552}; 1553 1554/// MemberPointerType - C++ 8.3.3 - Pointers to members 1555/// 1556class MemberPointerType : public Type, public llvm::FoldingSetNode { 1557 QualType PointeeType; 1558 /// The class of which the pointee is a member. Must ultimately be a 1559 /// RecordType, but could be a typedef or a template parameter too. 1560 const Type *Class; 1561 1562 MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) : 1563 Type(MemberPointer, CanonicalPtr, 1564 Cls->isDependentType() || Pointee->isDependentType(), 1565 Pointee->isVariablyModifiedType()), 1566 PointeeType(Pointee), Class(Cls) { 1567 } 1568 friend class ASTContext; // ASTContext creates these. 1569 1570protected: 1571 virtual CachedProperties getCachedProperties() const; 1572 1573public: 1574 QualType getPointeeType() const { return PointeeType; } 1575 1576 /// Returns true if the member type (i.e. the pointee type) is a 1577 /// function type rather than a data-member type. 1578 bool isMemberFunctionPointer() const { 1579 return PointeeType->isFunctionProtoType(); 1580 } 1581 1582 /// Returns true if the member type (i.e. the pointee type) is a 1583 /// data type rather than a function type. 1584 bool isMemberDataPointer() const { 1585 return !PointeeType->isFunctionProtoType(); 1586 } 1587 1588 const Type *getClass() const { return Class; } 1589 1590 bool isSugared() const { return false; } 1591 QualType desugar() const { return QualType(this, 0); } 1592 1593 void Profile(llvm::FoldingSetNodeID &ID) { 1594 Profile(ID, getPointeeType(), getClass()); 1595 } 1596 static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee, 1597 const Type *Class) { 1598 ID.AddPointer(Pointee.getAsOpaquePtr()); 1599 ID.AddPointer(Class); 1600 } 1601 1602 static bool classof(const Type *T) { 1603 return T->getTypeClass() == MemberPointer; 1604 } 1605 static bool classof(const MemberPointerType *) { return true; } 1606}; 1607 1608/// ArrayType - C99 6.7.5.2 - Array Declarators. 1609/// 1610class ArrayType : public Type, public llvm::FoldingSetNode { 1611public: 1612 /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4]) 1613 /// an array with a static size (e.g. int X[static 4]), or an array 1614 /// with a star size (e.g. int X[*]). 1615 /// 'static' is only allowed on function parameters. 1616 enum ArraySizeModifier { 1617 Normal, Static, Star 1618 }; 1619private: 1620 /// ElementType - The element type of the array. 1621 QualType ElementType; 1622 1623protected: 1624 // C++ [temp.dep.type]p1: 1625 // A type is dependent if it is... 1626 // - an array type constructed from any dependent type or whose 1627 // size is specified by a constant expression that is 1628 // value-dependent, 1629 ArrayType(TypeClass tc, QualType et, QualType can, 1630 ArraySizeModifier sm, unsigned tq) 1631 : Type(tc, can, et->isDependentType() || tc == DependentSizedArray, 1632 (tc == VariableArray || et->isVariablyModifiedType())), 1633 ElementType(et) { 1634 ArrayTypeBits.IndexTypeQuals = tq; 1635 ArrayTypeBits.SizeModifier = sm; 1636 } 1637 1638 friend class ASTContext; // ASTContext creates these. 1639 1640 virtual CachedProperties getCachedProperties() const; 1641 1642public: 1643 QualType getElementType() const { return ElementType; } 1644 ArraySizeModifier getSizeModifier() const { 1645 return ArraySizeModifier(ArrayTypeBits.SizeModifier); 1646 } 1647 Qualifiers getIndexTypeQualifiers() const { 1648 return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers()); 1649 } 1650 unsigned getIndexTypeCVRQualifiers() const { 1651 return ArrayTypeBits.IndexTypeQuals; 1652 } 1653 1654 static bool classof(const Type *T) { 1655 return T->getTypeClass() == ConstantArray || 1656 T->getTypeClass() == VariableArray || 1657 T->getTypeClass() == IncompleteArray || 1658 T->getTypeClass() == DependentSizedArray; 1659 } 1660 static bool classof(const ArrayType *) { return true; } 1661}; 1662 1663/// ConstantArrayType - This class represents the canonical version of 1664/// C arrays with a specified constant size. For example, the canonical 1665/// type for 'int A[4 + 4*100]' is a ConstantArrayType where the element 1666/// type is 'int' and the size is 404. 1667class ConstantArrayType : public ArrayType { 1668 llvm::APInt Size; // Allows us to unique the type. 1669 1670 ConstantArrayType(QualType et, QualType can, const llvm::APInt &size, 1671 ArraySizeModifier sm, unsigned tq) 1672 : ArrayType(ConstantArray, et, can, sm, tq), 1673 Size(size) {} 1674protected: 1675 ConstantArrayType(TypeClass tc, QualType et, QualType can, 1676 const llvm::APInt &size, ArraySizeModifier sm, unsigned tq) 1677 : ArrayType(tc, et, can, sm, tq), Size(size) {} 1678 friend class ASTContext; // ASTContext creates these. 1679public: 1680 const llvm::APInt &getSize() const { return Size; } 1681 bool isSugared() const { return false; } 1682 QualType desugar() const { return QualType(this, 0); } 1683 1684 1685 /// \brief Determine the number of bits required to address a member of 1686 // an array with the given element type and number of elements. 1687 static unsigned getNumAddressingBits(ASTContext &Context, 1688 QualType ElementType, 1689 const llvm::APInt &NumElements); 1690 1691 /// \brief Determine the maximum number of active bits that an array's size 1692 /// can require, which limits the maximum size of the array. 1693 static unsigned getMaxSizeBits(ASTContext &Context); 1694 1695 void Profile(llvm::FoldingSetNodeID &ID) { 1696 Profile(ID, getElementType(), getSize(), 1697 getSizeModifier(), getIndexTypeCVRQualifiers()); 1698 } 1699 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 1700 const llvm::APInt &ArraySize, ArraySizeModifier SizeMod, 1701 unsigned TypeQuals) { 1702 ID.AddPointer(ET.getAsOpaquePtr()); 1703 ID.AddInteger(ArraySize.getZExtValue()); 1704 ID.AddInteger(SizeMod); 1705 ID.AddInteger(TypeQuals); 1706 } 1707 static bool classof(const Type *T) { 1708 return T->getTypeClass() == ConstantArray; 1709 } 1710 static bool classof(const ConstantArrayType *) { return true; } 1711}; 1712 1713/// IncompleteArrayType - This class represents C arrays with an unspecified 1714/// size. For example 'int A[]' has an IncompleteArrayType where the element 1715/// type is 'int' and the size is unspecified. 1716class IncompleteArrayType : public ArrayType { 1717 1718 IncompleteArrayType(QualType et, QualType can, 1719 ArraySizeModifier sm, unsigned tq) 1720 : ArrayType(IncompleteArray, et, can, sm, tq) {} 1721 friend class ASTContext; // ASTContext creates these. 1722public: 1723 bool isSugared() const { return false; } 1724 QualType desugar() const { return QualType(this, 0); } 1725 1726 static bool classof(const Type *T) { 1727 return T->getTypeClass() == IncompleteArray; 1728 } 1729 static bool classof(const IncompleteArrayType *) { return true; } 1730 1731 friend class StmtIteratorBase; 1732 1733 void Profile(llvm::FoldingSetNodeID &ID) { 1734 Profile(ID, getElementType(), getSizeModifier(), 1735 getIndexTypeCVRQualifiers()); 1736 } 1737 1738 static void Profile(llvm::FoldingSetNodeID &ID, QualType ET, 1739 ArraySizeModifier SizeMod, unsigned TypeQuals) { 1740 ID.AddPointer(ET.getAsOpaquePtr()); 1741 ID.AddInteger(SizeMod); 1742 ID.AddInteger(TypeQuals); 1743 } 1744}; 1745 1746/// VariableArrayType - This class represents C arrays with a specified size 1747/// which is not an integer-constant-expression. For example, 'int s[x+foo()]'. 1748/// Since the size expression is an arbitrary expression, we store it as such. 1749/// 1750/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and 1751/// should not be: two lexically equivalent variable array types could mean 1752/// different things, for example, these variables do not have the same type 1753/// dynamically: 1754/// 1755/// void foo(int x) { 1756/// int Y[x]; 1757/// ++x; 1758/// int Z[x]; 1759/// } 1760/// 1761class VariableArrayType : public ArrayType { 1762 /// SizeExpr - An assignment expression. VLA's are only permitted within 1763 /// a function block. 1764 Stmt *SizeExpr; 1765 /// Brackets - The left and right array brackets. 1766 SourceRange Brackets; 1767 1768 VariableArrayType(QualType et, QualType can, Expr *e, 1769 ArraySizeModifier sm, unsigned tq, 1770 SourceRange brackets) 1771 : ArrayType(VariableArray, et, can, sm, tq), 1772 SizeExpr((Stmt*) e), Brackets(brackets) {} 1773 friend class ASTContext; // ASTContext creates these. 1774 1775public: 1776 Expr *getSizeExpr() const { 1777 // We use C-style casts instead of cast<> here because we do not wish 1778 // to have a dependency of Type.h on Stmt.h/Expr.h. 1779 return (Expr*) SizeExpr; 1780 } 1781 SourceRange getBracketsRange() const { return Brackets; } 1782 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 1783 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 1784 1785 bool isSugared() const { return false; } 1786 QualType desugar() const { return QualType(this, 0); } 1787 1788 static bool classof(const Type *T) { 1789 return T->getTypeClass() == VariableArray; 1790 } 1791 static bool classof(const VariableArrayType *) { return true; } 1792 1793 friend class StmtIteratorBase; 1794 1795 void Profile(llvm::FoldingSetNodeID &ID) { 1796 assert(0 && "Cannnot unique VariableArrayTypes."); 1797 } 1798}; 1799 1800/// DependentSizedArrayType - This type represents an array type in 1801/// C++ whose size is a value-dependent expression. For example: 1802/// 1803/// \code 1804/// template<typename T, int Size> 1805/// class array { 1806/// T data[Size]; 1807/// }; 1808/// \endcode 1809/// 1810/// For these types, we won't actually know what the array bound is 1811/// until template instantiation occurs, at which point this will 1812/// become either a ConstantArrayType or a VariableArrayType. 1813class DependentSizedArrayType : public ArrayType { 1814 ASTContext &Context; 1815 1816 /// \brief An assignment expression that will instantiate to the 1817 /// size of the array. 1818 /// 1819 /// The expression itself might be NULL, in which case the array 1820 /// type will have its size deduced from an initializer. 1821 Stmt *SizeExpr; 1822 1823 /// Brackets - The left and right array brackets. 1824 SourceRange Brackets; 1825 1826 DependentSizedArrayType(ASTContext &Context, QualType et, QualType can, 1827 Expr *e, ArraySizeModifier sm, unsigned tq, 1828 SourceRange brackets) 1829 : ArrayType(DependentSizedArray, et, can, sm, tq), 1830 Context(Context), SizeExpr((Stmt*) e), Brackets(brackets) {} 1831 friend class ASTContext; // ASTContext creates these. 1832 1833public: 1834 Expr *getSizeExpr() const { 1835 // We use C-style casts instead of cast<> here because we do not wish 1836 // to have a dependency of Type.h on Stmt.h/Expr.h. 1837 return (Expr*) SizeExpr; 1838 } 1839 SourceRange getBracketsRange() const { return Brackets; } 1840 SourceLocation getLBracketLoc() const { return Brackets.getBegin(); } 1841 SourceLocation getRBracketLoc() const { return Brackets.getEnd(); } 1842 1843 bool isSugared() const { return false; } 1844 QualType desugar() const { return QualType(this, 0); } 1845 1846 static bool classof(const Type *T) { 1847 return T->getTypeClass() == DependentSizedArray; 1848 } 1849 static bool classof(const DependentSizedArrayType *) { return true; } 1850 1851 friend class StmtIteratorBase; 1852 1853 1854 void Profile(llvm::FoldingSetNodeID &ID) { 1855 Profile(ID, Context, getElementType(), 1856 getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr()); 1857 } 1858 1859 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 1860 QualType ET, ArraySizeModifier SizeMod, 1861 unsigned TypeQuals, Expr *E); 1862}; 1863 1864/// DependentSizedExtVectorType - This type represent an extended vector type 1865/// where either the type or size is dependent. For example: 1866/// @code 1867/// template<typename T, int Size> 1868/// class vector { 1869/// typedef T __attribute__((ext_vector_type(Size))) type; 1870/// } 1871/// @endcode 1872class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode { 1873 ASTContext &Context; 1874 Expr *SizeExpr; 1875 /// ElementType - The element type of the array. 1876 QualType ElementType; 1877 SourceLocation loc; 1878 1879 DependentSizedExtVectorType(ASTContext &Context, QualType ElementType, 1880 QualType can, Expr *SizeExpr, SourceLocation loc) 1881 : Type(DependentSizedExtVector, can, /*Dependent=*/true, 1882 ElementType->isVariablyModifiedType()), 1883 Context(Context), SizeExpr(SizeExpr), ElementType(ElementType), 1884 loc(loc) {} 1885 friend class ASTContext; 1886 1887public: 1888 Expr *getSizeExpr() const { return SizeExpr; } 1889 QualType getElementType() const { return ElementType; } 1890 SourceLocation getAttributeLoc() const { return loc; } 1891 1892 bool isSugared() const { return false; } 1893 QualType desugar() const { return QualType(this, 0); } 1894 1895 static bool classof(const Type *T) { 1896 return T->getTypeClass() == DependentSizedExtVector; 1897 } 1898 static bool classof(const DependentSizedExtVectorType *) { return true; } 1899 1900 void Profile(llvm::FoldingSetNodeID &ID) { 1901 Profile(ID, Context, getElementType(), getSizeExpr()); 1902 } 1903 1904 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 1905 QualType ElementType, Expr *SizeExpr); 1906}; 1907 1908 1909/// VectorType - GCC generic vector type. This type is created using 1910/// __attribute__((vector_size(n)), where "n" specifies the vector size in 1911/// bytes; or from an Altivec __vector or vector declaration. 1912/// Since the constructor takes the number of vector elements, the 1913/// client is responsible for converting the size into the number of elements. 1914class VectorType : public Type, public llvm::FoldingSetNode { 1915public: 1916 enum AltiVecSpecific { 1917 NotAltiVec, // is not AltiVec vector 1918 AltiVec, // is AltiVec vector 1919 Pixel, // is AltiVec 'vector Pixel' 1920 Bool // is AltiVec 'vector bool ...' 1921 }; 1922protected: 1923 /// ElementType - The element type of the vector. 1924 QualType ElementType; 1925 1926 VectorType(QualType vecType, unsigned nElements, QualType canonType, 1927 AltiVecSpecific altiVecSpec) : 1928 Type(Vector, canonType, vecType->isDependentType(), 1929 vecType->isVariablyModifiedType()), ElementType(vecType) { 1930 VectorTypeBits.AltiVecSpec = altiVecSpec; 1931 VectorTypeBits.NumElements = nElements; 1932 } 1933 1934 VectorType(TypeClass tc, QualType vecType, unsigned nElements, 1935 QualType canonType, AltiVecSpecific altiVecSpec) 1936 : Type(tc, canonType, vecType->isDependentType(), 1937 vecType->isVariablyModifiedType()), ElementType(vecType) { 1938 VectorTypeBits.AltiVecSpec = altiVecSpec; 1939 VectorTypeBits.NumElements = nElements; 1940 } 1941 friend class ASTContext; // ASTContext creates these. 1942 1943 virtual CachedProperties getCachedProperties() const; 1944 1945public: 1946 1947 QualType getElementType() const { return ElementType; } 1948 unsigned getNumElements() const { return VectorTypeBits.NumElements; } 1949 1950 bool isSugared() const { return false; } 1951 QualType desugar() const { return QualType(this, 0); } 1952 1953 AltiVecSpecific getAltiVecSpecific() const { 1954 return AltiVecSpecific(VectorTypeBits.AltiVecSpec); 1955 } 1956 1957 void Profile(llvm::FoldingSetNodeID &ID) { 1958 Profile(ID, getElementType(), getNumElements(), 1959 getTypeClass(), getAltiVecSpecific()); 1960 } 1961 static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType, 1962 unsigned NumElements, TypeClass TypeClass, 1963 AltiVecSpecific AltiVecSpec) { 1964 ID.AddPointer(ElementType.getAsOpaquePtr()); 1965 ID.AddInteger(NumElements); 1966 ID.AddInteger(TypeClass); 1967 ID.AddInteger(AltiVecSpec); 1968 } 1969 1970 static bool classof(const Type *T) { 1971 return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector; 1972 } 1973 static bool classof(const VectorType *) { return true; } 1974}; 1975 1976/// ExtVectorType - Extended vector type. This type is created using 1977/// __attribute__((ext_vector_type(n)), where "n" is the number of elements. 1978/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This 1979/// class enables syntactic extensions, like Vector Components for accessing 1980/// points, colors, and textures (modeled after OpenGL Shading Language). 1981class ExtVectorType : public VectorType { 1982 ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) : 1983 VectorType(ExtVector, vecType, nElements, canonType, NotAltiVec) {} 1984 friend class ASTContext; // ASTContext creates these. 1985public: 1986 static int getPointAccessorIdx(char c) { 1987 switch (c) { 1988 default: return -1; 1989 case 'x': case 'r': return 0; 1990 case 'y': case 'g': return 1; 1991 case 'z': case 'b': return 2; 1992 case 'w': case 'a': return 3; 1993 } 1994 } 1995 static int getNumericAccessorIdx(char c) { 1996 switch (c) { 1997 default: return -1; 1998 case '0': return 0; 1999 case '1': return 1; 2000 case '2': return 2; 2001 case '3': return 3; 2002 case '4': return 4; 2003 case '5': return 5; 2004 case '6': return 6; 2005 case '7': return 7; 2006 case '8': return 8; 2007 case '9': return 9; 2008 case 'A': 2009 case 'a': return 10; 2010 case 'B': 2011 case 'b': return 11; 2012 case 'C': 2013 case 'c': return 12; 2014 case 'D': 2015 case 'd': return 13; 2016 case 'E': 2017 case 'e': return 14; 2018 case 'F': 2019 case 'f': return 15; 2020 } 2021 } 2022 2023 static int getAccessorIdx(char c) { 2024 if (int idx = getPointAccessorIdx(c)+1) return idx-1; 2025 return getNumericAccessorIdx(c); 2026 } 2027 2028 bool isAccessorWithinNumElements(char c) const { 2029 if (int idx = getAccessorIdx(c)+1) 2030 return unsigned(idx-1) < getNumElements(); 2031 return false; 2032 } 2033 bool isSugared() const { return false; } 2034 QualType desugar() const { return QualType(this, 0); } 2035 2036 static bool classof(const Type *T) { 2037 return T->getTypeClass() == ExtVector; 2038 } 2039 static bool classof(const ExtVectorType *) { return true; } 2040}; 2041 2042/// FunctionType - C99 6.7.5.3 - Function Declarators. This is the common base 2043/// class of FunctionNoProtoType and FunctionProtoType. 2044/// 2045class FunctionType : public Type { 2046 virtual void ANCHOR(); // Key function for FunctionType. 2047 2048 // The type returned by the function. 2049 QualType ResultType; 2050 2051 public: 2052 // This class is used for passing arround the information needed to 2053 // construct a call. It is not actually used for storage, just for 2054 // factoring together common arguments. 2055 // If you add a field (say Foo), other than the obvious places (both, constructors, 2056 // compile failures), what you need to update is 2057 // * Operetor== 2058 // * getFoo 2059 // * withFoo 2060 // * functionType. Add Foo, getFoo. 2061 // * ASTContext::getFooType 2062 // * ASTContext::mergeFunctionTypes 2063 // * FunctionNoProtoType::Profile 2064 // * FunctionProtoType::Profile 2065 // * TypePrinter::PrintFunctionProto 2066 // * AST read and write 2067 // * Codegen 2068 2069 class ExtInfo { 2070 enum { CallConvMask = 0x7 }; 2071 enum { NoReturnMask = 0x8 }; 2072 enum { RegParmMask = ~(CallConvMask | NoReturnMask), 2073 RegParmOffset = 4 }; 2074 2075 unsigned Bits; 2076 2077 ExtInfo(unsigned Bits) : Bits(Bits) {} 2078 2079 friend class FunctionType; 2080 2081 public: 2082 // Constructor with no defaults. Use this when you know that you 2083 // have all the elements (when reading an AST file for example). 2084 ExtInfo(bool noReturn, unsigned regParm, CallingConv cc) { 2085 Bits = ((unsigned) cc) | 2086 (noReturn ? NoReturnMask : 0) | 2087 (regParm << RegParmOffset); 2088 } 2089 2090 // Constructor with all defaults. Use when for example creating a 2091 // function know to use defaults. 2092 ExtInfo() : Bits(0) {} 2093 2094 bool getNoReturn() const { return Bits & NoReturnMask; } 2095 unsigned getRegParm() const { return Bits >> RegParmOffset; } 2096 CallingConv getCC() const { return CallingConv(Bits & CallConvMask); } 2097 2098 bool operator==(ExtInfo Other) const { 2099 return Bits == Other.Bits; 2100 } 2101 bool operator!=(ExtInfo Other) const { 2102 return Bits != Other.Bits; 2103 } 2104 2105 // Note that we don't have setters. That is by design, use 2106 // the following with methods instead of mutating these objects. 2107 2108 ExtInfo withNoReturn(bool noReturn) const { 2109 if (noReturn) 2110 return ExtInfo(Bits | NoReturnMask); 2111 else 2112 return ExtInfo(Bits & ~NoReturnMask); 2113 } 2114 2115 ExtInfo withRegParm(unsigned RegParm) const { 2116 return ExtInfo((Bits & ~RegParmMask) | (RegParm << RegParmOffset)); 2117 } 2118 2119 ExtInfo withCallingConv(CallingConv cc) const { 2120 return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc); 2121 } 2122 2123 void Profile(llvm::FoldingSetNodeID &ID) { 2124 ID.AddInteger(Bits); 2125 } 2126 }; 2127 2128protected: 2129 FunctionType(TypeClass tc, QualType res, bool SubclassInfo, 2130 unsigned typeQuals, QualType Canonical, bool Dependent, 2131 bool VariablyModified, ExtInfo Info) 2132 : Type(tc, Canonical, Dependent, VariablyModified), ResultType(res) { 2133 FunctionTypeBits.ExtInfo = Info.Bits; 2134 FunctionTypeBits.SubclassInfo = SubclassInfo; 2135 FunctionTypeBits.TypeQuals = typeQuals; 2136 } 2137 bool getSubClassData() const { return FunctionTypeBits.SubclassInfo; } 2138 unsigned getTypeQuals() const { return FunctionTypeBits.TypeQuals; } 2139public: 2140 2141 QualType getResultType() const { return ResultType; } 2142 2143 unsigned getRegParmType() const { return getExtInfo().getRegParm(); } 2144 bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); } 2145 CallingConv getCallConv() const { return getExtInfo().getCC(); } 2146 ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); } 2147 2148 /// \brief Determine the type of an expression that calls a function of 2149 /// this type. 2150 QualType getCallResultType(ASTContext &Context) const { 2151 return getResultType().getNonLValueExprType(Context); 2152 } 2153 2154 static llvm::StringRef getNameForCallConv(CallingConv CC); 2155 2156 static bool classof(const Type *T) { 2157 return T->getTypeClass() == FunctionNoProto || 2158 T->getTypeClass() == FunctionProto; 2159 } 2160 static bool classof(const FunctionType *) { return true; } 2161}; 2162 2163/// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has 2164/// no information available about its arguments. 2165class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode { 2166 FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info) 2167 : FunctionType(FunctionNoProto, Result, false, 0, Canonical, 2168 /*Dependent=*/false, Result->isVariablyModifiedType(), 2169 Info) {} 2170 friend class ASTContext; // ASTContext creates these. 2171 2172protected: 2173 virtual CachedProperties getCachedProperties() const; 2174 2175public: 2176 // No additional state past what FunctionType provides. 2177 2178 bool isSugared() const { return false; } 2179 QualType desugar() const { return QualType(this, 0); } 2180 2181 void Profile(llvm::FoldingSetNodeID &ID) { 2182 Profile(ID, getResultType(), getExtInfo()); 2183 } 2184 static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType, 2185 ExtInfo Info) { 2186 Info.Profile(ID); 2187 ID.AddPointer(ResultType.getAsOpaquePtr()); 2188 } 2189 2190 static bool classof(const Type *T) { 2191 return T->getTypeClass() == FunctionNoProto; 2192 } 2193 static bool classof(const FunctionNoProtoType *) { return true; } 2194}; 2195 2196/// FunctionProtoType - Represents a prototype with argument type info, e.g. 2197/// 'int foo(int)' or 'int foo(void)'. 'void' is represented as having no 2198/// arguments, not as having a single void argument. Such a type can have an 2199/// exception specification, but this specification is not part of the canonical 2200/// type. 2201class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode { 2202 FunctionProtoType(QualType Result, const QualType *ArgArray, unsigned numArgs, 2203 bool isVariadic, unsigned typeQuals, bool hasExs, 2204 bool hasAnyExs, const QualType *ExArray, 2205 unsigned numExs, QualType Canonical, 2206 const ExtInfo &Info); 2207 2208 /// NumArgs - The number of arguments this function has, not counting '...'. 2209 unsigned NumArgs : 20; 2210 2211 /// NumExceptions - The number of types in the exception spec, if any. 2212 unsigned NumExceptions : 10; 2213 2214 /// HasExceptionSpec - Whether this function has an exception spec at all. 2215 bool HasExceptionSpec : 1; 2216 2217 /// AnyExceptionSpec - Whether this function has a throw(...) spec. 2218 bool AnyExceptionSpec : 1; 2219 2220 /// ArgInfo - There is an variable size array after the class in memory that 2221 /// holds the argument types. 2222 2223 /// Exceptions - There is another variable size array after ArgInfo that 2224 /// holds the exception types. 2225 2226 friend class ASTContext; // ASTContext creates these. 2227 2228protected: 2229 virtual CachedProperties getCachedProperties() const; 2230 2231public: 2232 unsigned getNumArgs() const { return NumArgs; } 2233 QualType getArgType(unsigned i) const { 2234 assert(i < NumArgs && "Invalid argument number!"); 2235 return arg_type_begin()[i]; 2236 } 2237 2238 bool hasExceptionSpec() const { return HasExceptionSpec; } 2239 bool hasAnyExceptionSpec() const { return AnyExceptionSpec; } 2240 unsigned getNumExceptions() const { return NumExceptions; } 2241 QualType getExceptionType(unsigned i) const { 2242 assert(i < NumExceptions && "Invalid exception number!"); 2243 return exception_begin()[i]; 2244 } 2245 bool hasEmptyExceptionSpec() const { 2246 return hasExceptionSpec() && !hasAnyExceptionSpec() && 2247 getNumExceptions() == 0; 2248 } 2249 2250 bool isVariadic() const { return getSubClassData(); } 2251 unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); } 2252 2253 typedef const QualType *arg_type_iterator; 2254 arg_type_iterator arg_type_begin() const { 2255 return reinterpret_cast<const QualType *>(this+1); 2256 } 2257 arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; } 2258 2259 typedef const QualType *exception_iterator; 2260 exception_iterator exception_begin() const { 2261 // exceptions begin where arguments end 2262 return arg_type_end(); 2263 } 2264 exception_iterator exception_end() const { 2265 return exception_begin() + NumExceptions; 2266 } 2267 2268 bool isSugared() const { return false; } 2269 QualType desugar() const { return QualType(this, 0); } 2270 2271 static bool classof(const Type *T) { 2272 return T->getTypeClass() == FunctionProto; 2273 } 2274 static bool classof(const FunctionProtoType *) { return true; } 2275 2276 void Profile(llvm::FoldingSetNodeID &ID); 2277 static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, 2278 arg_type_iterator ArgTys, unsigned NumArgs, 2279 bool isVariadic, unsigned TypeQuals, 2280 bool hasExceptionSpec, bool anyExceptionSpec, 2281 unsigned NumExceptions, exception_iterator Exs, 2282 ExtInfo ExtInfo); 2283}; 2284 2285 2286/// \brief Represents the dependent type named by a dependently-scoped 2287/// typename using declaration, e.g. 2288/// using typename Base<T>::foo; 2289/// Template instantiation turns these into the underlying type. 2290class UnresolvedUsingType : public Type { 2291 UnresolvedUsingTypenameDecl *Decl; 2292 2293 UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D) 2294 : Type(UnresolvedUsing, QualType(), true, false), 2295 Decl(const_cast<UnresolvedUsingTypenameDecl*>(D)) {} 2296 friend class ASTContext; // ASTContext creates these. 2297public: 2298 2299 UnresolvedUsingTypenameDecl *getDecl() const { return Decl; } 2300 2301 bool isSugared() const { return false; } 2302 QualType desugar() const { return QualType(this, 0); } 2303 2304 static bool classof(const Type *T) { 2305 return T->getTypeClass() == UnresolvedUsing; 2306 } 2307 static bool classof(const UnresolvedUsingType *) { return true; } 2308 2309 void Profile(llvm::FoldingSetNodeID &ID) { 2310 return Profile(ID, Decl); 2311 } 2312 static void Profile(llvm::FoldingSetNodeID &ID, 2313 UnresolvedUsingTypenameDecl *D) { 2314 ID.AddPointer(D); 2315 } 2316}; 2317 2318 2319class TypedefType : public Type { 2320 TypedefDecl *Decl; 2321protected: 2322 TypedefType(TypeClass tc, const TypedefDecl *D, QualType can) 2323 : Type(tc, can, can->isDependentType(), can->isVariablyModifiedType()), 2324 Decl(const_cast<TypedefDecl*>(D)) { 2325 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 2326 } 2327 friend class ASTContext; // ASTContext creates these. 2328public: 2329 2330 TypedefDecl *getDecl() const { return Decl; } 2331 2332 /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to 2333 /// potentially looking through *all* consecutive typedefs. This returns the 2334 /// sum of the type qualifiers, so if you have: 2335 /// typedef const int A; 2336 /// typedef volatile A B; 2337 /// looking through the typedefs for B will give you "const volatile A". 2338 QualType LookThroughTypedefs() const; 2339 2340 bool isSugared() const { return true; } 2341 QualType desugar() const; 2342 2343 static bool classof(const Type *T) { return T->getTypeClass() == Typedef; } 2344 static bool classof(const TypedefType *) { return true; } 2345}; 2346 2347/// TypeOfExprType (GCC extension). 2348class TypeOfExprType : public Type { 2349 Expr *TOExpr; 2350 2351protected: 2352 TypeOfExprType(Expr *E, QualType can = QualType()); 2353 friend class ASTContext; // ASTContext creates these. 2354public: 2355 Expr *getUnderlyingExpr() const { return TOExpr; } 2356 2357 /// \brief Remove a single level of sugar. 2358 QualType desugar() const; 2359 2360 /// \brief Returns whether this type directly provides sugar. 2361 bool isSugared() const { return true; } 2362 2363 static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; } 2364 static bool classof(const TypeOfExprType *) { return true; } 2365}; 2366 2367/// \brief Internal representation of canonical, dependent 2368/// typeof(expr) types. 2369/// 2370/// This class is used internally by the ASTContext to manage 2371/// canonical, dependent types, only. Clients will only see instances 2372/// of this class via TypeOfExprType nodes. 2373class DependentTypeOfExprType 2374 : public TypeOfExprType, public llvm::FoldingSetNode { 2375 ASTContext &Context; 2376 2377public: 2378 DependentTypeOfExprType(ASTContext &Context, Expr *E) 2379 : TypeOfExprType(E), Context(Context) { } 2380 2381 bool isSugared() const { return false; } 2382 QualType desugar() const { return QualType(this, 0); } 2383 2384 void Profile(llvm::FoldingSetNodeID &ID) { 2385 Profile(ID, Context, getUnderlyingExpr()); 2386 } 2387 2388 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 2389 Expr *E); 2390}; 2391 2392/// TypeOfType (GCC extension). 2393class TypeOfType : public Type { 2394 QualType TOType; 2395 TypeOfType(QualType T, QualType can) 2396 : Type(TypeOf, can, T->isDependentType(), T->isVariablyModifiedType()), 2397 TOType(T) { 2398 assert(!isa<TypedefType>(can) && "Invalid canonical type"); 2399 } 2400 friend class ASTContext; // ASTContext creates these. 2401public: 2402 QualType getUnderlyingType() const { return TOType; } 2403 2404 /// \brief Remove a single level of sugar. 2405 QualType desugar() const { return getUnderlyingType(); } 2406 2407 /// \brief Returns whether this type directly provides sugar. 2408 bool isSugared() const { return true; } 2409 2410 static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; } 2411 static bool classof(const TypeOfType *) { return true; } 2412}; 2413 2414/// DecltypeType (C++0x) 2415class DecltypeType : public Type { 2416 Expr *E; 2417 2418 // FIXME: We could get rid of UnderlyingType if we wanted to: We would have to 2419 // Move getDesugaredType to ASTContext so that it can call getDecltypeForExpr 2420 // from it. 2421 QualType UnderlyingType; 2422 2423protected: 2424 DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType()); 2425 friend class ASTContext; // ASTContext creates these. 2426public: 2427 Expr *getUnderlyingExpr() const { return E; } 2428 QualType getUnderlyingType() const { return UnderlyingType; } 2429 2430 /// \brief Remove a single level of sugar. 2431 QualType desugar() const { return getUnderlyingType(); } 2432 2433 /// \brief Returns whether this type directly provides sugar. 2434 bool isSugared() const { return !isDependentType(); } 2435 2436 static bool classof(const Type *T) { return T->getTypeClass() == Decltype; } 2437 static bool classof(const DecltypeType *) { return true; } 2438}; 2439 2440/// \brief Internal representation of canonical, dependent 2441/// decltype(expr) types. 2442/// 2443/// This class is used internally by the ASTContext to manage 2444/// canonical, dependent types, only. Clients will only see instances 2445/// of this class via DecltypeType nodes. 2446class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode { 2447 ASTContext &Context; 2448 2449public: 2450 DependentDecltypeType(ASTContext &Context, Expr *E); 2451 2452 bool isSugared() const { return false; } 2453 QualType desugar() const { return QualType(this, 0); } 2454 2455 void Profile(llvm::FoldingSetNodeID &ID) { 2456 Profile(ID, Context, getUnderlyingExpr()); 2457 } 2458 2459 static void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context, 2460 Expr *E); 2461}; 2462 2463class TagType : public Type { 2464 /// Stores the TagDecl associated with this type. The decl may point to any 2465 /// TagDecl that declares the entity. 2466 TagDecl * decl; 2467 2468protected: 2469 TagType(TypeClass TC, const TagDecl *D, QualType can); 2470 2471 virtual CachedProperties getCachedProperties() const; 2472 2473public: 2474 TagDecl *getDecl() const; 2475 2476 /// @brief Determines whether this type is in the process of being 2477 /// defined. 2478 bool isBeingDefined() const; 2479 2480 static bool classof(const Type *T) { 2481 return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast; 2482 } 2483 static bool classof(const TagType *) { return true; } 2484 static bool classof(const RecordType *) { return true; } 2485 static bool classof(const EnumType *) { return true; } 2486}; 2487 2488/// RecordType - This is a helper class that allows the use of isa/cast/dyncast 2489/// to detect TagType objects of structs/unions/classes. 2490class RecordType : public TagType { 2491protected: 2492 explicit RecordType(const RecordDecl *D) 2493 : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) { } 2494 explicit RecordType(TypeClass TC, RecordDecl *D) 2495 : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) { } 2496 friend class ASTContext; // ASTContext creates these. 2497public: 2498 2499 RecordDecl *getDecl() const { 2500 return reinterpret_cast<RecordDecl*>(TagType::getDecl()); 2501 } 2502 2503 // FIXME: This predicate is a helper to QualType/Type. It needs to 2504 // recursively check all fields for const-ness. If any field is declared 2505 // const, it needs to return false. 2506 bool hasConstFields() const { return false; } 2507 2508 // FIXME: RecordType needs to check when it is created that all fields are in 2509 // the same address space, and return that. 2510 unsigned getAddressSpace() const { return 0; } 2511 2512 bool isSugared() const { return false; } 2513 QualType desugar() const { return QualType(this, 0); } 2514 2515 static bool classof(const TagType *T); 2516 static bool classof(const Type *T) { 2517 return isa<TagType>(T) && classof(cast<TagType>(T)); 2518 } 2519 static bool classof(const RecordType *) { return true; } 2520}; 2521 2522/// EnumType - This is a helper class that allows the use of isa/cast/dyncast 2523/// to detect TagType objects of enums. 2524class EnumType : public TagType { 2525 explicit EnumType(const EnumDecl *D) 2526 : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) { } 2527 friend class ASTContext; // ASTContext creates these. 2528public: 2529 2530 EnumDecl *getDecl() const { 2531 return reinterpret_cast<EnumDecl*>(TagType::getDecl()); 2532 } 2533 2534 bool isSugared() const { return false; } 2535 QualType desugar() const { return QualType(this, 0); } 2536 2537 static bool classof(const TagType *T); 2538 static bool classof(const Type *T) { 2539 return isa<TagType>(T) && classof(cast<TagType>(T)); 2540 } 2541 static bool classof(const EnumType *) { return true; } 2542}; 2543 2544class TemplateTypeParmType : public Type, public llvm::FoldingSetNode { 2545 unsigned Depth : 15; 2546 unsigned ParameterPack : 1; 2547 unsigned Index : 16; 2548 IdentifierInfo *Name; 2549 2550 TemplateTypeParmType(unsigned D, unsigned I, bool PP, IdentifierInfo *N, 2551 QualType Canon) 2552 : Type(TemplateTypeParm, Canon, /*Dependent=*/true, 2553 /*VariablyModified=*/false), 2554 Depth(D), ParameterPack(PP), Index(I), Name(N) { } 2555 2556 TemplateTypeParmType(unsigned D, unsigned I, bool PP) 2557 : Type(TemplateTypeParm, QualType(this, 0), /*Dependent=*/true, 2558 /*VariablyModified=*/false), 2559 Depth(D), ParameterPack(PP), Index(I), Name(0) { } 2560 2561 friend class ASTContext; // ASTContext creates these 2562 2563public: 2564 unsigned getDepth() const { return Depth; } 2565 unsigned getIndex() const { return Index; } 2566 bool isParameterPack() const { return ParameterPack; } 2567 IdentifierInfo *getName() const { return Name; } 2568 2569 bool isSugared() const { return false; } 2570 QualType desugar() const { return QualType(this, 0); } 2571 2572 void Profile(llvm::FoldingSetNodeID &ID) { 2573 Profile(ID, Depth, Index, ParameterPack, Name); 2574 } 2575 2576 static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth, 2577 unsigned Index, bool ParameterPack, 2578 IdentifierInfo *Name) { 2579 ID.AddInteger(Depth); 2580 ID.AddInteger(Index); 2581 ID.AddBoolean(ParameterPack); 2582 ID.AddPointer(Name); 2583 } 2584 2585 static bool classof(const Type *T) { 2586 return T->getTypeClass() == TemplateTypeParm; 2587 } 2588 static bool classof(const TemplateTypeParmType *T) { return true; } 2589}; 2590 2591/// \brief Represents the result of substituting a type for a template 2592/// type parameter. 2593/// 2594/// Within an instantiated template, all template type parameters have 2595/// been replaced with these. They are used solely to record that a 2596/// type was originally written as a template type parameter; 2597/// therefore they are never canonical. 2598class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode { 2599 // The original type parameter. 2600 const TemplateTypeParmType *Replaced; 2601 2602 SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon) 2603 : Type(SubstTemplateTypeParm, Canon, Canon->isDependentType(), 2604 Canon->isVariablyModifiedType()), 2605 Replaced(Param) { } 2606 2607 friend class ASTContext; 2608 2609public: 2610 IdentifierInfo *getName() const { return Replaced->getName(); } 2611 2612 /// Gets the template parameter that was substituted for. 2613 const TemplateTypeParmType *getReplacedParameter() const { 2614 return Replaced; 2615 } 2616 2617 /// Gets the type that was substituted for the template 2618 /// parameter. 2619 QualType getReplacementType() const { 2620 return getCanonicalTypeInternal(); 2621 } 2622 2623 bool isSugared() const { return true; } 2624 QualType desugar() const { return getReplacementType(); } 2625 2626 void Profile(llvm::FoldingSetNodeID &ID) { 2627 Profile(ID, getReplacedParameter(), getReplacementType()); 2628 } 2629 static void Profile(llvm::FoldingSetNodeID &ID, 2630 const TemplateTypeParmType *Replaced, 2631 QualType Replacement) { 2632 ID.AddPointer(Replaced); 2633 ID.AddPointer(Replacement.getAsOpaquePtr()); 2634 } 2635 2636 static bool classof(const Type *T) { 2637 return T->getTypeClass() == SubstTemplateTypeParm; 2638 } 2639 static bool classof(const SubstTemplateTypeParmType *T) { return true; } 2640}; 2641 2642/// \brief Represents the type of a template specialization as written 2643/// in the source code. 2644/// 2645/// Template specialization types represent the syntactic form of a 2646/// template-id that refers to a type, e.g., @c vector<int>. Some 2647/// template specialization types are syntactic sugar, whose canonical 2648/// type will point to some other type node that represents the 2649/// instantiation or class template specialization. For example, a 2650/// class template specialization type of @c vector<int> will refer to 2651/// a tag type for the instantiation 2652/// @c std::vector<int, std::allocator<int>>. 2653/// 2654/// Other template specialization types, for which the template name 2655/// is dependent, may be canonical types. These types are always 2656/// dependent. 2657class TemplateSpecializationType 2658 : public Type, public llvm::FoldingSetNode { 2659 /// \brief The name of the template being specialized. 2660 TemplateName Template; 2661 2662 /// \brief - The number of template arguments named in this class 2663 /// template specialization. 2664 unsigned NumArgs; 2665 2666 TemplateSpecializationType(TemplateName T, 2667 const TemplateArgument *Args, 2668 unsigned NumArgs, QualType Canon); 2669 2670 friend class ASTContext; // ASTContext creates these 2671 2672public: 2673 /// \brief Determine whether any of the given template arguments are 2674 /// dependent. 2675 static bool anyDependentTemplateArguments(const TemplateArgument *Args, 2676 unsigned NumArgs); 2677 2678 static bool anyDependentTemplateArguments(const TemplateArgumentLoc *Args, 2679 unsigned NumArgs); 2680 2681 static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &); 2682 2683 /// \brief Print a template argument list, including the '<' and '>' 2684 /// enclosing the template arguments. 2685 static std::string PrintTemplateArgumentList(const TemplateArgument *Args, 2686 unsigned NumArgs, 2687 const PrintingPolicy &Policy); 2688 2689 static std::string PrintTemplateArgumentList(const TemplateArgumentLoc *Args, 2690 unsigned NumArgs, 2691 const PrintingPolicy &Policy); 2692 2693 static std::string PrintTemplateArgumentList(const TemplateArgumentListInfo &, 2694 const PrintingPolicy &Policy); 2695 2696 /// True if this template specialization type matches a current 2697 /// instantiation in the context in which it is found. 2698 bool isCurrentInstantiation() const { 2699 return isa<InjectedClassNameType>(getCanonicalTypeInternal()); 2700 } 2701 2702 typedef const TemplateArgument * iterator; 2703 2704 iterator begin() const { return getArgs(); } 2705 iterator end() const; // defined inline in TemplateBase.h 2706 2707 /// \brief Retrieve the name of the template that we are specializing. 2708 TemplateName getTemplateName() const { return Template; } 2709 2710 /// \brief Retrieve the template arguments. 2711 const TemplateArgument *getArgs() const { 2712 return reinterpret_cast<const TemplateArgument *>(this + 1); 2713 } 2714 2715 /// \brief Retrieve the number of template arguments. 2716 unsigned getNumArgs() const { return NumArgs; } 2717 2718 /// \brief Retrieve a specific template argument as a type. 2719 /// \precondition @c isArgType(Arg) 2720 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 2721 2722 bool isSugared() const { 2723 return !isDependentType() || isCurrentInstantiation(); 2724 } 2725 QualType desugar() const { return getCanonicalTypeInternal(); } 2726 2727 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Ctx) { 2728 Profile(ID, Template, getArgs(), NumArgs, Ctx); 2729 } 2730 2731 static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T, 2732 const TemplateArgument *Args, 2733 unsigned NumArgs, 2734 ASTContext &Context); 2735 2736 static bool classof(const Type *T) { 2737 return T->getTypeClass() == TemplateSpecialization; 2738 } 2739 static bool classof(const TemplateSpecializationType *T) { return true; } 2740}; 2741 2742/// \brief The injected class name of a C++ class template or class 2743/// template partial specialization. Used to record that a type was 2744/// spelled with a bare identifier rather than as a template-id; the 2745/// equivalent for non-templated classes is just RecordType. 2746/// 2747/// Injected class name types are always dependent. Template 2748/// instantiation turns these into RecordTypes. 2749/// 2750/// Injected class name types are always canonical. This works 2751/// because it is impossible to compare an injected class name type 2752/// with the corresponding non-injected template type, for the same 2753/// reason that it is impossible to directly compare template 2754/// parameters from different dependent contexts: injected class name 2755/// types can only occur within the scope of a particular templated 2756/// declaration, and within that scope every template specialization 2757/// will canonicalize to the injected class name (when appropriate 2758/// according to the rules of the language). 2759class InjectedClassNameType : public Type { 2760 CXXRecordDecl *Decl; 2761 2762 /// The template specialization which this type represents. 2763 /// For example, in 2764 /// template <class T> class A { ... }; 2765 /// this is A<T>, whereas in 2766 /// template <class X, class Y> class A<B<X,Y> > { ... }; 2767 /// this is A<B<X,Y> >. 2768 /// 2769 /// It is always unqualified, always a template specialization type, 2770 /// and always dependent. 2771 QualType InjectedType; 2772 2773 friend class ASTContext; // ASTContext creates these. 2774 friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not 2775 // currently suitable for AST reading, too much 2776 // interdependencies. 2777 InjectedClassNameType(CXXRecordDecl *D, QualType TST) 2778 : Type(InjectedClassName, QualType(), /*Dependent=*/true, 2779 /*VariablyModified=*/false), 2780 Decl(D), InjectedType(TST) { 2781 assert(isa<TemplateSpecializationType>(TST)); 2782 assert(!TST.hasQualifiers()); 2783 assert(TST->isDependentType()); 2784 } 2785 2786public: 2787 QualType getInjectedSpecializationType() const { return InjectedType; } 2788 const TemplateSpecializationType *getInjectedTST() const { 2789 return cast<TemplateSpecializationType>(InjectedType.getTypePtr()); 2790 } 2791 2792 CXXRecordDecl *getDecl() const; 2793 2794 bool isSugared() const { return false; } 2795 QualType desugar() const { return QualType(this, 0); } 2796 2797 static bool classof(const Type *T) { 2798 return T->getTypeClass() == InjectedClassName; 2799 } 2800 static bool classof(const InjectedClassNameType *T) { return true; } 2801}; 2802 2803/// \brief The kind of a tag type. 2804enum TagTypeKind { 2805 /// \brief The "struct" keyword. 2806 TTK_Struct, 2807 /// \brief The "union" keyword. 2808 TTK_Union, 2809 /// \brief The "class" keyword. 2810 TTK_Class, 2811 /// \brief The "enum" keyword. 2812 TTK_Enum 2813}; 2814 2815/// \brief The elaboration keyword that precedes a qualified type name or 2816/// introduces an elaborated-type-specifier. 2817enum ElaboratedTypeKeyword { 2818 /// \brief The "struct" keyword introduces the elaborated-type-specifier. 2819 ETK_Struct, 2820 /// \brief The "union" keyword introduces the elaborated-type-specifier. 2821 ETK_Union, 2822 /// \brief The "class" keyword introduces the elaborated-type-specifier. 2823 ETK_Class, 2824 /// \brief The "enum" keyword introduces the elaborated-type-specifier. 2825 ETK_Enum, 2826 /// \brief The "typename" keyword precedes the qualified type name, e.g., 2827 /// \c typename T::type. 2828 ETK_Typename, 2829 /// \brief No keyword precedes the qualified type name. 2830 ETK_None 2831}; 2832 2833/// A helper class for Type nodes having an ElaboratedTypeKeyword. 2834/// The keyword in stored in the free bits of the base class. 2835/// Also provides a few static helpers for converting and printing 2836/// elaborated type keyword and tag type kind enumerations. 2837class TypeWithKeyword : public Type { 2838protected: 2839 TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc, 2840 QualType Canonical, bool Dependent, bool VariablyModified) 2841 : Type(tc, Canonical, Dependent, VariablyModified) { 2842 TypeWithKeywordBits.Keyword = Keyword; 2843 } 2844 2845public: 2846 virtual ~TypeWithKeyword(); // pin vtable to Type.cpp 2847 2848 ElaboratedTypeKeyword getKeyword() const { 2849 return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword); 2850 } 2851 2852 /// getKeywordForTypeSpec - Converts a type specifier (DeclSpec::TST) 2853 /// into an elaborated type keyword. 2854 static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec); 2855 2856 /// getTagTypeKindForTypeSpec - Converts a type specifier (DeclSpec::TST) 2857 /// into a tag type kind. It is an error to provide a type specifier 2858 /// which *isn't* a tag kind here. 2859 static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec); 2860 2861 /// getKeywordForTagDeclKind - Converts a TagTypeKind into an 2862 /// elaborated type keyword. 2863 static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag); 2864 2865 /// getTagTypeKindForKeyword - Converts an elaborated type keyword into 2866 // a TagTypeKind. It is an error to provide an elaborated type keyword 2867 /// which *isn't* a tag kind here. 2868 static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword); 2869 2870 static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword); 2871 2872 static const char *getKeywordName(ElaboratedTypeKeyword Keyword); 2873 2874 static const char *getTagTypeKindName(TagTypeKind Kind) { 2875 return getKeywordName(getKeywordForTagTypeKind(Kind)); 2876 } 2877 2878 class CannotCastToThisType {}; 2879 static CannotCastToThisType classof(const Type *); 2880}; 2881 2882/// \brief Represents a type that was referred to using an elaborated type 2883/// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type, 2884/// or both. 2885/// 2886/// This type is used to keep track of a type name as written in the 2887/// source code, including tag keywords and any nested-name-specifiers. 2888/// The type itself is always "sugar", used to express what was written 2889/// in the source code but containing no additional semantic information. 2890class ElaboratedType : public TypeWithKeyword, public llvm::FoldingSetNode { 2891 2892 /// \brief The nested name specifier containing the qualifier. 2893 NestedNameSpecifier *NNS; 2894 2895 /// \brief The type that this qualified name refers to. 2896 QualType NamedType; 2897 2898 ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 2899 QualType NamedType, QualType CanonType) 2900 : TypeWithKeyword(Keyword, Elaborated, CanonType, 2901 NamedType->isDependentType(), 2902 NamedType->isVariablyModifiedType()), 2903 NNS(NNS), NamedType(NamedType) { 2904 assert(!(Keyword == ETK_None && NNS == 0) && 2905 "ElaboratedType cannot have elaborated type keyword " 2906 "and name qualifier both null."); 2907 } 2908 2909 friend class ASTContext; // ASTContext creates these 2910 2911public: 2912 ~ElaboratedType(); 2913 2914 /// \brief Retrieve the qualification on this type. 2915 NestedNameSpecifier *getQualifier() const { return NNS; } 2916 2917 /// \brief Retrieve the type named by the qualified-id. 2918 QualType getNamedType() const { return NamedType; } 2919 2920 /// \brief Remove a single level of sugar. 2921 QualType desugar() const { return getNamedType(); } 2922 2923 /// \brief Returns whether this type directly provides sugar. 2924 bool isSugared() const { return true; } 2925 2926 void Profile(llvm::FoldingSetNodeID &ID) { 2927 Profile(ID, getKeyword(), NNS, NamedType); 2928 } 2929 2930 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 2931 NestedNameSpecifier *NNS, QualType NamedType) { 2932 ID.AddInteger(Keyword); 2933 ID.AddPointer(NNS); 2934 NamedType.Profile(ID); 2935 } 2936 2937 static bool classof(const Type *T) { 2938 return T->getTypeClass() == Elaborated; 2939 } 2940 static bool classof(const ElaboratedType *T) { return true; } 2941}; 2942 2943/// \brief Represents a qualified type name for which the type name is 2944/// dependent. 2945/// 2946/// DependentNameType represents a class of dependent types that involve a 2947/// dependent nested-name-specifier (e.g., "T::") followed by a (dependent) 2948/// name of a type. The DependentNameType may start with a "typename" (for a 2949/// typename-specifier), "class", "struct", "union", or "enum" (for a 2950/// dependent elaborated-type-specifier), or nothing (in contexts where we 2951/// know that we must be referring to a type, e.g., in a base class specifier). 2952class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode { 2953 2954 /// \brief The nested name specifier containing the qualifier. 2955 NestedNameSpecifier *NNS; 2956 2957 /// \brief The type that this typename specifier refers to. 2958 const IdentifierInfo *Name; 2959 2960 DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS, 2961 const IdentifierInfo *Name, QualType CanonType) 2962 : TypeWithKeyword(Keyword, DependentName, CanonType, /*Dependent=*/true, 2963 /*VariablyModified=*/false), 2964 NNS(NNS), Name(Name) { 2965 assert(NNS->isDependent() && 2966 "DependentNameType requires a dependent nested-name-specifier"); 2967 } 2968 2969 friend class ASTContext; // ASTContext creates these 2970 2971public: 2972 virtual ~DependentNameType(); 2973 2974 /// \brief Retrieve the qualification on this type. 2975 NestedNameSpecifier *getQualifier() const { return NNS; } 2976 2977 /// \brief Retrieve the type named by the typename specifier as an 2978 /// identifier. 2979 /// 2980 /// This routine will return a non-NULL identifier pointer when the 2981 /// form of the original typename was terminated by an identifier, 2982 /// e.g., "typename T::type". 2983 const IdentifierInfo *getIdentifier() const { 2984 return Name; 2985 } 2986 2987 bool isSugared() const { return false; } 2988 QualType desugar() const { return QualType(this, 0); } 2989 2990 void Profile(llvm::FoldingSetNodeID &ID) { 2991 Profile(ID, getKeyword(), NNS, Name); 2992 } 2993 2994 static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword, 2995 NestedNameSpecifier *NNS, const IdentifierInfo *Name) { 2996 ID.AddInteger(Keyword); 2997 ID.AddPointer(NNS); 2998 ID.AddPointer(Name); 2999 } 3000 3001 static bool classof(const Type *T) { 3002 return T->getTypeClass() == DependentName; 3003 } 3004 static bool classof(const DependentNameType *T) { return true; } 3005}; 3006 3007/// DependentTemplateSpecializationType - Represents a template 3008/// specialization type whose template cannot be resolved, e.g. 3009/// A<T>::template B<T> 3010class DependentTemplateSpecializationType : 3011 public TypeWithKeyword, public llvm::FoldingSetNode { 3012 3013 /// \brief The nested name specifier containing the qualifier. 3014 NestedNameSpecifier *NNS; 3015 3016 /// \brief The identifier of the template. 3017 const IdentifierInfo *Name; 3018 3019 /// \brief - The number of template arguments named in this class 3020 /// template specialization. 3021 unsigned NumArgs; 3022 3023 const TemplateArgument *getArgBuffer() const { 3024 return reinterpret_cast<const TemplateArgument*>(this+1); 3025 } 3026 TemplateArgument *getArgBuffer() { 3027 return reinterpret_cast<TemplateArgument*>(this+1); 3028 } 3029 3030 DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword, 3031 NestedNameSpecifier *NNS, 3032 const IdentifierInfo *Name, 3033 unsigned NumArgs, 3034 const TemplateArgument *Args, 3035 QualType Canon); 3036 3037 friend class ASTContext; // ASTContext creates these 3038 3039public: 3040 virtual ~DependentTemplateSpecializationType(); 3041 3042 NestedNameSpecifier *getQualifier() const { return NNS; } 3043 const IdentifierInfo *getIdentifier() const { return Name; } 3044 3045 /// \brief Retrieve the template arguments. 3046 const TemplateArgument *getArgs() const { 3047 return getArgBuffer(); 3048 } 3049 3050 /// \brief Retrieve the number of template arguments. 3051 unsigned getNumArgs() const { return NumArgs; } 3052 3053 const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h 3054 3055 typedef const TemplateArgument * iterator; 3056 iterator begin() const { return getArgs(); } 3057 iterator end() const; // inline in TemplateBase.h 3058 3059 bool isSugared() const { return false; } 3060 QualType desugar() const { return QualType(this, 0); } 3061 3062 void Profile(llvm::FoldingSetNodeID &ID, ASTContext &Context) { 3063 Profile(ID, Context, getKeyword(), NNS, Name, NumArgs, getArgs()); 3064 } 3065 3066 static void Profile(llvm::FoldingSetNodeID &ID, 3067 ASTContext &Context, 3068 ElaboratedTypeKeyword Keyword, 3069 NestedNameSpecifier *Qualifier, 3070 const IdentifierInfo *Name, 3071 unsigned NumArgs, 3072 const TemplateArgument *Args); 3073 3074 static bool classof(const Type *T) { 3075 return T->getTypeClass() == DependentTemplateSpecialization; 3076 } 3077 static bool classof(const DependentTemplateSpecializationType *T) { 3078 return true; 3079 } 3080}; 3081 3082/// ObjCObjectType - Represents a class type in Objective C. 3083/// Every Objective C type is a combination of a base type and a 3084/// list of protocols. 3085/// 3086/// Given the following declarations: 3087/// @class C; 3088/// @protocol P; 3089/// 3090/// 'C' is an ObjCInterfaceType C. It is sugar for an ObjCObjectType 3091/// with base C and no protocols. 3092/// 3093/// 'C<P>' is an ObjCObjectType with base C and protocol list [P]. 3094/// 3095/// 'id' is a TypedefType which is sugar for an ObjCPointerType whose 3096/// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType 3097/// and no protocols. 3098/// 3099/// 'id<P>' is an ObjCPointerType whose pointee is an ObjCObjecType 3100/// with base BuiltinType::ObjCIdType and protocol list [P]. Eventually 3101/// this should get its own sugar class to better represent the source. 3102class ObjCObjectType : public Type { 3103 // ObjCObjectType.NumProtocols - the number of protocols stored 3104 // after the ObjCObjectPointerType node. 3105 // 3106 // These protocols are those written directly on the type. If 3107 // protocol qualifiers ever become additive, the iterators will need 3108 // to get kindof complicated. 3109 // 3110 // In the canonical object type, these are sorted alphabetically 3111 // and uniqued. 3112 3113 /// Either a BuiltinType or an InterfaceType or sugar for either. 3114 QualType BaseType; 3115 3116 ObjCProtocolDecl * const *getProtocolStorage() const { 3117 return const_cast<ObjCObjectType*>(this)->getProtocolStorage(); 3118 } 3119 3120 ObjCProtocolDecl **getProtocolStorage(); 3121 3122protected: 3123 ObjCObjectType(QualType Canonical, QualType Base, 3124 ObjCProtocolDecl * const *Protocols, unsigned NumProtocols); 3125 3126 enum Nonce_ObjCInterface { Nonce_ObjCInterface }; 3127 ObjCObjectType(enum Nonce_ObjCInterface) 3128 : Type(ObjCInterface, QualType(), false, false), 3129 BaseType(QualType(this_(), 0)) { 3130 ObjCObjectTypeBits.NumProtocols = 0; 3131 } 3132 3133protected: 3134 CachedProperties getCachedProperties() const; // key function 3135 3136public: 3137 /// getBaseType - Gets the base type of this object type. This is 3138 /// always (possibly sugar for) one of: 3139 /// - the 'id' builtin type (as opposed to the 'id' type visible to the 3140 /// user, which is a typedef for an ObjCPointerType) 3141 /// - the 'Class' builtin type (same caveat) 3142 /// - an ObjCObjectType (currently always an ObjCInterfaceType) 3143 QualType getBaseType() const { return BaseType; } 3144 3145 bool isObjCId() const { 3146 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId); 3147 } 3148 bool isObjCClass() const { 3149 return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass); 3150 } 3151 bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); } 3152 bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); } 3153 bool isObjCUnqualifiedIdOrClass() const { 3154 if (!qual_empty()) return false; 3155 if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>()) 3156 return T->getKind() == BuiltinType::ObjCId || 3157 T->getKind() == BuiltinType::ObjCClass; 3158 return false; 3159 } 3160 bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); } 3161 bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); } 3162 3163 /// Gets the interface declaration for this object type, if the base type 3164 /// really is an interface. 3165 ObjCInterfaceDecl *getInterface() const; 3166 3167 typedef ObjCProtocolDecl * const *qual_iterator; 3168 3169 qual_iterator qual_begin() const { return getProtocolStorage(); } 3170 qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); } 3171 3172 bool qual_empty() const { return getNumProtocols() == 0; } 3173 3174 /// getNumProtocols - Return the number of qualifying protocols in this 3175 /// interface type, or 0 if there are none. 3176 unsigned getNumProtocols() const { return ObjCObjectTypeBits.NumProtocols; } 3177 3178 /// \brief Fetch a protocol by index. 3179 ObjCProtocolDecl *getProtocol(unsigned I) const { 3180 assert(I < getNumProtocols() && "Out-of-range protocol access"); 3181 return qual_begin()[I]; 3182 } 3183 3184 bool isSugared() const { return false; } 3185 QualType desugar() const { return QualType(this, 0); } 3186 3187 static bool classof(const Type *T) { 3188 return T->getTypeClass() == ObjCObject || 3189 T->getTypeClass() == ObjCInterface; 3190 } 3191 static bool classof(const ObjCObjectType *) { return true; } 3192}; 3193 3194/// ObjCObjectTypeImpl - A class providing a concrete implementation 3195/// of ObjCObjectType, so as to not increase the footprint of 3196/// ObjCInterfaceType. Code outside of ASTContext and the core type 3197/// system should not reference this type. 3198class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode { 3199 friend class ASTContext; 3200 3201 // If anyone adds fields here, ObjCObjectType::getProtocolStorage() 3202 // will need to be modified. 3203 3204 ObjCObjectTypeImpl(QualType Canonical, QualType Base, 3205 ObjCProtocolDecl * const *Protocols, 3206 unsigned NumProtocols) 3207 : ObjCObjectType(Canonical, Base, Protocols, NumProtocols) {} 3208 3209public: 3210 void Profile(llvm::FoldingSetNodeID &ID); 3211 static void Profile(llvm::FoldingSetNodeID &ID, 3212 QualType Base, 3213 ObjCProtocolDecl *const *protocols, 3214 unsigned NumProtocols); 3215}; 3216 3217inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorage() { 3218 return reinterpret_cast<ObjCProtocolDecl**>( 3219 static_cast<ObjCObjectTypeImpl*>(this) + 1); 3220} 3221 3222/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for 3223/// object oriented design. They basically correspond to C++ classes. There 3224/// are two kinds of interface types, normal interfaces like "NSString" and 3225/// qualified interfaces, which are qualified with a protocol list like 3226/// "NSString<NSCopyable, NSAmazing>". 3227/// 3228/// ObjCInterfaceType guarantees the following properties when considered 3229/// as a subtype of its superclass, ObjCObjectType: 3230/// - There are no protocol qualifiers. To reinforce this, code which 3231/// tries to invoke the protocol methods via an ObjCInterfaceType will 3232/// fail to compile. 3233/// - It is its own base type. That is, if T is an ObjCInterfaceType*, 3234/// T->getBaseType() == QualType(T, 0). 3235class ObjCInterfaceType : public ObjCObjectType { 3236 ObjCInterfaceDecl *Decl; 3237 3238 ObjCInterfaceType(const ObjCInterfaceDecl *D) 3239 : ObjCObjectType(Nonce_ObjCInterface), 3240 Decl(const_cast<ObjCInterfaceDecl*>(D)) {} 3241 friend class ASTContext; // ASTContext creates these. 3242 3243protected: 3244 virtual CachedProperties getCachedProperties() const; 3245 3246public: 3247 /// getDecl - Get the declaration of this interface. 3248 ObjCInterfaceDecl *getDecl() const { return Decl; } 3249 3250 bool isSugared() const { return false; } 3251 QualType desugar() const { return QualType(this, 0); } 3252 3253 static bool classof(const Type *T) { 3254 return T->getTypeClass() == ObjCInterface; 3255 } 3256 static bool classof(const ObjCInterfaceType *) { return true; } 3257 3258 // Nonsense to "hide" certain members of ObjCObjectType within this 3259 // class. People asking for protocols on an ObjCInterfaceType are 3260 // not going to get what they want: ObjCInterfaceTypes are 3261 // guaranteed to have no protocols. 3262 enum { 3263 qual_iterator, 3264 qual_begin, 3265 qual_end, 3266 getNumProtocols, 3267 getProtocol 3268 }; 3269}; 3270 3271inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const { 3272 if (const ObjCInterfaceType *T = 3273 getBaseType()->getAs<ObjCInterfaceType>()) 3274 return T->getDecl(); 3275 return 0; 3276} 3277 3278/// ObjCObjectPointerType - Used to represent a pointer to an 3279/// Objective C object. These are constructed from pointer 3280/// declarators when the pointee type is an ObjCObjectType (or sugar 3281/// for one). In addition, the 'id' and 'Class' types are typedefs 3282/// for these, and the protocol-qualified types 'id<P>' and 'Class<P>' 3283/// are translated into these. 3284/// 3285/// Pointers to pointers to Objective C objects are still PointerTypes; 3286/// only the first level of pointer gets it own type implementation. 3287class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode { 3288 QualType PointeeType; 3289 3290 ObjCObjectPointerType(QualType Canonical, QualType Pointee) 3291 : Type(ObjCObjectPointer, Canonical, false, false), 3292 PointeeType(Pointee) {} 3293 friend class ASTContext; // ASTContext creates these. 3294 3295protected: 3296 virtual CachedProperties getCachedProperties() const; 3297 3298public: 3299 /// getPointeeType - Gets the type pointed to by this ObjC pointer. 3300 /// The result will always be an ObjCObjectType or sugar thereof. 3301 QualType getPointeeType() const { return PointeeType; } 3302 3303 /// getObjCObjectType - Gets the type pointed to by this ObjC 3304 /// pointer. This method always returns non-null. 3305 /// 3306 /// This method is equivalent to getPointeeType() except that 3307 /// it discards any typedefs (or other sugar) between this 3308 /// type and the "outermost" object type. So for: 3309 /// @class A; @protocol P; @protocol Q; 3310 /// typedef A<P> AP; 3311 /// typedef A A1; 3312 /// typedef A1<P> A1P; 3313 /// typedef A1P<Q> A1PQ; 3314 /// For 'A*', getObjectType() will return 'A'. 3315 /// For 'A<P>*', getObjectType() will return 'A<P>'. 3316 /// For 'AP*', getObjectType() will return 'A<P>'. 3317 /// For 'A1*', getObjectType() will return 'A'. 3318 /// For 'A1<P>*', getObjectType() will return 'A1<P>'. 3319 /// For 'A1P*', getObjectType() will return 'A1<P>'. 3320 /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because 3321 /// adding protocols to a protocol-qualified base discards the 3322 /// old qualifiers (for now). But if it didn't, getObjectType() 3323 /// would return 'A1P<Q>' (and we'd have to make iterating over 3324 /// qualifiers more complicated). 3325 const ObjCObjectType *getObjectType() const { 3326 return PointeeType->getAs<ObjCObjectType>(); 3327 } 3328 3329 /// getInterfaceType - If this pointer points to an Objective C 3330 /// @interface type, gets the type for that interface. Any protocol 3331 /// qualifiers on the interface are ignored. 3332 /// 3333 /// \return null if the base type for this pointer is 'id' or 'Class' 3334 const ObjCInterfaceType *getInterfaceType() const { 3335 return getObjectType()->getBaseType()->getAs<ObjCInterfaceType>(); 3336 } 3337 3338 /// getInterfaceDecl - If this pointer points to an Objective @interface 3339 /// type, gets the declaration for that interface. 3340 /// 3341 /// \return null if the base type for this pointer is 'id' or 'Class' 3342 ObjCInterfaceDecl *getInterfaceDecl() const { 3343 return getObjectType()->getInterface(); 3344 } 3345 3346 /// isObjCIdType - True if this is equivalent to the 'id' type, i.e. if 3347 /// its object type is the primitive 'id' type with no protocols. 3348 bool isObjCIdType() const { 3349 return getObjectType()->isObjCUnqualifiedId(); 3350 } 3351 3352 /// isObjCClassType - True if this is equivalent to the 'Class' type, 3353 /// i.e. if its object tive is the primitive 'Class' type with no protocols. 3354 bool isObjCClassType() const { 3355 return getObjectType()->isObjCUnqualifiedClass(); 3356 } 3357 3358 /// isObjCQualifiedIdType - True if this is equivalent to 'id<P>' for some 3359 /// non-empty set of protocols. 3360 bool isObjCQualifiedIdType() const { 3361 return getObjectType()->isObjCQualifiedId(); 3362 } 3363 3364 /// isObjCQualifiedClassType - True if this is equivalent to 'Class<P>' for 3365 /// some non-empty set of protocols. 3366 bool isObjCQualifiedClassType() const { 3367 return getObjectType()->isObjCQualifiedClass(); 3368 } 3369 3370 /// An iterator over the qualifiers on the object type. Provided 3371 /// for convenience. This will always iterate over the full set of 3372 /// protocols on a type, not just those provided directly. 3373 typedef ObjCObjectType::qual_iterator qual_iterator; 3374 3375 qual_iterator qual_begin() const { 3376 return getObjectType()->qual_begin(); 3377 } 3378 qual_iterator qual_end() const { 3379 return getObjectType()->qual_end(); 3380 } 3381 bool qual_empty() const { return getObjectType()->qual_empty(); } 3382 3383 /// getNumProtocols - Return the number of qualifying protocols on 3384 /// the object type. 3385 unsigned getNumProtocols() const { 3386 return getObjectType()->getNumProtocols(); 3387 } 3388 3389 /// \brief Retrieve a qualifying protocol by index on the object 3390 /// type. 3391 ObjCProtocolDecl *getProtocol(unsigned I) const { 3392 return getObjectType()->getProtocol(I); 3393 } 3394 3395 bool isSugared() const { return false; } 3396 QualType desugar() const { return QualType(this, 0); } 3397 3398 void Profile(llvm::FoldingSetNodeID &ID) { 3399 Profile(ID, getPointeeType()); 3400 } 3401 static void Profile(llvm::FoldingSetNodeID &ID, QualType T) { 3402 ID.AddPointer(T.getAsOpaquePtr()); 3403 } 3404 static bool classof(const Type *T) { 3405 return T->getTypeClass() == ObjCObjectPointer; 3406 } 3407 static bool classof(const ObjCObjectPointerType *) { return true; } 3408}; 3409 3410/// A qualifier set is used to build a set of qualifiers. 3411class QualifierCollector : public Qualifiers { 3412 ASTContext *Context; 3413 3414public: 3415 QualifierCollector(Qualifiers Qs = Qualifiers()) 3416 : Qualifiers(Qs), Context(0) {} 3417 QualifierCollector(ASTContext &Context, Qualifiers Qs = Qualifiers()) 3418 : Qualifiers(Qs), Context(&Context) {} 3419 3420 void setContext(ASTContext &C) { Context = &C; } 3421 3422 /// Collect any qualifiers on the given type and return an 3423 /// unqualified type. 3424 const Type *strip(QualType QT) { 3425 addFastQualifiers(QT.getLocalFastQualifiers()); 3426 if (QT.hasLocalNonFastQualifiers()) { 3427 const ExtQuals *EQ = QT.getExtQualsUnsafe(); 3428 Context = &EQ->getContext(); 3429 addQualifiers(EQ->getQualifiers()); 3430 return EQ->getBaseType(); 3431 } 3432 return QT.getTypePtrUnsafe(); 3433 } 3434 3435 /// Apply the collected qualifiers to the given type. 3436 QualType apply(QualType QT) const; 3437 3438 /// Apply the collected qualifiers to the given type. 3439 QualType apply(const Type* T) const; 3440 3441}; 3442 3443 3444// Inline function definitions. 3445 3446inline bool QualType::isCanonical() const { 3447 const Type *T = getTypePtr(); 3448 if (hasLocalQualifiers()) 3449 return T->isCanonicalUnqualified() && !isa<ArrayType>(T); 3450 return T->isCanonicalUnqualified(); 3451} 3452 3453inline bool QualType::isCanonicalAsParam() const { 3454 if (hasLocalQualifiers()) return false; 3455 3456 const Type *T = getTypePtr(); 3457 if ((*this)->isPointerType()) { 3458 QualType BaseType = (*this)->getAs<PointerType>()->getPointeeType(); 3459 if (isa<VariableArrayType>(BaseType)) { 3460 ArrayType *AT = dyn_cast<ArrayType>(BaseType); 3461 VariableArrayType *VAT = cast<VariableArrayType>(AT); 3462 if (VAT->getSizeExpr()) 3463 T = BaseType.getTypePtr(); 3464 } 3465 } 3466 return T->isCanonicalUnqualified() && 3467 !isa<FunctionType>(T) && !isa<ArrayType>(T); 3468} 3469 3470inline bool QualType::isConstQualified() const { 3471 return isLocalConstQualified() || 3472 getTypePtr()->getCanonicalTypeInternal().isLocalConstQualified(); 3473} 3474 3475inline bool QualType::isRestrictQualified() const { 3476 return isLocalRestrictQualified() || 3477 getTypePtr()->getCanonicalTypeInternal().isLocalRestrictQualified(); 3478} 3479 3480 3481inline bool QualType::isVolatileQualified() const { 3482 return isLocalVolatileQualified() || 3483 getTypePtr()->getCanonicalTypeInternal().isLocalVolatileQualified(); 3484} 3485 3486inline bool QualType::hasQualifiers() const { 3487 return hasLocalQualifiers() || 3488 getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers(); 3489} 3490 3491inline Qualifiers QualType::getQualifiers() const { 3492 Qualifiers Quals = getLocalQualifiers(); 3493 Quals.addQualifiers( 3494 getTypePtr()->getCanonicalTypeInternal().getLocalQualifiers()); 3495 return Quals; 3496} 3497 3498inline unsigned QualType::getCVRQualifiers() const { 3499 return getLocalCVRQualifiers() | 3500 getTypePtr()->getCanonicalTypeInternal().getLocalCVRQualifiers(); 3501} 3502 3503/// getCVRQualifiersThroughArrayTypes - If there are CVR qualifiers for this 3504/// type, returns them. Otherwise, if this is an array type, recurses 3505/// on the element type until some qualifiers have been found or a non-array 3506/// type reached. 3507inline unsigned QualType::getCVRQualifiersThroughArrayTypes() const { 3508 if (unsigned Quals = getCVRQualifiers()) 3509 return Quals; 3510 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 3511 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 3512 return AT->getElementType().getCVRQualifiersThroughArrayTypes(); 3513 return 0; 3514} 3515 3516inline void QualType::removeConst() { 3517 removeFastQualifiers(Qualifiers::Const); 3518} 3519 3520inline void QualType::removeRestrict() { 3521 removeFastQualifiers(Qualifiers::Restrict); 3522} 3523 3524inline void QualType::removeVolatile() { 3525 QualifierCollector Qc; 3526 const Type *Ty = Qc.strip(*this); 3527 if (Qc.hasVolatile()) { 3528 Qc.removeVolatile(); 3529 *this = Qc.apply(Ty); 3530 } 3531} 3532 3533inline void QualType::removeCVRQualifiers(unsigned Mask) { 3534 assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits"); 3535 3536 // Fast path: we don't need to touch the slow qualifiers. 3537 if (!(Mask & ~Qualifiers::FastMask)) { 3538 removeFastQualifiers(Mask); 3539 return; 3540 } 3541 3542 QualifierCollector Qc; 3543 const Type *Ty = Qc.strip(*this); 3544 Qc.removeCVRQualifiers(Mask); 3545 *this = Qc.apply(Ty); 3546} 3547 3548/// getAddressSpace - Return the address space of this type. 3549inline unsigned QualType::getAddressSpace() const { 3550 if (hasLocalNonFastQualifiers()) { 3551 const ExtQuals *EQ = getExtQualsUnsafe(); 3552 if (EQ->hasAddressSpace()) 3553 return EQ->getAddressSpace(); 3554 } 3555 3556 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 3557 if (CT.hasLocalNonFastQualifiers()) { 3558 const ExtQuals *EQ = CT.getExtQualsUnsafe(); 3559 if (EQ->hasAddressSpace()) 3560 return EQ->getAddressSpace(); 3561 } 3562 3563 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 3564 return AT->getElementType().getAddressSpace(); 3565 if (const RecordType *RT = dyn_cast<RecordType>(CT)) 3566 return RT->getAddressSpace(); 3567 return 0; 3568} 3569 3570/// getObjCGCAttr - Return the gc attribute of this type. 3571inline Qualifiers::GC QualType::getObjCGCAttr() const { 3572 if (hasLocalNonFastQualifiers()) { 3573 const ExtQuals *EQ = getExtQualsUnsafe(); 3574 if (EQ->hasObjCGCAttr()) 3575 return EQ->getObjCGCAttr(); 3576 } 3577 3578 QualType CT = getTypePtr()->getCanonicalTypeInternal(); 3579 if (CT.hasLocalNonFastQualifiers()) { 3580 const ExtQuals *EQ = CT.getExtQualsUnsafe(); 3581 if (EQ->hasObjCGCAttr()) 3582 return EQ->getObjCGCAttr(); 3583 } 3584 3585 if (const ArrayType *AT = dyn_cast<ArrayType>(CT)) 3586 return AT->getElementType().getObjCGCAttr(); 3587 if (const ObjCObjectPointerType *PT = CT->getAs<ObjCObjectPointerType>()) 3588 return PT->getPointeeType().getObjCGCAttr(); 3589 // We most look at all pointer types, not just pointer to interface types. 3590 if (const PointerType *PT = CT->getAs<PointerType>()) 3591 return PT->getPointeeType().getObjCGCAttr(); 3592 return Qualifiers::GCNone; 3593} 3594 3595inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) { 3596 if (const PointerType *PT = t.getAs<PointerType>()) { 3597 if (const FunctionType *FT = PT->getPointeeType()->getAs<FunctionType>()) 3598 return FT->getExtInfo(); 3599 } else if (const FunctionType *FT = t.getAs<FunctionType>()) 3600 return FT->getExtInfo(); 3601 3602 return FunctionType::ExtInfo(); 3603} 3604 3605inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) { 3606 return getFunctionExtInfo(*t); 3607} 3608 3609/// \brief Determine whether this set of qualifiers is a superset of the given 3610/// set of qualifiers. 3611inline bool Qualifiers::isSupersetOf(Qualifiers Other) const { 3612 return Mask != Other.Mask && (Mask | Other.Mask) == Mask; 3613} 3614 3615/// isMoreQualifiedThan - Determine whether this type is more 3616/// qualified than the Other type. For example, "const volatile int" 3617/// is more qualified than "const int", "volatile int", and 3618/// "int". However, it is not more qualified than "const volatile 3619/// int". 3620inline bool QualType::isMoreQualifiedThan(QualType Other) const { 3621 // FIXME: work on arbitrary qualifiers 3622 unsigned MyQuals = this->getCVRQualifiersThroughArrayTypes(); 3623 unsigned OtherQuals = Other.getCVRQualifiersThroughArrayTypes(); 3624 if (getAddressSpace() != Other.getAddressSpace()) 3625 return false; 3626 return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals; 3627} 3628 3629/// isAtLeastAsQualifiedAs - Determine whether this type is at last 3630/// as qualified as the Other type. For example, "const volatile 3631/// int" is at least as qualified as "const int", "volatile int", 3632/// "int", and "const volatile int". 3633inline bool QualType::isAtLeastAsQualifiedAs(QualType Other) const { 3634 // FIXME: work on arbitrary qualifiers 3635 unsigned MyQuals = this->getCVRQualifiersThroughArrayTypes(); 3636 unsigned OtherQuals = Other.getCVRQualifiersThroughArrayTypes(); 3637 if (getAddressSpace() != Other.getAddressSpace()) 3638 return false; 3639 return (MyQuals | OtherQuals) == MyQuals; 3640} 3641 3642/// getNonReferenceType - If Type is a reference type (e.g., const 3643/// int&), returns the type that the reference refers to ("const 3644/// int"). Otherwise, returns the type itself. This routine is used 3645/// throughout Sema to implement C++ 5p6: 3646/// 3647/// If an expression initially has the type "reference to T" (8.3.2, 3648/// 8.5.3), the type is adjusted to "T" prior to any further 3649/// analysis, the expression designates the object or function 3650/// denoted by the reference, and the expression is an lvalue. 3651inline QualType QualType::getNonReferenceType() const { 3652 if (const ReferenceType *RefType = (*this)->getAs<ReferenceType>()) 3653 return RefType->getPointeeType(); 3654 else 3655 return *this; 3656} 3657 3658inline bool Type::isFunctionType() const { 3659 return isa<FunctionType>(CanonicalType); 3660} 3661inline bool Type::isPointerType() const { 3662 return isa<PointerType>(CanonicalType); 3663} 3664inline bool Type::isAnyPointerType() const { 3665 return isPointerType() || isObjCObjectPointerType(); 3666} 3667inline bool Type::isBlockPointerType() const { 3668 return isa<BlockPointerType>(CanonicalType); 3669} 3670inline bool Type::isReferenceType() const { 3671 return isa<ReferenceType>(CanonicalType); 3672} 3673inline bool Type::isLValueReferenceType() const { 3674 return isa<LValueReferenceType>(CanonicalType); 3675} 3676inline bool Type::isRValueReferenceType() const { 3677 return isa<RValueReferenceType>(CanonicalType); 3678} 3679inline bool Type::isFunctionPointerType() const { 3680 if (const PointerType* T = getAs<PointerType>()) 3681 return T->getPointeeType()->isFunctionType(); 3682 else 3683 return false; 3684} 3685inline bool Type::isMemberPointerType() const { 3686 return isa<MemberPointerType>(CanonicalType); 3687} 3688inline bool Type::isMemberFunctionPointerType() const { 3689 if (const MemberPointerType* T = getAs<MemberPointerType>()) 3690 return T->isMemberFunctionPointer(); 3691 else 3692 return false; 3693} 3694inline bool Type::isMemberDataPointerType() const { 3695 if (const MemberPointerType* T = getAs<MemberPointerType>()) 3696 return T->isMemberDataPointer(); 3697 else 3698 return false; 3699} 3700inline bool Type::isArrayType() const { 3701 return isa<ArrayType>(CanonicalType); 3702} 3703inline bool Type::isConstantArrayType() const { 3704 return isa<ConstantArrayType>(CanonicalType); 3705} 3706inline bool Type::isIncompleteArrayType() const { 3707 return isa<IncompleteArrayType>(CanonicalType); 3708} 3709inline bool Type::isVariableArrayType() const { 3710 return isa<VariableArrayType>(CanonicalType); 3711} 3712inline bool Type::isDependentSizedArrayType() const { 3713 return isa<DependentSizedArrayType>(CanonicalType); 3714} 3715inline bool Type::isBuiltinType() const { 3716 return isa<BuiltinType>(CanonicalType); 3717} 3718inline bool Type::isRecordType() const { 3719 return isa<RecordType>(CanonicalType); 3720} 3721inline bool Type::isEnumeralType() const { 3722 return isa<EnumType>(CanonicalType); 3723} 3724inline bool Type::isAnyComplexType() const { 3725 return isa<ComplexType>(CanonicalType); 3726} 3727inline bool Type::isVectorType() const { 3728 return isa<VectorType>(CanonicalType); 3729} 3730inline bool Type::isExtVectorType() const { 3731 return isa<ExtVectorType>(CanonicalType); 3732} 3733inline bool Type::isObjCObjectPointerType() const { 3734 return isa<ObjCObjectPointerType>(CanonicalType); 3735} 3736inline bool Type::isObjCObjectType() const { 3737 return isa<ObjCObjectType>(CanonicalType); 3738} 3739inline bool Type::isObjCObjectOrInterfaceType() const { 3740 return isa<ObjCInterfaceType>(CanonicalType) || 3741 isa<ObjCObjectType>(CanonicalType); 3742} 3743 3744inline bool Type::isObjCQualifiedIdType() const { 3745 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3746 return OPT->isObjCQualifiedIdType(); 3747 return false; 3748} 3749inline bool Type::isObjCQualifiedClassType() const { 3750 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3751 return OPT->isObjCQualifiedClassType(); 3752 return false; 3753} 3754inline bool Type::isObjCIdType() const { 3755 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3756 return OPT->isObjCIdType(); 3757 return false; 3758} 3759inline bool Type::isObjCClassType() const { 3760 if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>()) 3761 return OPT->isObjCClassType(); 3762 return false; 3763} 3764inline bool Type::isObjCSelType() const { 3765 if (const PointerType *OPT = getAs<PointerType>()) 3766 return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel); 3767 return false; 3768} 3769inline bool Type::isObjCBuiltinType() const { 3770 return isObjCIdType() || isObjCClassType() || isObjCSelType(); 3771} 3772inline bool Type::isTemplateTypeParmType() const { 3773 return isa<TemplateTypeParmType>(CanonicalType); 3774} 3775 3776inline bool Type::isSpecificBuiltinType(unsigned K) const { 3777 if (const BuiltinType *BT = getAs<BuiltinType>()) 3778 if (BT->getKind() == (BuiltinType::Kind) K) 3779 return true; 3780 return false; 3781} 3782 3783inline bool Type::isPlaceholderType() const { 3784 if (const BuiltinType *BT = getAs<BuiltinType>()) 3785 return BT->isPlaceholderType(); 3786 return false; 3787} 3788 3789/// \brief Determines whether this is a type for which one can define 3790/// an overloaded operator. 3791inline bool Type::isOverloadableType() const { 3792 return isDependentType() || isRecordType() || isEnumeralType(); 3793} 3794 3795inline bool Type::hasPointerRepresentation() const { 3796 return (isPointerType() || isReferenceType() || isBlockPointerType() || 3797 isObjCObjectPointerType() || isNullPtrType()); 3798} 3799 3800inline bool Type::hasObjCPointerRepresentation() const { 3801 return isObjCObjectPointerType(); 3802} 3803 3804/// Insertion operator for diagnostics. This allows sending QualType's into a 3805/// diagnostic with <<. 3806inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 3807 QualType T) { 3808 DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 3809 Diagnostic::ak_qualtype); 3810 return DB; 3811} 3812 3813/// Insertion operator for partial diagnostics. This allows sending QualType's 3814/// into a diagnostic with <<. 3815inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD, 3816 QualType T) { 3817 PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()), 3818 Diagnostic::ak_qualtype); 3819 return PD; 3820} 3821 3822// Helper class template that is used by Type::getAs to ensure that one does 3823// not try to look through a qualified type to get to an array type. 3824template<typename T, 3825 bool isArrayType = (llvm::is_same<T, ArrayType>::value || 3826 llvm::is_base_of<ArrayType, T>::value)> 3827struct ArrayType_cannot_be_used_with_getAs { }; 3828 3829template<typename T> 3830struct ArrayType_cannot_be_used_with_getAs<T, true>; 3831 3832/// Member-template getAs<specific type>'. 3833template <typename T> const T *Type::getAs() const { 3834 ArrayType_cannot_be_used_with_getAs<T> at; 3835 (void)at; 3836 3837 // If this is directly a T type, return it. 3838 if (const T *Ty = dyn_cast<T>(this)) 3839 return Ty; 3840 3841 // If the canonical form of this type isn't the right kind, reject it. 3842 if (!isa<T>(CanonicalType)) 3843 return 0; 3844 3845 // If this is a typedef for the type, strip the typedef off without 3846 // losing all typedef information. 3847 return cast<T>(getUnqualifiedDesugaredType()); 3848} 3849 3850} // end namespace clang 3851 3852#endif 3853