Type.h revision b43d87b0646aa04951056c7e0d1ab9a58eb09f66
1ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch//===--- Type.h - C Language Family Type Representation ---------*- C++ -*-===//
2ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch//
3ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch//                     The LLVM Compiler Infrastructure
4ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch//
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// License. See LICENSE.TXT for details.
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)//
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)//
1058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch//  This file defines the Type interface and subclasses.
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===----------------------------------------------------------------------===//
13effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#ifndef LLVM_CLANG_AST_TYPE_H
1558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#define LLVM_CLANG_AST_TYPE_H
16a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "clang/Basic/Diagnostic.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/ExceptionSpecificationType.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/IdentifierTable.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/Linkage.h"
21ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "clang/Basic/PartialDiagnostic.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Basic/Visibility.h"
2358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "clang/Basic/Specifiers.h"
2458e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "clang/AST/NestedNameSpecifier.h"
2558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "clang/AST/TemplateName.h"
2658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "llvm/Support/type_traits.h"
2758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "llvm/Support/ErrorHandling.h"
2858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch#include "llvm/ADT/APSInt.h"
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "llvm/ADT/FoldingSet.h"
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "llvm/ADT/Optional.h"
31010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "llvm/ADT/PointerIntPair.h"
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "llvm/ADT/PointerUnion.h"
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "llvm/ADT/Twine.h"
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "clang/Basic/LLVM.h"
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace clang {
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  enum {
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    TypeAlignmentInBits = 4,
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    TypeAlignment = 1 << TypeAlignmentInBits
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class Type;
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class ExtQuals;
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class QualType;
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace llvm {
47010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  template <typename T>
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class PointerLikeTypeTraits;
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  template<>
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class PointerLikeTypeTraits< ::clang::Type*> {
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  public:
5203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    static inline void *getAsVoidPointer(::clang::Type *P) { return P; }
5303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    static inline ::clang::Type *getFromVoidPointer(void *P) {
5403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      return static_cast< ::clang::Type*>(P);
5503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    }
5603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    enum { NumLowBitsAvailable = clang::TypeAlignmentInBits };
5703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  };
5803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  template<>
5903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  class PointerLikeTypeTraits< ::clang::ExtQuals*> {
6003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  public:
6103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    static inline void *getAsVoidPointer(::clang::ExtQuals *P) { return P; }
6203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    static inline ::clang::ExtQuals *getFromVoidPointer(void *P) {
6303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      return static_cast< ::clang::ExtQuals*>(P);
6403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    }
6503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    enum { NumLowBitsAvailable = clang::TypeAlignmentInBits };
6603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  };
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  template <>
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct isPodLike<clang::QualType> { static const bool value = true; };
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace clang {
7346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  class ASTContext;
7446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  class TypedefNameDecl;
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class TemplateDecl;
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class TemplateTypeParmDecl;
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class NonTypeTemplateParmDecl;
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class TemplateTemplateParmDecl;
79010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  class TagDecl;
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class RecordDecl;
81010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  class CXXRecordDecl;
82010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  class EnumDecl;
83010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  class FieldDecl;
84010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  class FunctionDecl;
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class ObjCInterfaceDecl;
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class ObjCProtocolDecl;
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class ObjCMethodDecl;
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  class UnresolvedUsingTypenameDecl;
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class Expr;
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class Stmt;
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class SourceLocation;
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class StmtIteratorBase;
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class TemplateArgument;
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class TemplateArgumentLoc;
953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  class TemplateArgumentListInfo;
96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class ElaboratedType;
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class ExtQuals;
983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  class ExtQualsTypeCommonBase;
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  struct PrintingPolicy;
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  template <typename> class CanQual;
1023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  typedef CanQual<Type> CanQualType;
103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Provide forward declarations for all of the *Type classes
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define TYPE(Class, Base) class Class##Type;
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "clang/AST/TypeNodes.def"
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// Qualifiers - The collection of all-type qualifiers we support.
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// Clang supports five independent qualifiers:
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/// * C99: const, volatile, and restrict
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// * Embedded C (TR18037): address spaces
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// * Objective C: the GC attributes (none, weak, or strong)
1131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass Qualifiers {
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)public:
1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  enum TQ { // NOTE: These flags must be kept in sync with DeclSpec::TQ.
1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    Const    = 0x1,
1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    Restrict = 0x2,
1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    Volatile = 0x4,
119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    CVRMask = Const | Volatile | Restrict
120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  };
121a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  enum GC {
123a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    GCNone = 0,
124010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    Weak,
125a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Strong
12603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  };
127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
128010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  enum ObjCLifetime {
129010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    /// There is no lifetime qualification on this type.
130010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    OCL_None,
131010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    /// This object can be modified without requiring retains or
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// releases.
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    OCL_ExplicitNone,
135effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
136effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// Assigning into this object requires the old value to be
137effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// released and the new value to be retained.  The timing of the
138effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// release of the old value is inexact: it may be moved to
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// immediately after the last known point where the value is
14058e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    /// live.
141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    OCL_Strong,
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// Reading or writing from this object requires a barrier call.
144effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    OCL_Weak,
145effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
146effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// Assigning into this object requires a lifetime extension.
147effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    OCL_Autoreleasing
148effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  };
149effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
150effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  enum {
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// The maximum supported address space number.
15203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    /// 24 bits should be enough for anyone.
15303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    MaxAddressSpace = 0xffffffu,
15403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /// The width of the "fast" qualifier mask.
15603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    FastWidth = 3,
15703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
15803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    /// The fast qualifier mask.
15903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    FastMask = (1 << FastWidth) - 1
16003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  };
16103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
16203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  Qualifiers() : Mask(0) {}
16303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
16403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  /// \brief Returns the common set of qualifiers while removing them from
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// the given sets.
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static Qualifiers removeCommonQualifiers(Qualifiers &L, Qualifiers &R) {
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If both are only CVR-qualified, bit operations are sufficient.
168a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (!(L.Mask & ~CVRMask) && !(R.Mask & ~CVRMask)) {
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Qualifiers Q;
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Q.Mask = L.Mask & R.Mask;
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      L.Mask &= ~Q.Mask;
17258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      R.Mask &= ~Q.Mask;
173a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return Q;
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Qualifiers Q;
17758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    unsigned CommonCRV = L.getCVRQualifiers() & R.getCVRQualifiers();
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Q.addCVRQualifiers(CommonCRV);
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    L.removeCVRQualifiers(CommonCRV);
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    R.removeCVRQualifiers(CommonCRV);
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    if (L.getObjCGCAttr() == R.getObjCGCAttr()) {
183a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Q.setObjCGCAttr(L.getObjCGCAttr());
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      L.removeObjCGCAttr();
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      R.removeObjCGCAttr();
18646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    }
18758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (L.getObjCLifetime() == R.getObjCLifetime()) {
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Q.setObjCLifetime(L.getObjCLifetime());
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      L.removeObjCLifetime();
1918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      R.removeObjCLifetime();
1928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (L.getAddressSpace() == R.getAddressSpace()) {
1958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      Q.setAddressSpace(L.getAddressSpace());
1964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      L.removeAddressSpace();
1974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      R.removeAddressSpace();
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
1994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return Q;
2004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
20146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
20258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  static Qualifiers fromFastMask(unsigned Mask) {
203a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Qualifiers Qs;
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Qs.addFastQualifiers(Mask);
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return Qs;
20646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
20758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static Qualifiers fromCVRMask(unsigned CVR) {
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Qualifiers Qs;
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Qs.addCVRQualifiers(CVR);
21146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return Qs;
21258e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  }
213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Deserialize qualifiers from an opaque representation.
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static Qualifiers fromOpaqueValue(unsigned opaque) {
21646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    Qualifiers Qs;
21746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    Qs.Mask = opaque;
21846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return Qs;
21946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
22046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
22158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Serialize these qualifiers into an opaque representation.
222a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  unsigned getAsOpaqueValue() const {
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return Mask;
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
22546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
22658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  bool hasConst() const { return Mask & Const; }
227a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void setConst(bool flag) {
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Mask = (Mask & ~Const) | (flag ? Const : 0);
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void removeConst() { Mask &= ~Const; }
23158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  void addConst() { Mask |= Const; }
2321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool hasVolatile() const { return Mask & Volatile; }
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void setVolatile(bool flag) {
23568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    Mask = (Mask & ~Volatile) | (flag ? Volatile : 0);
23668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  }
237a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void removeVolatile() { Mask &= ~Volatile; }
23868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  void addVolatile() { Mask |= Volatile; }
23968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
24046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool hasRestrict() const { return Mask & Restrict; }
24158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  void setRestrict(bool flag) {
242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Mask = (Mask & ~Restrict) | (flag ? Restrict : 0);
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void removeRestrict() { Mask &= ~Restrict; }
24546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void addRestrict() { Mask |= Restrict; }
24658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool hasCVRQualifiers() const { return getCVRQualifiers(); }
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  unsigned getCVRQualifiers() const { return Mask & CVRMask; }
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void setCVRQualifiers(unsigned mask) {
25046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits");
25158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    Mask = (Mask & ~CVRMask) | mask;
252a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void removeCVRQualifiers(unsigned mask) {
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits");
25546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    Mask &= ~mask;
25658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  }
257a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void removeCVRQualifiers() {
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    removeCVRQualifiers(CVRMask);
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
26090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void addCVRQualifiers(unsigned mask) {
26158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    assert(!(mask & ~CVRMask) && "bitmask contains non-CVR bits");
262a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Mask |= mask;
26390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  }
26490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool hasObjCGCAttr() const { return Mask & GCAttrMask; }
26658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  GC getObjCGCAttr() const { return GC((Mask & GCAttrMask) >> GCAttrShift); }
267a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void setObjCGCAttr(GC type) {
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Mask = (Mask & ~GCAttrMask) | (type << GCAttrShift);
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void removeObjCGCAttr() { setObjCGCAttr(GCNone); }
2718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void addObjCGCAttr(GC type) {
272a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    assert(type);
2738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    setObjCGCAttr(type);
2748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Qualifiers withoutObjCGCAttr() const {
27658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    Qualifiers qs = *this;
277a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    qs.removeObjCGCAttr();
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return qs;
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  Qualifiers withoutObjCLifetime() const {
28158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    Qualifiers qs = *this;
282a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    qs.removeObjCLifetime();
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return qs;
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
28658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  bool hasObjCLifetime() const { return Mask & LifetimeMask; }
287a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ObjCLifetime getObjCLifetime() const {
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return ObjCLifetime((Mask & LifetimeMask) >> LifetimeShift);
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void setObjCLifetime(ObjCLifetime type) {
29158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    Mask = (Mask & ~LifetimeMask) | (type << LifetimeShift);
292a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void removeObjCLifetime() { setObjCLifetime(OCL_None); }
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void addObjCLifetime(ObjCLifetime type) {
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    assert(type);
29658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    assert(!hasObjCLifetime());
297a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Mask |= (type << LifetimeShift);
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  /// True if the lifetime is neither None or ExplicitNone.
30158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  bool hasNonTrivialObjCLifetime() const {
302a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ObjCLifetime lifetime = getObjCLifetime();
3038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return (lifetime > OCL_ExplicitNone);
3048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  /// True if the lifetime is either strong or weak.
307a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool hasStrongOrWeakObjCLifetime() const {
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ObjCLifetime lifetime = getObjCLifetime();
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return (lifetime == OCL_Strong || lifetime == OCL_Weak);
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
31158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
312a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool hasAddressSpace() const { return Mask & AddressSpaceMask; }
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  unsigned getAddressSpace() const { return Mask >> AddressSpaceShift; }
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void setAddressSpace(unsigned space) {
3158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    assert(space <= MaxAddressSpace);
3168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    Mask = (Mask & ~AddressSpaceMask)
317a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)         | (((uint32_t) space) << AddressSpaceShift);
3188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void removeAddressSpace() { setAddressSpace(0); }
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void addAddressSpace(unsigned space) {
32158e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch    assert(space);
322a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    setAddressSpace(space);
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Fast qualifiers are those that can be allocated directly
3268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // on a QualType object.
327a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool hasFastQualifiers() const { return getFastQualifiers(); }
3288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  unsigned getFastQualifiers() const { return Mask & FastMask; }
3298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void setFastQualifiers(unsigned mask) {
33046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits");
3318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    Mask = (Mask & ~FastMask) | mask;
332a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
3338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void removeFastQualifiers(unsigned mask) {
3348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits");
33546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    Mask &= ~mask;
3368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void removeFastQualifiers() {
3381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    removeFastQualifiers(FastMask);
3391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
3401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void addFastQualifiers(unsigned mask) {
3411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    assert(!(mask & ~FastMask) && "bitmask contains non-fast qualifier bits");
3421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    Mask |= mask;
3431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
3441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  /// hasNonFastQualifiers - Return true if the set contains any
3461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  /// qualifiers which require an ExtQuals node to be allocated.
3471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool hasNonFastQualifiers() const { return Mask & ~FastMask; }
3481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Qualifiers getNonFastQualifiers() const {
3491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    Qualifiers Quals = *this;
3501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    Quals.setFastQualifiers(0);
3511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return Quals;
3521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
3531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  /// hasQualifiers - Return true if the set contains any qualifiers.
3558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool hasQualifiers() const { return Mask; }
3568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool empty() const { return !Mask; }
35746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
35846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// \brief Add the qualifiers from the given set to this set.
35946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  void addQualifiers(Qualifiers Q) {
36046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    // If the other set doesn't have any non-boolean qualifiers, just
36146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    // bit-or it in.
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!(Q.Mask & ~CVRMask))
36358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      Mask |= Q.Mask;
364a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    else {
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      Mask |= (Q.Mask & CVRMask);
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (Q.hasAddressSpace())
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        addAddressSpace(Q.getAddressSpace());
36858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      if (Q.hasObjCGCAttr())
369a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        addObjCGCAttr(Q.getObjCGCAttr());
3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (Q.hasObjCLifetime())
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        addObjCLifetime(Q.getObjCLifetime());
3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
37346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
37446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// \brief Remove the qualifiers from the given set from this set.
376eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void removeQualifiers(Qualifiers Q) {
3774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // If the other set doesn't have any non-boolean qualifiers, just
37868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    // bit-and the inverse in.
379a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (!(Q.Mask & ~CVRMask))
38068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      Mask &= ~Q.Mask;
38168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    else {
3824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      Mask &= ~(Q.Mask & CVRMask);
38358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch      if (getObjCGCAttr() == Q.getObjCGCAttr())
384a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        removeObjCGCAttr();
385eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (getObjCLifetime() == Q.getObjCLifetime())
386eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        removeObjCLifetime();
387eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (getAddressSpace() == Q.getAddressSpace())
38858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch        removeAddressSpace();
389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
390eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
391eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
392eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  /// \brief Add the qualifiers from the given set to this set, given that
39358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  /// they don't conflict.
394a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void addConsistentQualifiers(Qualifiers qs) {
395eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    assert(getAddressSpace() == qs.getAddressSpace() ||
396eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch           !hasAddressSpace() || !qs.hasAddressSpace());
397eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    assert(getObjCGCAttr() == qs.getObjCGCAttr() ||
39858e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch           !hasObjCGCAttr() || !qs.hasObjCGCAttr());
399a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    assert(getObjCLifetime() == qs.getObjCLifetime() ||
400eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch           !hasObjCLifetime() || !qs.hasObjCLifetime());
401eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    Mask |= qs.Mask;
402eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
40358e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
4041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  /// \brief Determines if these qualifiers compatibly include another set.
4051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  /// Generally this answers the question of whether an object with the other
4061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  /// qualifiers can be safely used as an object with these qualifiers.
4071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool compatiblyIncludes(Qualifiers other) const {
4081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return
4091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      // Address spaces must match exactly.
4101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      getAddressSpace() == other.getAddressSpace() &&
4111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      // ObjC GC qualifiers can match, be added, or be removed, but can't be
4121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      // changed.
4131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      (getObjCGCAttr() == other.getObjCGCAttr() ||
4141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci       !hasObjCGCAttr() || !other.hasObjCGCAttr()) &&
4151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      // ObjC lifetime qualifiers must match exactly.
416eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      getObjCLifetime() == other.getObjCLifetime() &&
417a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)      // CVR qualifiers may subset.
41846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      (((Mask & CVRMask) | (other.Mask & CVRMask)) == (Mask & CVRMask));
419a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
4203240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch
4213240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch  /// \brief Determines if these qualifiers compatibly include another set of
42246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// qualifiers from the narrow perspective of Objective-C ARC lifetime.
42346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ///
42446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// One set of Objective-C lifetime qualifiers compatibly includes the other
42546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// if the lifetime qualifiers match, or if both are non-__weak and the
42646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// including set also contains the 'const' qualifier.
427a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool compatiblyIncludesObjCLifetime(Qualifiers other) const {
4283240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch    if (getObjCLifetime() == other.getObjCLifetime())
4293240926e260ce088908e02ac07a6cf7b0c0cbf44Ben Murdoch      return true;
430bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
431bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    if (getObjCLifetime() == OCL_Weak || other.getObjCLifetime() == OCL_Weak)
432a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return false;
433bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
434bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    return hasConst();
435bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  }
436bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
437a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// \brief Determine whether this set of qualifiers is a strict superset of
438bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  /// another set of qualifiers, not considering qualifier compatibility.
439bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  bool isStrictSupersetOf(Qualifiers Other) const;
44046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
44146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool operator==(Qualifiers Other) const { return Mask == Other.Mask; }
44246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool operator!=(Qualifiers Other) const { return Mask != Other.Mask; }
443bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
444a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  operator bool() const { return hasQualifiers(); }
445bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch
446bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  Qualifiers &operator+=(Qualifiers R) {
4473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    addQualifiers(R);
4483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return *this;
449a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
4503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
4513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Union two qualifier sets.  If an enumerated qualifier appears
4528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // in both sets, use the one from the right.
4538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  friend Qualifiers operator+(Qualifiers L, Qualifiers R) {
4548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    L += R;
4558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return L;
4568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
45746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
45846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  Qualifiers &operator-=(Qualifiers R) {
459f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    removeQualifiers(R);
460f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return *this;
461a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
4628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  /// \brief Compute the difference between two qualifier sets.
4648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  friend Qualifiers operator-(Qualifiers L, Qualifiers R) {
4658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    L -= R;
4668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return L;
4678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
4688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  std::string getAsString() const;
4708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  std::string getAsString(const PrintingPolicy &Policy) const;
4718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool isEmptyWhenPrinted(const PrintingPolicy &Policy) const;
4738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void print(raw_ostream &OS, const PrintingPolicy &Policy,
4748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)             bool appendSpaceIfNonEmpty = false) const;
4758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void Profile(llvm::FoldingSetNodeID &ID) const {
4778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ID.AddInteger(Mask);
4788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
4793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
480f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)private:
4813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
4823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // bits:     |0 1 2|3 .. 4|5  ..  7|8   ...   31|
4833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  //           |C R V|GCAttr|Lifetime|AddressSpace|
4843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  uint32_t Mask;
485f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
486f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static const uint32_t GCAttrMask = 0x18;
487f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static const uint32_t GCAttrShift = 3;
488f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static const uint32_t LifetimeMask = 0xE0;
489f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static const uint32_t LifetimeShift = 5;
490f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static const uint32_t AddressSpaceMask = ~(CVRMask|GCAttrMask|LifetimeMask);
491a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static const uint32_t AddressSpaceShift = 8;
492f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
493f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
494f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)/// A std::pair-like structure for storing a qualified type split
4953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)/// into its local qualifiers and its locally-unqualified type.
4963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)struct SplitQualType {
497a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  /// The locally-unqualified type.
498a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  const Type *Ty;
499a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
5005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// The local qualifiers.
501a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Qualifiers Quals;
502a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
503a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  SplitQualType() : Ty(0), Quals() {}
504a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  SplitQualType(const Type *ty, Qualifiers qs) : Ty(ty), Quals(qs) {}
5055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
506a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SplitQualType getSingleStepDesugaredType() const; // end of this file
507a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
508a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Make llvm::tie work.
509a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  operator std::pair<const Type *,Qualifiers>() const {
5105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return std::pair<const Type *,Qualifiers>(Ty, Quals);
511a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
512a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
513a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  friend bool operator==(SplitQualType a, SplitQualType b) {
514bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    return a.Ty == b.Ty && a.Quals == b.Quals;
5155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
516a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  friend bool operator!=(SplitQualType a, SplitQualType b) {
517bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch    return a.Ty != b.Ty || a.Quals != b.Quals;
518bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  }
5191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci};
5201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
5211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/// QualType - For efficiency, we don't store CV-qualified types as nodes on
5221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/// their own: instead each reference to a type stores the qualifiers.  This
5231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/// greatly reduces the number of nodes we need to allocate for types (for
5241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/// example we only need one for 'int', 'const int', 'volatile int',
5251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/// 'const volatile int', etc).
5261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci///
5271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/// As an added efficiency bonus, instead of making this a pair, we
5281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/// just store the two bits we care about in the low bits of the
5291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/// pointer.  To handle the packing/unpacking, we make QualType be a
5301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/// simple wrapper class that acts like a smart pointer.  A third bit
53146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)/// indicates whether there are extended qualifiers present, in which
53246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)/// case the pointer points to a special structure.
53346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class QualType {
53446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Thankfully, these are efficiently composable.
5355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  llvm::PointerIntPair<llvm::PointerUnion<const Type*,const ExtQuals*>,
536a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                       Qualifiers::FastWidth> Value;
5375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const ExtQuals *getExtQualsUnsafe() const {
53946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return Value.getPointer().get<const ExtQuals*>();
540a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
5415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
5425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const Type *getTypePtrUnsafe() const {
543a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return Value.getPointer().get<const Type*>();
5445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
545a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
546a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  const ExtQualsTypeCommonBase *getCommonPtr() const {
547a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    assert(!isNull() && "Cannot retrieve a NULL type pointer");
548a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    uintptr_t CommonPtrVal
5495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      = reinterpret_cast<uintptr_t>(Value.getOpaqueValue());
550a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    CommonPtrVal &= ~(uintptr_t)((1 << TypeAlignmentInBits) - 1);
551a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return reinterpret_cast<ExtQualsTypeCommonBase*>(CommonPtrVal);
5524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
553a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
5545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  friend class QualifierCollector;
555a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)public:
556a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  QualType() {}
557a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
558a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  QualType(const Type *Ptr, unsigned Quals)
5595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : Value(Ptr, Quals) {}
560a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  QualType(const ExtQuals *Ptr, unsigned Quals)
561a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    : Value(Ptr, Quals) {}
562a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
563a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  unsigned getLocalFastQualifiers() const { return Value.getInt(); }
5645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void setLocalFastQualifiers(unsigned Quals) { Value.setInt(Quals); }
565a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
566a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  /// Retrieves a pointer to the underlying (unqualified) type.
567a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  ///
56846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// This function requires that the type not be NULL. If the type might be
56946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// NULL, use the (slightly less efficient) \c getTypePtrOrNull().
57046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const Type *getTypePtr() const;
57146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
57246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const Type *getTypePtrOrNull() const;
57346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
5745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// Retrieves a pointer to the name of the base type.
575a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const IdentifierInfo *getBaseTypeIdentifier() const;
576a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
577a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  /// Divides a QualType into its unqualified type and a set of local
578a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  /// qualifiers.
5795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SplitQualType split() const;
580a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
581a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
582a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  static QualType getFromOpaquePtr(const void *Ptr) {
583a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    QualType T;
5845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    T.Value.setFromOpaqueValue(const_cast<void*>(Ptr));
585a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return T;
586a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
587a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
58846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const Type &operator*() const {
58946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return *getTypePtr();
59046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
59146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
59246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const Type *operator->() const {
59346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return getTypePtr();
5945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
595a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
596a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  bool isCanonical() const;
597a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  bool isCanonicalAsParam() const;
598a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
5995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// isNull - Return true if this QualType doesn't point to a type yet.
600a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool isNull() const {
601a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return Value.getPointer().isNull();
602a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  }
60346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
60446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// \brief Determine whether this particular QualType instance has the
60546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// "const" qualifier set, without looking through typedefs that may have
60646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  /// added "const" at a different level.
607a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  bool isLocalConstQualified() const {
6085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return (getLocalFastQualifiers() & Qualifiers::Const);
609a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
610a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
611a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  /// \brief Determine whether this type is const-qualified.
612a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  bool isConstQualified() const;
6135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
614a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// \brief Determine whether this particular QualType instance has the
615a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  /// "restrict" qualifier set, without looking through typedefs that may have
616a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  /// added "restrict" at a different level.
617a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  bool isLocalRestrictQualified() const {
6185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return (getLocalFastQualifiers() & Qualifiers::Restrict);
619a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
620a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
621a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  /// \brief Determine whether this type is restrict-qualified.
622a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  bool isRestrictQualified() const;
6235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
624a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// \brief Determine whether this particular QualType instance has the
625a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  /// "volatile" qualifier set, without looking through typedefs that may have
626bb1529ce867d8845a77ec7cdf3e3003ef1771a40Ben Murdoch  /// added "volatile" at a different level.
627  bool isLocalVolatileQualified() const {
628    return (getLocalFastQualifiers() & Qualifiers::Volatile);
629  }
630
631  /// \brief Determine whether this type is volatile-qualified.
632  bool isVolatileQualified() const;
633
634  /// \brief Determine whether this particular QualType instance has any
635  /// qualifiers, without looking through any typedefs that might add
636  /// qualifiers at a different level.
637  bool hasLocalQualifiers() const {
638    return getLocalFastQualifiers() || hasLocalNonFastQualifiers();
639  }
640
641  /// \brief Determine whether this type has any qualifiers.
642  bool hasQualifiers() const;
643
644  /// \brief Determine whether this particular QualType instance has any
645  /// "non-fast" qualifiers, e.g., those that are stored in an ExtQualType
646  /// instance.
647  bool hasLocalNonFastQualifiers() const {
648    return Value.getPointer().is<const ExtQuals*>();
649  }
650
651  /// \brief Retrieve the set of qualifiers local to this particular QualType
652  /// instance, not including any qualifiers acquired through typedefs or
653  /// other sugar.
654  Qualifiers getLocalQualifiers() const;
655
656  /// \brief Retrieve the set of qualifiers applied to this type.
657  Qualifiers getQualifiers() const;
658
659  /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers
660  /// local to this particular QualType instance, not including any qualifiers
661  /// acquired through typedefs or other sugar.
662  unsigned getLocalCVRQualifiers() const {
663    return getLocalFastQualifiers();
664  }
665
666  /// \brief Retrieve the set of CVR (const-volatile-restrict) qualifiers
667  /// applied to this type.
668  unsigned getCVRQualifiers() const;
669
670  bool isConstant(ASTContext& Ctx) const {
671    return QualType::isConstant(*this, Ctx);
672  }
673
674  /// \brief Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
675  bool isPODType(ASTContext &Context) const;
676
677  /// isCXX98PODType() - Return true if this is a POD type according to the
678  /// rules of the C++98 standard, regardless of the current compilation's
679  /// language.
680  bool isCXX98PODType(ASTContext &Context) const;
681
682  /// isCXX11PODType() - Return true if this is a POD type according to the
683  /// more relaxed rules of the C++11 standard, regardless of the current
684  /// compilation's language.
685  /// (C++0x [basic.types]p9)
686  bool isCXX11PODType(ASTContext &Context) const;
687
688  /// isTrivialType - Return true if this is a trivial type
689  /// (C++0x [basic.types]p9)
690  bool isTrivialType(ASTContext &Context) const;
691
692  /// isTriviallyCopyableType - Return true if this is a trivially
693  /// copyable type (C++0x [basic.types]p9)
694  bool isTriviallyCopyableType(ASTContext &Context) const;
695
696  // Don't promise in the API that anything besides 'const' can be
697  // easily added.
698
699  /// addConst - add the specified type qualifier to this QualType.
700  void addConst() {
701    addFastQualifiers(Qualifiers::Const);
702  }
703  QualType withConst() const {
704    return withFastQualifiers(Qualifiers::Const);
705  }
706
707  /// addVolatile - add the specified type qualifier to this QualType.
708  void addVolatile() {
709    addFastQualifiers(Qualifiers::Volatile);
710  }
711  QualType withVolatile() const {
712    return withFastQualifiers(Qualifiers::Volatile);
713  }
714
715  /// Add the restrict qualifier to this QualType.
716  void addRestrict() {
717    addFastQualifiers(Qualifiers::Restrict);
718  }
719  QualType withRestrict() const {
720    return withFastQualifiers(Qualifiers::Restrict);
721  }
722
723  QualType withCVRQualifiers(unsigned CVR) const {
724    return withFastQualifiers(CVR);
725  }
726
727  void addFastQualifiers(unsigned TQs) {
728    assert(!(TQs & ~Qualifiers::FastMask)
729           && "non-fast qualifier bits set in mask!");
730    Value.setInt(Value.getInt() | TQs);
731  }
732
733  void removeLocalConst();
734  void removeLocalVolatile();
735  void removeLocalRestrict();
736  void removeLocalCVRQualifiers(unsigned Mask);
737
738  void removeLocalFastQualifiers() { Value.setInt(0); }
739  void removeLocalFastQualifiers(unsigned Mask) {
740    assert(!(Mask & ~Qualifiers::FastMask) && "mask has non-fast qualifiers");
741    Value.setInt(Value.getInt() & ~Mask);
742  }
743
744  // Creates a type with the given qualifiers in addition to any
745  // qualifiers already on this type.
746  QualType withFastQualifiers(unsigned TQs) const {
747    QualType T = *this;
748    T.addFastQualifiers(TQs);
749    return T;
750  }
751
752  // Creates a type with exactly the given fast qualifiers, removing
753  // any existing fast qualifiers.
754  QualType withExactLocalFastQualifiers(unsigned TQs) const {
755    return withoutLocalFastQualifiers().withFastQualifiers(TQs);
756  }
757
758  // Removes fast qualifiers, but leaves any extended qualifiers in place.
759  QualType withoutLocalFastQualifiers() const {
760    QualType T = *this;
761    T.removeLocalFastQualifiers();
762    return T;
763  }
764
765  QualType getCanonicalType() const;
766
767  /// \brief Return this type with all of the instance-specific qualifiers
768  /// removed, but without removing any qualifiers that may have been applied
769  /// through typedefs.
770  QualType getLocalUnqualifiedType() const { return QualType(getTypePtr(), 0); }
771
772  /// \brief Retrieve the unqualified variant of the given type,
773  /// removing as little sugar as possible.
774  ///
775  /// This routine looks through various kinds of sugar to find the
776  /// least-desugared type that is unqualified. For example, given:
777  ///
778  /// \code
779  /// typedef int Integer;
780  /// typedef const Integer CInteger;
781  /// typedef CInteger DifferenceType;
782  /// \endcode
783  ///
784  /// Executing \c getUnqualifiedType() on the type \c DifferenceType will
785  /// desugar until we hit the type \c Integer, which has no qualifiers on it.
786  ///
787  /// The resulting type might still be qualified if it's an array
788  /// type.  To strip qualifiers even from within an array type, use
789  /// ASTContext::getUnqualifiedArrayType.
790  inline QualType getUnqualifiedType() const;
791
792  /// getSplitUnqualifiedType - Retrieve the unqualified variant of the
793  /// given type, removing as little sugar as possible.
794  ///
795  /// Like getUnqualifiedType(), but also returns the set of
796  /// qualifiers that were built up.
797  ///
798  /// The resulting type might still be qualified if it's an array
799  /// type.  To strip qualifiers even from within an array type, use
800  /// ASTContext::getUnqualifiedArrayType.
801  inline SplitQualType getSplitUnqualifiedType() const;
802
803  /// \brief Determine whether this type is more qualified than the other
804  /// given type, requiring exact equality for non-CVR qualifiers.
805  bool isMoreQualifiedThan(QualType Other) const;
806
807  /// \brief Determine whether this type is at least as qualified as the other
808  /// given type, requiring exact equality for non-CVR qualifiers.
809  bool isAtLeastAsQualifiedAs(QualType Other) const;
810
811  QualType getNonReferenceType() const;
812
813  /// \brief Determine the type of a (typically non-lvalue) expression with the
814  /// specified result type.
815  ///
816  /// This routine should be used for expressions for which the return type is
817  /// explicitly specified (e.g., in a cast or call) and isn't necessarily
818  /// an lvalue. It removes a top-level reference (since there are no
819  /// expressions of reference type) and deletes top-level cvr-qualifiers
820  /// from non-class types (in C++) or all types (in C).
821  QualType getNonLValueExprType(ASTContext &Context) const;
822
823  /// getDesugaredType - Return the specified type with any "sugar" removed from
824  /// the type.  This takes off typedefs, typeof's etc.  If the outer level of
825  /// the type is already concrete, it returns it unmodified.  This is similar
826  /// to getting the canonical type, but it doesn't remove *all* typedefs.  For
827  /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
828  /// concrete.
829  ///
830  /// Qualifiers are left in place.
831  QualType getDesugaredType(const ASTContext &Context) const {
832    return getDesugaredType(*this, Context);
833  }
834
835  SplitQualType getSplitDesugaredType() const {
836    return getSplitDesugaredType(*this);
837  }
838
839  /// \brief Return the specified type with one level of "sugar" removed from
840  /// the type.
841  ///
842  /// This routine takes off the first typedef, typeof, etc. If the outer level
843  /// of the type is already concrete, it returns it unmodified.
844  QualType getSingleStepDesugaredType(const ASTContext &Context) const {
845    return getSingleStepDesugaredTypeImpl(*this, Context);
846  }
847
848  /// IgnoreParens - Returns the specified type after dropping any
849  /// outer-level parentheses.
850  QualType IgnoreParens() const {
851    if (isa<ParenType>(*this))
852      return QualType::IgnoreParens(*this);
853    return *this;
854  }
855
856  /// operator==/!= - Indicate whether the specified types and qualifiers are
857  /// identical.
858  friend bool operator==(const QualType &LHS, const QualType &RHS) {
859    return LHS.Value == RHS.Value;
860  }
861  friend bool operator!=(const QualType &LHS, const QualType &RHS) {
862    return LHS.Value != RHS.Value;
863  }
864  std::string getAsString() const {
865    return getAsString(split());
866  }
867  static std::string getAsString(SplitQualType split) {
868    return getAsString(split.Ty, split.Quals);
869  }
870  static std::string getAsString(const Type *ty, Qualifiers qs);
871
872  std::string getAsString(const PrintingPolicy &Policy) const;
873
874  void print(raw_ostream &OS, const PrintingPolicy &Policy,
875             const Twine &PlaceHolder = Twine()) const {
876    print(split(), OS, Policy, PlaceHolder);
877  }
878  static void print(SplitQualType split, raw_ostream &OS,
879                    const PrintingPolicy &policy, const Twine &PlaceHolder) {
880    return print(split.Ty, split.Quals, OS, policy, PlaceHolder);
881  }
882  static void print(const Type *ty, Qualifiers qs,
883                    raw_ostream &OS, const PrintingPolicy &policy,
884                    const Twine &PlaceHolder);
885
886  void getAsStringInternal(std::string &Str,
887                           const PrintingPolicy &Policy) const {
888    return getAsStringInternal(split(), Str, Policy);
889  }
890  static void getAsStringInternal(SplitQualType split, std::string &out,
891                                  const PrintingPolicy &policy) {
892    return getAsStringInternal(split.Ty, split.Quals, out, policy);
893  }
894  static void getAsStringInternal(const Type *ty, Qualifiers qs,
895                                  std::string &out,
896                                  const PrintingPolicy &policy);
897
898  class StreamedQualTypeHelper {
899    const QualType &T;
900    const PrintingPolicy &Policy;
901    const Twine &PlaceHolder;
902  public:
903    StreamedQualTypeHelper(const QualType &T, const PrintingPolicy &Policy,
904                           const Twine &PlaceHolder)
905      : T(T), Policy(Policy), PlaceHolder(PlaceHolder) { }
906
907    friend raw_ostream &operator<<(raw_ostream &OS,
908                                   const StreamedQualTypeHelper &SQT) {
909      SQT.T.print(OS, SQT.Policy, SQT.PlaceHolder);
910      return OS;
911    }
912  };
913
914  StreamedQualTypeHelper stream(const PrintingPolicy &Policy,
915                                const Twine &PlaceHolder = Twine()) const {
916    return StreamedQualTypeHelper(*this, Policy, PlaceHolder);
917  }
918
919  void dump(const char *s) const;
920  void dump() const;
921
922  void Profile(llvm::FoldingSetNodeID &ID) const {
923    ID.AddPointer(getAsOpaquePtr());
924  }
925
926  /// getAddressSpace - Return the address space of this type.
927  inline unsigned getAddressSpace() const;
928
929  /// getObjCGCAttr - Returns gc attribute of this type.
930  inline Qualifiers::GC getObjCGCAttr() const;
931
932  /// isObjCGCWeak true when Type is objc's weak.
933  bool isObjCGCWeak() const {
934    return getObjCGCAttr() == Qualifiers::Weak;
935  }
936
937  /// isObjCGCStrong true when Type is objc's strong.
938  bool isObjCGCStrong() const {
939    return getObjCGCAttr() == Qualifiers::Strong;
940  }
941
942  /// getObjCLifetime - Returns lifetime attribute of this type.
943  Qualifiers::ObjCLifetime getObjCLifetime() const {
944    return getQualifiers().getObjCLifetime();
945  }
946
947  bool hasNonTrivialObjCLifetime() const {
948    return getQualifiers().hasNonTrivialObjCLifetime();
949  }
950
951  bool hasStrongOrWeakObjCLifetime() const {
952    return getQualifiers().hasStrongOrWeakObjCLifetime();
953  }
954
955  enum DestructionKind {
956    DK_none,
957    DK_cxx_destructor,
958    DK_objc_strong_lifetime,
959    DK_objc_weak_lifetime
960  };
961
962  /// isDestructedType - nonzero if objects of this type require
963  /// non-trivial work to clean up after.  Non-zero because it's
964  /// conceivable that qualifiers (objc_gc(weak)?) could make
965  /// something require destruction.
966  DestructionKind isDestructedType() const {
967    return isDestructedTypeImpl(*this);
968  }
969
970  /// \brief Determine whether expressions of the given type are forbidden
971  /// from being lvalues in C.
972  ///
973  /// The expression types that are forbidden to be lvalues are:
974  ///   - 'void', but not qualified void
975  ///   - function types
976  ///
977  /// The exact rule here is C99 6.3.2.1:
978  ///   An lvalue is an expression with an object type or an incomplete
979  ///   type other than void.
980  bool isCForbiddenLValueType() const;
981
982  /// \brief Determine whether this type has trivial copy/move-assignment
983  ///        semantics.
984  bool hasTrivialAssignment(ASTContext &Context, bool Copying) const;
985
986private:
987  // These methods are implemented in a separate translation unit;
988  // "static"-ize them to avoid creating temporary QualTypes in the
989  // caller.
990  static bool isConstant(QualType T, ASTContext& Ctx);
991  static QualType getDesugaredType(QualType T, const ASTContext &Context);
992  static SplitQualType getSplitDesugaredType(QualType T);
993  static SplitQualType getSplitUnqualifiedTypeImpl(QualType type);
994  static QualType getSingleStepDesugaredTypeImpl(QualType type,
995                                                 const ASTContext &C);
996  static QualType IgnoreParens(QualType T);
997  static DestructionKind isDestructedTypeImpl(QualType type);
998};
999
1000} // end clang.
1001
1002namespace llvm {
1003/// Implement simplify_type for QualType, so that we can dyn_cast from QualType
1004/// to a specific Type class.
1005template<> struct simplify_type<const ::clang::QualType> {
1006  typedef const ::clang::Type *SimpleType;
1007  static SimpleType getSimplifiedValue(const ::clang::QualType &Val) {
1008    return Val.getTypePtr();
1009  }
1010};
1011template<> struct simplify_type< ::clang::QualType>
1012  : public simplify_type<const ::clang::QualType> {};
1013
1014// Teach SmallPtrSet that QualType is "basically a pointer".
1015template<>
1016class PointerLikeTypeTraits<clang::QualType> {
1017public:
1018  static inline void *getAsVoidPointer(clang::QualType P) {
1019    return P.getAsOpaquePtr();
1020  }
1021  static inline clang::QualType getFromVoidPointer(void *P) {
1022    return clang::QualType::getFromOpaquePtr(P);
1023  }
1024  // Various qualifiers go in low bits.
1025  enum { NumLowBitsAvailable = 0 };
1026};
1027
1028} // end namespace llvm
1029
1030namespace clang {
1031
1032/// \brief Base class that is common to both the \c ExtQuals and \c Type
1033/// classes, which allows \c QualType to access the common fields between the
1034/// two.
1035///
1036class ExtQualsTypeCommonBase {
1037  ExtQualsTypeCommonBase(const Type *baseType, QualType canon)
1038    : BaseType(baseType), CanonicalType(canon) {}
1039
1040  /// \brief The "base" type of an extended qualifiers type (\c ExtQuals) or
1041  /// a self-referential pointer (for \c Type).
1042  ///
1043  /// This pointer allows an efficient mapping from a QualType to its
1044  /// underlying type pointer.
1045  const Type *const BaseType;
1046
1047  /// \brief The canonical type of this type.  A QualType.
1048  QualType CanonicalType;
1049
1050  friend class QualType;
1051  friend class Type;
1052  friend class ExtQuals;
1053};
1054
1055/// ExtQuals - We can encode up to four bits in the low bits of a
1056/// type pointer, but there are many more type qualifiers that we want
1057/// to be able to apply to an arbitrary type.  Therefore we have this
1058/// struct, intended to be heap-allocated and used by QualType to
1059/// store qualifiers.
1060///
1061/// The current design tags the 'const', 'restrict', and 'volatile' qualifiers
1062/// in three low bits on the QualType pointer; a fourth bit records whether
1063/// the pointer is an ExtQuals node. The extended qualifiers (address spaces,
1064/// Objective-C GC attributes) are much more rare.
1065class ExtQuals : public ExtQualsTypeCommonBase, public llvm::FoldingSetNode {
1066  // NOTE: changing the fast qualifiers should be straightforward as
1067  // long as you don't make 'const' non-fast.
1068  // 1. Qualifiers:
1069  //    a) Modify the bitmasks (Qualifiers::TQ and DeclSpec::TQ).
1070  //       Fast qualifiers must occupy the low-order bits.
1071  //    b) Update Qualifiers::FastWidth and FastMask.
1072  // 2. QualType:
1073  //    a) Update is{Volatile,Restrict}Qualified(), defined inline.
1074  //    b) Update remove{Volatile,Restrict}, defined near the end of
1075  //       this header.
1076  // 3. ASTContext:
1077  //    a) Update get{Volatile,Restrict}Type.
1078
1079  /// Quals - the immutable set of qualifiers applied by this
1080  /// node;  always contains extended qualifiers.
1081  Qualifiers Quals;
1082
1083  ExtQuals *this_() { return this; }
1084
1085public:
1086  ExtQuals(const Type *baseType, QualType canon, Qualifiers quals)
1087    : ExtQualsTypeCommonBase(baseType,
1088                             canon.isNull() ? QualType(this_(), 0) : canon),
1089      Quals(quals)
1090  {
1091    assert(Quals.hasNonFastQualifiers()
1092           && "ExtQuals created with no fast qualifiers");
1093    assert(!Quals.hasFastQualifiers()
1094           && "ExtQuals created with fast qualifiers");
1095  }
1096
1097  Qualifiers getQualifiers() const { return Quals; }
1098
1099  bool hasObjCGCAttr() const { return Quals.hasObjCGCAttr(); }
1100  Qualifiers::GC getObjCGCAttr() const { return Quals.getObjCGCAttr(); }
1101
1102  bool hasObjCLifetime() const { return Quals.hasObjCLifetime(); }
1103  Qualifiers::ObjCLifetime getObjCLifetime() const {
1104    return Quals.getObjCLifetime();
1105  }
1106
1107  bool hasAddressSpace() const { return Quals.hasAddressSpace(); }
1108  unsigned getAddressSpace() const { return Quals.getAddressSpace(); }
1109
1110  const Type *getBaseType() const { return BaseType; }
1111
1112public:
1113  void Profile(llvm::FoldingSetNodeID &ID) const {
1114    Profile(ID, getBaseType(), Quals);
1115  }
1116  static void Profile(llvm::FoldingSetNodeID &ID,
1117                      const Type *BaseType,
1118                      Qualifiers Quals) {
1119    assert(!Quals.hasFastQualifiers() && "fast qualifiers in ExtQuals hash!");
1120    ID.AddPointer(BaseType);
1121    Quals.Profile(ID);
1122  }
1123};
1124
1125/// \brief The kind of C++0x ref-qualifier associated with a function type,
1126/// which determines whether a member function's "this" object can be an
1127/// lvalue, rvalue, or neither.
1128enum RefQualifierKind {
1129  /// \brief No ref-qualifier was provided.
1130  RQ_None = 0,
1131  /// \brief An lvalue ref-qualifier was provided (\c &).
1132  RQ_LValue,
1133  /// \brief An rvalue ref-qualifier was provided (\c &&).
1134  RQ_RValue
1135};
1136
1137/// Type - This is the base class of the type hierarchy.  A central concept
1138/// with types is that each type always has a canonical type.  A canonical type
1139/// is the type with any typedef names stripped out of it or the types it
1140/// references.  For example, consider:
1141///
1142///  typedef int  foo;
1143///  typedef foo* bar;
1144///    'int *'    'foo *'    'bar'
1145///
1146/// There will be a Type object created for 'int'.  Since int is canonical, its
1147/// canonicaltype pointer points to itself.  There is also a Type for 'foo' (a
1148/// TypedefType).  Its CanonicalType pointer points to the 'int' Type.  Next
1149/// there is a PointerType that represents 'int*', which, like 'int', is
1150/// canonical.  Finally, there is a PointerType type for 'foo*' whose canonical
1151/// type is 'int*', and there is a TypedefType for 'bar', whose canonical type
1152/// is also 'int*'.
1153///
1154/// Non-canonical types are useful for emitting diagnostics, without losing
1155/// information about typedefs being used.  Canonical types are useful for type
1156/// comparisons (they allow by-pointer equality tests) and useful for reasoning
1157/// about whether something has a particular form (e.g. is a function type),
1158/// because they implicitly, recursively, strip all typedefs out of a type.
1159///
1160/// Types, once created, are immutable.
1161///
1162class Type : public ExtQualsTypeCommonBase {
1163public:
1164  enum TypeClass {
1165#define TYPE(Class, Base) Class,
1166#define LAST_TYPE(Class) TypeLast = Class,
1167#define ABSTRACT_TYPE(Class, Base)
1168#include "clang/AST/TypeNodes.def"
1169    TagFirst = Record, TagLast = Enum
1170  };
1171
1172private:
1173  Type(const Type &) LLVM_DELETED_FUNCTION;
1174  void operator=(const Type &) LLVM_DELETED_FUNCTION;
1175
1176  /// Bitfields required by the Type class.
1177  class TypeBitfields {
1178    friend class Type;
1179    template <class T> friend class TypePropertyCache;
1180
1181    /// TypeClass bitfield - Enum that specifies what subclass this belongs to.
1182    unsigned TC : 8;
1183
1184    /// Dependent - Whether this type is a dependent type (C++ [temp.dep.type]).
1185    unsigned Dependent : 1;
1186
1187    /// \brief Whether this type somehow involves a template parameter, even
1188    /// if the resolution of the type does not depend on a template parameter.
1189    unsigned InstantiationDependent : 1;
1190
1191    /// \brief Whether this type is a variably-modified type (C99 6.7.5).
1192    unsigned VariablyModified : 1;
1193
1194    /// \brief Whether this type contains an unexpanded parameter pack
1195    /// (for C++0x variadic templates).
1196    unsigned ContainsUnexpandedParameterPack : 1;
1197
1198    /// \brief Nonzero if the cache (i.e. the bitfields here starting
1199    /// with 'Cache') is valid.  If so, then this is a
1200    /// LangOptions::VisibilityMode+1.
1201    mutable unsigned CacheValidAndVisibility : 2;
1202
1203    /// \brief True if the visibility was set explicitly in the source code.
1204    mutable unsigned CachedExplicitVisibility : 1;
1205
1206    /// \brief Linkage of this type.
1207    mutable unsigned CachedLinkage : 2;
1208
1209    /// \brief Whether this type involves and local or unnamed types.
1210    mutable unsigned CachedLocalOrUnnamed : 1;
1211
1212    /// \brief FromAST - Whether this type comes from an AST file.
1213    mutable unsigned FromAST : 1;
1214
1215    bool isCacheValid() const {
1216      return (CacheValidAndVisibility != 0);
1217    }
1218    Visibility getVisibility() const {
1219      assert(isCacheValid() && "getting linkage from invalid cache");
1220      return static_cast<Visibility>(CacheValidAndVisibility-1);
1221    }
1222    bool isVisibilityExplicit() const {
1223      assert(isCacheValid() && "getting linkage from invalid cache");
1224      return CachedExplicitVisibility;
1225    }
1226    Linkage getLinkage() const {
1227      assert(isCacheValid() && "getting linkage from invalid cache");
1228      return static_cast<Linkage>(CachedLinkage);
1229    }
1230    bool hasLocalOrUnnamedType() const {
1231      assert(isCacheValid() && "getting linkage from invalid cache");
1232      return CachedLocalOrUnnamed;
1233    }
1234  };
1235  enum { NumTypeBits = 19 };
1236
1237protected:
1238  // These classes allow subclasses to somewhat cleanly pack bitfields
1239  // into Type.
1240
1241  class ArrayTypeBitfields {
1242    friend class ArrayType;
1243
1244    unsigned : NumTypeBits;
1245
1246    /// IndexTypeQuals - CVR qualifiers from declarations like
1247    /// 'int X[static restrict 4]'. For function parameters only.
1248    unsigned IndexTypeQuals : 3;
1249
1250    /// SizeModifier - storage class qualifiers from declarations like
1251    /// 'int X[static restrict 4]'. For function parameters only.
1252    /// Actually an ArrayType::ArraySizeModifier.
1253    unsigned SizeModifier : 3;
1254  };
1255
1256  class BuiltinTypeBitfields {
1257    friend class BuiltinType;
1258
1259    unsigned : NumTypeBits;
1260
1261    /// The kind (BuiltinType::Kind) of builtin type this is.
1262    unsigned Kind : 8;
1263  };
1264
1265  class FunctionTypeBitfields {
1266    friend class FunctionType;
1267
1268    unsigned : NumTypeBits;
1269
1270    /// Extra information which affects how the function is called, like
1271    /// regparm and the calling convention.
1272    unsigned ExtInfo : 8;
1273
1274    /// TypeQuals - Used only by FunctionProtoType, put here to pack with the
1275    /// other bitfields.
1276    /// The qualifiers are part of FunctionProtoType because...
1277    ///
1278    /// C++ 8.3.5p4: The return type, the parameter type list and the
1279    /// cv-qualifier-seq, [...], are part of the function type.
1280    unsigned TypeQuals : 3;
1281
1282    /// \brief The ref-qualifier associated with a \c FunctionProtoType.
1283    ///
1284    /// This is a value of type \c RefQualifierKind.
1285    unsigned RefQualifier : 2;
1286  };
1287
1288  class ObjCObjectTypeBitfields {
1289    friend class ObjCObjectType;
1290
1291    unsigned : NumTypeBits;
1292
1293    /// NumProtocols - The number of protocols stored directly on this
1294    /// object type.
1295    unsigned NumProtocols : 32 - NumTypeBits;
1296  };
1297
1298  class ReferenceTypeBitfields {
1299    friend class ReferenceType;
1300
1301    unsigned : NumTypeBits;
1302
1303    /// True if the type was originally spelled with an lvalue sigil.
1304    /// This is never true of rvalue references but can also be false
1305    /// on lvalue references because of C++0x [dcl.typedef]p9,
1306    /// as follows:
1307    ///
1308    ///   typedef int &ref;    // lvalue, spelled lvalue
1309    ///   typedef int &&rvref; // rvalue
1310    ///   ref &a;              // lvalue, inner ref, spelled lvalue
1311    ///   ref &&a;             // lvalue, inner ref
1312    ///   rvref &a;            // lvalue, inner ref, spelled lvalue
1313    ///   rvref &&a;           // rvalue, inner ref
1314    unsigned SpelledAsLValue : 1;
1315
1316    /// True if the inner type is a reference type.  This only happens
1317    /// in non-canonical forms.
1318    unsigned InnerRef : 1;
1319  };
1320
1321  class TypeWithKeywordBitfields {
1322    friend class TypeWithKeyword;
1323
1324    unsigned : NumTypeBits;
1325
1326    /// An ElaboratedTypeKeyword.  8 bits for efficient access.
1327    unsigned Keyword : 8;
1328  };
1329
1330  class VectorTypeBitfields {
1331    friend class VectorType;
1332
1333    unsigned : NumTypeBits;
1334
1335    /// VecKind - The kind of vector, either a generic vector type or some
1336    /// target-specific vector type such as for AltiVec or Neon.
1337    unsigned VecKind : 3;
1338
1339    /// NumElements - The number of elements in the vector.
1340    unsigned NumElements : 29 - NumTypeBits;
1341  };
1342
1343  class AttributedTypeBitfields {
1344    friend class AttributedType;
1345
1346    unsigned : NumTypeBits;
1347
1348    /// AttrKind - an AttributedType::Kind
1349    unsigned AttrKind : 32 - NumTypeBits;
1350  };
1351
1352  union {
1353    TypeBitfields TypeBits;
1354    ArrayTypeBitfields ArrayTypeBits;
1355    AttributedTypeBitfields AttributedTypeBits;
1356    BuiltinTypeBitfields BuiltinTypeBits;
1357    FunctionTypeBitfields FunctionTypeBits;
1358    ObjCObjectTypeBitfields ObjCObjectTypeBits;
1359    ReferenceTypeBitfields ReferenceTypeBits;
1360    TypeWithKeywordBitfields TypeWithKeywordBits;
1361    VectorTypeBitfields VectorTypeBits;
1362  };
1363
1364private:
1365  /// \brief Set whether this type comes from an AST file.
1366  void setFromAST(bool V = true) const {
1367    TypeBits.FromAST = V;
1368  }
1369
1370  template <class T> friend class TypePropertyCache;
1371
1372protected:
1373  // silence VC++ warning C4355: 'this' : used in base member initializer list
1374  Type *this_() { return this; }
1375  Type(TypeClass tc, QualType canon, bool Dependent,
1376       bool InstantiationDependent, bool VariablyModified,
1377       bool ContainsUnexpandedParameterPack)
1378    : ExtQualsTypeCommonBase(this,
1379                             canon.isNull() ? QualType(this_(), 0) : canon) {
1380    TypeBits.TC = tc;
1381    TypeBits.Dependent = Dependent;
1382    TypeBits.InstantiationDependent = Dependent || InstantiationDependent;
1383    TypeBits.VariablyModified = VariablyModified;
1384    TypeBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
1385    TypeBits.CacheValidAndVisibility = 0;
1386    TypeBits.CachedExplicitVisibility = false;
1387    TypeBits.CachedLocalOrUnnamed = false;
1388    TypeBits.CachedLinkage = NoLinkage;
1389    TypeBits.FromAST = false;
1390  }
1391  friend class ASTContext;
1392
1393  void setDependent(bool D = true) {
1394    TypeBits.Dependent = D;
1395    if (D)
1396      TypeBits.InstantiationDependent = true;
1397  }
1398  void setInstantiationDependent(bool D = true) {
1399    TypeBits.InstantiationDependent = D; }
1400  void setVariablyModified(bool VM = true) { TypeBits.VariablyModified = VM;
1401  }
1402  void setContainsUnexpandedParameterPack(bool PP = true) {
1403    TypeBits.ContainsUnexpandedParameterPack = PP;
1404  }
1405
1406public:
1407  TypeClass getTypeClass() const { return static_cast<TypeClass>(TypeBits.TC); }
1408
1409  /// \brief Whether this type comes from an AST file.
1410  bool isFromAST() const { return TypeBits.FromAST; }
1411
1412  /// \brief Whether this type is or contains an unexpanded parameter
1413  /// pack, used to support C++0x variadic templates.
1414  ///
1415  /// A type that contains a parameter pack shall be expanded by the
1416  /// ellipsis operator at some point. For example, the typedef in the
1417  /// following example contains an unexpanded parameter pack 'T':
1418  ///
1419  /// \code
1420  /// template<typename ...T>
1421  /// struct X {
1422  ///   typedef T* pointer_types; // ill-formed; T is a parameter pack.
1423  /// };
1424  /// \endcode
1425  ///
1426  /// Note that this routine does not specify which
1427  bool containsUnexpandedParameterPack() const {
1428    return TypeBits.ContainsUnexpandedParameterPack;
1429  }
1430
1431  /// Determines if this type would be canonical if it had no further
1432  /// qualification.
1433  bool isCanonicalUnqualified() const {
1434    return CanonicalType == QualType(this, 0);
1435  }
1436
1437  /// Pull a single level of sugar off of this locally-unqualified type.
1438  /// Users should generally prefer SplitQualType::getSingleStepDesugaredType()
1439  /// or QualType::getSingleStepDesugaredType(const ASTContext&).
1440  QualType getLocallyUnqualifiedSingleStepDesugaredType() const;
1441
1442  /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
1443  /// object types, function types, and incomplete types.
1444
1445  /// isIncompleteType - Return true if this is an incomplete type.
1446  /// A type that can describe objects, but which lacks information needed to
1447  /// determine its size (e.g. void, or a fwd declared struct). Clients of this
1448  /// routine will need to determine if the size is actually required.
1449  ///
1450  /// \brief Def If non-NULL, and the type refers to some kind of declaration
1451  /// that can be completed (such as a C struct, C++ class, or Objective-C
1452  /// class), will be set to the declaration.
1453  bool isIncompleteType(NamedDecl **Def = 0) const;
1454
1455  /// isIncompleteOrObjectType - Return true if this is an incomplete or object
1456  /// type, in other words, not a function type.
1457  bool isIncompleteOrObjectType() const {
1458    return !isFunctionType();
1459  }
1460
1461  /// \brief Determine whether this type is an object type.
1462  bool isObjectType() const {
1463    // C++ [basic.types]p8:
1464    //   An object type is a (possibly cv-qualified) type that is not a
1465    //   function type, not a reference type, and not a void type.
1466    return !isReferenceType() && !isFunctionType() && !isVoidType();
1467  }
1468
1469  /// isLiteralType - Return true if this is a literal type
1470  /// (C++0x [basic.types]p10)
1471  bool isLiteralType() const;
1472
1473  /// \brief Test if this type is a standard-layout type.
1474  /// (C++0x [basic.type]p9)
1475  bool isStandardLayoutType() const;
1476
1477  /// Helper methods to distinguish type categories. All type predicates
1478  /// operate on the canonical type, ignoring typedefs and qualifiers.
1479
1480  /// isBuiltinType - returns true if the type is a builtin type.
1481  bool isBuiltinType() const;
1482
1483  /// isSpecificBuiltinType - Test for a particular builtin type.
1484  bool isSpecificBuiltinType(unsigned K) const;
1485
1486  /// isPlaceholderType - Test for a type which does not represent an
1487  /// actual type-system type but is instead used as a placeholder for
1488  /// various convenient purposes within Clang.  All such types are
1489  /// BuiltinTypes.
1490  bool isPlaceholderType() const;
1491  const BuiltinType *getAsPlaceholderType() const;
1492
1493  /// isSpecificPlaceholderType - Test for a specific placeholder type.
1494  bool isSpecificPlaceholderType(unsigned K) const;
1495
1496  /// isNonOverloadPlaceholderType - Test for a placeholder type
1497  /// other than Overload;  see BuiltinType::isNonOverloadPlaceholderType.
1498  bool isNonOverloadPlaceholderType() const;
1499
1500  /// isIntegerType() does *not* include complex integers (a GCC extension).
1501  /// isComplexIntegerType() can be used to test for complex integers.
1502  bool isIntegerType() const;     // C99 6.2.5p17 (int, char, bool, enum)
1503  bool isEnumeralType() const;
1504  bool isBooleanType() const;
1505  bool isCharType() const;
1506  bool isWideCharType() const;
1507  bool isChar16Type() const;
1508  bool isChar32Type() const;
1509  bool isAnyCharacterType() const;
1510  bool isIntegralType(ASTContext &Ctx) const;
1511
1512  /// \brief Determine whether this type is an integral or enumeration type.
1513  bool isIntegralOrEnumerationType() const;
1514  /// \brief Determine whether this type is an integral or unscoped enumeration
1515  /// type.
1516  bool isIntegralOrUnscopedEnumerationType() const;
1517
1518  /// Floating point categories.
1519  bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double)
1520  /// isComplexType() does *not* include complex integers (a GCC extension).
1521  /// isComplexIntegerType() can be used to test for complex integers.
1522  bool isComplexType() const;      // C99 6.2.5p11 (complex)
1523  bool isAnyComplexType() const;   // C99 6.2.5p11 (complex) + Complex Int.
1524  bool isFloatingType() const;     // C99 6.2.5p11 (real floating + complex)
1525  bool isHalfType() const;         // OpenCL 6.1.1.1, NEON (IEEE 754-2008 half)
1526  bool isRealType() const;         // C99 6.2.5p17 (real floating + integer)
1527  bool isArithmeticType() const;   // C99 6.2.5p18 (integer + floating)
1528  bool isVoidType() const;         // C99 6.2.5p19
1529  bool isDerivedType() const;      // C99 6.2.5p20
1530  bool isScalarType() const;       // C99 6.2.5p21 (arithmetic + pointers)
1531  bool isAggregateType() const;
1532  bool isFundamentalType() const;
1533  bool isCompoundType() const;
1534
1535  // Type Predicates: Check to see if this type is structurally the specified
1536  // type, ignoring typedefs and qualifiers.
1537  bool isFunctionType() const;
1538  bool isFunctionNoProtoType() const { return getAs<FunctionNoProtoType>(); }
1539  bool isFunctionProtoType() const { return getAs<FunctionProtoType>(); }
1540  bool isPointerType() const;
1541  bool isAnyPointerType() const;   // Any C pointer or ObjC object pointer
1542  bool isBlockPointerType() const;
1543  bool isVoidPointerType() const;
1544  bool isReferenceType() const;
1545  bool isLValueReferenceType() const;
1546  bool isRValueReferenceType() const;
1547  bool isFunctionPointerType() const;
1548  bool isMemberPointerType() const;
1549  bool isMemberFunctionPointerType() const;
1550  bool isMemberDataPointerType() const;
1551  bool isArrayType() const;
1552  bool isConstantArrayType() const;
1553  bool isIncompleteArrayType() const;
1554  bool isVariableArrayType() const;
1555  bool isDependentSizedArrayType() const;
1556  bool isRecordType() const;
1557  bool isClassType() const;
1558  bool isStructureType() const;
1559  bool isInterfaceType() const;
1560  bool isStructureOrClassType() const;
1561  bool isUnionType() const;
1562  bool isComplexIntegerType() const;            // GCC _Complex integer type.
1563  bool isVectorType() const;                    // GCC vector type.
1564  bool isExtVectorType() const;                 // Extended vector type.
1565  bool isObjCObjectPointerType() const;         // pointer to ObjC object
1566  bool isObjCRetainableType() const;            // ObjC object or block pointer
1567  bool isObjCLifetimeType() const;              // (array of)* retainable type
1568  bool isObjCIndirectLifetimeType() const;      // (pointer to)* lifetime type
1569  bool isObjCNSObjectType() const;              // __attribute__((NSObject))
1570  // FIXME: change this to 'raw' interface type, so we can used 'interface' type
1571  // for the common case.
1572  bool isObjCObjectType() const;                // NSString or typeof(*(id)0)
1573  bool isObjCQualifiedInterfaceType() const;    // NSString<foo>
1574  bool isObjCQualifiedIdType() const;           // id<foo>
1575  bool isObjCQualifiedClassType() const;        // Class<foo>
1576  bool isObjCObjectOrInterfaceType() const;
1577  bool isObjCIdType() const;                    // id
1578  bool isObjCClassType() const;                 // Class
1579  bool isObjCSelType() const;                 // Class
1580  bool isObjCBuiltinType() const;               // 'id' or 'Class'
1581  bool isObjCARCBridgableType() const;
1582  bool isCARCBridgableType() const;
1583  bool isTemplateTypeParmType() const;          // C++ template type parameter
1584  bool isNullPtrType() const;                   // C++0x nullptr_t
1585  bool isAtomicType() const;                    // C11 _Atomic()
1586
1587  /// Determines if this type, which must satisfy
1588  /// isObjCLifetimeType(), is implicitly __unsafe_unretained rather
1589  /// than implicitly __strong.
1590  bool isObjCARCImplicitlyUnretainedType() const;
1591
1592  /// Return the implicit lifetime for this type, which must not be dependent.
1593  Qualifiers::ObjCLifetime getObjCARCImplicitLifetime() const;
1594
1595  enum ScalarTypeKind {
1596    STK_CPointer,
1597    STK_BlockPointer,
1598    STK_ObjCObjectPointer,
1599    STK_MemberPointer,
1600    STK_Bool,
1601    STK_Integral,
1602    STK_Floating,
1603    STK_IntegralComplex,
1604    STK_FloatingComplex
1605  };
1606  /// getScalarTypeKind - Given that this is a scalar type, classify it.
1607  ScalarTypeKind getScalarTypeKind() const;
1608
1609  /// isDependentType - Whether this type is a dependent type, meaning
1610  /// that its definition somehow depends on a template parameter
1611  /// (C++ [temp.dep.type]).
1612  bool isDependentType() const { return TypeBits.Dependent; }
1613
1614  /// \brief Determine whether this type is an instantiation-dependent type,
1615  /// meaning that the type involves a template parameter (even if the
1616  /// definition does not actually depend on the type substituted for that
1617  /// template parameter).
1618  bool isInstantiationDependentType() const {
1619    return TypeBits.InstantiationDependent;
1620  }
1621
1622  /// \brief Whether this type is a variably-modified type (C99 6.7.5).
1623  bool isVariablyModifiedType() const { return TypeBits.VariablyModified; }
1624
1625  /// \brief Whether this type involves a variable-length array type
1626  /// with a definite size.
1627  bool hasSizedVLAType() const;
1628
1629  /// \brief Whether this type is or contains a local or unnamed type.
1630  bool hasUnnamedOrLocalType() const;
1631
1632  bool isOverloadableType() const;
1633
1634  /// \brief Determine wither this type is a C++ elaborated-type-specifier.
1635  bool isElaboratedTypeSpecifier() const;
1636
1637  bool canDecayToPointerType() const;
1638
1639  /// hasPointerRepresentation - Whether this type is represented
1640  /// natively as a pointer; this includes pointers, references, block
1641  /// pointers, and Objective-C interface, qualified id, and qualified
1642  /// interface types, as well as nullptr_t.
1643  bool hasPointerRepresentation() const;
1644
1645  /// hasObjCPointerRepresentation - Whether this type can represent
1646  /// an objective pointer type for the purpose of GC'ability
1647  bool hasObjCPointerRepresentation() const;
1648
1649  /// \brief Determine whether this type has an integer representation
1650  /// of some sort, e.g., it is an integer type or a vector.
1651  bool hasIntegerRepresentation() const;
1652
1653  /// \brief Determine whether this type has an signed integer representation
1654  /// of some sort, e.g., it is an signed integer type or a vector.
1655  bool hasSignedIntegerRepresentation() const;
1656
1657  /// \brief Determine whether this type has an unsigned integer representation
1658  /// of some sort, e.g., it is an unsigned integer type or a vector.
1659  bool hasUnsignedIntegerRepresentation() const;
1660
1661  /// \brief Determine whether this type has a floating-point representation
1662  /// of some sort, e.g., it is a floating-point type or a vector thereof.
1663  bool hasFloatingRepresentation() const;
1664
1665  // Type Checking Functions: Check to see if this type is structurally the
1666  // specified type, ignoring typedefs and qualifiers, and return a pointer to
1667  // the best type we can.
1668  const RecordType *getAsStructureType() const;
1669  /// NOTE: getAs*ArrayType are methods on ASTContext.
1670  const RecordType *getAsUnionType() const;
1671  const ComplexType *getAsComplexIntegerType() const; // GCC complex int type.
1672  // The following is a convenience method that returns an ObjCObjectPointerType
1673  // for object declared using an interface.
1674  const ObjCObjectPointerType *getAsObjCInterfacePointerType() const;
1675  const ObjCObjectPointerType *getAsObjCQualifiedIdType() const;
1676  const ObjCObjectPointerType *getAsObjCQualifiedClassType() const;
1677  const ObjCObjectType *getAsObjCQualifiedInterfaceType() const;
1678
1679  /// \brief Retrieves the CXXRecordDecl that this type refers to, either
1680  /// because the type is a RecordType or because it is the injected-class-name
1681  /// type of a class template or class template partial specialization.
1682  CXXRecordDecl *getAsCXXRecordDecl() const;
1683
1684  /// If this is a pointer or reference to a RecordType, return the
1685  /// CXXRecordDecl that that type refers to.
1686  ///
1687  /// If this is not a pointer or reference, or the type being pointed to does
1688  /// not refer to a CXXRecordDecl, returns NULL.
1689  const CXXRecordDecl *getPointeeCXXRecordDecl() const;
1690
1691  /// \brief Get the AutoType whose type will be deduced for a variable with
1692  /// an initializer of this type. This looks through declarators like pointer
1693  /// types, but not through decltype or typedefs.
1694  AutoType *getContainedAutoType() const;
1695
1696  /// Member-template getAs<specific type>'.  Look through sugar for
1697  /// an instance of \<specific type>.   This scheme will eventually
1698  /// replace the specific getAsXXXX methods above.
1699  ///
1700  /// There are some specializations of this member template listed
1701  /// immediately following this class.
1702  template <typename T> const T *getAs() const;
1703
1704  /// A variant of getAs<> for array types which silently discards
1705  /// qualifiers from the outermost type.
1706  const ArrayType *getAsArrayTypeUnsafe() const;
1707
1708  /// Member-template castAs<specific type>.  Look through sugar for
1709  /// the underlying instance of \<specific type>.
1710  ///
1711  /// This method has the same relationship to getAs<T> as cast<T> has
1712  /// to dyn_cast<T>; which is to say, the underlying type *must*
1713  /// have the intended type, and this method will never return null.
1714  template <typename T> const T *castAs() const;
1715
1716  /// A variant of castAs<> for array type which silently discards
1717  /// qualifiers from the outermost type.
1718  const ArrayType *castAsArrayTypeUnsafe() const;
1719
1720  /// getBaseElementTypeUnsafe - Get the base element type of this
1721  /// type, potentially discarding type qualifiers.  This method
1722  /// should never be used when type qualifiers are meaningful.
1723  const Type *getBaseElementTypeUnsafe() const;
1724
1725  /// getArrayElementTypeNoTypeQual - If this is an array type, return the
1726  /// element type of the array, potentially with type qualifiers missing.
1727  /// This method should never be used when type qualifiers are meaningful.
1728  const Type *getArrayElementTypeNoTypeQual() const;
1729
1730  /// getPointeeType - If this is a pointer, ObjC object pointer, or block
1731  /// pointer, this returns the respective pointee.
1732  QualType getPointeeType() const;
1733
1734  /// getUnqualifiedDesugaredType() - Return the specified type with
1735  /// any "sugar" removed from the type, removing any typedefs,
1736  /// typeofs, etc., as well as any qualifiers.
1737  const Type *getUnqualifiedDesugaredType() const;
1738
1739  /// More type predicates useful for type checking/promotion
1740  bool isPromotableIntegerType() const; // C99 6.3.1.1p2
1741
1742  /// isSignedIntegerType - Return true if this is an integer type that is
1743  /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
1744  /// or an enum decl which has a signed representation.
1745  bool isSignedIntegerType() const;
1746
1747  /// isUnsignedIntegerType - Return true if this is an integer type that is
1748  /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool],
1749  /// or an enum decl which has an unsigned representation.
1750  bool isUnsignedIntegerType() const;
1751
1752  /// Determines whether this is an integer type that is signed or an
1753  /// enumeration types whose underlying type is a signed integer type.
1754  bool isSignedIntegerOrEnumerationType() const;
1755
1756  /// Determines whether this is an integer type that is unsigned or an
1757  /// enumeration types whose underlying type is a unsigned integer type.
1758  bool isUnsignedIntegerOrEnumerationType() const;
1759
1760  /// isConstantSizeType - Return true if this is not a variable sized type,
1761  /// according to the rules of C99 6.7.5p3.  It is not legal to call this on
1762  /// incomplete types.
1763  bool isConstantSizeType() const;
1764
1765  /// isSpecifierType - Returns true if this type can be represented by some
1766  /// set of type specifiers.
1767  bool isSpecifierType() const;
1768
1769  /// \brief Determine the linkage of this type.
1770  Linkage getLinkage() const;
1771
1772  /// \brief Determine the visibility of this type.
1773  Visibility getVisibility() const;
1774
1775  /// \brief Return true if the visibility was explicitly set is the code.
1776  bool isVisibilityExplicit() const;
1777
1778  /// \brief Determine the linkage and visibility of this type.
1779  std::pair<Linkage,Visibility> getLinkageAndVisibility() const;
1780
1781  /// \brief Note that the linkage is no longer known.
1782  void ClearLinkageCache();
1783
1784  const char *getTypeClassName() const;
1785
1786  QualType getCanonicalTypeInternal() const {
1787    return CanonicalType;
1788  }
1789  CanQualType getCanonicalTypeUnqualified() const; // in CanonicalType.h
1790  LLVM_ATTRIBUTE_USED void dump() const;
1791
1792  friend class ASTReader;
1793  friend class ASTWriter;
1794};
1795
1796/// \brief This will check for a TypedefType by removing any existing sugar
1797/// until it reaches a TypedefType or a non-sugared type.
1798template <> const TypedefType *Type::getAs() const;
1799
1800/// \brief This will check for a TemplateSpecializationType by removing any
1801/// existing sugar until it reaches a TemplateSpecializationType or a
1802/// non-sugared type.
1803template <> const TemplateSpecializationType *Type::getAs() const;
1804
1805// We can do canonical leaf types faster, because we don't have to
1806// worry about preserving child type decoration.
1807#define TYPE(Class, Base)
1808#define LEAF_TYPE(Class) \
1809template <> inline const Class##Type *Type::getAs() const { \
1810  return dyn_cast<Class##Type>(CanonicalType); \
1811} \
1812template <> inline const Class##Type *Type::castAs() const { \
1813  return cast<Class##Type>(CanonicalType); \
1814}
1815#include "clang/AST/TypeNodes.def"
1816
1817
1818/// BuiltinType - This class is used for builtin types like 'int'.  Builtin
1819/// types are always canonical and have a literal name field.
1820class BuiltinType : public Type {
1821public:
1822  enum Kind {
1823#define BUILTIN_TYPE(Id, SingletonId) Id,
1824#define LAST_BUILTIN_TYPE(Id) LastKind = Id
1825#include "clang/AST/BuiltinTypes.def"
1826  };
1827
1828public:
1829  BuiltinType(Kind K)
1830    : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent),
1831           /*InstantiationDependent=*/(K == Dependent),
1832           /*VariablyModified=*/false,
1833           /*Unexpanded paramter pack=*/false) {
1834    BuiltinTypeBits.Kind = K;
1835  }
1836
1837  Kind getKind() const { return static_cast<Kind>(BuiltinTypeBits.Kind); }
1838  StringRef getName(const PrintingPolicy &Policy) const;
1839  const char *getNameAsCString(const PrintingPolicy &Policy) const {
1840    // The StringRef is null-terminated.
1841    StringRef str = getName(Policy);
1842    assert(!str.empty() && str.data()[str.size()] == '\0');
1843    return str.data();
1844  }
1845
1846  bool isSugared() const { return false; }
1847  QualType desugar() const { return QualType(this, 0); }
1848
1849  bool isInteger() const {
1850    return getKind() >= Bool && getKind() <= Int128;
1851  }
1852
1853  bool isSignedInteger() const {
1854    return getKind() >= Char_S && getKind() <= Int128;
1855  }
1856
1857  bool isUnsignedInteger() const {
1858    return getKind() >= Bool && getKind() <= UInt128;
1859  }
1860
1861  bool isFloatingPoint() const {
1862    return getKind() >= Half && getKind() <= LongDouble;
1863  }
1864
1865  /// Determines whether the given kind corresponds to a placeholder type.
1866  static bool isPlaceholderTypeKind(Kind K) {
1867    return K >= Overload;
1868  }
1869
1870  /// Determines whether this type is a placeholder type, i.e. a type
1871  /// which cannot appear in arbitrary positions in a fully-formed
1872  /// expression.
1873  bool isPlaceholderType() const {
1874    return isPlaceholderTypeKind(getKind());
1875  }
1876
1877  /// Determines whether this type is a placeholder type other than
1878  /// Overload.  Most placeholder types require only syntactic
1879  /// information about their context in order to be resolved (e.g.
1880  /// whether it is a call expression), which means they can (and
1881  /// should) be resolved in an earlier "phase" of analysis.
1882  /// Overload expressions sometimes pick up further information
1883  /// from their context, like whether the context expects a
1884  /// specific function-pointer type, and so frequently need
1885  /// special treatment.
1886  bool isNonOverloadPlaceholderType() const {
1887    return getKind() > Overload;
1888  }
1889
1890  static bool classof(const Type *T) { return T->getTypeClass() == Builtin; }
1891};
1892
1893/// ComplexType - C99 6.2.5p11 - Complex values.  This supports the C99 complex
1894/// types (_Complex float etc) as well as the GCC integer complex extensions.
1895///
1896class ComplexType : public Type, public llvm::FoldingSetNode {
1897  QualType ElementType;
1898  ComplexType(QualType Element, QualType CanonicalPtr) :
1899    Type(Complex, CanonicalPtr, Element->isDependentType(),
1900         Element->isInstantiationDependentType(),
1901         Element->isVariablyModifiedType(),
1902         Element->containsUnexpandedParameterPack()),
1903    ElementType(Element) {
1904  }
1905  friend class ASTContext;  // ASTContext creates these.
1906
1907public:
1908  QualType getElementType() const { return ElementType; }
1909
1910  bool isSugared() const { return false; }
1911  QualType desugar() const { return QualType(this, 0); }
1912
1913  void Profile(llvm::FoldingSetNodeID &ID) {
1914    Profile(ID, getElementType());
1915  }
1916  static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) {
1917    ID.AddPointer(Element.getAsOpaquePtr());
1918  }
1919
1920  static bool classof(const Type *T) { return T->getTypeClass() == Complex; }
1921};
1922
1923/// ParenType - Sugar for parentheses used when specifying types.
1924///
1925class ParenType : public Type, public llvm::FoldingSetNode {
1926  QualType Inner;
1927
1928  ParenType(QualType InnerType, QualType CanonType) :
1929    Type(Paren, CanonType, InnerType->isDependentType(),
1930         InnerType->isInstantiationDependentType(),
1931         InnerType->isVariablyModifiedType(),
1932         InnerType->containsUnexpandedParameterPack()),
1933    Inner(InnerType) {
1934  }
1935  friend class ASTContext;  // ASTContext creates these.
1936
1937public:
1938
1939  QualType getInnerType() const { return Inner; }
1940
1941  bool isSugared() const { return true; }
1942  QualType desugar() const { return getInnerType(); }
1943
1944  void Profile(llvm::FoldingSetNodeID &ID) {
1945    Profile(ID, getInnerType());
1946  }
1947  static void Profile(llvm::FoldingSetNodeID &ID, QualType Inner) {
1948    Inner.Profile(ID);
1949  }
1950
1951  static bool classof(const Type *T) { return T->getTypeClass() == Paren; }
1952};
1953
1954/// PointerType - C99 6.7.5.1 - Pointer Declarators.
1955///
1956class PointerType : public Type, public llvm::FoldingSetNode {
1957  QualType PointeeType;
1958
1959  PointerType(QualType Pointee, QualType CanonicalPtr) :
1960    Type(Pointer, CanonicalPtr, Pointee->isDependentType(),
1961         Pointee->isInstantiationDependentType(),
1962         Pointee->isVariablyModifiedType(),
1963         Pointee->containsUnexpandedParameterPack()),
1964    PointeeType(Pointee) {
1965  }
1966  friend class ASTContext;  // ASTContext creates these.
1967
1968public:
1969
1970  QualType getPointeeType() const { return PointeeType; }
1971
1972  bool isSugared() const { return false; }
1973  QualType desugar() const { return QualType(this, 0); }
1974
1975  void Profile(llvm::FoldingSetNodeID &ID) {
1976    Profile(ID, getPointeeType());
1977  }
1978  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
1979    ID.AddPointer(Pointee.getAsOpaquePtr());
1980  }
1981
1982  static bool classof(const Type *T) { return T->getTypeClass() == Pointer; }
1983};
1984
1985/// BlockPointerType - pointer to a block type.
1986/// This type is to represent types syntactically represented as
1987/// "void (^)(int)", etc. Pointee is required to always be a function type.
1988///
1989class BlockPointerType : public Type, public llvm::FoldingSetNode {
1990  QualType PointeeType;  // Block is some kind of pointer type
1991  BlockPointerType(QualType Pointee, QualType CanonicalCls) :
1992    Type(BlockPointer, CanonicalCls, Pointee->isDependentType(),
1993         Pointee->isInstantiationDependentType(),
1994         Pointee->isVariablyModifiedType(),
1995         Pointee->containsUnexpandedParameterPack()),
1996    PointeeType(Pointee) {
1997  }
1998  friend class ASTContext;  // ASTContext creates these.
1999
2000public:
2001
2002  // Get the pointee type. Pointee is required to always be a function type.
2003  QualType getPointeeType() const { return PointeeType; }
2004
2005  bool isSugared() const { return false; }
2006  QualType desugar() const { return QualType(this, 0); }
2007
2008  void Profile(llvm::FoldingSetNodeID &ID) {
2009      Profile(ID, getPointeeType());
2010  }
2011  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
2012      ID.AddPointer(Pointee.getAsOpaquePtr());
2013  }
2014
2015  static bool classof(const Type *T) {
2016    return T->getTypeClass() == BlockPointer;
2017  }
2018};
2019
2020/// ReferenceType - Base for LValueReferenceType and RValueReferenceType
2021///
2022class ReferenceType : public Type, public llvm::FoldingSetNode {
2023  QualType PointeeType;
2024
2025protected:
2026  ReferenceType(TypeClass tc, QualType Referencee, QualType CanonicalRef,
2027                bool SpelledAsLValue) :
2028    Type(tc, CanonicalRef, Referencee->isDependentType(),
2029         Referencee->isInstantiationDependentType(),
2030         Referencee->isVariablyModifiedType(),
2031         Referencee->containsUnexpandedParameterPack()),
2032    PointeeType(Referencee)
2033  {
2034    ReferenceTypeBits.SpelledAsLValue = SpelledAsLValue;
2035    ReferenceTypeBits.InnerRef = Referencee->isReferenceType();
2036  }
2037
2038public:
2039  bool isSpelledAsLValue() const { return ReferenceTypeBits.SpelledAsLValue; }
2040  bool isInnerRef() const { return ReferenceTypeBits.InnerRef; }
2041
2042  QualType getPointeeTypeAsWritten() const { return PointeeType; }
2043  QualType getPointeeType() const {
2044    // FIXME: this might strip inner qualifiers; okay?
2045    const ReferenceType *T = this;
2046    while (T->isInnerRef())
2047      T = T->PointeeType->castAs<ReferenceType>();
2048    return T->PointeeType;
2049  }
2050
2051  void Profile(llvm::FoldingSetNodeID &ID) {
2052    Profile(ID, PointeeType, isSpelledAsLValue());
2053  }
2054  static void Profile(llvm::FoldingSetNodeID &ID,
2055                      QualType Referencee,
2056                      bool SpelledAsLValue) {
2057    ID.AddPointer(Referencee.getAsOpaquePtr());
2058    ID.AddBoolean(SpelledAsLValue);
2059  }
2060
2061  static bool classof(const Type *T) {
2062    return T->getTypeClass() == LValueReference ||
2063           T->getTypeClass() == RValueReference;
2064  }
2065};
2066
2067/// LValueReferenceType - C++ [dcl.ref] - Lvalue reference
2068///
2069class LValueReferenceType : public ReferenceType {
2070  LValueReferenceType(QualType Referencee, QualType CanonicalRef,
2071                      bool SpelledAsLValue) :
2072    ReferenceType(LValueReference, Referencee, CanonicalRef, SpelledAsLValue)
2073  {}
2074  friend class ASTContext; // ASTContext creates these
2075public:
2076  bool isSugared() const { return false; }
2077  QualType desugar() const { return QualType(this, 0); }
2078
2079  static bool classof(const Type *T) {
2080    return T->getTypeClass() == LValueReference;
2081  }
2082};
2083
2084/// RValueReferenceType - C++0x [dcl.ref] - Rvalue reference
2085///
2086class RValueReferenceType : public ReferenceType {
2087  RValueReferenceType(QualType Referencee, QualType CanonicalRef) :
2088    ReferenceType(RValueReference, Referencee, CanonicalRef, false) {
2089  }
2090  friend class ASTContext; // ASTContext creates these
2091public:
2092  bool isSugared() const { return false; }
2093  QualType desugar() const { return QualType(this, 0); }
2094
2095  static bool classof(const Type *T) {
2096    return T->getTypeClass() == RValueReference;
2097  }
2098};
2099
2100/// MemberPointerType - C++ 8.3.3 - Pointers to members
2101///
2102class MemberPointerType : public Type, public llvm::FoldingSetNode {
2103  QualType PointeeType;
2104  /// The class of which the pointee is a member. Must ultimately be a
2105  /// RecordType, but could be a typedef or a template parameter too.
2106  const Type *Class;
2107
2108  MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) :
2109    Type(MemberPointer, CanonicalPtr,
2110         Cls->isDependentType() || Pointee->isDependentType(),
2111         (Cls->isInstantiationDependentType() ||
2112          Pointee->isInstantiationDependentType()),
2113         Pointee->isVariablyModifiedType(),
2114         (Cls->containsUnexpandedParameterPack() ||
2115          Pointee->containsUnexpandedParameterPack())),
2116    PointeeType(Pointee), Class(Cls) {
2117  }
2118  friend class ASTContext; // ASTContext creates these.
2119
2120public:
2121  QualType getPointeeType() const { return PointeeType; }
2122
2123  /// Returns true if the member type (i.e. the pointee type) is a
2124  /// function type rather than a data-member type.
2125  bool isMemberFunctionPointer() const {
2126    return PointeeType->isFunctionProtoType();
2127  }
2128
2129  /// Returns true if the member type (i.e. the pointee type) is a
2130  /// data type rather than a function type.
2131  bool isMemberDataPointer() const {
2132    return !PointeeType->isFunctionProtoType();
2133  }
2134
2135  const Type *getClass() const { return Class; }
2136
2137  bool isSugared() const { return false; }
2138  QualType desugar() const { return QualType(this, 0); }
2139
2140  void Profile(llvm::FoldingSetNodeID &ID) {
2141    Profile(ID, getPointeeType(), getClass());
2142  }
2143  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee,
2144                      const Type *Class) {
2145    ID.AddPointer(Pointee.getAsOpaquePtr());
2146    ID.AddPointer(Class);
2147  }
2148
2149  static bool classof(const Type *T) {
2150    return T->getTypeClass() == MemberPointer;
2151  }
2152};
2153
2154/// ArrayType - C99 6.7.5.2 - Array Declarators.
2155///
2156class ArrayType : public Type, public llvm::FoldingSetNode {
2157public:
2158  /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4])
2159  /// an array with a static size (e.g. int X[static 4]), or an array
2160  /// with a star size (e.g. int X[*]).
2161  /// 'static' is only allowed on function parameters.
2162  enum ArraySizeModifier {
2163    Normal, Static, Star
2164  };
2165private:
2166  /// ElementType - The element type of the array.
2167  QualType ElementType;
2168
2169protected:
2170  // C++ [temp.dep.type]p1:
2171  //   A type is dependent if it is...
2172  //     - an array type constructed from any dependent type or whose
2173  //       size is specified by a constant expression that is
2174  //       value-dependent,
2175  ArrayType(TypeClass tc, QualType et, QualType can,
2176            ArraySizeModifier sm, unsigned tq,
2177            bool ContainsUnexpandedParameterPack)
2178    : Type(tc, can, et->isDependentType() || tc == DependentSizedArray,
2179           et->isInstantiationDependentType() || tc == DependentSizedArray,
2180           (tc == VariableArray || et->isVariablyModifiedType()),
2181           ContainsUnexpandedParameterPack),
2182      ElementType(et) {
2183    ArrayTypeBits.IndexTypeQuals = tq;
2184    ArrayTypeBits.SizeModifier = sm;
2185  }
2186
2187  friend class ASTContext;  // ASTContext creates these.
2188
2189public:
2190  QualType getElementType() const { return ElementType; }
2191  ArraySizeModifier getSizeModifier() const {
2192    return ArraySizeModifier(ArrayTypeBits.SizeModifier);
2193  }
2194  Qualifiers getIndexTypeQualifiers() const {
2195    return Qualifiers::fromCVRMask(getIndexTypeCVRQualifiers());
2196  }
2197  unsigned getIndexTypeCVRQualifiers() const {
2198    return ArrayTypeBits.IndexTypeQuals;
2199  }
2200
2201  static bool classof(const Type *T) {
2202    return T->getTypeClass() == ConstantArray ||
2203           T->getTypeClass() == VariableArray ||
2204           T->getTypeClass() == IncompleteArray ||
2205           T->getTypeClass() == DependentSizedArray;
2206  }
2207};
2208
2209/// ConstantArrayType - This class represents the canonical version of
2210/// C arrays with a specified constant size.  For example, the canonical
2211/// type for 'int A[4 + 4*100]' is a ConstantArrayType where the element
2212/// type is 'int' and the size is 404.
2213class ConstantArrayType : public ArrayType {
2214  llvm::APInt Size; // Allows us to unique the type.
2215
2216  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
2217                    ArraySizeModifier sm, unsigned tq)
2218    : ArrayType(ConstantArray, et, can, sm, tq,
2219                et->containsUnexpandedParameterPack()),
2220      Size(size) {}
2221protected:
2222  ConstantArrayType(TypeClass tc, QualType et, QualType can,
2223                    const llvm::APInt &size, ArraySizeModifier sm, unsigned tq)
2224    : ArrayType(tc, et, can, sm, tq, et->containsUnexpandedParameterPack()),
2225      Size(size) {}
2226  friend class ASTContext;  // ASTContext creates these.
2227public:
2228  const llvm::APInt &getSize() const { return Size; }
2229  bool isSugared() const { return false; }
2230  QualType desugar() const { return QualType(this, 0); }
2231
2232
2233  /// \brief Determine the number of bits required to address a member of
2234  // an array with the given element type and number of elements.
2235  static unsigned getNumAddressingBits(ASTContext &Context,
2236                                       QualType ElementType,
2237                                       const llvm::APInt &NumElements);
2238
2239  /// \brief Determine the maximum number of active bits that an array's size
2240  /// can require, which limits the maximum size of the array.
2241  static unsigned getMaxSizeBits(ASTContext &Context);
2242
2243  void Profile(llvm::FoldingSetNodeID &ID) {
2244    Profile(ID, getElementType(), getSize(),
2245            getSizeModifier(), getIndexTypeCVRQualifiers());
2246  }
2247  static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
2248                      const llvm::APInt &ArraySize, ArraySizeModifier SizeMod,
2249                      unsigned TypeQuals) {
2250    ID.AddPointer(ET.getAsOpaquePtr());
2251    ID.AddInteger(ArraySize.getZExtValue());
2252    ID.AddInteger(SizeMod);
2253    ID.AddInteger(TypeQuals);
2254  }
2255  static bool classof(const Type *T) {
2256    return T->getTypeClass() == ConstantArray;
2257  }
2258};
2259
2260/// IncompleteArrayType - This class represents C arrays with an unspecified
2261/// size.  For example 'int A[]' has an IncompleteArrayType where the element
2262/// type is 'int' and the size is unspecified.
2263class IncompleteArrayType : public ArrayType {
2264
2265  IncompleteArrayType(QualType et, QualType can,
2266                      ArraySizeModifier sm, unsigned tq)
2267    : ArrayType(IncompleteArray, et, can, sm, tq,
2268                et->containsUnexpandedParameterPack()) {}
2269  friend class ASTContext;  // ASTContext creates these.
2270public:
2271  bool isSugared() const { return false; }
2272  QualType desugar() const { return QualType(this, 0); }
2273
2274  static bool classof(const Type *T) {
2275    return T->getTypeClass() == IncompleteArray;
2276  }
2277
2278  friend class StmtIteratorBase;
2279
2280  void Profile(llvm::FoldingSetNodeID &ID) {
2281    Profile(ID, getElementType(), getSizeModifier(),
2282            getIndexTypeCVRQualifiers());
2283  }
2284
2285  static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
2286                      ArraySizeModifier SizeMod, unsigned TypeQuals) {
2287    ID.AddPointer(ET.getAsOpaquePtr());
2288    ID.AddInteger(SizeMod);
2289    ID.AddInteger(TypeQuals);
2290  }
2291};
2292
2293/// VariableArrayType - This class represents C arrays with a specified size
2294/// which is not an integer-constant-expression.  For example, 'int s[x+foo()]'.
2295/// Since the size expression is an arbitrary expression, we store it as such.
2296///
2297/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and
2298/// should not be: two lexically equivalent variable array types could mean
2299/// different things, for example, these variables do not have the same type
2300/// dynamically:
2301///
2302/// void foo(int x) {
2303///   int Y[x];
2304///   ++x;
2305///   int Z[x];
2306/// }
2307///
2308class VariableArrayType : public ArrayType {
2309  /// SizeExpr - An assignment expression. VLA's are only permitted within
2310  /// a function block.
2311  Stmt *SizeExpr;
2312  /// Brackets - The left and right array brackets.
2313  SourceRange Brackets;
2314
2315  VariableArrayType(QualType et, QualType can, Expr *e,
2316                    ArraySizeModifier sm, unsigned tq,
2317                    SourceRange brackets)
2318    : ArrayType(VariableArray, et, can, sm, tq,
2319                et->containsUnexpandedParameterPack()),
2320      SizeExpr((Stmt*) e), Brackets(brackets) {}
2321  friend class ASTContext;  // ASTContext creates these.
2322
2323public:
2324  Expr *getSizeExpr() const {
2325    // We use C-style casts instead of cast<> here because we do not wish
2326    // to have a dependency of Type.h on Stmt.h/Expr.h.
2327    return (Expr*) SizeExpr;
2328  }
2329  SourceRange getBracketsRange() const { return Brackets; }
2330  SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
2331  SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
2332
2333  bool isSugared() const { return false; }
2334  QualType desugar() const { return QualType(this, 0); }
2335
2336  static bool classof(const Type *T) {
2337    return T->getTypeClass() == VariableArray;
2338  }
2339
2340  friend class StmtIteratorBase;
2341
2342  void Profile(llvm::FoldingSetNodeID &ID) {
2343    llvm_unreachable("Cannot unique VariableArrayTypes.");
2344  }
2345};
2346
2347/// DependentSizedArrayType - This type represents an array type in
2348/// C++ whose size is a value-dependent expression. For example:
2349///
2350/// \code
2351/// template<typename T, int Size>
2352/// class array {
2353///   T data[Size];
2354/// };
2355/// \endcode
2356///
2357/// For these types, we won't actually know what the array bound is
2358/// until template instantiation occurs, at which point this will
2359/// become either a ConstantArrayType or a VariableArrayType.
2360class DependentSizedArrayType : public ArrayType {
2361  const ASTContext &Context;
2362
2363  /// \brief An assignment expression that will instantiate to the
2364  /// size of the array.
2365  ///
2366  /// The expression itself might be NULL, in which case the array
2367  /// type will have its size deduced from an initializer.
2368  Stmt *SizeExpr;
2369
2370  /// Brackets - The left and right array brackets.
2371  SourceRange Brackets;
2372
2373  DependentSizedArrayType(const ASTContext &Context, QualType et, QualType can,
2374                          Expr *e, ArraySizeModifier sm, unsigned tq,
2375                          SourceRange brackets);
2376
2377  friend class ASTContext;  // ASTContext creates these.
2378
2379public:
2380  Expr *getSizeExpr() const {
2381    // We use C-style casts instead of cast<> here because we do not wish
2382    // to have a dependency of Type.h on Stmt.h/Expr.h.
2383    return (Expr*) SizeExpr;
2384  }
2385  SourceRange getBracketsRange() const { return Brackets; }
2386  SourceLocation getLBracketLoc() const { return Brackets.getBegin(); }
2387  SourceLocation getRBracketLoc() const { return Brackets.getEnd(); }
2388
2389  bool isSugared() const { return false; }
2390  QualType desugar() const { return QualType(this, 0); }
2391
2392  static bool classof(const Type *T) {
2393    return T->getTypeClass() == DependentSizedArray;
2394  }
2395
2396  friend class StmtIteratorBase;
2397
2398
2399  void Profile(llvm::FoldingSetNodeID &ID) {
2400    Profile(ID, Context, getElementType(),
2401            getSizeModifier(), getIndexTypeCVRQualifiers(), getSizeExpr());
2402  }
2403
2404  static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
2405                      QualType ET, ArraySizeModifier SizeMod,
2406                      unsigned TypeQuals, Expr *E);
2407};
2408
2409/// DependentSizedExtVectorType - This type represent an extended vector type
2410/// where either the type or size is dependent. For example:
2411/// @code
2412/// template<typename T, int Size>
2413/// class vector {
2414///   typedef T __attribute__((ext_vector_type(Size))) type;
2415/// }
2416/// @endcode
2417class DependentSizedExtVectorType : public Type, public llvm::FoldingSetNode {
2418  const ASTContext &Context;
2419  Expr *SizeExpr;
2420  /// ElementType - The element type of the array.
2421  QualType ElementType;
2422  SourceLocation loc;
2423
2424  DependentSizedExtVectorType(const ASTContext &Context, QualType ElementType,
2425                              QualType can, Expr *SizeExpr, SourceLocation loc);
2426
2427  friend class ASTContext;
2428
2429public:
2430  Expr *getSizeExpr() const { return SizeExpr; }
2431  QualType getElementType() const { return ElementType; }
2432  SourceLocation getAttributeLoc() const { return loc; }
2433
2434  bool isSugared() const { return false; }
2435  QualType desugar() const { return QualType(this, 0); }
2436
2437  static bool classof(const Type *T) {
2438    return T->getTypeClass() == DependentSizedExtVector;
2439  }
2440
2441  void Profile(llvm::FoldingSetNodeID &ID) {
2442    Profile(ID, Context, getElementType(), getSizeExpr());
2443  }
2444
2445  static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
2446                      QualType ElementType, Expr *SizeExpr);
2447};
2448
2449
2450/// VectorType - GCC generic vector type. This type is created using
2451/// __attribute__((vector_size(n)), where "n" specifies the vector size in
2452/// bytes; or from an Altivec __vector or vector declaration.
2453/// Since the constructor takes the number of vector elements, the
2454/// client is responsible for converting the size into the number of elements.
2455class VectorType : public Type, public llvm::FoldingSetNode {
2456public:
2457  enum VectorKind {
2458    GenericVector,  // not a target-specific vector type
2459    AltiVecVector,  // is AltiVec vector
2460    AltiVecPixel,   // is AltiVec 'vector Pixel'
2461    AltiVecBool,    // is AltiVec 'vector bool ...'
2462    NeonVector,     // is ARM Neon vector
2463    NeonPolyVector  // is ARM Neon polynomial vector
2464  };
2465protected:
2466  /// ElementType - The element type of the vector.
2467  QualType ElementType;
2468
2469  VectorType(QualType vecType, unsigned nElements, QualType canonType,
2470             VectorKind vecKind);
2471
2472  VectorType(TypeClass tc, QualType vecType, unsigned nElements,
2473             QualType canonType, VectorKind vecKind);
2474
2475  friend class ASTContext;  // ASTContext creates these.
2476
2477public:
2478
2479  QualType getElementType() const { return ElementType; }
2480  unsigned getNumElements() const { return VectorTypeBits.NumElements; }
2481
2482  bool isSugared() const { return false; }
2483  QualType desugar() const { return QualType(this, 0); }
2484
2485  VectorKind getVectorKind() const {
2486    return VectorKind(VectorTypeBits.VecKind);
2487  }
2488
2489  void Profile(llvm::FoldingSetNodeID &ID) {
2490    Profile(ID, getElementType(), getNumElements(),
2491            getTypeClass(), getVectorKind());
2492  }
2493  static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType,
2494                      unsigned NumElements, TypeClass TypeClass,
2495                      VectorKind VecKind) {
2496    ID.AddPointer(ElementType.getAsOpaquePtr());
2497    ID.AddInteger(NumElements);
2498    ID.AddInteger(TypeClass);
2499    ID.AddInteger(VecKind);
2500  }
2501
2502  static bool classof(const Type *T) {
2503    return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector;
2504  }
2505};
2506
2507/// ExtVectorType - Extended vector type. This type is created using
2508/// __attribute__((ext_vector_type(n)), where "n" is the number of elements.
2509/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This
2510/// class enables syntactic extensions, like Vector Components for accessing
2511/// points, colors, and textures (modeled after OpenGL Shading Language).
2512class ExtVectorType : public VectorType {
2513  ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) :
2514    VectorType(ExtVector, vecType, nElements, canonType, GenericVector) {}
2515  friend class ASTContext;  // ASTContext creates these.
2516public:
2517  static int getPointAccessorIdx(char c) {
2518    switch (c) {
2519    default: return -1;
2520    case 'x': return 0;
2521    case 'y': return 1;
2522    case 'z': return 2;
2523    case 'w': return 3;
2524    }
2525  }
2526  static int getNumericAccessorIdx(char c) {
2527    switch (c) {
2528      default: return -1;
2529      case '0': return 0;
2530      case '1': return 1;
2531      case '2': return 2;
2532      case '3': return 3;
2533      case '4': return 4;
2534      case '5': return 5;
2535      case '6': return 6;
2536      case '7': return 7;
2537      case '8': return 8;
2538      case '9': return 9;
2539      case 'A':
2540      case 'a': return 10;
2541      case 'B':
2542      case 'b': return 11;
2543      case 'C':
2544      case 'c': return 12;
2545      case 'D':
2546      case 'd': return 13;
2547      case 'E':
2548      case 'e': return 14;
2549      case 'F':
2550      case 'f': return 15;
2551    }
2552  }
2553
2554  static int getAccessorIdx(char c) {
2555    if (int idx = getPointAccessorIdx(c)+1) return idx-1;
2556    return getNumericAccessorIdx(c);
2557  }
2558
2559  bool isAccessorWithinNumElements(char c) const {
2560    if (int idx = getAccessorIdx(c)+1)
2561      return unsigned(idx-1) < getNumElements();
2562    return false;
2563  }
2564  bool isSugared() const { return false; }
2565  QualType desugar() const { return QualType(this, 0); }
2566
2567  static bool classof(const Type *T) {
2568    return T->getTypeClass() == ExtVector;
2569  }
2570};
2571
2572/// FunctionType - C99 6.7.5.3 - Function Declarators.  This is the common base
2573/// class of FunctionNoProtoType and FunctionProtoType.
2574///
2575class FunctionType : public Type {
2576  // The type returned by the function.
2577  QualType ResultType;
2578
2579 public:
2580  /// ExtInfo - A class which abstracts out some details necessary for
2581  /// making a call.
2582  ///
2583  /// It is not actually used directly for storing this information in
2584  /// a FunctionType, although FunctionType does currently use the
2585  /// same bit-pattern.
2586  ///
2587  // If you add a field (say Foo), other than the obvious places (both,
2588  // constructors, compile failures), what you need to update is
2589  // * Operator==
2590  // * getFoo
2591  // * withFoo
2592  // * functionType. Add Foo, getFoo.
2593  // * ASTContext::getFooType
2594  // * ASTContext::mergeFunctionTypes
2595  // * FunctionNoProtoType::Profile
2596  // * FunctionProtoType::Profile
2597  // * TypePrinter::PrintFunctionProto
2598  // * AST read and write
2599  // * Codegen
2600  class ExtInfo {
2601    // Feel free to rearrange or add bits, but if you go over 8,
2602    // you'll need to adjust both the Bits field below and
2603    // Type::FunctionTypeBitfields.
2604
2605    //   |  CC  |noreturn|produces|regparm|
2606    //   |0 .. 2|   3    |    4   | 5 .. 7|
2607    //
2608    // regparm is either 0 (no regparm attribute) or the regparm value+1.
2609    enum { CallConvMask = 0x7 };
2610    enum { NoReturnMask = 0x8 };
2611    enum { ProducesResultMask = 0x10 };
2612    enum { RegParmMask = ~(CallConvMask | NoReturnMask | ProducesResultMask),
2613           RegParmOffset = 5 }; // Assumed to be the last field
2614
2615    uint16_t Bits;
2616
2617    ExtInfo(unsigned Bits) : Bits(static_cast<uint16_t>(Bits)) {}
2618
2619    friend class FunctionType;
2620
2621   public:
2622    // Constructor with no defaults. Use this when you know that you
2623    // have all the elements (when reading an AST file for example).
2624    ExtInfo(bool noReturn, bool hasRegParm, unsigned regParm, CallingConv cc,
2625            bool producesResult) {
2626      assert((!hasRegParm || regParm < 7) && "Invalid regparm value");
2627      Bits = ((unsigned) cc) |
2628             (noReturn ? NoReturnMask : 0) |
2629             (producesResult ? ProducesResultMask : 0) |
2630             (hasRegParm ? ((regParm + 1) << RegParmOffset) : 0);
2631    }
2632
2633    // Constructor with all defaults. Use when for example creating a
2634    // function know to use defaults.
2635    ExtInfo() : Bits(0) {}
2636
2637    bool getNoReturn() const { return Bits & NoReturnMask; }
2638    bool getProducesResult() const { return Bits & ProducesResultMask; }
2639    bool getHasRegParm() const { return (Bits >> RegParmOffset) != 0; }
2640    unsigned getRegParm() const {
2641      unsigned RegParm = Bits >> RegParmOffset;
2642      if (RegParm > 0)
2643        --RegParm;
2644      return RegParm;
2645    }
2646    CallingConv getCC() const { return CallingConv(Bits & CallConvMask); }
2647
2648    bool operator==(ExtInfo Other) const {
2649      return Bits == Other.Bits;
2650    }
2651    bool operator!=(ExtInfo Other) const {
2652      return Bits != Other.Bits;
2653    }
2654
2655    // Note that we don't have setters. That is by design, use
2656    // the following with methods instead of mutating these objects.
2657
2658    ExtInfo withNoReturn(bool noReturn) const {
2659      if (noReturn)
2660        return ExtInfo(Bits | NoReturnMask);
2661      else
2662        return ExtInfo(Bits & ~NoReturnMask);
2663    }
2664
2665    ExtInfo withProducesResult(bool producesResult) const {
2666      if (producesResult)
2667        return ExtInfo(Bits | ProducesResultMask);
2668      else
2669        return ExtInfo(Bits & ~ProducesResultMask);
2670    }
2671
2672    ExtInfo withRegParm(unsigned RegParm) const {
2673      assert(RegParm < 7 && "Invalid regparm value");
2674      return ExtInfo((Bits & ~RegParmMask) |
2675                     ((RegParm + 1) << RegParmOffset));
2676    }
2677
2678    ExtInfo withCallingConv(CallingConv cc) const {
2679      return ExtInfo((Bits & ~CallConvMask) | (unsigned) cc);
2680    }
2681
2682    void Profile(llvm::FoldingSetNodeID &ID) const {
2683      ID.AddInteger(Bits);
2684    }
2685  };
2686
2687protected:
2688  FunctionType(TypeClass tc, QualType res,
2689               unsigned typeQuals, RefQualifierKind RefQualifier,
2690               QualType Canonical, bool Dependent,
2691               bool InstantiationDependent,
2692               bool VariablyModified, bool ContainsUnexpandedParameterPack,
2693               ExtInfo Info)
2694    : Type(tc, Canonical, Dependent, InstantiationDependent, VariablyModified,
2695           ContainsUnexpandedParameterPack),
2696      ResultType(res) {
2697    FunctionTypeBits.ExtInfo = Info.Bits;
2698    FunctionTypeBits.TypeQuals = typeQuals;
2699    FunctionTypeBits.RefQualifier = static_cast<unsigned>(RefQualifier);
2700  }
2701  unsigned getTypeQuals() const { return FunctionTypeBits.TypeQuals; }
2702
2703  RefQualifierKind getRefQualifier() const {
2704    return static_cast<RefQualifierKind>(FunctionTypeBits.RefQualifier);
2705  }
2706
2707public:
2708
2709  QualType getResultType() const { return ResultType; }
2710
2711  bool getHasRegParm() const { return getExtInfo().getHasRegParm(); }
2712  unsigned getRegParmType() const { return getExtInfo().getRegParm(); }
2713  bool getNoReturnAttr() const { return getExtInfo().getNoReturn(); }
2714  CallingConv getCallConv() const { return getExtInfo().getCC(); }
2715  ExtInfo getExtInfo() const { return ExtInfo(FunctionTypeBits.ExtInfo); }
2716  bool isConst() const { return getTypeQuals() & Qualifiers::Const; }
2717  bool isVolatile() const { return getTypeQuals() & Qualifiers::Volatile; }
2718  bool isRestrict() const { return getTypeQuals() & Qualifiers::Restrict; }
2719
2720  /// \brief Determine the type of an expression that calls a function of
2721  /// this type.
2722  QualType getCallResultType(ASTContext &Context) const {
2723    return getResultType().getNonLValueExprType(Context);
2724  }
2725
2726  static StringRef getNameForCallConv(CallingConv CC);
2727
2728  static bool classof(const Type *T) {
2729    return T->getTypeClass() == FunctionNoProto ||
2730           T->getTypeClass() == FunctionProto;
2731  }
2732};
2733
2734/// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has
2735/// no information available about its arguments.
2736class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode {
2737  FunctionNoProtoType(QualType Result, QualType Canonical, ExtInfo Info)
2738    : FunctionType(FunctionNoProto, Result, 0, RQ_None, Canonical,
2739                   /*Dependent=*/false, /*InstantiationDependent=*/false,
2740                   Result->isVariablyModifiedType(),
2741                   /*ContainsUnexpandedParameterPack=*/false, Info) {}
2742
2743  friend class ASTContext;  // ASTContext creates these.
2744
2745public:
2746  // No additional state past what FunctionType provides.
2747
2748  bool isSugared() const { return false; }
2749  QualType desugar() const { return QualType(this, 0); }
2750
2751  void Profile(llvm::FoldingSetNodeID &ID) {
2752    Profile(ID, getResultType(), getExtInfo());
2753  }
2754  static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType,
2755                      ExtInfo Info) {
2756    Info.Profile(ID);
2757    ID.AddPointer(ResultType.getAsOpaquePtr());
2758  }
2759
2760  static bool classof(const Type *T) {
2761    return T->getTypeClass() == FunctionNoProto;
2762  }
2763};
2764
2765/// FunctionProtoType - Represents a prototype with argument type info, e.g.
2766/// 'int foo(int)' or 'int foo(void)'.  'void' is represented as having no
2767/// arguments, not as having a single void argument. Such a type can have an
2768/// exception specification, but this specification is not part of the canonical
2769/// type.
2770class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode {
2771public:
2772  /// ExtProtoInfo - Extra information about a function prototype.
2773  struct ExtProtoInfo {
2774    ExtProtoInfo() :
2775      Variadic(false), HasTrailingReturn(false), TypeQuals(0),
2776      ExceptionSpecType(EST_None), RefQualifier(RQ_None),
2777      NumExceptions(0), Exceptions(0), NoexceptExpr(0),
2778      ExceptionSpecDecl(0), ExceptionSpecTemplate(0),
2779      ConsumedArguments(0) {}
2780
2781    FunctionType::ExtInfo ExtInfo;
2782    bool Variadic : 1;
2783    bool HasTrailingReturn : 1;
2784    unsigned char TypeQuals;
2785    ExceptionSpecificationType ExceptionSpecType;
2786    RefQualifierKind RefQualifier;
2787    unsigned NumExceptions;
2788    const QualType *Exceptions;
2789    Expr *NoexceptExpr;
2790    FunctionDecl *ExceptionSpecDecl;
2791    FunctionDecl *ExceptionSpecTemplate;
2792    const bool *ConsumedArguments;
2793  };
2794
2795private:
2796  /// \brief Determine whether there are any argument types that
2797  /// contain an unexpanded parameter pack.
2798  static bool containsAnyUnexpandedParameterPack(const QualType *ArgArray,
2799                                                 unsigned numArgs) {
2800    for (unsigned Idx = 0; Idx < numArgs; ++Idx)
2801      if (ArgArray[Idx]->containsUnexpandedParameterPack())
2802        return true;
2803
2804    return false;
2805  }
2806
2807  FunctionProtoType(QualType result, const QualType *args, unsigned numArgs,
2808                    QualType canonical, const ExtProtoInfo &epi);
2809
2810  /// NumArgs - The number of arguments this function has, not counting '...'.
2811  unsigned NumArgs : 17;
2812
2813  /// NumExceptions - The number of types in the exception spec, if any.
2814  unsigned NumExceptions : 9;
2815
2816  /// ExceptionSpecType - The type of exception specification this function has.
2817  unsigned ExceptionSpecType : 3;
2818
2819  /// HasAnyConsumedArgs - Whether this function has any consumed arguments.
2820  unsigned HasAnyConsumedArgs : 1;
2821
2822  /// Variadic - Whether the function is variadic.
2823  unsigned Variadic : 1;
2824
2825  /// HasTrailingReturn - Whether this function has a trailing return type.
2826  unsigned HasTrailingReturn : 1;
2827
2828  // ArgInfo - There is an variable size array after the class in memory that
2829  // holds the argument types.
2830
2831  // Exceptions - There is another variable size array after ArgInfo that
2832  // holds the exception types.
2833
2834  // NoexceptExpr - Instead of Exceptions, there may be a single Expr* pointing
2835  // to the expression in the noexcept() specifier.
2836
2837  // ExceptionSpecDecl, ExceptionSpecTemplate - Instead of Exceptions, there may
2838  // be a pair of FunctionDecl* pointing to the function which should be used to
2839  // instantiate this function type's exception specification, and the function
2840  // from which it should be instantiated.
2841
2842  // ConsumedArgs - A variable size array, following Exceptions
2843  // and of length NumArgs, holding flags indicating which arguments
2844  // are consumed.  This only appears if HasAnyConsumedArgs is true.
2845
2846  friend class ASTContext;  // ASTContext creates these.
2847
2848  const bool *getConsumedArgsBuffer() const {
2849    assert(hasAnyConsumedArgs());
2850
2851    // Find the end of the exceptions.
2852    Expr * const *eh_end = reinterpret_cast<Expr * const *>(arg_type_end());
2853    if (getExceptionSpecType() != EST_ComputedNoexcept)
2854      eh_end += NumExceptions;
2855    else
2856      eh_end += 1; // NoexceptExpr
2857
2858    return reinterpret_cast<const bool*>(eh_end);
2859  }
2860
2861public:
2862  unsigned getNumArgs() const { return NumArgs; }
2863  QualType getArgType(unsigned i) const {
2864    assert(i < NumArgs && "Invalid argument number!");
2865    return arg_type_begin()[i];
2866  }
2867
2868  ExtProtoInfo getExtProtoInfo() const {
2869    ExtProtoInfo EPI;
2870    EPI.ExtInfo = getExtInfo();
2871    EPI.Variadic = isVariadic();
2872    EPI.HasTrailingReturn = hasTrailingReturn();
2873    EPI.ExceptionSpecType = getExceptionSpecType();
2874    EPI.TypeQuals = static_cast<unsigned char>(getTypeQuals());
2875    EPI.RefQualifier = getRefQualifier();
2876    if (EPI.ExceptionSpecType == EST_Dynamic) {
2877      EPI.NumExceptions = NumExceptions;
2878      EPI.Exceptions = exception_begin();
2879    } else if (EPI.ExceptionSpecType == EST_ComputedNoexcept) {
2880      EPI.NoexceptExpr = getNoexceptExpr();
2881    } else if (EPI.ExceptionSpecType == EST_Uninstantiated) {
2882      EPI.ExceptionSpecDecl = getExceptionSpecDecl();
2883      EPI.ExceptionSpecTemplate = getExceptionSpecTemplate();
2884    } else if (EPI.ExceptionSpecType == EST_Unevaluated) {
2885      EPI.ExceptionSpecDecl = getExceptionSpecDecl();
2886    }
2887    if (hasAnyConsumedArgs())
2888      EPI.ConsumedArguments = getConsumedArgsBuffer();
2889    return EPI;
2890  }
2891
2892  /// \brief Get the kind of exception specification on this function.
2893  ExceptionSpecificationType getExceptionSpecType() const {
2894    return static_cast<ExceptionSpecificationType>(ExceptionSpecType);
2895  }
2896  /// \brief Return whether this function has any kind of exception spec.
2897  bool hasExceptionSpec() const {
2898    return getExceptionSpecType() != EST_None;
2899  }
2900  /// \brief Return whether this function has a dynamic (throw) exception spec.
2901  bool hasDynamicExceptionSpec() const {
2902    return isDynamicExceptionSpec(getExceptionSpecType());
2903  }
2904  /// \brief Return whether this function has a noexcept exception spec.
2905  bool hasNoexceptExceptionSpec() const {
2906    return isNoexceptExceptionSpec(getExceptionSpecType());
2907  }
2908  /// \brief Result type of getNoexceptSpec().
2909  enum NoexceptResult {
2910    NR_NoNoexcept,  ///< There is no noexcept specifier.
2911    NR_BadNoexcept, ///< The noexcept specifier has a bad expression.
2912    NR_Dependent,   ///< The noexcept specifier is dependent.
2913    NR_Throw,       ///< The noexcept specifier evaluates to false.
2914    NR_Nothrow      ///< The noexcept specifier evaluates to true.
2915  };
2916  /// \brief Get the meaning of the noexcept spec on this function, if any.
2917  NoexceptResult getNoexceptSpec(ASTContext &Ctx) const;
2918  unsigned getNumExceptions() const { return NumExceptions; }
2919  QualType getExceptionType(unsigned i) const {
2920    assert(i < NumExceptions && "Invalid exception number!");
2921    return exception_begin()[i];
2922  }
2923  Expr *getNoexceptExpr() const {
2924    if (getExceptionSpecType() != EST_ComputedNoexcept)
2925      return 0;
2926    // NoexceptExpr sits where the arguments end.
2927    return *reinterpret_cast<Expr *const *>(arg_type_end());
2928  }
2929  /// \brief If this function type has an exception specification which hasn't
2930  /// been determined yet (either because it has not been evaluated or because
2931  /// it has not been instantiated), this is the function whose exception
2932  /// specification is represented by this type.
2933  FunctionDecl *getExceptionSpecDecl() const {
2934    if (getExceptionSpecType() != EST_Uninstantiated &&
2935        getExceptionSpecType() != EST_Unevaluated)
2936      return 0;
2937    return reinterpret_cast<FunctionDecl * const *>(arg_type_end())[0];
2938  }
2939  /// \brief If this function type has an uninstantiated exception
2940  /// specification, this is the function whose exception specification
2941  /// should be instantiated to find the exception specification for
2942  /// this type.
2943  FunctionDecl *getExceptionSpecTemplate() const {
2944    if (getExceptionSpecType() != EST_Uninstantiated)
2945      return 0;
2946    return reinterpret_cast<FunctionDecl * const *>(arg_type_end())[1];
2947  }
2948  bool isNothrow(ASTContext &Ctx) const {
2949    ExceptionSpecificationType EST = getExceptionSpecType();
2950    assert(EST != EST_Unevaluated && EST != EST_Uninstantiated);
2951    if (EST == EST_DynamicNone || EST == EST_BasicNoexcept)
2952      return true;
2953    if (EST != EST_ComputedNoexcept)
2954      return false;
2955    return getNoexceptSpec(Ctx) == NR_Nothrow;
2956  }
2957
2958  bool isVariadic() const { return Variadic; }
2959
2960  /// \brief Determines whether this function prototype contains a
2961  /// parameter pack at the end.
2962  ///
2963  /// A function template whose last parameter is a parameter pack can be
2964  /// called with an arbitrary number of arguments, much like a variadic
2965  /// function.
2966  bool isTemplateVariadic() const;
2967
2968  bool hasTrailingReturn() const { return HasTrailingReturn; }
2969
2970  unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); }
2971
2972
2973  /// \brief Retrieve the ref-qualifier associated with this function type.
2974  RefQualifierKind getRefQualifier() const {
2975    return FunctionType::getRefQualifier();
2976  }
2977
2978  typedef const QualType *arg_type_iterator;
2979  arg_type_iterator arg_type_begin() const {
2980    return reinterpret_cast<const QualType *>(this+1);
2981  }
2982  arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; }
2983
2984  typedef const QualType *exception_iterator;
2985  exception_iterator exception_begin() const {
2986    // exceptions begin where arguments end
2987    return arg_type_end();
2988  }
2989  exception_iterator exception_end() const {
2990    if (getExceptionSpecType() != EST_Dynamic)
2991      return exception_begin();
2992    return exception_begin() + NumExceptions;
2993  }
2994
2995  bool hasAnyConsumedArgs() const {
2996    return HasAnyConsumedArgs;
2997  }
2998  bool isArgConsumed(unsigned I) const {
2999    assert(I < getNumArgs() && "argument index out of range!");
3000    if (hasAnyConsumedArgs())
3001      return getConsumedArgsBuffer()[I];
3002    return false;
3003  }
3004
3005  bool isSugared() const { return false; }
3006  QualType desugar() const { return QualType(this, 0); }
3007
3008  // FIXME: Remove the string version.
3009  void printExceptionSpecification(std::string &S,
3010                                   const PrintingPolicy &Policy) const;
3011  void printExceptionSpecification(raw_ostream &OS,
3012                                   const PrintingPolicy &Policy) const;
3013
3014  static bool classof(const Type *T) {
3015    return T->getTypeClass() == FunctionProto;
3016  }
3017
3018  void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx);
3019  static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
3020                      arg_type_iterator ArgTys, unsigned NumArgs,
3021                      const ExtProtoInfo &EPI, const ASTContext &Context);
3022};
3023
3024
3025/// \brief Represents the dependent type named by a dependently-scoped
3026/// typename using declaration, e.g.
3027///   using typename Base<T>::foo;
3028/// Template instantiation turns these into the underlying type.
3029class UnresolvedUsingType : public Type {
3030  UnresolvedUsingTypenameDecl *Decl;
3031
3032  UnresolvedUsingType(const UnresolvedUsingTypenameDecl *D)
3033    : Type(UnresolvedUsing, QualType(), true, true, false,
3034           /*ContainsUnexpandedParameterPack=*/false),
3035      Decl(const_cast<UnresolvedUsingTypenameDecl*>(D)) {}
3036  friend class ASTContext; // ASTContext creates these.
3037public:
3038
3039  UnresolvedUsingTypenameDecl *getDecl() const { return Decl; }
3040
3041  bool isSugared() const { return false; }
3042  QualType desugar() const { return QualType(this, 0); }
3043
3044  static bool classof(const Type *T) {
3045    return T->getTypeClass() == UnresolvedUsing;
3046  }
3047
3048  void Profile(llvm::FoldingSetNodeID &ID) {
3049    return Profile(ID, Decl);
3050  }
3051  static void Profile(llvm::FoldingSetNodeID &ID,
3052                      UnresolvedUsingTypenameDecl *D) {
3053    ID.AddPointer(D);
3054  }
3055};
3056
3057
3058class TypedefType : public Type {
3059  TypedefNameDecl *Decl;
3060protected:
3061  TypedefType(TypeClass tc, const TypedefNameDecl *D, QualType can)
3062    : Type(tc, can, can->isDependentType(),
3063           can->isInstantiationDependentType(),
3064           can->isVariablyModifiedType(),
3065           /*ContainsUnexpandedParameterPack=*/false),
3066      Decl(const_cast<TypedefNameDecl*>(D)) {
3067    assert(!isa<TypedefType>(can) && "Invalid canonical type");
3068  }
3069  friend class ASTContext;  // ASTContext creates these.
3070public:
3071
3072  TypedefNameDecl *getDecl() const { return Decl; }
3073
3074  bool isSugared() const { return true; }
3075  QualType desugar() const;
3076
3077  static bool classof(const Type *T) { return T->getTypeClass() == Typedef; }
3078};
3079
3080/// TypeOfExprType (GCC extension).
3081class TypeOfExprType : public Type {
3082  Expr *TOExpr;
3083
3084protected:
3085  TypeOfExprType(Expr *E, QualType can = QualType());
3086  friend class ASTContext;  // ASTContext creates these.
3087public:
3088  Expr *getUnderlyingExpr() const { return TOExpr; }
3089
3090  /// \brief Remove a single level of sugar.
3091  QualType desugar() const;
3092
3093  /// \brief Returns whether this type directly provides sugar.
3094  bool isSugared() const;
3095
3096  static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; }
3097};
3098
3099/// \brief Internal representation of canonical, dependent
3100/// typeof(expr) types.
3101///
3102/// This class is used internally by the ASTContext to manage
3103/// canonical, dependent types, only. Clients will only see instances
3104/// of this class via TypeOfExprType nodes.
3105class DependentTypeOfExprType
3106  : public TypeOfExprType, public llvm::FoldingSetNode {
3107  const ASTContext &Context;
3108
3109public:
3110  DependentTypeOfExprType(const ASTContext &Context, Expr *E)
3111    : TypeOfExprType(E), Context(Context) { }
3112
3113  void Profile(llvm::FoldingSetNodeID &ID) {
3114    Profile(ID, Context, getUnderlyingExpr());
3115  }
3116
3117  static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3118                      Expr *E);
3119};
3120
3121/// TypeOfType (GCC extension).
3122class TypeOfType : public Type {
3123  QualType TOType;
3124  TypeOfType(QualType T, QualType can)
3125    : Type(TypeOf, can, T->isDependentType(),
3126           T->isInstantiationDependentType(),
3127           T->isVariablyModifiedType(),
3128           T->containsUnexpandedParameterPack()),
3129      TOType(T) {
3130    assert(!isa<TypedefType>(can) && "Invalid canonical type");
3131  }
3132  friend class ASTContext;  // ASTContext creates these.
3133public:
3134  QualType getUnderlyingType() const { return TOType; }
3135
3136  /// \brief Remove a single level of sugar.
3137  QualType desugar() const { return getUnderlyingType(); }
3138
3139  /// \brief Returns whether this type directly provides sugar.
3140  bool isSugared() const { return true; }
3141
3142  static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; }
3143};
3144
3145/// DecltypeType (C++0x)
3146class DecltypeType : public Type {
3147  Expr *E;
3148  QualType UnderlyingType;
3149
3150protected:
3151  DecltypeType(Expr *E, QualType underlyingType, QualType can = QualType());
3152  friend class ASTContext;  // ASTContext creates these.
3153public:
3154  Expr *getUnderlyingExpr() const { return E; }
3155  QualType getUnderlyingType() const { return UnderlyingType; }
3156
3157  /// \brief Remove a single level of sugar.
3158  QualType desugar() const;
3159
3160  /// \brief Returns whether this type directly provides sugar.
3161  bool isSugared() const;
3162
3163  static bool classof(const Type *T) { return T->getTypeClass() == Decltype; }
3164};
3165
3166/// \brief Internal representation of canonical, dependent
3167/// decltype(expr) types.
3168///
3169/// This class is used internally by the ASTContext to manage
3170/// canonical, dependent types, only. Clients will only see instances
3171/// of this class via DecltypeType nodes.
3172class DependentDecltypeType : public DecltypeType, public llvm::FoldingSetNode {
3173  const ASTContext &Context;
3174
3175public:
3176  DependentDecltypeType(const ASTContext &Context, Expr *E);
3177
3178  void Profile(llvm::FoldingSetNodeID &ID) {
3179    Profile(ID, Context, getUnderlyingExpr());
3180  }
3181
3182  static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
3183                      Expr *E);
3184};
3185
3186/// \brief A unary type transform, which is a type constructed from another
3187class UnaryTransformType : public Type {
3188public:
3189  enum UTTKind {
3190    EnumUnderlyingType
3191  };
3192
3193private:
3194  /// The untransformed type.
3195  QualType BaseType;
3196  /// The transformed type if not dependent, otherwise the same as BaseType.
3197  QualType UnderlyingType;
3198
3199  UTTKind UKind;
3200protected:
3201  UnaryTransformType(QualType BaseTy, QualType UnderlyingTy, UTTKind UKind,
3202                     QualType CanonicalTy);
3203  friend class ASTContext;
3204public:
3205  bool isSugared() const { return !isDependentType(); }
3206  QualType desugar() const { return UnderlyingType; }
3207
3208  QualType getUnderlyingType() const { return UnderlyingType; }
3209  QualType getBaseType() const { return BaseType; }
3210
3211  UTTKind getUTTKind() const { return UKind; }
3212
3213  static bool classof(const Type *T) {
3214    return T->getTypeClass() == UnaryTransform;
3215  }
3216};
3217
3218class TagType : public Type {
3219  /// Stores the TagDecl associated with this type. The decl may point to any
3220  /// TagDecl that declares the entity.
3221  TagDecl * decl;
3222
3223  friend class ASTReader;
3224
3225protected:
3226  TagType(TypeClass TC, const TagDecl *D, QualType can);
3227
3228public:
3229  TagDecl *getDecl() const;
3230
3231  /// @brief Determines whether this type is in the process of being
3232  /// defined.
3233  bool isBeingDefined() const;
3234
3235  static bool classof(const Type *T) {
3236    return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast;
3237  }
3238};
3239
3240/// RecordType - This is a helper class that allows the use of isa/cast/dyncast
3241/// to detect TagType objects of structs/unions/classes.
3242class RecordType : public TagType {
3243protected:
3244  explicit RecordType(const RecordDecl *D)
3245    : TagType(Record, reinterpret_cast<const TagDecl*>(D), QualType()) { }
3246  explicit RecordType(TypeClass TC, RecordDecl *D)
3247    : TagType(TC, reinterpret_cast<const TagDecl*>(D), QualType()) { }
3248  friend class ASTContext;   // ASTContext creates these.
3249public:
3250
3251  RecordDecl *getDecl() const {
3252    return reinterpret_cast<RecordDecl*>(TagType::getDecl());
3253  }
3254
3255  // FIXME: This predicate is a helper to QualType/Type. It needs to
3256  // recursively check all fields for const-ness. If any field is declared
3257  // const, it needs to return false.
3258  bool hasConstFields() const { return false; }
3259
3260  bool isSugared() const { return false; }
3261  QualType desugar() const { return QualType(this, 0); }
3262
3263  static bool classof(const Type *T) { return T->getTypeClass() == Record; }
3264};
3265
3266/// EnumType - This is a helper class that allows the use of isa/cast/dyncast
3267/// to detect TagType objects of enums.
3268class EnumType : public TagType {
3269  explicit EnumType(const EnumDecl *D)
3270    : TagType(Enum, reinterpret_cast<const TagDecl*>(D), QualType()) { }
3271  friend class ASTContext;   // ASTContext creates these.
3272public:
3273
3274  EnumDecl *getDecl() const {
3275    return reinterpret_cast<EnumDecl*>(TagType::getDecl());
3276  }
3277
3278  bool isSugared() const { return false; }
3279  QualType desugar() const { return QualType(this, 0); }
3280
3281  static bool classof(const Type *T) { return T->getTypeClass() == Enum; }
3282};
3283
3284/// AttributedType - An attributed type is a type to which a type
3285/// attribute has been applied.  The "modified type" is the
3286/// fully-sugared type to which the attributed type was applied;
3287/// generally it is not canonically equivalent to the attributed type.
3288/// The "equivalent type" is the minimally-desugared type which the
3289/// type is canonically equivalent to.
3290///
3291/// For example, in the following attributed type:
3292///     int32_t __attribute__((vector_size(16)))
3293///   - the modified type is the TypedefType for int32_t
3294///   - the equivalent type is VectorType(16, int32_t)
3295///   - the canonical type is VectorType(16, int)
3296class AttributedType : public Type, public llvm::FoldingSetNode {
3297public:
3298  // It is really silly to have yet another attribute-kind enum, but
3299  // clang::attr::Kind doesn't currently cover the pure type attrs.
3300  enum Kind {
3301    // Expression operand.
3302    attr_address_space,
3303    attr_regparm,
3304    attr_vector_size,
3305    attr_neon_vector_type,
3306    attr_neon_polyvector_type,
3307
3308    FirstExprOperandKind = attr_address_space,
3309    LastExprOperandKind = attr_neon_polyvector_type,
3310
3311    // Enumerated operand (string or keyword).
3312    attr_objc_gc,
3313    attr_objc_ownership,
3314    attr_pcs,
3315
3316    FirstEnumOperandKind = attr_objc_gc,
3317    LastEnumOperandKind = attr_pcs,
3318
3319    // No operand.
3320    attr_noreturn,
3321    attr_cdecl,
3322    attr_fastcall,
3323    attr_stdcall,
3324    attr_thiscall,
3325    attr_pascal
3326  };
3327
3328private:
3329  QualType ModifiedType;
3330  QualType EquivalentType;
3331
3332  friend class ASTContext; // creates these
3333
3334  AttributedType(QualType canon, Kind attrKind,
3335                 QualType modified, QualType equivalent)
3336    : Type(Attributed, canon, canon->isDependentType(),
3337           canon->isInstantiationDependentType(),
3338           canon->isVariablyModifiedType(),
3339           canon->containsUnexpandedParameterPack()),
3340      ModifiedType(modified), EquivalentType(equivalent) {
3341    AttributedTypeBits.AttrKind = attrKind;
3342  }
3343
3344public:
3345  Kind getAttrKind() const {
3346    return static_cast<Kind>(AttributedTypeBits.AttrKind);
3347  }
3348
3349  QualType getModifiedType() const { return ModifiedType; }
3350  QualType getEquivalentType() const { return EquivalentType; }
3351
3352  bool isSugared() const { return true; }
3353  QualType desugar() const { return getEquivalentType(); }
3354
3355  void Profile(llvm::FoldingSetNodeID &ID) {
3356    Profile(ID, getAttrKind(), ModifiedType, EquivalentType);
3357  }
3358
3359  static void Profile(llvm::FoldingSetNodeID &ID, Kind attrKind,
3360                      QualType modified, QualType equivalent) {
3361    ID.AddInteger(attrKind);
3362    ID.AddPointer(modified.getAsOpaquePtr());
3363    ID.AddPointer(equivalent.getAsOpaquePtr());
3364  }
3365
3366  static bool classof(const Type *T) {
3367    return T->getTypeClass() == Attributed;
3368  }
3369};
3370
3371class TemplateTypeParmType : public Type, public llvm::FoldingSetNode {
3372  // Helper data collector for canonical types.
3373  struct CanonicalTTPTInfo {
3374    unsigned Depth : 15;
3375    unsigned ParameterPack : 1;
3376    unsigned Index : 16;
3377  };
3378
3379  union {
3380    // Info for the canonical type.
3381    CanonicalTTPTInfo CanTTPTInfo;
3382    // Info for the non-canonical type.
3383    TemplateTypeParmDecl *TTPDecl;
3384  };
3385
3386  /// Build a non-canonical type.
3387  TemplateTypeParmType(TemplateTypeParmDecl *TTPDecl, QualType Canon)
3388    : Type(TemplateTypeParm, Canon, /*Dependent=*/true,
3389           /*InstantiationDependent=*/true,
3390           /*VariablyModified=*/false,
3391           Canon->containsUnexpandedParameterPack()),
3392      TTPDecl(TTPDecl) { }
3393
3394  /// Build the canonical type.
3395  TemplateTypeParmType(unsigned D, unsigned I, bool PP)
3396    : Type(TemplateTypeParm, QualType(this, 0),
3397           /*Dependent=*/true,
3398           /*InstantiationDependent=*/true,
3399           /*VariablyModified=*/false, PP) {
3400    CanTTPTInfo.Depth = D;
3401    CanTTPTInfo.Index = I;
3402    CanTTPTInfo.ParameterPack = PP;
3403  }
3404
3405  friend class ASTContext;  // ASTContext creates these
3406
3407  const CanonicalTTPTInfo& getCanTTPTInfo() const {
3408    QualType Can = getCanonicalTypeInternal();
3409    return Can->castAs<TemplateTypeParmType>()->CanTTPTInfo;
3410  }
3411
3412public:
3413  unsigned getDepth() const { return getCanTTPTInfo().Depth; }
3414  unsigned getIndex() const { return getCanTTPTInfo().Index; }
3415  bool isParameterPack() const { return getCanTTPTInfo().ParameterPack; }
3416
3417  TemplateTypeParmDecl *getDecl() const {
3418    return isCanonicalUnqualified() ? 0 : TTPDecl;
3419  }
3420
3421  IdentifierInfo *getIdentifier() const;
3422
3423  bool isSugared() const { return false; }
3424  QualType desugar() const { return QualType(this, 0); }
3425
3426  void Profile(llvm::FoldingSetNodeID &ID) {
3427    Profile(ID, getDepth(), getIndex(), isParameterPack(), getDecl());
3428  }
3429
3430  static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth,
3431                      unsigned Index, bool ParameterPack,
3432                      TemplateTypeParmDecl *TTPDecl) {
3433    ID.AddInteger(Depth);
3434    ID.AddInteger(Index);
3435    ID.AddBoolean(ParameterPack);
3436    ID.AddPointer(TTPDecl);
3437  }
3438
3439  static bool classof(const Type *T) {
3440    return T->getTypeClass() == TemplateTypeParm;
3441  }
3442};
3443
3444/// \brief Represents the result of substituting a type for a template
3445/// type parameter.
3446///
3447/// Within an instantiated template, all template type parameters have
3448/// been replaced with these.  They are used solely to record that a
3449/// type was originally written as a template type parameter;
3450/// therefore they are never canonical.
3451class SubstTemplateTypeParmType : public Type, public llvm::FoldingSetNode {
3452  // The original type parameter.
3453  const TemplateTypeParmType *Replaced;
3454
3455  SubstTemplateTypeParmType(const TemplateTypeParmType *Param, QualType Canon)
3456    : Type(SubstTemplateTypeParm, Canon, Canon->isDependentType(),
3457           Canon->isInstantiationDependentType(),
3458           Canon->isVariablyModifiedType(),
3459           Canon->containsUnexpandedParameterPack()),
3460      Replaced(Param) { }
3461
3462  friend class ASTContext;
3463
3464public:
3465  /// Gets the template parameter that was substituted for.
3466  const TemplateTypeParmType *getReplacedParameter() const {
3467    return Replaced;
3468  }
3469
3470  /// Gets the type that was substituted for the template
3471  /// parameter.
3472  QualType getReplacementType() const {
3473    return getCanonicalTypeInternal();
3474  }
3475
3476  bool isSugared() const { return true; }
3477  QualType desugar() const { return getReplacementType(); }
3478
3479  void Profile(llvm::FoldingSetNodeID &ID) {
3480    Profile(ID, getReplacedParameter(), getReplacementType());
3481  }
3482  static void Profile(llvm::FoldingSetNodeID &ID,
3483                      const TemplateTypeParmType *Replaced,
3484                      QualType Replacement) {
3485    ID.AddPointer(Replaced);
3486    ID.AddPointer(Replacement.getAsOpaquePtr());
3487  }
3488
3489  static bool classof(const Type *T) {
3490    return T->getTypeClass() == SubstTemplateTypeParm;
3491  }
3492};
3493
3494/// \brief Represents the result of substituting a set of types for a template
3495/// type parameter pack.
3496///
3497/// When a pack expansion in the source code contains multiple parameter packs
3498/// and those parameter packs correspond to different levels of template
3499/// parameter lists, this type node is used to represent a template type
3500/// parameter pack from an outer level, which has already had its argument pack
3501/// substituted but that still lives within a pack expansion that itself
3502/// could not be instantiated. When actually performing a substitution into
3503/// that pack expansion (e.g., when all template parameters have corresponding
3504/// arguments), this type will be replaced with the \c SubstTemplateTypeParmType
3505/// at the current pack substitution index.
3506class SubstTemplateTypeParmPackType : public Type, public llvm::FoldingSetNode {
3507  /// \brief The original type parameter.
3508  const TemplateTypeParmType *Replaced;
3509
3510  /// \brief A pointer to the set of template arguments that this
3511  /// parameter pack is instantiated with.
3512  const TemplateArgument *Arguments;
3513
3514  /// \brief The number of template arguments in \c Arguments.
3515  unsigned NumArguments;
3516
3517  SubstTemplateTypeParmPackType(const TemplateTypeParmType *Param,
3518                                QualType Canon,
3519                                const TemplateArgument &ArgPack);
3520
3521  friend class ASTContext;
3522
3523public:
3524  IdentifierInfo *getIdentifier() const { return Replaced->getIdentifier(); }
3525
3526  /// Gets the template parameter that was substituted for.
3527  const TemplateTypeParmType *getReplacedParameter() const {
3528    return Replaced;
3529  }
3530
3531  bool isSugared() const { return false; }
3532  QualType desugar() const { return QualType(this, 0); }
3533
3534  TemplateArgument getArgumentPack() const;
3535
3536  void Profile(llvm::FoldingSetNodeID &ID);
3537  static void Profile(llvm::FoldingSetNodeID &ID,
3538                      const TemplateTypeParmType *Replaced,
3539                      const TemplateArgument &ArgPack);
3540
3541  static bool classof(const Type *T) {
3542    return T->getTypeClass() == SubstTemplateTypeParmPack;
3543  }
3544};
3545
3546/// \brief Represents a C++0x auto type.
3547///
3548/// These types are usually a placeholder for a deduced type. However, within
3549/// templates and before the initializer is attached, there is no deduced type
3550/// and an auto type is type-dependent and canonical.
3551class AutoType : public Type, public llvm::FoldingSetNode {
3552  AutoType(QualType DeducedType)
3553    : Type(Auto, DeducedType.isNull() ? QualType(this, 0) : DeducedType,
3554           /*Dependent=*/DeducedType.isNull(),
3555           /*InstantiationDependent=*/DeducedType.isNull(),
3556           /*VariablyModified=*/false, /*ContainsParameterPack=*/false) {
3557    assert((DeducedType.isNull() || !DeducedType->isDependentType()) &&
3558           "deduced a dependent type for auto");
3559  }
3560
3561  friend class ASTContext;  // ASTContext creates these
3562
3563public:
3564  bool isSugared() const { return isDeduced(); }
3565  QualType desugar() const { return getCanonicalTypeInternal(); }
3566
3567  QualType getDeducedType() const {
3568    return isDeduced() ? getCanonicalTypeInternal() : QualType();
3569  }
3570  bool isDeduced() const {
3571    return !isDependentType();
3572  }
3573
3574  void Profile(llvm::FoldingSetNodeID &ID) {
3575    Profile(ID, getDeducedType());
3576  }
3577
3578  static void Profile(llvm::FoldingSetNodeID &ID,
3579                      QualType Deduced) {
3580    ID.AddPointer(Deduced.getAsOpaquePtr());
3581  }
3582
3583  static bool classof(const Type *T) {
3584    return T->getTypeClass() == Auto;
3585  }
3586};
3587
3588/// \brief Represents a type template specialization; the template
3589/// must be a class template, a type alias template, or a template
3590/// template parameter.  A template which cannot be resolved to one of
3591/// these, e.g. because it is written with a dependent scope
3592/// specifier, is instead represented as a
3593/// @c DependentTemplateSpecializationType.
3594///
3595/// A non-dependent template specialization type is always "sugar",
3596/// typically for a @c RecordType.  For example, a class template
3597/// specialization type of @c vector<int> will refer to a tag type for
3598/// the instantiation @c std::vector<int, std::allocator<int>>
3599///
3600/// Template specializations are dependent if either the template or
3601/// any of the template arguments are dependent, in which case the
3602/// type may also be canonical.
3603///
3604/// Instances of this type are allocated with a trailing array of
3605/// TemplateArguments, followed by a QualType representing the
3606/// non-canonical aliased type when the template is a type alias
3607/// template.
3608class TemplateSpecializationType
3609  : public Type, public llvm::FoldingSetNode {
3610  /// \brief The name of the template being specialized.  This is
3611  /// either a TemplateName::Template (in which case it is a
3612  /// ClassTemplateDecl*, a TemplateTemplateParmDecl*, or a
3613  /// TypeAliasTemplateDecl*), a
3614  /// TemplateName::SubstTemplateTemplateParmPack, or a
3615  /// TemplateName::SubstTemplateTemplateParm (in which case the
3616  /// replacement must, recursively, be one of these).
3617  TemplateName Template;
3618
3619  /// \brief - The number of template arguments named in this class
3620  /// template specialization.
3621  unsigned NumArgs : 31;
3622
3623  /// \brief Whether this template specialization type is a substituted
3624  /// type alias.
3625  bool TypeAlias : 1;
3626
3627  TemplateSpecializationType(TemplateName T,
3628                             const TemplateArgument *Args,
3629                             unsigned NumArgs, QualType Canon,
3630                             QualType Aliased);
3631
3632  friend class ASTContext;  // ASTContext creates these
3633
3634public:
3635  /// \brief Determine whether any of the given template arguments are
3636  /// dependent.
3637  static bool anyDependentTemplateArguments(const TemplateArgument *Args,
3638                                            unsigned NumArgs,
3639                                            bool &InstantiationDependent);
3640
3641  static bool anyDependentTemplateArguments(const TemplateArgumentLoc *Args,
3642                                            unsigned NumArgs,
3643                                            bool &InstantiationDependent);
3644
3645  static bool anyDependentTemplateArguments(const TemplateArgumentListInfo &,
3646                                            bool &InstantiationDependent);
3647
3648  /// \brief Print a template argument list, including the '<' and '>'
3649  /// enclosing the template arguments.
3650  // FIXME: remove the string ones.
3651  static std::string PrintTemplateArgumentList(const TemplateArgument *Args,
3652                                               unsigned NumArgs,
3653                                               const PrintingPolicy &Policy,
3654                                               bool SkipBrackets = false);
3655
3656  static std::string PrintTemplateArgumentList(const TemplateArgumentLoc *Args,
3657                                               unsigned NumArgs,
3658                                               const PrintingPolicy &Policy);
3659
3660  static std::string PrintTemplateArgumentList(const TemplateArgumentListInfo &,
3661                                               const PrintingPolicy &Policy);
3662
3663  /// \brief Print a template argument list, including the '<' and '>'
3664  /// enclosing the template arguments.
3665  static void PrintTemplateArgumentList(raw_ostream &OS,
3666                                        const TemplateArgument *Args,
3667                                        unsigned NumArgs,
3668                                        const PrintingPolicy &Policy,
3669                                        bool SkipBrackets = false);
3670
3671  static void PrintTemplateArgumentList(raw_ostream &OS,
3672                                        const TemplateArgumentLoc *Args,
3673                                        unsigned NumArgs,
3674                                        const PrintingPolicy &Policy);
3675
3676  static void PrintTemplateArgumentList(raw_ostream &OS,
3677                                        const TemplateArgumentListInfo &,
3678                                        const PrintingPolicy &Policy);
3679
3680  /// True if this template specialization type matches a current
3681  /// instantiation in the context in which it is found.
3682  bool isCurrentInstantiation() const {
3683    return isa<InjectedClassNameType>(getCanonicalTypeInternal());
3684  }
3685
3686  /// \brief Determine if this template specialization type is for a type alias
3687  /// template that has been substituted.
3688  ///
3689  /// Nearly every template specialization type whose template is an alias
3690  /// template will be substituted. However, this is not the case when
3691  /// the specialization contains a pack expansion but the template alias
3692  /// does not have a corresponding parameter pack, e.g.,
3693  ///
3694  /// \code
3695  /// template<typename T, typename U, typename V> struct S;
3696  /// template<typename T, typename U> using A = S<T, int, U>;
3697  /// template<typename... Ts> struct X {
3698  ///   typedef A<Ts...> type; // not a type alias
3699  /// };
3700  /// \endcode
3701  bool isTypeAlias() const { return TypeAlias; }
3702
3703  /// Get the aliased type, if this is a specialization of a type alias
3704  /// template.
3705  QualType getAliasedType() const {
3706    assert(isTypeAlias() && "not a type alias template specialization");
3707    return *reinterpret_cast<const QualType*>(end());
3708  }
3709
3710  typedef const TemplateArgument * iterator;
3711
3712  iterator begin() const { return getArgs(); }
3713  iterator end() const; // defined inline in TemplateBase.h
3714
3715  /// \brief Retrieve the name of the template that we are specializing.
3716  TemplateName getTemplateName() const { return Template; }
3717
3718  /// \brief Retrieve the template arguments.
3719  const TemplateArgument *getArgs() const {
3720    return reinterpret_cast<const TemplateArgument *>(this + 1);
3721  }
3722
3723  /// \brief Retrieve the number of template arguments.
3724  unsigned getNumArgs() const { return NumArgs; }
3725
3726  /// \brief Retrieve a specific template argument as a type.
3727  /// \pre @c isArgType(Arg)
3728  const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h
3729
3730  bool isSugared() const {
3731    return !isDependentType() || isCurrentInstantiation() || isTypeAlias();
3732  }
3733  QualType desugar() const { return getCanonicalTypeInternal(); }
3734
3735  void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx) {
3736    Profile(ID, Template, getArgs(), NumArgs, Ctx);
3737    if (isTypeAlias())
3738      getAliasedType().Profile(ID);
3739  }
3740
3741  static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T,
3742                      const TemplateArgument *Args,
3743                      unsigned NumArgs,
3744                      const ASTContext &Context);
3745
3746  static bool classof(const Type *T) {
3747    return T->getTypeClass() == TemplateSpecialization;
3748  }
3749};
3750
3751/// \brief The injected class name of a C++ class template or class
3752/// template partial specialization.  Used to record that a type was
3753/// spelled with a bare identifier rather than as a template-id; the
3754/// equivalent for non-templated classes is just RecordType.
3755///
3756/// Injected class name types are always dependent.  Template
3757/// instantiation turns these into RecordTypes.
3758///
3759/// Injected class name types are always canonical.  This works
3760/// because it is impossible to compare an injected class name type
3761/// with the corresponding non-injected template type, for the same
3762/// reason that it is impossible to directly compare template
3763/// parameters from different dependent contexts: injected class name
3764/// types can only occur within the scope of a particular templated
3765/// declaration, and within that scope every template specialization
3766/// will canonicalize to the injected class name (when appropriate
3767/// according to the rules of the language).
3768class InjectedClassNameType : public Type {
3769  CXXRecordDecl *Decl;
3770
3771  /// The template specialization which this type represents.
3772  /// For example, in
3773  ///   template <class T> class A { ... };
3774  /// this is A<T>, whereas in
3775  ///   template <class X, class Y> class A<B<X,Y> > { ... };
3776  /// this is A<B<X,Y> >.
3777  ///
3778  /// It is always unqualified, always a template specialization type,
3779  /// and always dependent.
3780  QualType InjectedType;
3781
3782  friend class ASTContext; // ASTContext creates these.
3783  friend class ASTReader; // FIXME: ASTContext::getInjectedClassNameType is not
3784                          // currently suitable for AST reading, too much
3785                          // interdependencies.
3786  InjectedClassNameType(CXXRecordDecl *D, QualType TST)
3787    : Type(InjectedClassName, QualType(), /*Dependent=*/true,
3788           /*InstantiationDependent=*/true,
3789           /*VariablyModified=*/false,
3790           /*ContainsUnexpandedParameterPack=*/false),
3791      Decl(D), InjectedType(TST) {
3792    assert(isa<TemplateSpecializationType>(TST));
3793    assert(!TST.hasQualifiers());
3794    assert(TST->isDependentType());
3795  }
3796
3797public:
3798  QualType getInjectedSpecializationType() const { return InjectedType; }
3799  const TemplateSpecializationType *getInjectedTST() const {
3800    return cast<TemplateSpecializationType>(InjectedType.getTypePtr());
3801  }
3802
3803  CXXRecordDecl *getDecl() const;
3804
3805  bool isSugared() const { return false; }
3806  QualType desugar() const { return QualType(this, 0); }
3807
3808  static bool classof(const Type *T) {
3809    return T->getTypeClass() == InjectedClassName;
3810  }
3811};
3812
3813/// \brief The kind of a tag type.
3814enum TagTypeKind {
3815  /// \brief The "struct" keyword.
3816  TTK_Struct,
3817  /// \brief The "__interface" keyword.
3818  TTK_Interface,
3819  /// \brief The "union" keyword.
3820  TTK_Union,
3821  /// \brief The "class" keyword.
3822  TTK_Class,
3823  /// \brief The "enum" keyword.
3824  TTK_Enum
3825};
3826
3827/// \brief The elaboration keyword that precedes a qualified type name or
3828/// introduces an elaborated-type-specifier.
3829enum ElaboratedTypeKeyword {
3830  /// \brief The "struct" keyword introduces the elaborated-type-specifier.
3831  ETK_Struct,
3832  /// \brief The "__interface" keyword introduces the elaborated-type-specifier.
3833  ETK_Interface,
3834  /// \brief The "union" keyword introduces the elaborated-type-specifier.
3835  ETK_Union,
3836  /// \brief The "class" keyword introduces the elaborated-type-specifier.
3837  ETK_Class,
3838  /// \brief The "enum" keyword introduces the elaborated-type-specifier.
3839  ETK_Enum,
3840  /// \brief The "typename" keyword precedes the qualified type name, e.g.,
3841  /// \c typename T::type.
3842  ETK_Typename,
3843  /// \brief No keyword precedes the qualified type name.
3844  ETK_None
3845};
3846
3847/// A helper class for Type nodes having an ElaboratedTypeKeyword.
3848/// The keyword in stored in the free bits of the base class.
3849/// Also provides a few static helpers for converting and printing
3850/// elaborated type keyword and tag type kind enumerations.
3851class TypeWithKeyword : public Type {
3852protected:
3853  TypeWithKeyword(ElaboratedTypeKeyword Keyword, TypeClass tc,
3854                  QualType Canonical, bool Dependent,
3855                  bool InstantiationDependent, bool VariablyModified,
3856                  bool ContainsUnexpandedParameterPack)
3857  : Type(tc, Canonical, Dependent, InstantiationDependent, VariablyModified,
3858         ContainsUnexpandedParameterPack) {
3859    TypeWithKeywordBits.Keyword = Keyword;
3860  }
3861
3862public:
3863  ElaboratedTypeKeyword getKeyword() const {
3864    return static_cast<ElaboratedTypeKeyword>(TypeWithKeywordBits.Keyword);
3865  }
3866
3867  /// getKeywordForTypeSpec - Converts a type specifier (DeclSpec::TST)
3868  /// into an elaborated type keyword.
3869  static ElaboratedTypeKeyword getKeywordForTypeSpec(unsigned TypeSpec);
3870
3871  /// getTagTypeKindForTypeSpec - Converts a type specifier (DeclSpec::TST)
3872  /// into a tag type kind.  It is an error to provide a type specifier
3873  /// which *isn't* a tag kind here.
3874  static TagTypeKind getTagTypeKindForTypeSpec(unsigned TypeSpec);
3875
3876  /// getKeywordForTagDeclKind - Converts a TagTypeKind into an
3877  /// elaborated type keyword.
3878  static ElaboratedTypeKeyword getKeywordForTagTypeKind(TagTypeKind Tag);
3879
3880  /// getTagTypeKindForKeyword - Converts an elaborated type keyword into
3881  // a TagTypeKind. It is an error to provide an elaborated type keyword
3882  /// which *isn't* a tag kind here.
3883  static TagTypeKind getTagTypeKindForKeyword(ElaboratedTypeKeyword Keyword);
3884
3885  static bool KeywordIsTagTypeKind(ElaboratedTypeKeyword Keyword);
3886
3887  static const char *getKeywordName(ElaboratedTypeKeyword Keyword);
3888
3889  static const char *getTagTypeKindName(TagTypeKind Kind) {
3890    return getKeywordName(getKeywordForTagTypeKind(Kind));
3891  }
3892
3893  class CannotCastToThisType {};
3894  static CannotCastToThisType classof(const Type *);
3895};
3896
3897/// \brief Represents a type that was referred to using an elaborated type
3898/// keyword, e.g., struct S, or via a qualified name, e.g., N::M::type,
3899/// or both.
3900///
3901/// This type is used to keep track of a type name as written in the
3902/// source code, including tag keywords and any nested-name-specifiers.
3903/// The type itself is always "sugar", used to express what was written
3904/// in the source code but containing no additional semantic information.
3905class ElaboratedType : public TypeWithKeyword, public llvm::FoldingSetNode {
3906
3907  /// \brief The nested name specifier containing the qualifier.
3908  NestedNameSpecifier *NNS;
3909
3910  /// \brief The type that this qualified name refers to.
3911  QualType NamedType;
3912
3913  ElaboratedType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
3914                 QualType NamedType, QualType CanonType)
3915    : TypeWithKeyword(Keyword, Elaborated, CanonType,
3916                      NamedType->isDependentType(),
3917                      NamedType->isInstantiationDependentType(),
3918                      NamedType->isVariablyModifiedType(),
3919                      NamedType->containsUnexpandedParameterPack()),
3920      NNS(NNS), NamedType(NamedType) {
3921    assert(!(Keyword == ETK_None && NNS == 0) &&
3922           "ElaboratedType cannot have elaborated type keyword "
3923           "and name qualifier both null.");
3924  }
3925
3926  friend class ASTContext;  // ASTContext creates these
3927
3928public:
3929  ~ElaboratedType();
3930
3931  /// \brief Retrieve the qualification on this type.
3932  NestedNameSpecifier *getQualifier() const { return NNS; }
3933
3934  /// \brief Retrieve the type named by the qualified-id.
3935  QualType getNamedType() const { return NamedType; }
3936
3937  /// \brief Remove a single level of sugar.
3938  QualType desugar() const { return getNamedType(); }
3939
3940  /// \brief Returns whether this type directly provides sugar.
3941  bool isSugared() const { return true; }
3942
3943  void Profile(llvm::FoldingSetNodeID &ID) {
3944    Profile(ID, getKeyword(), NNS, NamedType);
3945  }
3946
3947  static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword,
3948                      NestedNameSpecifier *NNS, QualType NamedType) {
3949    ID.AddInteger(Keyword);
3950    ID.AddPointer(NNS);
3951    NamedType.Profile(ID);
3952  }
3953
3954  static bool classof(const Type *T) {
3955    return T->getTypeClass() == Elaborated;
3956  }
3957};
3958
3959/// \brief Represents a qualified type name for which the type name is
3960/// dependent.
3961///
3962/// DependentNameType represents a class of dependent types that involve a
3963/// dependent nested-name-specifier (e.g., "T::") followed by a (dependent)
3964/// name of a type. The DependentNameType may start with a "typename" (for a
3965/// typename-specifier), "class", "struct", "union", or "enum" (for a
3966/// dependent elaborated-type-specifier), or nothing (in contexts where we
3967/// know that we must be referring to a type, e.g., in a base class specifier).
3968class DependentNameType : public TypeWithKeyword, public llvm::FoldingSetNode {
3969
3970  /// \brief The nested name specifier containing the qualifier.
3971  NestedNameSpecifier *NNS;
3972
3973  /// \brief The type that this typename specifier refers to.
3974  const IdentifierInfo *Name;
3975
3976  DependentNameType(ElaboratedTypeKeyword Keyword, NestedNameSpecifier *NNS,
3977                    const IdentifierInfo *Name, QualType CanonType)
3978    : TypeWithKeyword(Keyword, DependentName, CanonType, /*Dependent=*/true,
3979                      /*InstantiationDependent=*/true,
3980                      /*VariablyModified=*/false,
3981                      NNS->containsUnexpandedParameterPack()),
3982      NNS(NNS), Name(Name) {
3983    assert(NNS->isDependent() &&
3984           "DependentNameType requires a dependent nested-name-specifier");
3985  }
3986
3987  friend class ASTContext;  // ASTContext creates these
3988
3989public:
3990  /// \brief Retrieve the qualification on this type.
3991  NestedNameSpecifier *getQualifier() const { return NNS; }
3992
3993  /// \brief Retrieve the type named by the typename specifier as an
3994  /// identifier.
3995  ///
3996  /// This routine will return a non-NULL identifier pointer when the
3997  /// form of the original typename was terminated by an identifier,
3998  /// e.g., "typename T::type".
3999  const IdentifierInfo *getIdentifier() const {
4000    return Name;
4001  }
4002
4003  bool isSugared() const { return false; }
4004  QualType desugar() const { return QualType(this, 0); }
4005
4006  void Profile(llvm::FoldingSetNodeID &ID) {
4007    Profile(ID, getKeyword(), NNS, Name);
4008  }
4009
4010  static void Profile(llvm::FoldingSetNodeID &ID, ElaboratedTypeKeyword Keyword,
4011                      NestedNameSpecifier *NNS, const IdentifierInfo *Name) {
4012    ID.AddInteger(Keyword);
4013    ID.AddPointer(NNS);
4014    ID.AddPointer(Name);
4015  }
4016
4017  static bool classof(const Type *T) {
4018    return T->getTypeClass() == DependentName;
4019  }
4020};
4021
4022/// DependentTemplateSpecializationType - Represents a template
4023/// specialization type whose template cannot be resolved, e.g.
4024///   A<T>::template B<T>
4025class DependentTemplateSpecializationType :
4026  public TypeWithKeyword, public llvm::FoldingSetNode {
4027
4028  /// \brief The nested name specifier containing the qualifier.
4029  NestedNameSpecifier *NNS;
4030
4031  /// \brief The identifier of the template.
4032  const IdentifierInfo *Name;
4033
4034  /// \brief - The number of template arguments named in this class
4035  /// template specialization.
4036  unsigned NumArgs;
4037
4038  const TemplateArgument *getArgBuffer() const {
4039    return reinterpret_cast<const TemplateArgument*>(this+1);
4040  }
4041  TemplateArgument *getArgBuffer() {
4042    return reinterpret_cast<TemplateArgument*>(this+1);
4043  }
4044
4045  DependentTemplateSpecializationType(ElaboratedTypeKeyword Keyword,
4046                                      NestedNameSpecifier *NNS,
4047                                      const IdentifierInfo *Name,
4048                                      unsigned NumArgs,
4049                                      const TemplateArgument *Args,
4050                                      QualType Canon);
4051
4052  friend class ASTContext;  // ASTContext creates these
4053
4054public:
4055  NestedNameSpecifier *getQualifier() const { return NNS; }
4056  const IdentifierInfo *getIdentifier() const { return Name; }
4057
4058  /// \brief Retrieve the template arguments.
4059  const TemplateArgument *getArgs() const {
4060    return getArgBuffer();
4061  }
4062
4063  /// \brief Retrieve the number of template arguments.
4064  unsigned getNumArgs() const { return NumArgs; }
4065
4066  const TemplateArgument &getArg(unsigned Idx) const; // in TemplateBase.h
4067
4068  typedef const TemplateArgument * iterator;
4069  iterator begin() const { return getArgs(); }
4070  iterator end() const; // inline in TemplateBase.h
4071
4072  bool isSugared() const { return false; }
4073  QualType desugar() const { return QualType(this, 0); }
4074
4075  void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context) {
4076    Profile(ID, Context, getKeyword(), NNS, Name, NumArgs, getArgs());
4077  }
4078
4079  static void Profile(llvm::FoldingSetNodeID &ID,
4080                      const ASTContext &Context,
4081                      ElaboratedTypeKeyword Keyword,
4082                      NestedNameSpecifier *Qualifier,
4083                      const IdentifierInfo *Name,
4084                      unsigned NumArgs,
4085                      const TemplateArgument *Args);
4086
4087  static bool classof(const Type *T) {
4088    return T->getTypeClass() == DependentTemplateSpecialization;
4089  }
4090};
4091
4092/// \brief Represents a pack expansion of types.
4093///
4094/// Pack expansions are part of C++0x variadic templates. A pack
4095/// expansion contains a pattern, which itself contains one or more
4096/// "unexpanded" parameter packs. When instantiated, a pack expansion
4097/// produces a series of types, each instantiated from the pattern of
4098/// the expansion, where the Ith instantiation of the pattern uses the
4099/// Ith arguments bound to each of the unexpanded parameter packs. The
4100/// pack expansion is considered to "expand" these unexpanded
4101/// parameter packs.
4102///
4103/// \code
4104/// template<typename ...Types> struct tuple;
4105///
4106/// template<typename ...Types>
4107/// struct tuple_of_references {
4108///   typedef tuple<Types&...> type;
4109/// };
4110/// \endcode
4111///
4112/// Here, the pack expansion \c Types&... is represented via a
4113/// PackExpansionType whose pattern is Types&.
4114class PackExpansionType : public Type, public llvm::FoldingSetNode {
4115  /// \brief The pattern of the pack expansion.
4116  QualType Pattern;
4117
4118  /// \brief The number of expansions that this pack expansion will
4119  /// generate when substituted (+1), or indicates that
4120  ///
4121  /// This field will only have a non-zero value when some of the parameter
4122  /// packs that occur within the pattern have been substituted but others have
4123  /// not.
4124  unsigned NumExpansions;
4125
4126  PackExpansionType(QualType Pattern, QualType Canon,
4127                    llvm::Optional<unsigned> NumExpansions)
4128    : Type(PackExpansion, Canon, /*Dependent=*/Pattern->isDependentType(),
4129           /*InstantiationDependent=*/true,
4130           /*VariableModified=*/Pattern->isVariablyModifiedType(),
4131           /*ContainsUnexpandedParameterPack=*/false),
4132      Pattern(Pattern),
4133      NumExpansions(NumExpansions? *NumExpansions + 1: 0) { }
4134
4135  friend class ASTContext;  // ASTContext creates these
4136
4137public:
4138  /// \brief Retrieve the pattern of this pack expansion, which is the
4139  /// type that will be repeatedly instantiated when instantiating the
4140  /// pack expansion itself.
4141  QualType getPattern() const { return Pattern; }
4142
4143  /// \brief Retrieve the number of expansions that this pack expansion will
4144  /// generate, if known.
4145  llvm::Optional<unsigned> getNumExpansions() const {
4146    if (NumExpansions)
4147      return NumExpansions - 1;
4148
4149    return llvm::Optional<unsigned>();
4150  }
4151
4152  bool isSugared() const { return false; }
4153  QualType desugar() const { return QualType(this, 0); }
4154
4155  void Profile(llvm::FoldingSetNodeID &ID) {
4156    Profile(ID, getPattern(), getNumExpansions());
4157  }
4158
4159  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pattern,
4160                      llvm::Optional<unsigned> NumExpansions) {
4161    ID.AddPointer(Pattern.getAsOpaquePtr());
4162    ID.AddBoolean(NumExpansions);
4163    if (NumExpansions)
4164      ID.AddInteger(*NumExpansions);
4165  }
4166
4167  static bool classof(const Type *T) {
4168    return T->getTypeClass() == PackExpansion;
4169  }
4170};
4171
4172/// ObjCObjectType - Represents a class type in Objective C.
4173/// Every Objective C type is a combination of a base type and a
4174/// list of protocols.
4175///
4176/// Given the following declarations:
4177/// \code
4178///   \@class C;
4179///   \@protocol P;
4180/// \endcode
4181///
4182/// 'C' is an ObjCInterfaceType C.  It is sugar for an ObjCObjectType
4183/// with base C and no protocols.
4184///
4185/// 'C<P>' is an ObjCObjectType with base C and protocol list [P].
4186///
4187/// 'id' is a TypedefType which is sugar for an ObjCPointerType whose
4188/// pointee is an ObjCObjectType with base BuiltinType::ObjCIdType
4189/// and no protocols.
4190///
4191/// 'id<P>' is an ObjCPointerType whose pointee is an ObjCObjecType
4192/// with base BuiltinType::ObjCIdType and protocol list [P].  Eventually
4193/// this should get its own sugar class to better represent the source.
4194class ObjCObjectType : public Type {
4195  // ObjCObjectType.NumProtocols - the number of protocols stored
4196  // after the ObjCObjectPointerType node.
4197  //
4198  // These protocols are those written directly on the type.  If
4199  // protocol qualifiers ever become additive, the iterators will need
4200  // to get kindof complicated.
4201  //
4202  // In the canonical object type, these are sorted alphabetically
4203  // and uniqued.
4204
4205  /// Either a BuiltinType or an InterfaceType or sugar for either.
4206  QualType BaseType;
4207
4208  ObjCProtocolDecl * const *getProtocolStorage() const {
4209    return const_cast<ObjCObjectType*>(this)->getProtocolStorage();
4210  }
4211
4212  ObjCProtocolDecl **getProtocolStorage();
4213
4214protected:
4215  ObjCObjectType(QualType Canonical, QualType Base,
4216                 ObjCProtocolDecl * const *Protocols, unsigned NumProtocols);
4217
4218  enum Nonce_ObjCInterface { Nonce_ObjCInterface };
4219  ObjCObjectType(enum Nonce_ObjCInterface)
4220        : Type(ObjCInterface, QualType(), false, false, false, false),
4221      BaseType(QualType(this_(), 0)) {
4222    ObjCObjectTypeBits.NumProtocols = 0;
4223  }
4224
4225public:
4226  /// getBaseType - Gets the base type of this object type.  This is
4227  /// always (possibly sugar for) one of:
4228  ///  - the 'id' builtin type (as opposed to the 'id' type visible to the
4229  ///    user, which is a typedef for an ObjCPointerType)
4230  ///  - the 'Class' builtin type (same caveat)
4231  ///  - an ObjCObjectType (currently always an ObjCInterfaceType)
4232  QualType getBaseType() const { return BaseType; }
4233
4234  bool isObjCId() const {
4235    return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCId);
4236  }
4237  bool isObjCClass() const {
4238    return getBaseType()->isSpecificBuiltinType(BuiltinType::ObjCClass);
4239  }
4240  bool isObjCUnqualifiedId() const { return qual_empty() && isObjCId(); }
4241  bool isObjCUnqualifiedClass() const { return qual_empty() && isObjCClass(); }
4242  bool isObjCUnqualifiedIdOrClass() const {
4243    if (!qual_empty()) return false;
4244    if (const BuiltinType *T = getBaseType()->getAs<BuiltinType>())
4245      return T->getKind() == BuiltinType::ObjCId ||
4246             T->getKind() == BuiltinType::ObjCClass;
4247    return false;
4248  }
4249  bool isObjCQualifiedId() const { return !qual_empty() && isObjCId(); }
4250  bool isObjCQualifiedClass() const { return !qual_empty() && isObjCClass(); }
4251
4252  /// Gets the interface declaration for this object type, if the base type
4253  /// really is an interface.
4254  ObjCInterfaceDecl *getInterface() const;
4255
4256  typedef ObjCProtocolDecl * const *qual_iterator;
4257
4258  qual_iterator qual_begin() const { return getProtocolStorage(); }
4259  qual_iterator qual_end() const { return qual_begin() + getNumProtocols(); }
4260
4261  bool qual_empty() const { return getNumProtocols() == 0; }
4262
4263  /// getNumProtocols - Return the number of qualifying protocols in this
4264  /// interface type, or 0 if there are none.
4265  unsigned getNumProtocols() const { return ObjCObjectTypeBits.NumProtocols; }
4266
4267  /// \brief Fetch a protocol by index.
4268  ObjCProtocolDecl *getProtocol(unsigned I) const {
4269    assert(I < getNumProtocols() && "Out-of-range protocol access");
4270    return qual_begin()[I];
4271  }
4272
4273  bool isSugared() const { return false; }
4274  QualType desugar() const { return QualType(this, 0); }
4275
4276  static bool classof(const Type *T) {
4277    return T->getTypeClass() == ObjCObject ||
4278           T->getTypeClass() == ObjCInterface;
4279  }
4280};
4281
4282/// ObjCObjectTypeImpl - A class providing a concrete implementation
4283/// of ObjCObjectType, so as to not increase the footprint of
4284/// ObjCInterfaceType.  Code outside of ASTContext and the core type
4285/// system should not reference this type.
4286class ObjCObjectTypeImpl : public ObjCObjectType, public llvm::FoldingSetNode {
4287  friend class ASTContext;
4288
4289  // If anyone adds fields here, ObjCObjectType::getProtocolStorage()
4290  // will need to be modified.
4291
4292  ObjCObjectTypeImpl(QualType Canonical, QualType Base,
4293                     ObjCProtocolDecl * const *Protocols,
4294                     unsigned NumProtocols)
4295    : ObjCObjectType(Canonical, Base, Protocols, NumProtocols) {}
4296
4297public:
4298  void Profile(llvm::FoldingSetNodeID &ID);
4299  static void Profile(llvm::FoldingSetNodeID &ID,
4300                      QualType Base,
4301                      ObjCProtocolDecl *const *protocols,
4302                      unsigned NumProtocols);
4303};
4304
4305inline ObjCProtocolDecl **ObjCObjectType::getProtocolStorage() {
4306  return reinterpret_cast<ObjCProtocolDecl**>(
4307            static_cast<ObjCObjectTypeImpl*>(this) + 1);
4308}
4309
4310/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for
4311/// object oriented design.  They basically correspond to C++ classes.  There
4312/// are two kinds of interface types, normal interfaces like "NSString" and
4313/// qualified interfaces, which are qualified with a protocol list like
4314/// "NSString<NSCopyable, NSAmazing>".
4315///
4316/// ObjCInterfaceType guarantees the following properties when considered
4317/// as a subtype of its superclass, ObjCObjectType:
4318///   - There are no protocol qualifiers.  To reinforce this, code which
4319///     tries to invoke the protocol methods via an ObjCInterfaceType will
4320///     fail to compile.
4321///   - It is its own base type.  That is, if T is an ObjCInterfaceType*,
4322///     T->getBaseType() == QualType(T, 0).
4323class ObjCInterfaceType : public ObjCObjectType {
4324  mutable ObjCInterfaceDecl *Decl;
4325
4326  ObjCInterfaceType(const ObjCInterfaceDecl *D)
4327    : ObjCObjectType(Nonce_ObjCInterface),
4328      Decl(const_cast<ObjCInterfaceDecl*>(D)) {}
4329  friend class ASTContext;  // ASTContext creates these.
4330  friend class ASTReader;
4331  friend class ObjCInterfaceDecl;
4332
4333public:
4334  /// getDecl - Get the declaration of this interface.
4335  ObjCInterfaceDecl *getDecl() const { return Decl; }
4336
4337  bool isSugared() const { return false; }
4338  QualType desugar() const { return QualType(this, 0); }
4339
4340  static bool classof(const Type *T) {
4341    return T->getTypeClass() == ObjCInterface;
4342  }
4343
4344  // Nonsense to "hide" certain members of ObjCObjectType within this
4345  // class.  People asking for protocols on an ObjCInterfaceType are
4346  // not going to get what they want: ObjCInterfaceTypes are
4347  // guaranteed to have no protocols.
4348  enum {
4349    qual_iterator,
4350    qual_begin,
4351    qual_end,
4352    getNumProtocols,
4353    getProtocol
4354  };
4355};
4356
4357inline ObjCInterfaceDecl *ObjCObjectType::getInterface() const {
4358  if (const ObjCInterfaceType *T =
4359        getBaseType()->getAs<ObjCInterfaceType>())
4360    return T->getDecl();
4361  return 0;
4362}
4363
4364/// ObjCObjectPointerType - Used to represent a pointer to an
4365/// Objective C object.  These are constructed from pointer
4366/// declarators when the pointee type is an ObjCObjectType (or sugar
4367/// for one).  In addition, the 'id' and 'Class' types are typedefs
4368/// for these, and the protocol-qualified types 'id<P>' and 'Class<P>'
4369/// are translated into these.
4370///
4371/// Pointers to pointers to Objective C objects are still PointerTypes;
4372/// only the first level of pointer gets it own type implementation.
4373class ObjCObjectPointerType : public Type, public llvm::FoldingSetNode {
4374  QualType PointeeType;
4375
4376  ObjCObjectPointerType(QualType Canonical, QualType Pointee)
4377    : Type(ObjCObjectPointer, Canonical, false, false, false, false),
4378      PointeeType(Pointee) {}
4379  friend class ASTContext;  // ASTContext creates these.
4380
4381public:
4382  /// getPointeeType - Gets the type pointed to by this ObjC pointer.
4383  /// The result will always be an ObjCObjectType or sugar thereof.
4384  QualType getPointeeType() const { return PointeeType; }
4385
4386  /// getObjCObjectType - Gets the type pointed to by this ObjC
4387  /// pointer.  This method always returns non-null.
4388  ///
4389  /// This method is equivalent to getPointeeType() except that
4390  /// it discards any typedefs (or other sugar) between this
4391  /// type and the "outermost" object type.  So for:
4392  /// \code
4393  ///   \@class A; \@protocol P; \@protocol Q;
4394  ///   typedef A<P> AP;
4395  ///   typedef A A1;
4396  ///   typedef A1<P> A1P;
4397  ///   typedef A1P<Q> A1PQ;
4398  /// \endcode
4399  /// For 'A*', getObjectType() will return 'A'.
4400  /// For 'A<P>*', getObjectType() will return 'A<P>'.
4401  /// For 'AP*', getObjectType() will return 'A<P>'.
4402  /// For 'A1*', getObjectType() will return 'A'.
4403  /// For 'A1<P>*', getObjectType() will return 'A1<P>'.
4404  /// For 'A1P*', getObjectType() will return 'A1<P>'.
4405  /// For 'A1PQ*', getObjectType() will return 'A1<Q>', because
4406  ///   adding protocols to a protocol-qualified base discards the
4407  ///   old qualifiers (for now).  But if it didn't, getObjectType()
4408  ///   would return 'A1P<Q>' (and we'd have to make iterating over
4409  ///   qualifiers more complicated).
4410  const ObjCObjectType *getObjectType() const {
4411    return PointeeType->castAs<ObjCObjectType>();
4412  }
4413
4414  /// getInterfaceType - If this pointer points to an Objective C
4415  /// \@interface type, gets the type for that interface.  Any protocol
4416  /// qualifiers on the interface are ignored.
4417  ///
4418  /// \return null if the base type for this pointer is 'id' or 'Class'
4419  const ObjCInterfaceType *getInterfaceType() const {
4420    return getObjectType()->getBaseType()->getAs<ObjCInterfaceType>();
4421  }
4422
4423  /// getInterfaceDecl - If this pointer points to an Objective \@interface
4424  /// type, gets the declaration for that interface.
4425  ///
4426  /// \return null if the base type for this pointer is 'id' or 'Class'
4427  ObjCInterfaceDecl *getInterfaceDecl() const {
4428    return getObjectType()->getInterface();
4429  }
4430
4431  /// isObjCIdType - True if this is equivalent to the 'id' type, i.e. if
4432  /// its object type is the primitive 'id' type with no protocols.
4433  bool isObjCIdType() const {
4434    return getObjectType()->isObjCUnqualifiedId();
4435  }
4436
4437  /// isObjCClassType - True if this is equivalent to the 'Class' type,
4438  /// i.e. if its object tive is the primitive 'Class' type with no protocols.
4439  bool isObjCClassType() const {
4440    return getObjectType()->isObjCUnqualifiedClass();
4441  }
4442
4443  /// isObjCQualifiedIdType - True if this is equivalent to 'id<P>' for some
4444  /// non-empty set of protocols.
4445  bool isObjCQualifiedIdType() const {
4446    return getObjectType()->isObjCQualifiedId();
4447  }
4448
4449  /// isObjCQualifiedClassType - True if this is equivalent to 'Class<P>' for
4450  /// some non-empty set of protocols.
4451  bool isObjCQualifiedClassType() const {
4452    return getObjectType()->isObjCQualifiedClass();
4453  }
4454
4455  /// An iterator over the qualifiers on the object type.  Provided
4456  /// for convenience.  This will always iterate over the full set of
4457  /// protocols on a type, not just those provided directly.
4458  typedef ObjCObjectType::qual_iterator qual_iterator;
4459
4460  qual_iterator qual_begin() const {
4461    return getObjectType()->qual_begin();
4462  }
4463  qual_iterator qual_end() const {
4464    return getObjectType()->qual_end();
4465  }
4466  bool qual_empty() const { return getObjectType()->qual_empty(); }
4467
4468  /// getNumProtocols - Return the number of qualifying protocols on
4469  /// the object type.
4470  unsigned getNumProtocols() const {
4471    return getObjectType()->getNumProtocols();
4472  }
4473
4474  /// \brief Retrieve a qualifying protocol by index on the object
4475  /// type.
4476  ObjCProtocolDecl *getProtocol(unsigned I) const {
4477    return getObjectType()->getProtocol(I);
4478  }
4479
4480  bool isSugared() const { return false; }
4481  QualType desugar() const { return QualType(this, 0); }
4482
4483  void Profile(llvm::FoldingSetNodeID &ID) {
4484    Profile(ID, getPointeeType());
4485  }
4486  static void Profile(llvm::FoldingSetNodeID &ID, QualType T) {
4487    ID.AddPointer(T.getAsOpaquePtr());
4488  }
4489  static bool classof(const Type *T) {
4490    return T->getTypeClass() == ObjCObjectPointer;
4491  }
4492};
4493
4494class AtomicType : public Type, public llvm::FoldingSetNode {
4495  QualType ValueType;
4496
4497  AtomicType(QualType ValTy, QualType Canonical)
4498    : Type(Atomic, Canonical, ValTy->isDependentType(),
4499           ValTy->isInstantiationDependentType(),
4500           ValTy->isVariablyModifiedType(),
4501           ValTy->containsUnexpandedParameterPack()),
4502      ValueType(ValTy) {}
4503  friend class ASTContext;  // ASTContext creates these.
4504
4505  public:
4506  /// getValueType - Gets the type contained by this atomic type, i.e.
4507  /// the type returned by performing an atomic load of this atomic type.
4508  QualType getValueType() const { return ValueType; }
4509
4510  bool isSugared() const { return false; }
4511  QualType desugar() const { return QualType(this, 0); }
4512
4513  void Profile(llvm::FoldingSetNodeID &ID) {
4514    Profile(ID, getValueType());
4515  }
4516  static void Profile(llvm::FoldingSetNodeID &ID, QualType T) {
4517    ID.AddPointer(T.getAsOpaquePtr());
4518  }
4519  static bool classof(const Type *T) {
4520    return T->getTypeClass() == Atomic;
4521  }
4522};
4523
4524/// A qualifier set is used to build a set of qualifiers.
4525class QualifierCollector : public Qualifiers {
4526public:
4527  QualifierCollector(Qualifiers Qs = Qualifiers()) : Qualifiers(Qs) {}
4528
4529  /// Collect any qualifiers on the given type and return an
4530  /// unqualified type.  The qualifiers are assumed to be consistent
4531  /// with those already in the type.
4532  const Type *strip(QualType type) {
4533    addFastQualifiers(type.getLocalFastQualifiers());
4534    if (!type.hasLocalNonFastQualifiers())
4535      return type.getTypePtrUnsafe();
4536
4537    const ExtQuals *extQuals = type.getExtQualsUnsafe();
4538    addConsistentQualifiers(extQuals->getQualifiers());
4539    return extQuals->getBaseType();
4540  }
4541
4542  /// Apply the collected qualifiers to the given type.
4543  QualType apply(const ASTContext &Context, QualType QT) const;
4544
4545  /// Apply the collected qualifiers to the given type.
4546  QualType apply(const ASTContext &Context, const Type* T) const;
4547};
4548
4549
4550// Inline function definitions.
4551
4552inline SplitQualType SplitQualType::getSingleStepDesugaredType() const {
4553  SplitQualType desugar =
4554    Ty->getLocallyUnqualifiedSingleStepDesugaredType().split();
4555  desugar.Quals.addConsistentQualifiers(Quals);
4556  return desugar;
4557}
4558
4559inline const Type *QualType::getTypePtr() const {
4560  return getCommonPtr()->BaseType;
4561}
4562
4563inline const Type *QualType::getTypePtrOrNull() const {
4564  return (isNull() ? 0 : getCommonPtr()->BaseType);
4565}
4566
4567inline SplitQualType QualType::split() const {
4568  if (!hasLocalNonFastQualifiers())
4569    return SplitQualType(getTypePtrUnsafe(),
4570                         Qualifiers::fromFastMask(getLocalFastQualifiers()));
4571
4572  const ExtQuals *eq = getExtQualsUnsafe();
4573  Qualifiers qs = eq->getQualifiers();
4574  qs.addFastQualifiers(getLocalFastQualifiers());
4575  return SplitQualType(eq->getBaseType(), qs);
4576}
4577
4578inline Qualifiers QualType::getLocalQualifiers() const {
4579  Qualifiers Quals;
4580  if (hasLocalNonFastQualifiers())
4581    Quals = getExtQualsUnsafe()->getQualifiers();
4582  Quals.addFastQualifiers(getLocalFastQualifiers());
4583  return Quals;
4584}
4585
4586inline Qualifiers QualType::getQualifiers() const {
4587  Qualifiers quals = getCommonPtr()->CanonicalType.getLocalQualifiers();
4588  quals.addFastQualifiers(getLocalFastQualifiers());
4589  return quals;
4590}
4591
4592inline unsigned QualType::getCVRQualifiers() const {
4593  unsigned cvr = getCommonPtr()->CanonicalType.getLocalCVRQualifiers();
4594  cvr |= getLocalCVRQualifiers();
4595  return cvr;
4596}
4597
4598inline QualType QualType::getCanonicalType() const {
4599  QualType canon = getCommonPtr()->CanonicalType;
4600  return canon.withFastQualifiers(getLocalFastQualifiers());
4601}
4602
4603inline bool QualType::isCanonical() const {
4604  return getTypePtr()->isCanonicalUnqualified();
4605}
4606
4607inline bool QualType::isCanonicalAsParam() const {
4608  if (!isCanonical()) return false;
4609  if (hasLocalQualifiers()) return false;
4610
4611  const Type *T = getTypePtr();
4612  if (T->isVariablyModifiedType() && T->hasSizedVLAType())
4613    return false;
4614
4615  return !isa<FunctionType>(T) && !isa<ArrayType>(T);
4616}
4617
4618inline bool QualType::isConstQualified() const {
4619  return isLocalConstQualified() ||
4620         getCommonPtr()->CanonicalType.isLocalConstQualified();
4621}
4622
4623inline bool QualType::isRestrictQualified() const {
4624  return isLocalRestrictQualified() ||
4625         getCommonPtr()->CanonicalType.isLocalRestrictQualified();
4626}
4627
4628
4629inline bool QualType::isVolatileQualified() const {
4630  return isLocalVolatileQualified() ||
4631         getCommonPtr()->CanonicalType.isLocalVolatileQualified();
4632}
4633
4634inline bool QualType::hasQualifiers() const {
4635  return hasLocalQualifiers() ||
4636         getCommonPtr()->CanonicalType.hasLocalQualifiers();
4637}
4638
4639inline QualType QualType::getUnqualifiedType() const {
4640  if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers())
4641    return QualType(getTypePtr(), 0);
4642
4643  return QualType(getSplitUnqualifiedTypeImpl(*this).Ty, 0);
4644}
4645
4646inline SplitQualType QualType::getSplitUnqualifiedType() const {
4647  if (!getTypePtr()->getCanonicalTypeInternal().hasLocalQualifiers())
4648    return split();
4649
4650  return getSplitUnqualifiedTypeImpl(*this);
4651}
4652
4653inline void QualType::removeLocalConst() {
4654  removeLocalFastQualifiers(Qualifiers::Const);
4655}
4656
4657inline void QualType::removeLocalRestrict() {
4658  removeLocalFastQualifiers(Qualifiers::Restrict);
4659}
4660
4661inline void QualType::removeLocalVolatile() {
4662  removeLocalFastQualifiers(Qualifiers::Volatile);
4663}
4664
4665inline void QualType::removeLocalCVRQualifiers(unsigned Mask) {
4666  assert(!(Mask & ~Qualifiers::CVRMask) && "mask has non-CVR bits");
4667  assert((int)Qualifiers::CVRMask == (int)Qualifiers::FastMask);
4668
4669  // Fast path: we don't need to touch the slow qualifiers.
4670  removeLocalFastQualifiers(Mask);
4671}
4672
4673/// getAddressSpace - Return the address space of this type.
4674inline unsigned QualType::getAddressSpace() const {
4675  return getQualifiers().getAddressSpace();
4676}
4677
4678/// getObjCGCAttr - Return the gc attribute of this type.
4679inline Qualifiers::GC QualType::getObjCGCAttr() const {
4680  return getQualifiers().getObjCGCAttr();
4681}
4682
4683inline FunctionType::ExtInfo getFunctionExtInfo(const Type &t) {
4684  if (const PointerType *PT = t.getAs<PointerType>()) {
4685    if (const FunctionType *FT = PT->getPointeeType()->getAs<FunctionType>())
4686      return FT->getExtInfo();
4687  } else if (const FunctionType *FT = t.getAs<FunctionType>())
4688    return FT->getExtInfo();
4689
4690  return FunctionType::ExtInfo();
4691}
4692
4693inline FunctionType::ExtInfo getFunctionExtInfo(QualType t) {
4694  return getFunctionExtInfo(*t);
4695}
4696
4697/// isMoreQualifiedThan - Determine whether this type is more
4698/// qualified than the Other type. For example, "const volatile int"
4699/// is more qualified than "const int", "volatile int", and
4700/// "int". However, it is not more qualified than "const volatile
4701/// int".
4702inline bool QualType::isMoreQualifiedThan(QualType other) const {
4703  Qualifiers myQuals = getQualifiers();
4704  Qualifiers otherQuals = other.getQualifiers();
4705  return (myQuals != otherQuals && myQuals.compatiblyIncludes(otherQuals));
4706}
4707
4708/// isAtLeastAsQualifiedAs - Determine whether this type is at last
4709/// as qualified as the Other type. For example, "const volatile
4710/// int" is at least as qualified as "const int", "volatile int",
4711/// "int", and "const volatile int".
4712inline bool QualType::isAtLeastAsQualifiedAs(QualType other) const {
4713  return getQualifiers().compatiblyIncludes(other.getQualifiers());
4714}
4715
4716/// getNonReferenceType - If Type is a reference type (e.g., const
4717/// int&), returns the type that the reference refers to ("const
4718/// int"). Otherwise, returns the type itself. This routine is used
4719/// throughout Sema to implement C++ 5p6:
4720///
4721///   If an expression initially has the type "reference to T" (8.3.2,
4722///   8.5.3), the type is adjusted to "T" prior to any further
4723///   analysis, the expression designates the object or function
4724///   denoted by the reference, and the expression is an lvalue.
4725inline QualType QualType::getNonReferenceType() const {
4726  if (const ReferenceType *RefType = (*this)->getAs<ReferenceType>())
4727    return RefType->getPointeeType();
4728  else
4729    return *this;
4730}
4731
4732inline bool QualType::isCForbiddenLValueType() const {
4733  return ((getTypePtr()->isVoidType() && !hasQualifiers()) ||
4734          getTypePtr()->isFunctionType());
4735}
4736
4737/// \brief Tests whether the type is categorized as a fundamental type.
4738///
4739/// \returns True for types specified in C++0x [basic.fundamental].
4740inline bool Type::isFundamentalType() const {
4741  return isVoidType() ||
4742         // FIXME: It's really annoying that we don't have an
4743         // 'isArithmeticType()' which agrees with the standard definition.
4744         (isArithmeticType() && !isEnumeralType());
4745}
4746
4747/// \brief Tests whether the type is categorized as a compound type.
4748///
4749/// \returns True for types specified in C++0x [basic.compound].
4750inline bool Type::isCompoundType() const {
4751  // C++0x [basic.compound]p1:
4752  //   Compound types can be constructed in the following ways:
4753  //    -- arrays of objects of a given type [...];
4754  return isArrayType() ||
4755  //    -- functions, which have parameters of given types [...];
4756         isFunctionType() ||
4757  //    -- pointers to void or objects or functions [...];
4758         isPointerType() ||
4759  //    -- references to objects or functions of a given type. [...]
4760         isReferenceType() ||
4761  //    -- classes containing a sequence of objects of various types, [...];
4762         isRecordType() ||
4763  //    -- unions, which are classes capable of containing objects of different
4764  //               types at different times;
4765         isUnionType() ||
4766  //    -- enumerations, which comprise a set of named constant values. [...];
4767         isEnumeralType() ||
4768  //    -- pointers to non-static class members, [...].
4769         isMemberPointerType();
4770}
4771
4772inline bool Type::isFunctionType() const {
4773  return isa<FunctionType>(CanonicalType);
4774}
4775inline bool Type::isPointerType() const {
4776  return isa<PointerType>(CanonicalType);
4777}
4778inline bool Type::isAnyPointerType() const {
4779  return isPointerType() || isObjCObjectPointerType();
4780}
4781inline bool Type::isBlockPointerType() const {
4782  return isa<BlockPointerType>(CanonicalType);
4783}
4784inline bool Type::isReferenceType() const {
4785  return isa<ReferenceType>(CanonicalType);
4786}
4787inline bool Type::isLValueReferenceType() const {
4788  return isa<LValueReferenceType>(CanonicalType);
4789}
4790inline bool Type::isRValueReferenceType() const {
4791  return isa<RValueReferenceType>(CanonicalType);
4792}
4793inline bool Type::isFunctionPointerType() const {
4794  if (const PointerType *T = getAs<PointerType>())
4795    return T->getPointeeType()->isFunctionType();
4796  else
4797    return false;
4798}
4799inline bool Type::isMemberPointerType() const {
4800  return isa<MemberPointerType>(CanonicalType);
4801}
4802inline bool Type::isMemberFunctionPointerType() const {
4803  if (const MemberPointerType* T = getAs<MemberPointerType>())
4804    return T->isMemberFunctionPointer();
4805  else
4806    return false;
4807}
4808inline bool Type::isMemberDataPointerType() const {
4809  if (const MemberPointerType* T = getAs<MemberPointerType>())
4810    return T->isMemberDataPointer();
4811  else
4812    return false;
4813}
4814inline bool Type::isArrayType() const {
4815  return isa<ArrayType>(CanonicalType);
4816}
4817inline bool Type::isConstantArrayType() const {
4818  return isa<ConstantArrayType>(CanonicalType);
4819}
4820inline bool Type::isIncompleteArrayType() const {
4821  return isa<IncompleteArrayType>(CanonicalType);
4822}
4823inline bool Type::isVariableArrayType() const {
4824  return isa<VariableArrayType>(CanonicalType);
4825}
4826inline bool Type::isDependentSizedArrayType() const {
4827  return isa<DependentSizedArrayType>(CanonicalType);
4828}
4829inline bool Type::isBuiltinType() const {
4830  return isa<BuiltinType>(CanonicalType);
4831}
4832inline bool Type::isRecordType() const {
4833  return isa<RecordType>(CanonicalType);
4834}
4835inline bool Type::isEnumeralType() const {
4836  return isa<EnumType>(CanonicalType);
4837}
4838inline bool Type::isAnyComplexType() const {
4839  return isa<ComplexType>(CanonicalType);
4840}
4841inline bool Type::isVectorType() const {
4842  return isa<VectorType>(CanonicalType);
4843}
4844inline bool Type::isExtVectorType() const {
4845  return isa<ExtVectorType>(CanonicalType);
4846}
4847inline bool Type::isObjCObjectPointerType() const {
4848  return isa<ObjCObjectPointerType>(CanonicalType);
4849}
4850inline bool Type::isObjCObjectType() const {
4851  return isa<ObjCObjectType>(CanonicalType);
4852}
4853inline bool Type::isObjCObjectOrInterfaceType() const {
4854  return isa<ObjCInterfaceType>(CanonicalType) ||
4855    isa<ObjCObjectType>(CanonicalType);
4856}
4857inline bool Type::isAtomicType() const {
4858  return isa<AtomicType>(CanonicalType);
4859}
4860
4861inline bool Type::isObjCQualifiedIdType() const {
4862  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
4863    return OPT->isObjCQualifiedIdType();
4864  return false;
4865}
4866inline bool Type::isObjCQualifiedClassType() const {
4867  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
4868    return OPT->isObjCQualifiedClassType();
4869  return false;
4870}
4871inline bool Type::isObjCIdType() const {
4872  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
4873    return OPT->isObjCIdType();
4874  return false;
4875}
4876inline bool Type::isObjCClassType() const {
4877  if (const ObjCObjectPointerType *OPT = getAs<ObjCObjectPointerType>())
4878    return OPT->isObjCClassType();
4879  return false;
4880}
4881inline bool Type::isObjCSelType() const {
4882  if (const PointerType *OPT = getAs<PointerType>())
4883    return OPT->getPointeeType()->isSpecificBuiltinType(BuiltinType::ObjCSel);
4884  return false;
4885}
4886inline bool Type::isObjCBuiltinType() const {
4887  return isObjCIdType() || isObjCClassType() || isObjCSelType();
4888}
4889inline bool Type::isTemplateTypeParmType() const {
4890  return isa<TemplateTypeParmType>(CanonicalType);
4891}
4892
4893inline bool Type::isSpecificBuiltinType(unsigned K) const {
4894  if (const BuiltinType *BT = getAs<BuiltinType>())
4895    if (BT->getKind() == (BuiltinType::Kind) K)
4896      return true;
4897  return false;
4898}
4899
4900inline bool Type::isPlaceholderType() const {
4901  if (const BuiltinType *BT = dyn_cast<BuiltinType>(this))
4902    return BT->isPlaceholderType();
4903  return false;
4904}
4905
4906inline const BuiltinType *Type::getAsPlaceholderType() const {
4907  if (const BuiltinType *BT = dyn_cast<BuiltinType>(this))
4908    if (BT->isPlaceholderType())
4909      return BT;
4910  return 0;
4911}
4912
4913inline bool Type::isSpecificPlaceholderType(unsigned K) const {
4914  assert(BuiltinType::isPlaceholderTypeKind((BuiltinType::Kind) K));
4915  if (const BuiltinType *BT = dyn_cast<BuiltinType>(this))
4916    return (BT->getKind() == (BuiltinType::Kind) K);
4917  return false;
4918}
4919
4920inline bool Type::isNonOverloadPlaceholderType() const {
4921  if (const BuiltinType *BT = dyn_cast<BuiltinType>(this))
4922    return BT->isNonOverloadPlaceholderType();
4923  return false;
4924}
4925
4926inline bool Type::isVoidType() const {
4927  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
4928    return BT->getKind() == BuiltinType::Void;
4929  return false;
4930}
4931
4932inline bool Type::isHalfType() const {
4933  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
4934    return BT->getKind() == BuiltinType::Half;
4935  // FIXME: Should we allow complex __fp16? Probably not.
4936  return false;
4937}
4938
4939inline bool Type::isNullPtrType() const {
4940  if (const BuiltinType *BT = getAs<BuiltinType>())
4941    return BT->getKind() == BuiltinType::NullPtr;
4942  return false;
4943}
4944
4945extern bool IsEnumDeclComplete(EnumDecl *);
4946extern bool IsEnumDeclScoped(EnumDecl *);
4947
4948inline bool Type::isIntegerType() const {
4949  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
4950    return BT->getKind() >= BuiltinType::Bool &&
4951           BT->getKind() <= BuiltinType::Int128;
4952  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType)) {
4953    // Incomplete enum types are not treated as integer types.
4954    // FIXME: In C++, enum types are never integer types.
4955    return IsEnumDeclComplete(ET->getDecl()) &&
4956      !IsEnumDeclScoped(ET->getDecl());
4957  }
4958  return false;
4959}
4960
4961inline bool Type::isScalarType() const {
4962  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
4963    return BT->getKind() > BuiltinType::Void &&
4964           BT->getKind() <= BuiltinType::NullPtr;
4965  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
4966    // Enums are scalar types, but only if they are defined.  Incomplete enums
4967    // are not treated as scalar types.
4968    return IsEnumDeclComplete(ET->getDecl());
4969  return isa<PointerType>(CanonicalType) ||
4970         isa<BlockPointerType>(CanonicalType) ||
4971         isa<MemberPointerType>(CanonicalType) ||
4972         isa<ComplexType>(CanonicalType) ||
4973         isa<ObjCObjectPointerType>(CanonicalType);
4974}
4975
4976inline bool Type::isIntegralOrEnumerationType() const {
4977  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
4978    return BT->getKind() >= BuiltinType::Bool &&
4979           BT->getKind() <= BuiltinType::Int128;
4980
4981  // Check for a complete enum type; incomplete enum types are not properly an
4982  // enumeration type in the sense required here.
4983  if (const EnumType *ET = dyn_cast<EnumType>(CanonicalType))
4984    return IsEnumDeclComplete(ET->getDecl());
4985
4986  return false;
4987}
4988
4989inline bool Type::isBooleanType() const {
4990  if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
4991    return BT->getKind() == BuiltinType::Bool;
4992  return false;
4993}
4994
4995/// \brief Determines whether this is a type for which one can define
4996/// an overloaded operator.
4997inline bool Type::isOverloadableType() const {
4998  return isDependentType() || isRecordType() || isEnumeralType();
4999}
5000
5001/// \brief Determines whether this type can decay to a pointer type.
5002inline bool Type::canDecayToPointerType() const {
5003  return isFunctionType() || isArrayType();
5004}
5005
5006inline bool Type::hasPointerRepresentation() const {
5007  return (isPointerType() || isReferenceType() || isBlockPointerType() ||
5008          isObjCObjectPointerType() || isNullPtrType());
5009}
5010
5011inline bool Type::hasObjCPointerRepresentation() const {
5012  return isObjCObjectPointerType();
5013}
5014
5015inline const Type *Type::getBaseElementTypeUnsafe() const {
5016  const Type *type = this;
5017  while (const ArrayType *arrayType = type->getAsArrayTypeUnsafe())
5018    type = arrayType->getElementType().getTypePtr();
5019  return type;
5020}
5021
5022/// Insertion operator for diagnostics.  This allows sending QualType's into a
5023/// diagnostic with <<.
5024inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
5025                                           QualType T) {
5026  DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()),
5027                  DiagnosticsEngine::ak_qualtype);
5028  return DB;
5029}
5030
5031/// Insertion operator for partial diagnostics.  This allows sending QualType's
5032/// into a diagnostic with <<.
5033inline const PartialDiagnostic &operator<<(const PartialDiagnostic &PD,
5034                                           QualType T) {
5035  PD.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()),
5036                  DiagnosticsEngine::ak_qualtype);
5037  return PD;
5038}
5039
5040// Helper class template that is used by Type::getAs to ensure that one does
5041// not try to look through a qualified type to get to an array type.
5042template<typename T,
5043         bool isArrayType = (llvm::is_same<T, ArrayType>::value ||
5044                             llvm::is_base_of<ArrayType, T>::value)>
5045struct ArrayType_cannot_be_used_with_getAs { };
5046
5047template<typename T>
5048struct ArrayType_cannot_be_used_with_getAs<T, true>;
5049
5050// Member-template getAs<specific type>'.
5051template <typename T> const T *Type::getAs() const {
5052  ArrayType_cannot_be_used_with_getAs<T> at;
5053  (void)at;
5054
5055  // If this is directly a T type, return it.
5056  if (const T *Ty = dyn_cast<T>(this))
5057    return Ty;
5058
5059  // If the canonical form of this type isn't the right kind, reject it.
5060  if (!isa<T>(CanonicalType))
5061    return 0;
5062
5063  // If this is a typedef for the type, strip the typedef off without
5064  // losing all typedef information.
5065  return cast<T>(getUnqualifiedDesugaredType());
5066}
5067
5068inline const ArrayType *Type::getAsArrayTypeUnsafe() const {
5069  // If this is directly an array type, return it.
5070  if (const ArrayType *arr = dyn_cast<ArrayType>(this))
5071    return arr;
5072
5073  // If the canonical form of this type isn't the right kind, reject it.
5074  if (!isa<ArrayType>(CanonicalType))
5075    return 0;
5076
5077  // If this is a typedef for the type, strip the typedef off without
5078  // losing all typedef information.
5079  return cast<ArrayType>(getUnqualifiedDesugaredType());
5080}
5081
5082template <typename T> const T *Type::castAs() const {
5083  ArrayType_cannot_be_used_with_getAs<T> at;
5084  (void) at;
5085
5086  assert(isa<T>(CanonicalType));
5087  if (const T *ty = dyn_cast<T>(this)) return ty;
5088  return cast<T>(getUnqualifiedDesugaredType());
5089}
5090
5091inline const ArrayType *Type::castAsArrayTypeUnsafe() const {
5092  assert(isa<ArrayType>(CanonicalType));
5093  if (const ArrayType *arr = dyn_cast<ArrayType>(this)) return arr;
5094  return cast<ArrayType>(getUnqualifiedDesugaredType());
5095}
5096
5097}  // end namespace clang
5098
5099#endif
5100