Type.h revision 72564e73277e29f6db3305d1f27ba408abb7ed88
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//===--- Type.h - C Language Family Type Representation ---------*- C++ -*-===//
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//                     The LLVM Compiler Infrastructure
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// This file is distributed under the University of Illinois Open Source
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// License. See LICENSE.TXT for details.
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//===----------------------------------------------------------------------===//
9c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//
10c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//  This file defines the Type interface and subclasses.
11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch//
12c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)//===----------------------------------------------------------------------===//
137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#ifndef LLVM_CLANG_AST_TYPE_H
15c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#define LLVM_CLANG_AST_TYPE_H
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "clang/Basic/Diagnostic.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "llvm/Support/Casting.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "llvm/ADT/APSInt.h"
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "llvm/ADT/FoldingSet.h"
21116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "llvm/ADT/PointerIntPair.h"
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "llvm/Bitcode/SerializationFwd.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using llvm::isa;
24a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)using llvm::cast;
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using llvm::cast_or_null;
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)using llvm::dyn_cast;
27c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)using llvm::dyn_cast_or_null;
28c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace clang {
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class ASTContext;
31effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  class Type;
32e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  class TypedefDecl;
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class TemplateDecl;
347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  class TemplateTypeParmDecl;
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class NonTypeTemplateParmDecl;
36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  class TemplateTemplateParamDecl;
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  class TagDecl;
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class RecordDecl;
39c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  class CXXRecordDecl;
40c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  class EnumDecl;
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class FieldDecl;
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class ObjCInterfaceDecl;
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class ObjCProtocolDecl;
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class ObjCMethodDecl;
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class Expr;
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class Stmt;
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class SourceLocation;
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class StmtIteratorBase;
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Provide forward declarations for all of the *Type classes
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define TYPE(Class, Base) class Class##Type;
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/AST/TypeNodes.def"
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/// QualType - For efficiency, we don't store CVR-qualified types as nodes on
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// their own: instead each reference to a type stores the qualifiers.  This
56c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// greatly reduces the number of nodes we need to allocate for types (for
57c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// example we only need one for 'int', 'const int', 'volatile int',
58c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// 'const volatile int', etc).
59c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)///
60c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// As an added efficiency bonus, instead of making this a pair, we just store
61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// the three bits we care about in the low bits of the pointer.  To handle the
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch/// packing/unpacking, we make QualType be a simple wrapper class that acts like
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// a smart pointer.
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class QualType {
653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  llvm::PointerIntPair<Type*, 3> Value;
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)public:
67a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  enum TQ {   // NOTE: These flags must be kept in sync with DeclSpec::TQ.
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Const    = 0x1,
69a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    Restrict = 0x2,
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Volatile = 0x4,
7158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    CVRFlags = Const|Restrict|Volatile
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
73a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  enum GCAttrTypes {
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    GCNone = 0,
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    Weak,
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    Strong
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  QualType() {}
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  QualType(const Type *Ptr, unsigned Quals)
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : Value(const_cast<Type*>(Ptr), Quals) {}
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  unsigned getCVRQualifiers() const { return Value.getInt(); }
8658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void setCVRQualifiers(unsigned Quals) { Value.setInt(Quals); }
8758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  Type *getTypePtr() const { return Value.getPointer(); }
8858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
8958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void *getAsOpaquePtr() const { return Value.getOpaqueValue(); }
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static QualType getFromOpaquePtr(void *Ptr) {
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    QualType T;
9258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    T.Value.setFromOpaqueValue(Ptr);
93c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return T;
94c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
95c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
96a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  Type &operator*() const {
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return *getTypePtr();
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  Type *operator->() const {
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return getTypePtr();
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
104a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  /// isNull - Return true if this QualType doesn't point to a type yet.
1053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  bool isNull() const {
1063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return getTypePtr() == 0;
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
108a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
1093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  bool isConstQualified() const {
110c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return (getCVRQualifiers() & Const) ? true : false;
111c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
112c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isVolatileQualified() const {
113c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return (getCVRQualifiers() & Volatile) ? true : false;
114c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
115c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isRestrictQualified() const {
116c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return (getCVRQualifiers() & Restrict) ? true : false;
117c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
118c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
119c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isConstant(ASTContext& Ctx) const;
120c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
121c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// addConst/addVolatile/addRestrict - add the specified type qual to this
122c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// QualType.
123c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void addConst()    { Value.setInt(Value.getInt() | Const); }
124c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void addVolatile() { Value.setInt(Value.getInt() | Volatile); }
125c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void addRestrict() { Value.setInt(Value.getInt() | Restrict); }
126c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
127c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void removeConst()    { Value.setInt(Value.getInt() & ~Const); }
128c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void removeVolatile() { Value.setInt(Value.getInt() & ~Volatile); }
129c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void removeRestrict() { Value.setInt(Value.getInt() & ~Restrict); }
130c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
131c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  QualType getQualifiedType(unsigned TQs) const {
132c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return QualType(getTypePtr(), TQs);
133c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
134c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  QualType getWithAdditionalQualifiers(unsigned TQs) const {
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return QualType(getTypePtr(), TQs|getCVRQualifiers());
136c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
137c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  QualType withConst() const { return getWithAdditionalQualifiers(Const); }
139c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  QualType withVolatile() const { return getWithAdditionalQualifiers(Volatile);}
140c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  QualType withRestrict() const { return getWithAdditionalQualifiers(Restrict);}
141c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
142c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  QualType getUnqualifiedType() const;
143c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isMoreQualifiedThan(QualType Other) const;
144c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isAtLeastAsQualifiedAs(QualType Other) const;
145c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  QualType getNonReferenceType() const;
146c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
147c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
148c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  /// operator==/!= - Indicate whether the specified types and qualifiers are
149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// identical.
150c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool operator==(const QualType &RHS) const {
151c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return Value == RHS.Value;
152c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
153c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool operator!=(const QualType &RHS) const {
154c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return Value != RHS.Value;
155f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
156c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  std::string getAsString() const {
157c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    std::string S;
158c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    getAsStringInternal(S);
159effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    return S;
160effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
161effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void getAsStringInternal(std::string &Str) const;
162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
163116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void dump(const char *s) const;
164116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void dump() const;
165116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
166116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void Profile(llvm::FoldingSetNodeID &ID) const {
167effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    ID.AddPointer(getAsOpaquePtr());
168effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  }
169effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
170effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochpublic:
171effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
172effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  /// getAddressSpace - Return the address space of this type.
173effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  inline unsigned getAddressSpace() const;
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
175a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// GCAttrTypesAttr - Returns gc attribute of this type.
176a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  inline QualType::GCAttrTypes getObjCGCAttr() const;
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// isObjCGCWeak true when Type is objc's weak.
179116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isObjCGCWeak() const {
180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return getObjCGCAttr() == Weak;
181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
183116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// isObjCGCStrong true when Type is objc's strong.
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isObjCGCStrong() const {
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return getObjCGCAttr() == Strong;
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// Emit - Serialize a QualType to Bitcode.
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void Emit(llvm::Serializer& S) const;
190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
191116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// Read - Deserialize a QualType from Bitcode.
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static QualType ReadVal(llvm::Deserializer& D);
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void ReadBackpatch(llvm::Deserializer& D);
195116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch};
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} // end clang.
198c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
199a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace llvm {
2003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)/// Implement simplify_type for QualType, so that we can dyn_cast from QualType
201a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)/// to a specific Type class.
202a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)template<> struct simplify_type<const ::clang::QualType> {
2035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef ::clang::Type* SimpleType;
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static SimpleType getSimplifiedValue(const ::clang::QualType &Val) {
205a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return Val.getTypePtr();
206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
208f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)template<> struct simplify_type< ::clang::QualType>
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  : public simplify_type<const ::clang::QualType> {};
210c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
211f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} // end namespace llvm
212f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
213f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace clang {
214f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
215f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)/// Type - This is the base class of the type hierarchy.  A central concept
216f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)/// with types is that each type always has a canonical type.  A canonical type
217f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)/// is the type with any typedef names stripped out of it or the types it
218f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)/// references.  For example, consider:
219f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)///
220f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)///  typedef int  foo;
221f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)///  typedef foo* bar;
222f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)///    'int *'    'foo *'    'bar'
223f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)///
224f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)/// There will be a Type object created for 'int'.  Since int is canonical, its
225f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)/// canonicaltype pointer points to itself.  There is also a Type for 'foo' (a
226a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)/// TypedefType).  Its CanonicalType pointer points to the 'int' Type.  Next
227f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)/// there is a PointerType that represents 'int*', which, like 'int', is
228c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// canonical.  Finally, there is a PointerType type for 'foo*' whose canonical
229c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// type is 'int*', and there is a TypedefType for 'bar', whose canonical type
230c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)/// is also 'int*'.
231a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)///
232a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)/// Non-canonical types are useful for emitting diagnostics, without losing
233a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)/// information about typedefs being used.  Canonical types are useful for type
234a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)/// comparisons (they allow by-pointer equality tests) and useful for reasoning
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)/// about whether something has a particular form (e.g. is a function type),
236a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)/// because they implicitly, recursively, strip all typedefs out of a type.
237a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)///
238a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)/// Types, once created, are immutable.
239a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)///
240a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)class Type {
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)public:
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum TypeClass {
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define TYPE(Class, Base) Class,
244116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define ABSTRACT_TYPE(Class, Base)
245116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "clang/AST/TypeNodes.def"
246116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    TagFirst = Record, TagLast = Enum
247116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  };
248116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
249116680a4aac90f2aa7413d9095a592090648e557Ben Murdochprivate:
250116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  QualType CanonicalType;
251116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
252116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// Dependent - Whether this type is a dependent type (C++ [temp.dep.type]).
253116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool Dependent : 1;
254116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
255116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// TypeClass bitfield - Enum that specifies what subclass this belongs to.
256116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// Note that this should stay at the end of the ivars for Type so that
257116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// subclasses can pack their bitfields into the same word.
258116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  unsigned TC : 5;
259116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
260116680a4aac90f2aa7413d9095a592090648e557Ben Murdochprotected:
261116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // silence VC++ warning C4355: 'this' : used in base member initializer list
262116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  Type *this_() { return this; }
263116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  Type(TypeClass tc, QualType Canonical, bool dependent)
264116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : CanonicalType(Canonical.isNull() ? QualType(this_(), 0) : Canonical),
265116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      Dependent(dependent), TC(tc) {}
266116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual ~Type() {}
267116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void Destroy(ASTContext& C);
268116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  friend class ASTContext;
269116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
270116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void EmitTypeInternal(llvm::Serializer& S) const;
271116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void ReadTypeInternal(llvm::Deserializer& D);
272116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
273116680a4aac90f2aa7413d9095a592090648e557Ben Murdochpublic:
274116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  TypeClass getTypeClass() const { return static_cast<TypeClass>(TC); }
275116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
276116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isCanonical() const { return CanonicalType.getTypePtr() == this; }
277116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
278116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// Types are partitioned into 3 broad categories (C99 6.2.5p1):
279116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// object types, function types, and incomplete types.
280116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
281116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// isObjectType - types that fully describe objects. An object is a region
282116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// of memory that can be examined and stored into (H&S).
283116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isObjectType() const;
284116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
285116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// isIncompleteType - Return true if this is an incomplete type.
286116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// A type that can describe objects, but which lacks information needed to
287116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// determine its size (e.g. void, or a fwd declared struct). Clients of this
288116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// routine will need to determine if the size is actually required.
289116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isIncompleteType() const;
290116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
291116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// isIncompleteOrObjectType - Return true if this is an incomplete or object
292116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// type, in other words, not a function type.
293116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isIncompleteOrObjectType() const {
294116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return !isFunctionType();
295116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
296116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
297116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// isPODType - Return true if this is a plain-old-data type (C++ 3.9p10).
298116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isPODType() const;
299116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
300116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array
301116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// types that have a non-constant expression. This does not include "[]".
302116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isVariablyModifiedType() const;
303116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
304116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// Helper methods to distinguish type categories. All type predicates
305116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// operate on the canonical type, ignoring typedefs and qualifiers.
306116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
307116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// isSpecificBuiltinType - Test for a particular builtin type.
308116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isSpecificBuiltinType(unsigned K) const;
309116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
310116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// isIntegerType() does *not* include complex integers (a GCC extension).
311116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// isComplexIntegerType() can be used to test for complex integers.
312116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isIntegerType() const;     // C99 6.2.5p17 (int, char, bool, enum)
313116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isEnumeralType() const;
314116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool isBooleanType() const;
31558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool isCharType() const;
31658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool isWideCharType() const;
31758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool isIntegralType() const;
31858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
31958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  /// Floating point categories.
32058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool isRealFloatingType() const; // C99 6.2.5p10 (float, double, long double)
3212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// isComplexType() does *not* include complex integers (a GCC extension).
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  /// isComplexIntegerType() can be used to test for complex integers.
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool isComplexType() const;      // C99 6.2.5p11 (complex)
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool isAnyComplexType() const;   // C99 6.2.5p11 (complex) + Complex Int.
32568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  bool isFloatingType() const;     // C99 6.2.5p11 (real floating + complex)
326c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isRealType() const;         // C99 6.2.5p17 (real floating + integer)
327c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isArithmeticType() const;   // C99 6.2.5p18 (integer + floating)
3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool isVoidType() const;         // C99 6.2.5p19
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool isDerivedType() const;      // C99 6.2.5p20
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool isScalarType() const;       // C99 6.2.5p21 (arithmetic + pointers)
3314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool isAggregateType() const;
332a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
333a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Type Predicates: Check to see if this type is structurally the specified
334c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // type, ignoring typedefs and qualifiers.
335c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isFunctionType() const;
33658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool isPointerType() const;
33758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  bool isBlockPointerType() const;
338c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isReferenceType() const;
339c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isFunctionPointerType() const;
340c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isMemberPointerType() const;
341c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isMemberFunctionPointerType() const;
342c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool isArrayType() const;
3435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isConstantArrayType() const;
3445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isIncompleteArrayType() const;
34523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  bool isVariableArrayType() const;
34623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  bool isDependentSizedArrayType() const;
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool isRecordType() const;
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool isClassType() const;
3495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isStructureType() const;
3505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isUnionType() const;
3515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isComplexIntegerType() const;            // GCC _Complex integer type.
3525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isVectorType() const;                    // GCC vector type.
3535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isExtVectorType() const;                 // Extended vector type.
3545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isObjCInterfaceType() const;             // NSString or NSString<foo>
3555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isObjCQualifiedInterfaceType() const;    // NSString<foo>
3565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isObjCQualifiedIdType() const;           // id<foo>
3575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isTemplateTypeParmType() const;          // C++ template type parameter
3585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// isDependentType - Whether this type is a dependent type, meaning
360a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  /// that its definition somehow depends on a template parameter
3615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// (C++ [temp.dep.type]).
362a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool isDependentType() const { return Dependent; }
3635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool isOverloadType() const;                  // C++ overloaded function
3645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  /// hasPointerRepresentation - Whether this type is represented
366116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// natively as a pointer; this includes pointers, references, block
367116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// pointers, and Objective-C interface, qualified id, and qualified
368116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// interface types.
369116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool hasPointerRepresentation() const;
370116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
371116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Type Checking Functions: Check to see if this type is structurally the
372116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // specified type, ignoring typedefs and qualifiers, and return a pointer to
373116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // the best type we can.
374116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const BuiltinType *getAsBuiltinType() const;
375116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const FunctionType *getAsFunctionType() const;
376a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const FunctionNoProtoType *getAsFunctionNoProtoType() const;
377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const FunctionProtoType *getAsFunctionProtoType() const;
378a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const PointerType *getAsPointerType() const;
379116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const BlockPointerType *getAsBlockPointerType() const;
380116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const ReferenceType *getAsReferenceType() const;
381116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const MemberPointerType *getAsMemberPointerType() const;
382116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const TagType *getAsTagType() const;
383116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const RecordType *getAsRecordType() const;
384116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const RecordType *getAsStructureType() const;
385116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  /// NOTE: getAs*ArrayType are methods on ASTContext.
386116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const TypedefType *getAsTypedefType() const;
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const RecordType *getAsUnionType() const;
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const EnumType *getAsEnumType() const;
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const VectorType *getAsVectorType() const; // GCC vector type.
39058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const ComplexType *getAsComplexType() const;
39158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const ComplexType *getAsComplexIntegerType() const; // GCC complex int type.
392a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const ExtVectorType *getAsExtVectorType() const; // Extended vector type.
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const ObjCInterfaceType *getAsObjCInterfaceType() const;
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const ObjCQualifiedInterfaceType *getAsObjCQualifiedInterfaceType() const;
3955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const ObjCQualifiedIdType *getAsObjCQualifiedIdType() const;
3965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const TemplateTypeParmType *getAsTemplateTypeParmType() const;
3975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const ClassTemplateSpecializationType *
3995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    getClassTemplateSpecializationType() const;
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
401  /// getAsPointerToObjCInterfaceType - If this is a pointer to an ObjC
402  /// interface, return the interface type, otherwise return null.
403  const ObjCInterfaceType *getAsPointerToObjCInterfaceType() const;
404
405  /// getArrayElementTypeNoTypeQual - If this is an array type, return the
406  /// element type of the array, potentially with type qualifiers missing.
407  /// This method should never be used when type qualifiers are meaningful.
408  const Type *getArrayElementTypeNoTypeQual() const;
409
410  /// getDesugaredType - Return the specified type with any "sugar" removed from
411  /// the type.  This takes off typedefs, typeof's etc.  If the outer level of
412  /// the type is already concrete, it returns it unmodified.  This is similar
413  /// to getting the canonical type, but it doesn't remove *all* typedefs.  For
414  /// example, it returns "T*" as "T*", (not as "int*"), because the pointer is
415  /// concrete.
416  QualType getDesugaredType() const;
417
418  /// More type predicates useful for type checking/promotion
419  bool isPromotableIntegerType() const; // C99 6.3.1.1p2
420
421  /// isSignedIntegerType - Return true if this is an integer type that is
422  /// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
423  /// an enum decl which has a signed representation, or a vector of signed
424  /// integer element type.
425  bool isSignedIntegerType() const;
426
427  /// isUnsignedIntegerType - Return true if this is an integer type that is
428  /// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
429  /// decl which has an unsigned representation, or a vector of unsigned integer
430  /// element type.
431  bool isUnsignedIntegerType() const;
432
433  /// isConstantSizeType - Return true if this is not a variable sized type,
434  /// according to the rules of C99 6.7.5p3.  It is not legal to call this on
435  /// incomplete types.
436  bool isConstantSizeType() const;
437
438  QualType getCanonicalTypeInternal() const { return CanonicalType; }
439  void dump() const;
440  virtual void getAsStringInternal(std::string &InnerString) const = 0;
441  static bool classof(const Type *) { return true; }
442
443protected:
444  /// Emit - Emit a Type to bitcode.  Used by ASTContext.
445  void Emit(llvm::Serializer& S) const;
446
447  /// Create - Construct a Type from bitcode.  Used by ASTContext.
448  static void Create(ASTContext& Context, unsigned i, llvm::Deserializer& S);
449
450  /// EmitImpl - Subclasses must implement this method in order to
451  ///  be serialized.
452  // FIXME: Make this abstract once implemented.
453  virtual void EmitImpl(llvm::Serializer& S) const {
454    assert(false && "Serialization for type not supported.");
455  }
456};
457
458/// ExtQualType - TR18037 (C embedded extensions) 6.2.5p26
459/// This supports all kinds of type attributes; including,
460/// address space qualified types, objective-c's __weak and
461/// __strong attributes.
462///
463class ExtQualType : public Type, public llvm::FoldingSetNode {
464  /// BaseType - This is the underlying type that this qualifies.  All CVR
465  /// qualifiers are stored on the QualType that references this type, so we
466  /// can't have any here.
467  Type *BaseType;
468
469  /// Address Space ID - The address space ID this type is qualified with.
470  unsigned AddressSpace;
471  /// GC __weak/__strong attributes
472  QualType::GCAttrTypes GCAttrType;
473
474  ExtQualType(Type *Base, QualType CanonicalPtr, unsigned AddrSpace,
475              QualType::GCAttrTypes gcAttr) :
476      Type(ExtQual, CanonicalPtr, Base->isDependentType()), BaseType(Base),
477      AddressSpace(AddrSpace), GCAttrType(gcAttr) {
478    assert(!isa<ExtQualType>(BaseType) &&
479           "Cannot have ExtQualType of ExtQualType");
480  }
481  friend class ASTContext;  // ASTContext creates these.
482public:
483  Type *getBaseType() const { return BaseType; }
484  QualType::GCAttrTypes getObjCGCAttr() const { return GCAttrType; }
485  unsigned getAddressSpace() const { return AddressSpace; }
486
487  virtual void getAsStringInternal(std::string &InnerString) const;
488
489  void Profile(llvm::FoldingSetNodeID &ID) {
490    Profile(ID, getBaseType(), AddressSpace, GCAttrType);
491  }
492  static void Profile(llvm::FoldingSetNodeID &ID, Type *Base,
493                      unsigned AddrSpace, QualType::GCAttrTypes gcAttr) {
494    ID.AddPointer(Base);
495    ID.AddInteger(AddrSpace);
496    ID.AddInteger(gcAttr);
497  }
498
499  static bool classof(const Type *T) { return T->getTypeClass() == ExtQual; }
500  static bool classof(const ExtQualType *) { return true; }
501
502protected:
503  virtual void EmitImpl(llvm::Serializer& S) const;
504  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
505  friend class Type;
506};
507
508
509/// BuiltinType - This class is used for builtin types like 'int'.  Builtin
510/// types are always canonical and have a literal name field.
511class BuiltinType : public Type {
512public:
513  enum Kind {
514    Void,
515
516    Bool,     // This is bool and/or _Bool.
517    Char_U,   // This is 'char' for targets where char is unsigned.
518    UChar,    // This is explicitly qualified unsigned char.
519    UShort,
520    UInt,
521    ULong,
522    ULongLong,
523
524    Char_S,   // This is 'char' for targets where char is signed.
525    SChar,    // This is explicitly qualified signed char.
526    WChar,    // This is 'wchar_t' for C++.
527    Short,
528    Int,
529    Long,
530    LongLong,
531
532    Float, Double, LongDouble,
533
534    Overload,  // This represents the type of an overloaded function declaration.
535    Dependent  // This represents the type of a type-dependent expression.
536  };
537private:
538  Kind TypeKind;
539public:
540  BuiltinType(Kind K)
541    : Type(Builtin, QualType(), /*Dependent=*/(K == Dependent)),
542      TypeKind(K) {}
543
544  Kind getKind() const { return TypeKind; }
545  const char *getName() const;
546
547  virtual void getAsStringInternal(std::string &InnerString) const;
548
549  static bool classof(const Type *T) { return T->getTypeClass() == Builtin; }
550  static bool classof(const BuiltinType *) { return true; }
551};
552
553/// FixedWidthIntType - Used for arbitrary width types that we either don't
554/// want to or can't map to named integer types.  These always have a lower
555/// integer rank than builtin types of the same width.
556class FixedWidthIntType : public Type {
557private:
558  unsigned Width;
559  bool Signed;
560public:
561  FixedWidthIntType(unsigned W, bool S) : Type(FixedWidthInt, QualType(), false),
562                                          Width(W), Signed(S) {}
563
564  unsigned getWidth() const { return Width; }
565  bool isSigned() const { return Signed; }
566  const char *getName() const;
567
568  virtual void getAsStringInternal(std::string &InnerString) const;
569
570  static bool classof(const Type *T) { return T->getTypeClass() == FixedWidthInt; }
571  static bool classof(const FixedWidthIntType *) { return true; }
572};
573
574/// ComplexType - C99 6.2.5p11 - Complex values.  This supports the C99 complex
575/// types (_Complex float etc) as well as the GCC integer complex extensions.
576///
577class ComplexType : public Type, public llvm::FoldingSetNode {
578  QualType ElementType;
579  ComplexType(QualType Element, QualType CanonicalPtr) :
580    Type(Complex, CanonicalPtr, Element->isDependentType()),
581    ElementType(Element) {
582  }
583  friend class ASTContext;  // ASTContext creates these.
584public:
585  QualType getElementType() const { return ElementType; }
586
587  virtual void getAsStringInternal(std::string &InnerString) const;
588
589  void Profile(llvm::FoldingSetNodeID &ID) {
590    Profile(ID, getElementType());
591  }
592  static void Profile(llvm::FoldingSetNodeID &ID, QualType Element) {
593    ID.AddPointer(Element.getAsOpaquePtr());
594  }
595
596  static bool classof(const Type *T) { return T->getTypeClass() == Complex; }
597  static bool classof(const ComplexType *) { return true; }
598
599protected:
600  virtual void EmitImpl(llvm::Serializer& S) const;
601  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
602  friend class Type;
603};
604
605/// PointerType - C99 6.7.5.1 - Pointer Declarators.
606///
607class PointerType : public Type, public llvm::FoldingSetNode {
608  QualType PointeeType;
609
610  PointerType(QualType Pointee, QualType CanonicalPtr) :
611    Type(Pointer, CanonicalPtr, Pointee->isDependentType()), PointeeType(Pointee) {
612  }
613  friend class ASTContext;  // ASTContext creates these.
614public:
615
616  virtual void getAsStringInternal(std::string &InnerString) const;
617
618  QualType getPointeeType() const { return PointeeType; }
619
620  void Profile(llvm::FoldingSetNodeID &ID) {
621    Profile(ID, getPointeeType());
622  }
623  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
624    ID.AddPointer(Pointee.getAsOpaquePtr());
625  }
626
627  static bool classof(const Type *T) { return T->getTypeClass() == Pointer; }
628  static bool classof(const PointerType *) { return true; }
629
630protected:
631  virtual void EmitImpl(llvm::Serializer& S) const;
632  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
633  friend class Type;
634};
635
636/// BlockPointerType - pointer to a block type.
637/// This type is to represent types syntactically represented as
638/// "void (^)(int)", etc. Pointee is required to always be a function type.
639///
640class BlockPointerType : public Type, public llvm::FoldingSetNode {
641  QualType PointeeType;  // Block is some kind of pointer type
642  BlockPointerType(QualType Pointee, QualType CanonicalCls) :
643    Type(BlockPointer, CanonicalCls, Pointee->isDependentType()),
644    PointeeType(Pointee) {
645  }
646  friend class ASTContext;  // ASTContext creates these.
647public:
648
649  // Get the pointee type. Pointee is required to always be a function type.
650  QualType getPointeeType() const { return PointeeType; }
651
652  virtual void getAsStringInternal(std::string &InnerString) const;
653
654  void Profile(llvm::FoldingSetNodeID &ID) {
655      Profile(ID, getPointeeType());
656  }
657  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee) {
658      ID.AddPointer(Pointee.getAsOpaquePtr());
659  }
660
661  static bool classof(const Type *T) {
662    return T->getTypeClass() == BlockPointer;
663  }
664  static bool classof(const BlockPointerType *) { return true; }
665
666  protected:
667    virtual void EmitImpl(llvm::Serializer& S) const;
668    static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
669    friend class Type;
670};
671
672/// ReferenceType - C++ 8.3.2 - Reference Declarators.
673///
674class ReferenceType : public Type, public llvm::FoldingSetNode {
675  QualType PointeeType;
676
677  ReferenceType(QualType Referencee, QualType CanonicalRef) :
678    Type(Reference, CanonicalRef, Referencee->isDependentType()),
679    PointeeType(Referencee) {
680  }
681  friend class ASTContext;  // ASTContext creates these.
682public:
683  virtual void getAsStringInternal(std::string &InnerString) const;
684
685  QualType getPointeeType() const { return PointeeType; }
686
687  void Profile(llvm::FoldingSetNodeID &ID) {
688    Profile(ID, getPointeeType());
689  }
690  static void Profile(llvm::FoldingSetNodeID &ID, QualType Referencee) {
691    ID.AddPointer(Referencee.getAsOpaquePtr());
692  }
693
694  static bool classof(const Type *T) { return T->getTypeClass() == Reference; }
695  static bool classof(const ReferenceType *) { return true; }
696
697protected:
698  virtual void EmitImpl(llvm::Serializer& S) const;
699  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
700  friend class Type;
701};
702
703/// MemberPointerType - C++ 8.3.3 - Pointers to members
704///
705class MemberPointerType : public Type, public llvm::FoldingSetNode {
706  QualType PointeeType;
707  /// The class of which the pointee is a member. Must ultimately be a
708  /// RecordType, but could be a typedef or a template parameter too.
709  const Type *Class;
710
711  MemberPointerType(QualType Pointee, const Type *Cls, QualType CanonicalPtr) :
712    Type(MemberPointer, CanonicalPtr,
713         Cls->isDependentType() || Pointee->isDependentType()),
714    PointeeType(Pointee), Class(Cls) {
715  }
716  friend class ASTContext; // ASTContext creates these.
717public:
718
719  QualType getPointeeType() const { return PointeeType; }
720
721  const Type *getClass() const { return Class; }
722
723  virtual void getAsStringInternal(std::string &InnerString) const;
724
725  void Profile(llvm::FoldingSetNodeID &ID) {
726    Profile(ID, getPointeeType(), getClass());
727  }
728  static void Profile(llvm::FoldingSetNodeID &ID, QualType Pointee,
729                      const Type *Class) {
730    ID.AddPointer(Pointee.getAsOpaquePtr());
731    ID.AddPointer(Class);
732  }
733
734  static bool classof(const Type *T) {
735    return T->getTypeClass() == MemberPointer;
736  }
737  static bool classof(const MemberPointerType *) { return true; }
738
739protected:
740  virtual void EmitImpl(llvm::Serializer& S) const;
741  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
742  friend class Type;
743};
744
745/// ArrayType - C99 6.7.5.2 - Array Declarators.
746///
747class ArrayType : public Type, public llvm::FoldingSetNode {
748public:
749  /// ArraySizeModifier - Capture whether this is a normal array (e.g. int X[4])
750  /// an array with a static size (e.g. int X[static 4]), or an array
751  /// with a star size (e.g. int X[*]).
752  /// 'static' is only allowed on function parameters.
753  enum ArraySizeModifier {
754    Normal, Static, Star
755  };
756private:
757  /// ElementType - The element type of the array.
758  QualType ElementType;
759
760  // NOTE: VC++ treats enums as signed, avoid using the ArraySizeModifier enum
761  /// NOTE: These fields are packed into the bitfields space in the Type class.
762  unsigned SizeModifier : 2;
763
764  /// IndexTypeQuals - Capture qualifiers in declarations like:
765  /// 'int X[static restrict 4]'. For function parameters only.
766  unsigned IndexTypeQuals : 3;
767
768protected:
769  // C++ [temp.dep.type]p1:
770  //   A type is dependent if it is...
771  //     - an array type constructed from any dependent type or whose
772  //       size is specified by a constant expression that is
773  //       value-dependent,
774  ArrayType(TypeClass tc, QualType et, QualType can,
775            ArraySizeModifier sm, unsigned tq)
776    : Type(tc, can, et->isDependentType() || tc == DependentSizedArray),
777      ElementType(et), SizeModifier(sm), IndexTypeQuals(tq) {}
778
779  friend class ASTContext;  // ASTContext creates these.
780public:
781  QualType getElementType() const { return ElementType; }
782  ArraySizeModifier getSizeModifier() const {
783    return ArraySizeModifier(SizeModifier);
784  }
785  unsigned getIndexTypeQualifier() const { return IndexTypeQuals; }
786
787  static bool classof(const Type *T) {
788    return T->getTypeClass() == ConstantArray ||
789           T->getTypeClass() == VariableArray ||
790           T->getTypeClass() == IncompleteArray ||
791           T->getTypeClass() == DependentSizedArray;
792  }
793  static bool classof(const ArrayType *) { return true; }
794};
795
796/// ConstantArrayType - This class represents C arrays with a specified constant
797/// size.  For example 'int A[100]' has ConstantArrayType where the element type
798/// is 'int' and the size is 100.
799class ConstantArrayType : public ArrayType {
800  llvm::APInt Size; // Allows us to unique the type.
801
802  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
803                    ArraySizeModifier sm, unsigned tq)
804    : ArrayType(ConstantArray, et, can, sm, tq), Size(size) {}
805  friend class ASTContext;  // ASTContext creates these.
806public:
807  const llvm::APInt &getSize() const { return Size; }
808  virtual void getAsStringInternal(std::string &InnerString) const;
809
810  void Profile(llvm::FoldingSetNodeID &ID) {
811    Profile(ID, getElementType(), getSize(),
812            getSizeModifier(), getIndexTypeQualifier());
813  }
814  static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
815                      const llvm::APInt &ArraySize, ArraySizeModifier SizeMod,
816                      unsigned TypeQuals) {
817    ID.AddPointer(ET.getAsOpaquePtr());
818    ID.AddInteger(ArraySize.getZExtValue());
819    ID.AddInteger(SizeMod);
820    ID.AddInteger(TypeQuals);
821  }
822  static bool classof(const Type *T) {
823    return T->getTypeClass() == ConstantArray;
824  }
825  static bool classof(const ConstantArrayType *) { return true; }
826
827protected:
828  virtual void EmitImpl(llvm::Serializer& S) const;
829  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
830  friend class Type;
831};
832
833/// IncompleteArrayType - This class represents C arrays with an unspecified
834/// size.  For example 'int A[]' has an IncompleteArrayType where the element
835/// type is 'int' and the size is unspecified.
836class IncompleteArrayType : public ArrayType {
837  IncompleteArrayType(QualType et, QualType can,
838                    ArraySizeModifier sm, unsigned tq)
839    : ArrayType(IncompleteArray, et, can, sm, tq) {}
840  friend class ASTContext;  // ASTContext creates these.
841public:
842
843  virtual void getAsStringInternal(std::string &InnerString) const;
844
845  static bool classof(const Type *T) {
846    return T->getTypeClass() == IncompleteArray;
847  }
848  static bool classof(const IncompleteArrayType *) { return true; }
849
850  friend class StmtIteratorBase;
851
852  void Profile(llvm::FoldingSetNodeID &ID) {
853    Profile(ID, getElementType(), getSizeModifier(), getIndexTypeQualifier());
854  }
855
856  static void Profile(llvm::FoldingSetNodeID &ID, QualType ET,
857                      ArraySizeModifier SizeMod, unsigned TypeQuals) {
858    ID.AddPointer(ET.getAsOpaquePtr());
859    ID.AddInteger(SizeMod);
860    ID.AddInteger(TypeQuals);
861  }
862
863protected:
864  virtual void EmitImpl(llvm::Serializer& S) const;
865  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
866  friend class Type;
867};
868
869/// VariableArrayType - This class represents C arrays with a specified size
870/// which is not an integer-constant-expression.  For example, 'int s[x+foo()]'.
871/// Since the size expression is an arbitrary expression, we store it as such.
872///
873/// Note: VariableArrayType's aren't uniqued (since the expressions aren't) and
874/// should not be: two lexically equivalent variable array types could mean
875/// different things, for example, these variables do not have the same type
876/// dynamically:
877///
878/// void foo(int x) {
879///   int Y[x];
880///   ++x;
881///   int Z[x];
882/// }
883///
884class VariableArrayType : public ArrayType {
885  /// SizeExpr - An assignment expression. VLA's are only permitted within
886  /// a function block.
887  Stmt *SizeExpr;
888
889  VariableArrayType(QualType et, QualType can, Expr *e,
890                    ArraySizeModifier sm, unsigned tq)
891    : ArrayType(VariableArray, et, can, sm, tq), SizeExpr((Stmt*) e) {}
892  friend class ASTContext;  // ASTContext creates these.
893  virtual void Destroy(ASTContext& C);
894
895public:
896  Expr *getSizeExpr() const {
897    // We use C-style casts instead of cast<> here because we do not wish
898    // to have a dependency of Type.h on Stmt.h/Expr.h.
899    return (Expr*) SizeExpr;
900  }
901
902  virtual void getAsStringInternal(std::string &InnerString) const;
903
904  static bool classof(const Type *T) {
905    return T->getTypeClass() == VariableArray;
906  }
907  static bool classof(const VariableArrayType *) { return true; }
908
909  friend class StmtIteratorBase;
910
911  void Profile(llvm::FoldingSetNodeID &ID) {
912    assert(0 && "Cannnot unique VariableArrayTypes.");
913  }
914
915protected:
916  virtual void EmitImpl(llvm::Serializer& S) const;
917  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
918  friend class Type;
919};
920
921/// DependentSizedArrayType - This type represents an array type in
922/// C++ whose size is a value-dependent expression. For example:
923/// @code
924/// template<typename T, int Size>
925/// class array {
926///   T data[Size];
927/// };
928/// @endcode
929/// For these types, we won't actually know what the array bound is
930/// until template instantiation occurs, at which point this will
931/// become either a ConstantArrayType or a VariableArrayType.
932class DependentSizedArrayType : public ArrayType {
933  /// SizeExpr - An assignment expression that will instantiate to the
934  /// size of the array.
935  Stmt *SizeExpr;
936
937  DependentSizedArrayType(QualType et, QualType can, Expr *e,
938			  ArraySizeModifier sm, unsigned tq)
939    : ArrayType(DependentSizedArray, et, can, sm, tq), SizeExpr((Stmt*) e) {}
940  friend class ASTContext;  // ASTContext creates these.
941  virtual void Destroy(ASTContext& C);
942
943public:
944  Expr *getSizeExpr() const {
945    // We use C-style casts instead of cast<> here because we do not wish
946    // to have a dependency of Type.h on Stmt.h/Expr.h.
947    return (Expr*) SizeExpr;
948  }
949
950  virtual void getAsStringInternal(std::string &InnerString) const;
951
952  static bool classof(const Type *T) {
953    return T->getTypeClass() == DependentSizedArray;
954  }
955  static bool classof(const DependentSizedArrayType *) { return true; }
956
957  friend class StmtIteratorBase;
958
959  void Profile(llvm::FoldingSetNodeID &ID) {
960    assert(0 && "Cannnot unique DependentSizedArrayTypes.");
961  }
962
963protected:
964  virtual void EmitImpl(llvm::Serializer& S) const;
965  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
966  friend class Type;
967};
968
969/// VectorType - GCC generic vector type. This type is created using
970/// __attribute__((vector_size(n)), where "n" specifies the vector size in
971/// bytes. Since the constructor takes the number of vector elements, the
972/// client is responsible for converting the size into the number of elements.
973class VectorType : public Type, public llvm::FoldingSetNode {
974protected:
975  /// ElementType - The element type of the vector.
976  QualType ElementType;
977
978  /// NumElements - The number of elements in the vector.
979  unsigned NumElements;
980
981  VectorType(QualType vecType, unsigned nElements, QualType canonType) :
982    Type(Vector, canonType, vecType->isDependentType()),
983    ElementType(vecType), NumElements(nElements) {}
984  VectorType(TypeClass tc, QualType vecType, unsigned nElements,
985             QualType canonType)
986    : Type(tc, canonType, vecType->isDependentType()), ElementType(vecType),
987      NumElements(nElements) {}
988  friend class ASTContext;  // ASTContext creates these.
989public:
990
991  QualType getElementType() const { return ElementType; }
992  unsigned getNumElements() const { return NumElements; }
993
994  virtual void getAsStringInternal(std::string &InnerString) const;
995
996  void Profile(llvm::FoldingSetNodeID &ID) {
997    Profile(ID, getElementType(), getNumElements(), getTypeClass());
998  }
999  static void Profile(llvm::FoldingSetNodeID &ID, QualType ElementType,
1000                      unsigned NumElements, TypeClass TypeClass) {
1001    ID.AddPointer(ElementType.getAsOpaquePtr());
1002    ID.AddInteger(NumElements);
1003    ID.AddInteger(TypeClass);
1004  }
1005  static bool classof(const Type *T) {
1006    return T->getTypeClass() == Vector || T->getTypeClass() == ExtVector;
1007  }
1008  static bool classof(const VectorType *) { return true; }
1009};
1010
1011/// ExtVectorType - Extended vector type. This type is created using
1012/// __attribute__((ext_vector_type(n)), where "n" is the number of elements.
1013/// Unlike vector_size, ext_vector_type is only allowed on typedef's. This
1014/// class enables syntactic extensions, like Vector Components for accessing
1015/// points, colors, and textures (modeled after OpenGL Shading Language).
1016class ExtVectorType : public VectorType {
1017  ExtVectorType(QualType vecType, unsigned nElements, QualType canonType) :
1018    VectorType(ExtVector, vecType, nElements, canonType) {}
1019  friend class ASTContext;  // ASTContext creates these.
1020public:
1021  static int getPointAccessorIdx(char c) {
1022    switch (c) {
1023    default: return -1;
1024    case 'x': return 0;
1025    case 'y': return 1;
1026    case 'z': return 2;
1027    case 'w': return 3;
1028    }
1029  }
1030  static int getNumericAccessorIdx(char c) {
1031    switch (c) {
1032      default: return -1;
1033      case '0': return 0;
1034      case '1': return 1;
1035      case '2': return 2;
1036      case '3': return 3;
1037      case '4': return 4;
1038      case '5': return 5;
1039      case '6': return 6;
1040      case '7': return 7;
1041      case '8': return 8;
1042      case '9': return 9;
1043      case 'a': return 10;
1044      case 'b': return 11;
1045      case 'c': return 12;
1046      case 'd': return 13;
1047      case 'e': return 14;
1048      case 'f': return 15;
1049    }
1050  }
1051
1052  static int getAccessorIdx(char c) {
1053    if (int idx = getPointAccessorIdx(c)+1) return idx-1;
1054    return getNumericAccessorIdx(c);
1055  }
1056
1057  bool isAccessorWithinNumElements(char c) const {
1058    if (int idx = getAccessorIdx(c)+1)
1059      return unsigned(idx-1) < NumElements;
1060    return false;
1061  }
1062  virtual void getAsStringInternal(std::string &InnerString) const;
1063
1064  static bool classof(const Type *T) {
1065    return T->getTypeClass() == ExtVector;
1066  }
1067  static bool classof(const ExtVectorType *) { return true; }
1068};
1069
1070/// FunctionType - C99 6.7.5.3 - Function Declarators.  This is the common base
1071/// class of FunctionNoProtoType and FunctionProtoType.
1072///
1073class FunctionType : public Type {
1074  /// SubClassData - This field is owned by the subclass, put here to pack
1075  /// tightly with the ivars in Type.
1076  bool SubClassData : 1;
1077
1078  /// TypeQuals - Used only by FunctionProtoType, put here to pack with the
1079  /// other bitfields.
1080  /// The qualifiers are part of FunctionProtoType because...
1081  ///
1082  /// C++ 8.3.5p4: The return type, the parameter type list and the
1083  /// cv-qualifier-seq, [...], are part of the function type.
1084  ///
1085  unsigned TypeQuals : 3;
1086
1087  // The type returned by the function.
1088  QualType ResultType;
1089protected:
1090  FunctionType(TypeClass tc, QualType res, bool SubclassInfo,
1091               unsigned typeQuals, QualType Canonical, bool Dependent)
1092    : Type(tc, Canonical, Dependent),
1093      SubClassData(SubclassInfo), TypeQuals(typeQuals), ResultType(res) {}
1094  bool getSubClassData() const { return SubClassData; }
1095  unsigned getTypeQuals() const { return TypeQuals; }
1096public:
1097
1098  QualType getResultType() const { return ResultType; }
1099
1100
1101  static bool classof(const Type *T) {
1102    return T->getTypeClass() == FunctionNoProto ||
1103           T->getTypeClass() == FunctionProto;
1104  }
1105  static bool classof(const FunctionType *) { return true; }
1106};
1107
1108/// FunctionNoProtoType - Represents a K&R-style 'int foo()' function, which has
1109/// no information available about its arguments.
1110class FunctionNoProtoType : public FunctionType, public llvm::FoldingSetNode {
1111  FunctionNoProtoType(QualType Result, QualType Canonical)
1112    : FunctionType(FunctionNoProto, Result, false, 0, Canonical,
1113                   /*Dependent=*/false) {}
1114  friend class ASTContext;  // ASTContext creates these.
1115public:
1116  // No additional state past what FunctionType provides.
1117
1118  virtual void getAsStringInternal(std::string &InnerString) const;
1119
1120  void Profile(llvm::FoldingSetNodeID &ID) {
1121    Profile(ID, getResultType());
1122  }
1123  static void Profile(llvm::FoldingSetNodeID &ID, QualType ResultType) {
1124    ID.AddPointer(ResultType.getAsOpaquePtr());
1125  }
1126
1127  static bool classof(const Type *T) {
1128    return T->getTypeClass() == FunctionNoProto;
1129  }
1130  static bool classof(const FunctionNoProtoType *) { return true; }
1131
1132protected:
1133  virtual void EmitImpl(llvm::Serializer& S) const;
1134  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
1135  friend class Type;
1136};
1137
1138/// FunctionProtoType - Represents a prototype with argument type info, e.g.
1139/// 'int foo(int)' or 'int foo(void)'.  'void' is represented as having no
1140/// arguments, not as having a single void argument.
1141class FunctionProtoType : public FunctionType, public llvm::FoldingSetNode {
1142  /// hasAnyDependentType - Determine whether there are any dependent
1143  /// types within the arguments passed in.
1144  static bool hasAnyDependentType(const QualType *ArgArray, unsigned numArgs) {
1145    for (unsigned Idx = 0; Idx < numArgs; ++Idx)
1146      if (ArgArray[Idx]->isDependentType())
1147    return true;
1148
1149    return false;
1150  }
1151
1152  FunctionProtoType(QualType Result, const QualType *ArgArray, unsigned numArgs,
1153                    bool isVariadic, unsigned typeQuals, QualType Canonical)
1154    : FunctionType(FunctionProto, Result, isVariadic, typeQuals, Canonical,
1155                   (Result->isDependentType() ||
1156                    hasAnyDependentType(ArgArray, numArgs))),
1157      NumArgs(numArgs) {
1158    // Fill in the trailing argument array.
1159    QualType *ArgInfo = reinterpret_cast<QualType *>(this+1);;
1160    for (unsigned i = 0; i != numArgs; ++i)
1161      ArgInfo[i] = ArgArray[i];
1162  }
1163
1164  /// NumArgs - The number of arguments this function has, not counting '...'.
1165  unsigned NumArgs;
1166
1167  /// ArgInfo - There is an variable size array after the class in memory that
1168  /// holds the argument types.
1169
1170  friend class ASTContext;  // ASTContext creates these.
1171
1172public:
1173  unsigned getNumArgs() const { return NumArgs; }
1174  QualType getArgType(unsigned i) const {
1175    assert(i < NumArgs && "Invalid argument number!");
1176    return arg_type_begin()[i];
1177  }
1178
1179  bool isVariadic() const { return getSubClassData(); }
1180  unsigned getTypeQuals() const { return FunctionType::getTypeQuals(); }
1181
1182  typedef const QualType *arg_type_iterator;
1183  arg_type_iterator arg_type_begin() const {
1184    return reinterpret_cast<const QualType *>(this+1);
1185  }
1186  arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; }
1187
1188  virtual void getAsStringInternal(std::string &InnerString) const;
1189
1190  static bool classof(const Type *T) {
1191    return T->getTypeClass() == FunctionProto;
1192  }
1193  static bool classof(const FunctionProtoType *) { return true; }
1194
1195  void Profile(llvm::FoldingSetNodeID &ID);
1196  static void Profile(llvm::FoldingSetNodeID &ID, QualType Result,
1197                      arg_type_iterator ArgTys, unsigned NumArgs,
1198                      bool isVariadic, unsigned TypeQuals);
1199
1200protected:
1201  virtual void EmitImpl(llvm::Serializer& S) const;
1202  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
1203  friend class Type;
1204};
1205
1206
1207class TypedefType : public Type {
1208  TypedefDecl *Decl;
1209protected:
1210  TypedefType(TypeClass tc, TypedefDecl *D, QualType can)
1211    : Type(tc, can, can->isDependentType()), Decl(D) {
1212    assert(!isa<TypedefType>(can) && "Invalid canonical type");
1213  }
1214  friend class ASTContext;  // ASTContext creates these.
1215public:
1216
1217  TypedefDecl *getDecl() const { return Decl; }
1218
1219  /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
1220  /// potentially looking through *all* consecutive typedefs.  This returns the
1221  /// sum of the type qualifiers, so if you have:
1222  ///   typedef const int A;
1223  ///   typedef volatile A B;
1224  /// looking through the typedefs for B will give you "const volatile A".
1225  QualType LookThroughTypedefs() const;
1226
1227  virtual void getAsStringInternal(std::string &InnerString) const;
1228
1229  static bool classof(const Type *T) { return T->getTypeClass() == Typedef; }
1230  static bool classof(const TypedefType *) { return true; }
1231
1232protected:
1233  virtual void EmitImpl(llvm::Serializer& S) const;
1234  static Type* CreateImpl(ASTContext& Context,llvm::Deserializer& D);
1235  friend class Type;
1236};
1237
1238/// TypeOfExprType (GCC extension).
1239class TypeOfExprType : public Type {
1240  Expr *TOExpr;
1241  TypeOfExprType(Expr *E, QualType can);
1242  friend class ASTContext;  // ASTContext creates these.
1243public:
1244  Expr *getUnderlyingExpr() const { return TOExpr; }
1245
1246  virtual void getAsStringInternal(std::string &InnerString) const;
1247
1248  static bool classof(const Type *T) { return T->getTypeClass() == TypeOfExpr; }
1249  static bool classof(const TypeOfExprType *) { return true; }
1250
1251protected:
1252  virtual void EmitImpl(llvm::Serializer& S) const;
1253  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
1254  friend class Type;
1255};
1256
1257/// TypeOfType (GCC extension).
1258class TypeOfType : public Type {
1259  QualType TOType;
1260  TypeOfType(QualType T, QualType can)
1261    : Type(TypeOf, can, T->isDependentType()), TOType(T) {
1262    assert(!isa<TypedefType>(can) && "Invalid canonical type");
1263  }
1264  friend class ASTContext;  // ASTContext creates these.
1265public:
1266  QualType getUnderlyingType() const { return TOType; }
1267
1268  virtual void getAsStringInternal(std::string &InnerString) const;
1269
1270  static bool classof(const Type *T) { return T->getTypeClass() == TypeOf; }
1271  static bool classof(const TypeOfType *) { return true; }
1272
1273protected:
1274  virtual void EmitImpl(llvm::Serializer& S) const;
1275  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
1276  friend class Type;
1277};
1278
1279class TagType : public Type {
1280  /// Stores the TagDecl associated with this type. The decl will
1281  /// point to the TagDecl that actually defines the entity (or is a
1282  /// definition in progress), if there is such a definition. The
1283  /// single-bit value will be non-zero when this tag is in the
1284  /// process of being defined.
1285  mutable llvm::PointerIntPair<TagDecl *, 1> decl;
1286  friend class ASTContext;
1287  friend class TagDecl;
1288
1289protected:
1290  // FIXME: We'll need the user to pass in information about whether
1291  // this type is dependent or not, because we don't have enough
1292  // information to compute it here.
1293  TagType(TypeClass TC, TagDecl *D, QualType can)
1294    : Type(TC, can, /*Dependent=*/false), decl(D, 0) {}
1295
1296public:
1297  TagDecl *getDecl() const { return decl.getPointer(); }
1298
1299  /// @brief Determines whether this type is in the process of being
1300  /// defined.
1301  bool isBeingDefined() const { return decl.getInt(); }
1302  void setBeingDefined(bool Def) { decl.setInt(Def? 1 : 0); }
1303
1304  virtual void getAsStringInternal(std::string &InnerString) const;
1305
1306  static bool classof(const Type *T) {
1307    return T->getTypeClass() >= TagFirst && T->getTypeClass() <= TagLast;
1308  }
1309  static bool classof(const TagType *) { return true; }
1310  static bool classof(const RecordType *) { return true; }
1311  static bool classof(const CXXRecordType *) { return true; }
1312  static bool classof(const EnumType *) { return true; }
1313
1314protected:
1315  virtual void EmitImpl(llvm::Serializer& S) const;
1316  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
1317  friend class Type;
1318};
1319
1320/// RecordType - This is a helper class that allows the use of isa/cast/dyncast
1321/// to detect TagType objects of structs/unions/classes.
1322class RecordType : public TagType {
1323protected:
1324  explicit RecordType(RecordDecl *D)
1325    : TagType(Record, reinterpret_cast<TagDecl*>(D), QualType()) { }
1326  explicit RecordType(TypeClass TC, RecordDecl *D)
1327    : TagType(TC, reinterpret_cast<TagDecl*>(D), QualType()) { }
1328  friend class ASTContext;   // ASTContext creates these.
1329public:
1330
1331  RecordDecl *getDecl() const {
1332    return reinterpret_cast<RecordDecl*>(TagType::getDecl());
1333  }
1334
1335  // FIXME: This predicate is a helper to QualType/Type. It needs to
1336  // recursively check all fields for const-ness. If any field is declared
1337  // const, it needs to return false.
1338  bool hasConstFields() const { return false; }
1339
1340  // FIXME: RecordType needs to check when it is created that all fields are in
1341  // the same address space, and return that.
1342  unsigned getAddressSpace() const { return 0; }
1343
1344  static bool classof(const TagType *T);
1345  static bool classof(const Type *T) {
1346    return isa<TagType>(T) && classof(cast<TagType>(T));
1347  }
1348  static bool classof(const RecordType *) { return true; }
1349};
1350
1351/// CXXRecordType - This is a helper class that allows the use of
1352/// isa/cast/dyncast to detect TagType objects of C++ structs/unions/classes.
1353class CXXRecordType : public RecordType {
1354  explicit CXXRecordType(CXXRecordDecl *D)
1355    : RecordType(CXXRecord, reinterpret_cast<RecordDecl*>(D)) { }
1356  friend class ASTContext;   // ASTContext creates these.
1357public:
1358
1359  CXXRecordDecl *getDecl() const {
1360    return reinterpret_cast<CXXRecordDecl*>(RecordType::getDecl());
1361  }
1362
1363  static bool classof(const TagType *T);
1364  static bool classof(const Type *T) {
1365    return isa<TagType>(T) && classof(cast<TagType>(T));
1366  }
1367  static bool classof(const CXXRecordType *) { return true; }
1368};
1369
1370/// EnumType - This is a helper class that allows the use of isa/cast/dyncast
1371/// to detect TagType objects of enums.
1372class EnumType : public TagType {
1373  explicit EnumType(EnumDecl *D)
1374    : TagType(Enum, reinterpret_cast<TagDecl*>(D), QualType()) { }
1375  friend class ASTContext;   // ASTContext creates these.
1376public:
1377
1378  EnumDecl *getDecl() const {
1379    return reinterpret_cast<EnumDecl*>(TagType::getDecl());
1380  }
1381
1382  static bool classof(const TagType *T);
1383  static bool classof(const Type *T) {
1384    return isa<TagType>(T) && classof(cast<TagType>(T));
1385  }
1386  static bool classof(const EnumType *) { return true; }
1387};
1388
1389class TemplateTypeParmType : public Type, public llvm::FoldingSetNode {
1390  unsigned Depth : 16;
1391  unsigned Index : 16;
1392  IdentifierInfo *Name;
1393
1394  TemplateTypeParmType(unsigned D, unsigned I, IdentifierInfo *N,
1395                       QualType Canon)
1396    : Type(TemplateTypeParm, Canon, /*Dependent=*/true),
1397      Depth(D), Index(I), Name(N) { }
1398
1399  TemplateTypeParmType(unsigned D, unsigned I)
1400    : Type(TemplateTypeParm, QualType(this, 0), /*Dependent=*/true),
1401      Depth(D), Index(I), Name(0) { }
1402
1403  friend class ASTContext;  // ASTContext creates these
1404
1405public:
1406  unsigned getDepth() const { return Depth; }
1407  unsigned getIndex() const { return Index; }
1408  IdentifierInfo *getName() const { return Name; }
1409
1410  virtual void getAsStringInternal(std::string &InnerString) const;
1411
1412  void Profile(llvm::FoldingSetNodeID &ID) {
1413    Profile(ID, Depth, Index, Name);
1414  }
1415
1416  static void Profile(llvm::FoldingSetNodeID &ID, unsigned Depth,
1417                      unsigned Index, IdentifierInfo *Name) {
1418    ID.AddInteger(Depth);
1419    ID.AddInteger(Index);
1420    ID.AddPointer(Name);
1421  }
1422
1423  static bool classof(const Type *T) {
1424    return T->getTypeClass() == TemplateTypeParm;
1425  }
1426  static bool classof(const TemplateTypeParmType *T) { return true; }
1427
1428protected:
1429  virtual void EmitImpl(llvm::Serializer& S) const;
1430  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
1431  friend class Type;
1432};
1433
1434/// \brief Represents the type of a class template specialization as
1435/// written in the source code.
1436///
1437/// Class template specialization types represent the syntactic form
1438/// of a template-id that refers to a type, e.g., @c vector<int>. All
1439/// class template specialization types are syntactic sugar, whose
1440/// canonical type will point to some other type node that represents
1441/// the instantiation or class template specialization. For example, a
1442/// class template specialization type of @c vector<int> will refer to
1443/// a tag type for the instantiation
1444/// @c std::vector<int, std::allocator<int>>.
1445class ClassTemplateSpecializationType
1446  : public Type, public llvm::FoldingSetNode {
1447
1448  // FIXME: Do we want templates to have a representation in the type
1449  // system? It will probably help with dependent templates and
1450  // possibly with template-names preceded by a nested-name-specifier.
1451  TemplateDecl *Template;
1452
1453  unsigned NumArgs;
1454
1455  ClassTemplateSpecializationType(TemplateDecl *T, unsigned NumArgs,
1456                                  uintptr_t *Args, bool *ArgIsType,
1457                                  QualType Canon);
1458
1459  /// \brief Retrieve the number of packed words that precede the
1460  /// actual arguments.
1461  ///
1462  /// The flags that specify whether each argument is a type or an
1463  /// expression are packed into the
1464  /// ClassTemplateSpecializationType. This routine computes the
1465  /// number of pointer-sized words we need to store this information,
1466  /// based on the number of template arguments
1467  static unsigned getNumPackedWords(unsigned NumArgs) {
1468    const unsigned BitsPerWord = sizeof(uintptr_t) * 8;
1469    return NumArgs / BitsPerWord + (NumArgs % BitsPerWord > 0);
1470  }
1471
1472  /// \brief Pack the given boolean values into words.
1473  static void
1474  packBooleanValues(unsigned NumArgs, bool *Values, uintptr_t *Words);
1475
1476  virtual void Destroy(ASTContext& C);
1477
1478  friend class ASTContext;  // ASTContext creates these
1479
1480public:
1481  /// \brief Retrieve the template that we are specializing.
1482  TemplateDecl *getTemplate() const { return Template; }
1483
1484  /// \briefe Retrieve the number of template arguments.
1485  unsigned getNumArgs() const { return NumArgs; }
1486
1487  /// \brief Retrieve a specific template argument as a type.
1488  /// \precondition @c isArgType(Arg)
1489  QualType getArgAsType(unsigned Arg) const {
1490    assert(isArgType(Arg) && "Argument is not a type");
1491    return QualType::getFromOpaquePtr(
1492                          reinterpret_cast<void *>(getArgAsOpaqueValue(Arg)));
1493  }
1494
1495  /// \brief Retrieve a specific template argument as an expression.
1496  /// \precondition @c !isArgType(Arg)
1497  Expr *getArgAsExpr(unsigned Arg) const {
1498    assert(!isArgType(Arg) && "Argument is not an expression");
1499    return reinterpret_cast<Expr *>(getArgAsOpaqueValue(Arg));
1500  }
1501
1502  /// \brief Retrieve the specified template argument as an opaque value.
1503  uintptr_t getArgAsOpaqueValue(unsigned Arg) const;
1504
1505  /// \brief Determine whether the given template argument is a type.
1506  bool isArgType(unsigned Arg) const;
1507
1508  virtual void getAsStringInternal(std::string &InnerString) const;
1509
1510  void Profile(llvm::FoldingSetNodeID &ID) {
1511    // Add the template
1512    ID.AddPointer(Template);
1513
1514    // Add the packed words describing what kind of template arguments
1515    // we have.
1516    // FIXME: Would like to be smarter about the profile of expressions,
1517    // so that we can combine expression nodes more effectively.
1518    uintptr_t *Data = reinterpret_cast<uintptr_t *>(this + 1);
1519    for (unsigned Packed = 0, NumPacked = getNumPackedWords(NumArgs);
1520         Packed != NumPacked; ++Packed)
1521      ID.AddInteger(Data[Packed]);
1522
1523    // Add the template arguments themselves.
1524    for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
1525      ID.AddInteger(getArgAsOpaqueValue(Arg));
1526  }
1527
1528  static void Profile(llvm::FoldingSetNodeID &ID, TemplateDecl *T,
1529                      unsigned NumArgs, uintptr_t *Args, bool *ArgIsType) {
1530    // Add the template
1531    ID.AddPointer(T);
1532
1533    // Add the packed words describing what kind of template arguments
1534    // we have.
1535    unsigned NumPackedWords = getNumPackedWords(NumArgs);
1536    unsigned NumPackedBytes = NumPackedWords * sizeof(uintptr_t);
1537    uintptr_t *PackedWords
1538      = reinterpret_cast<uintptr_t *>(alloca(NumPackedBytes));
1539    packBooleanValues(NumArgs, ArgIsType, PackedWords);
1540    for (unsigned Packed = 0; Packed != NumPackedWords; ++Packed)
1541      ID.AddInteger(PackedWords[Packed]);
1542
1543    // Add the template arguments themselves.
1544    for (unsigned Arg = 0; Arg < NumArgs; ++Arg)
1545      ID.AddInteger(Args[Arg]);
1546  }
1547
1548  static bool classof(const Type *T) {
1549    return T->getTypeClass() == ClassTemplateSpecialization;
1550  }
1551  static bool classof(const ClassTemplateSpecializationType *T) { return true; }
1552
1553protected:
1554  virtual void EmitImpl(llvm::Serializer& S) const;
1555  static Type* CreateImpl(ASTContext& Context, llvm::Deserializer& D);
1556  friend class Type;
1557};
1558
1559/// ObjCInterfaceType - Interfaces are the core concept in Objective-C for
1560/// object oriented design.  They basically correspond to C++ classes.  There
1561/// are two kinds of interface types, normal interfaces like "NSString" and
1562/// qualified interfaces, which are qualified with a protocol list like
1563/// "NSString<NSCopyable, NSAmazing>".  Qualified interface types are instances
1564/// of ObjCQualifiedInterfaceType, which is a subclass of ObjCInterfaceType.
1565class ObjCInterfaceType : public Type {
1566  ObjCInterfaceDecl *Decl;
1567protected:
1568  ObjCInterfaceType(TypeClass tc, ObjCInterfaceDecl *D) :
1569    Type(tc, QualType(), /*Dependent=*/false), Decl(D) { }
1570  friend class ASTContext;  // ASTContext creates these.
1571public:
1572
1573  ObjCInterfaceDecl *getDecl() const { return Decl; }
1574
1575  /// qual_iterator and friends: this provides access to the (potentially empty)
1576  /// list of protocols qualifying this interface.  If this is an instance of
1577  /// ObjCQualifiedInterfaceType it returns the list, otherwise it returns an
1578  /// empty list if there are no qualifying protocols.
1579  typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator;
1580  inline qual_iterator qual_begin() const;
1581  inline qual_iterator qual_end() const;
1582  bool qual_empty() const { return getTypeClass() != ObjCQualifiedInterface; }
1583
1584  /// getNumProtocols - Return the number of qualifying protocols in this
1585  /// interface type, or 0 if there are none.
1586  inline unsigned getNumProtocols() const;
1587
1588  /// getProtocol - Return the specified qualifying protocol.
1589  inline ObjCProtocolDecl *getProtocol(unsigned i) const;
1590
1591
1592  virtual void getAsStringInternal(std::string &InnerString) const;
1593  static bool classof(const Type *T) {
1594    return T->getTypeClass() == ObjCInterface ||
1595           T->getTypeClass() == ObjCQualifiedInterface;
1596  }
1597  static bool classof(const ObjCInterfaceType *) { return true; }
1598};
1599
1600/// ObjCQualifiedInterfaceType - This class represents interface types
1601/// conforming to a list of protocols, such as INTF<Proto1, Proto2, Proto1>.
1602///
1603/// Duplicate protocols are removed and protocol list is canonicalized to be in
1604/// alphabetical order.
1605class ObjCQualifiedInterfaceType : public ObjCInterfaceType,
1606                                   public llvm::FoldingSetNode {
1607
1608  // List of protocols for this protocol conforming object type
1609  // List is sorted on protocol name. No protocol is enterred more than once.
1610  llvm::SmallVector<ObjCProtocolDecl*, 4> Protocols;
1611
1612  ObjCQualifiedInterfaceType(ObjCInterfaceDecl *D,
1613                             ObjCProtocolDecl **Protos, unsigned NumP) :
1614    ObjCInterfaceType(ObjCQualifiedInterface, D),
1615    Protocols(Protos, Protos+NumP) { }
1616  friend class ASTContext;  // ASTContext creates these.
1617public:
1618
1619  ObjCProtocolDecl *getProtocol(unsigned i) const {
1620    return Protocols[i];
1621  }
1622  unsigned getNumProtocols() const {
1623    return Protocols.size();
1624  }
1625
1626  qual_iterator qual_begin() const { return Protocols.begin(); }
1627  qual_iterator qual_end() const   { return Protocols.end(); }
1628
1629  virtual void getAsStringInternal(std::string &InnerString) const;
1630
1631  void Profile(llvm::FoldingSetNodeID &ID);
1632  static void Profile(llvm::FoldingSetNodeID &ID,
1633                      const ObjCInterfaceDecl *Decl,
1634                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
1635
1636  static bool classof(const Type *T) {
1637    return T->getTypeClass() == ObjCQualifiedInterface;
1638  }
1639  static bool classof(const ObjCQualifiedInterfaceType *) { return true; }
1640};
1641
1642inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_begin() const {
1643  if (const ObjCQualifiedInterfaceType *QIT =
1644         dyn_cast<ObjCQualifiedInterfaceType>(this))
1645    return QIT->qual_begin();
1646  return 0;
1647}
1648inline ObjCInterfaceType::qual_iterator ObjCInterfaceType::qual_end() const {
1649  if (const ObjCQualifiedInterfaceType *QIT =
1650         dyn_cast<ObjCQualifiedInterfaceType>(this))
1651    return QIT->qual_end();
1652  return 0;
1653}
1654
1655/// getNumProtocols - Return the number of qualifying protocols in this
1656/// interface type, or 0 if there are none.
1657inline unsigned ObjCInterfaceType::getNumProtocols() const {
1658  if (const ObjCQualifiedInterfaceType *QIT =
1659        dyn_cast<ObjCQualifiedInterfaceType>(this))
1660    return QIT->getNumProtocols();
1661  return 0;
1662}
1663
1664/// getProtocol - Return the specified qualifying protocol.
1665inline ObjCProtocolDecl *ObjCInterfaceType::getProtocol(unsigned i) const {
1666  return cast<ObjCQualifiedInterfaceType>(this)->getProtocol(i);
1667}
1668
1669
1670
1671/// ObjCQualifiedIdType - to represent id<protocol-list>.
1672///
1673/// Duplicate protocols are removed and protocol list is canonicalized to be in
1674/// alphabetical order.
1675class ObjCQualifiedIdType : public Type,
1676                            public llvm::FoldingSetNode {
1677  // List of protocols for this protocol conforming 'id' type
1678  // List is sorted on protocol name. No protocol is enterred more than once.
1679  llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
1680
1681  ObjCQualifiedIdType(ObjCProtocolDecl **Protos, unsigned NumP)
1682    : Type(ObjCQualifiedId, QualType()/*these are always canonical*/,
1683           /*Dependent=*/false),
1684  Protocols(Protos, Protos+NumP) { }
1685  friend class ASTContext;  // ASTContext creates these.
1686public:
1687
1688  ObjCProtocolDecl *getProtocols(unsigned i) const {
1689    return Protocols[i];
1690  }
1691  unsigned getNumProtocols() const {
1692    return Protocols.size();
1693  }
1694  ObjCProtocolDecl **getReferencedProtocols() {
1695    return &Protocols[0];
1696  }
1697
1698  typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator;
1699  qual_iterator qual_begin() const { return Protocols.begin(); }
1700  qual_iterator qual_end() const   { return Protocols.end(); }
1701
1702  virtual void getAsStringInternal(std::string &InnerString) const;
1703
1704  void Profile(llvm::FoldingSetNodeID &ID);
1705  static void Profile(llvm::FoldingSetNodeID &ID,
1706                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
1707
1708  static bool classof(const Type *T) {
1709    return T->getTypeClass() == ObjCQualifiedId;
1710  }
1711  static bool classof(const ObjCQualifiedIdType *) { return true; }
1712
1713};
1714
1715/// ObjCQualifiedClassType - to represent Class<protocol-list>.
1716///
1717/// Duplicate protocols are removed and protocol list is canonicalized to be in
1718/// alphabetical order.
1719class ObjCQualifiedClassType : public Type,
1720                               public llvm::FoldingSetNode {
1721  // List of protocols for this protocol conforming 'id' type
1722  // List is sorted on protocol name. No protocol is enterred more than once.
1723  llvm::SmallVector<ObjCProtocolDecl*, 8> Protocols;
1724
1725  ObjCQualifiedClassType(ObjCProtocolDecl **Protos, unsigned NumP)
1726    : Type(ObjCQualifiedClass, QualType()/*these are always canonical*/,
1727           /*Dependent=*/false),
1728  Protocols(Protos, Protos+NumP) { }
1729  friend class ASTContext;  // ASTContext creates these.
1730public:
1731
1732  ObjCProtocolDecl *getProtocols(unsigned i) const {
1733    return Protocols[i];
1734  }
1735  unsigned getNumProtocols() const {
1736    return Protocols.size();
1737  }
1738  ObjCProtocolDecl **getReferencedProtocols() {
1739    return &Protocols[0];
1740  }
1741
1742  typedef llvm::SmallVector<ObjCProtocolDecl*, 8>::const_iterator qual_iterator;
1743  qual_iterator qual_begin() const { return Protocols.begin(); }
1744  qual_iterator qual_end() const   { return Protocols.end(); }
1745
1746  virtual void getAsStringInternal(std::string &InnerString) const;
1747
1748  void Profile(llvm::FoldingSetNodeID &ID);
1749  static void Profile(llvm::FoldingSetNodeID &ID,
1750                      ObjCProtocolDecl **protocols, unsigned NumProtocols);
1751
1752  static bool classof(const Type *T) {
1753    return T->getTypeClass() == ObjCQualifiedClass;
1754  }
1755  static bool classof(const ObjCQualifiedClassType *) { return true; }
1756
1757};
1758
1759// Inline function definitions.
1760
1761/// getUnqualifiedType - Return the type without any qualifiers.
1762inline QualType QualType::getUnqualifiedType() const {
1763  Type *TP = getTypePtr();
1764  if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(TP))
1765    TP = EXTQT->getBaseType();
1766  return QualType(TP, 0);
1767}
1768
1769/// getAddressSpace - Return the address space of this type.
1770inline unsigned QualType::getAddressSpace() const {
1771  QualType CT = getTypePtr()->getCanonicalTypeInternal();
1772  if (const ArrayType *AT = dyn_cast<ArrayType>(CT))
1773    return AT->getElementType().getAddressSpace();
1774  if (const RecordType *RT = dyn_cast<RecordType>(CT))
1775    return RT->getAddressSpace();
1776  if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CT))
1777    return EXTQT->getAddressSpace();
1778  return 0;
1779}
1780
1781/// getObjCGCAttr - Return the gc attribute of this type.
1782inline QualType::GCAttrTypes QualType::getObjCGCAttr() const {
1783  QualType CT = getTypePtr()->getCanonicalTypeInternal();
1784  if (const ArrayType *AT = dyn_cast<ArrayType>(CT))
1785      return AT->getElementType().getObjCGCAttr();
1786  if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(CT))
1787    return EXTQT->getObjCGCAttr();
1788  if (const PointerType *PT = CT->getAsPointerType())
1789    return PT->getPointeeType().getObjCGCAttr();
1790  return GCNone;
1791}
1792
1793/// isMoreQualifiedThan - Determine whether this type is more
1794/// qualified than the Other type. For example, "const volatile int"
1795/// is more qualified than "const int", "volatile int", and
1796/// "int". However, it is not more qualified than "const volatile
1797/// int".
1798inline bool QualType::isMoreQualifiedThan(QualType Other) const {
1799  // FIXME: Handle address spaces
1800  unsigned MyQuals = this->getCVRQualifiers();
1801  unsigned OtherQuals = Other.getCVRQualifiers();
1802  assert(this->getAddressSpace() == 0 && "Address space not checked");
1803  assert(Other.getAddressSpace() == 0 && "Address space not checked");
1804  return MyQuals != OtherQuals && (MyQuals | OtherQuals) == MyQuals;
1805}
1806
1807/// isAtLeastAsQualifiedAs - Determine whether this type is at last
1808/// as qualified as the Other type. For example, "const volatile
1809/// int" is at least as qualified as "const int", "volatile int",
1810/// "int", and "const volatile int".
1811inline bool QualType::isAtLeastAsQualifiedAs(QualType Other) const {
1812  // FIXME: Handle address spaces
1813  unsigned MyQuals = this->getCVRQualifiers();
1814  unsigned OtherQuals = Other.getCVRQualifiers();
1815  assert(this->getAddressSpace() == 0 && "Address space not checked");
1816  assert(Other.getAddressSpace() == 0 && "Address space not checked");
1817  return (MyQuals | OtherQuals) == MyQuals;
1818}
1819
1820/// getNonReferenceType - If Type is a reference type (e.g., const
1821/// int&), returns the type that the reference refers to ("const
1822/// int"). Otherwise, returns the type itself. This routine is used
1823/// throughout Sema to implement C++ 5p6:
1824///
1825///   If an expression initially has the type "reference to T" (8.3.2,
1826///   8.5.3), the type is adjusted to "T" prior to any further
1827///   analysis, the expression designates the object or function
1828///   denoted by the reference, and the expression is an lvalue.
1829inline QualType QualType::getNonReferenceType() const {
1830  if (const ReferenceType *RefType = (*this)->getAsReferenceType())
1831    return RefType->getPointeeType();
1832  else
1833    return *this;
1834}
1835
1836inline const TypedefType* Type::getAsTypedefType() const {
1837  return dyn_cast<TypedefType>(this);
1838}
1839inline const ObjCInterfaceType *Type::getAsPointerToObjCInterfaceType() const {
1840  if (const PointerType *PT = getAsPointerType())
1841    return PT->getPointeeType()->getAsObjCInterfaceType();
1842  return 0;
1843}
1844
1845// NOTE: All of these methods use "getUnqualifiedType" to strip off address
1846// space qualifiers if present.
1847inline bool Type::isFunctionType() const {
1848  return isa<FunctionType>(CanonicalType.getUnqualifiedType());
1849}
1850inline bool Type::isPointerType() const {
1851  return isa<PointerType>(CanonicalType.getUnqualifiedType());
1852}
1853inline bool Type::isBlockPointerType() const {
1854    return isa<BlockPointerType>(CanonicalType);
1855}
1856inline bool Type::isReferenceType() const {
1857  return isa<ReferenceType>(CanonicalType.getUnqualifiedType());
1858}
1859inline bool Type::isFunctionPointerType() const {
1860  if (const PointerType* T = getAsPointerType())
1861    return T->getPointeeType()->isFunctionType();
1862  else
1863    return false;
1864}
1865inline bool Type::isMemberPointerType() const {
1866  return isa<MemberPointerType>(CanonicalType.getUnqualifiedType());
1867}
1868inline bool Type::isMemberFunctionPointerType() const {
1869  if (const MemberPointerType* T = getAsMemberPointerType())
1870    return T->getPointeeType()->isFunctionType();
1871  else
1872    return false;
1873}
1874inline bool Type::isArrayType() const {
1875  return isa<ArrayType>(CanonicalType.getUnqualifiedType());
1876}
1877inline bool Type::isConstantArrayType() const {
1878  return isa<ConstantArrayType>(CanonicalType.getUnqualifiedType());
1879}
1880inline bool Type::isIncompleteArrayType() const {
1881  return isa<IncompleteArrayType>(CanonicalType.getUnqualifiedType());
1882}
1883inline bool Type::isVariableArrayType() const {
1884  return isa<VariableArrayType>(CanonicalType.getUnqualifiedType());
1885}
1886inline bool Type::isDependentSizedArrayType() const {
1887  return isa<DependentSizedArrayType>(CanonicalType.getUnqualifiedType());
1888}
1889inline bool Type::isRecordType() const {
1890  return isa<RecordType>(CanonicalType.getUnqualifiedType());
1891}
1892inline bool Type::isAnyComplexType() const {
1893  return isa<ComplexType>(CanonicalType.getUnqualifiedType());
1894}
1895inline bool Type::isVectorType() const {
1896  return isa<VectorType>(CanonicalType.getUnqualifiedType());
1897}
1898inline bool Type::isExtVectorType() const {
1899  return isa<ExtVectorType>(CanonicalType.getUnqualifiedType());
1900}
1901inline bool Type::isObjCInterfaceType() const {
1902  return isa<ObjCInterfaceType>(CanonicalType.getUnqualifiedType());
1903}
1904inline bool Type::isObjCQualifiedInterfaceType() const {
1905  return isa<ObjCQualifiedInterfaceType>(CanonicalType.getUnqualifiedType());
1906}
1907inline bool Type::isObjCQualifiedIdType() const {
1908  return isa<ObjCQualifiedIdType>(CanonicalType.getUnqualifiedType());
1909}
1910inline bool Type::isTemplateTypeParmType() const {
1911  return isa<TemplateTypeParmType>(CanonicalType.getUnqualifiedType());
1912}
1913
1914inline bool Type::isSpecificBuiltinType(unsigned K) const {
1915  if (const BuiltinType *BT = getAsBuiltinType())
1916    if (BT->getKind() == (BuiltinType::Kind) K)
1917      return true;
1918  return false;
1919}
1920
1921inline bool Type::isOverloadType() const {
1922  return isSpecificBuiltinType(BuiltinType::Overload);
1923}
1924
1925inline bool Type::hasPointerRepresentation() const {
1926  return (isPointerType() || isReferenceType() || isBlockPointerType() ||
1927          isObjCInterfaceType() || isObjCQualifiedIdType() ||
1928          isObjCQualifiedInterfaceType());
1929}
1930
1931/// Insertion operator for diagnostics.  This allows sending QualType's into a
1932/// diagnostic with <<.
1933inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
1934                                           QualType T) {
1935  DB.AddTaggedVal(reinterpret_cast<intptr_t>(T.getAsOpaquePtr()),
1936                  Diagnostic::ak_qualtype);
1937  return DB;
1938}
1939
1940}  // end namespace clang
1941
1942#endif
1943