ASTContext.cpp revision a36a61f218b9f7a97f2c0f511e0b29eb42e8f78b
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the ASTContext interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/ASTContext.h"
155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/AST/Decl.h"
16980e508ca70d6de75d2abfd96b4681fc98bb2698Steve Naroff#include "clang/AST/DeclObjC.h"
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/TargetInfo.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/SmallVector.h"
1985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson#include "llvm/ADT/StringExtras.h"
207192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek#include "llvm/Bitcode/Serialize.h"
217192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek#include "llvm/Bitcode/Deserialize.h"
2285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerenum FloatingRank {
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatRank, DoubleRank, LongDoubleRank
275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer};
285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerASTContext::~ASTContext() {
305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Deallocate all the types.
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (!Types.empty()) {
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      // Destroy the object, but don't call delete.  These are malloc'd.
345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      FT->~FunctionTypeProto();
355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      free(FT);
365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    } else {
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      delete Types.back();
385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Types.pop_back();
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::PrintStats() const {
445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "*** AST Context Stats:\n");
455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "  %d types total.\n", (int)Types.size());
465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
476d87fc66ca0e156c4144b692c9e71700b8c18d17Chris Lattner  unsigned NumVector = 0, NumComplex = 0;
485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
51a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
52a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  unsigned NumObjCQualifiedIds = 0;
535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0, e = Types.size(); i != e; ++i) {
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    Type *T = Types[i];
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (isa<BuiltinType>(T))
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumBuiltin;
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (isa<PointerType>(T))
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumPointer;
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (isa<ReferenceType>(T))
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumReference;
626d87fc66ca0e156c4144b692c9e71700b8c18d17Chris Lattner    else if (isa<ComplexType>(T))
636d87fc66ca0e156c4144b692c9e71700b8c18d17Chris Lattner      ++NumComplex;
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (isa<ArrayType>(T))
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumArray;
666d87fc66ca0e156c4144b692c9e71700b8c18d17Chris Lattner    else if (isa<VectorType>(T))
676d87fc66ca0e156c4144b692c9e71700b8c18d17Chris Lattner      ++NumVector;
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (isa<FunctionTypeNoProto>(T))
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumFunctionNP;
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (isa<FunctionTypeProto>(T))
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumFunctionP;
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (isa<TypedefType>(T))
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumTypeName;
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    else if (TagType *TT = dyn_cast<TagType>(T)) {
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      ++NumTagged;
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      switch (TT->getDecl()->getKind()) {
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      default: assert(0 && "Unknown tagged type!");
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      case Decl::Struct: ++NumTagStruct; break;
795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      case Decl::Union:  ++NumTagUnion; break;
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      case Decl::Class:  ++NumTagClass; break;
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      case Decl::Enum:   ++NumTagEnum; break;
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      }
83a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    } else if (isa<ObjCInterfaceType>(T))
84a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ++NumObjCInterfaces;
85a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    else if (isa<ObjCQualifiedInterfaceType>(T))
86a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ++NumObjCQualifiedInterfaces;
87a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    else if (isa<ObjCQualifiedIdType>(T))
88a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ++NumObjCQualifiedIds;
893f128ad2691d299b96663da85a9e069c4081ea7cSteve Naroff    else {
90beb663677aa20db59da4e5ab7d535804ec6f963cChris Lattner      QualType(T, 0).dump();
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      assert(0 && "Unknown type!");
925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "    %d builtin types\n", NumBuiltin);
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "    %d pointer types\n", NumPointer);
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "    %d reference types\n", NumReference);
986d87fc66ca0e156c4144b692c9e71700b8c18d17Chris Lattner  fprintf(stderr, "    %d complex types\n", NumComplex);
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "    %d array types\n", NumArray);
1006d87fc66ca0e156c4144b692c9e71700b8c18d17Chris Lattner  fprintf(stderr, "    %d vector types\n", NumVector);
1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "    %d function types with proto\n", NumFunctionP);
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "    %d function types with no proto\n", NumFunctionNP);
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "    %d typename (typedef) types\n", NumTypeName);
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "    %d tagged types\n", NumTagged);
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "      %d struct types\n", NumTagStruct);
1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "      %d union types\n", NumTagUnion);
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "      %d class types\n", NumTagClass);
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "      %d enum types\n", NumTagEnum);
109a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  fprintf(stderr, "    %d interface types\n", NumObjCInterfaces);
110beb663677aa20db59da4e5ab7d535804ec6f963cChris Lattner  fprintf(stderr, "    %d protocol qualified interface types\n",
111a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek          NumObjCQualifiedInterfaces);
112c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  fprintf(stderr, "    %d protocol qualified id types\n",
113a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek          NumObjCQualifiedIds);
1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
1155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
1166d87fc66ca0e156c4144b692c9e71700b8c18d17Chris Lattner    NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    NumFunctionP*sizeof(FunctionTypeProto)+
1185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    NumFunctionNP*sizeof(FunctionTypeNoProto)+
1195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
1205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencervoid ASTContext::InitBuiltinTypes() {
1285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  assert(VoidTy.isNull() && "Context reinitialized?");
1295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p19.
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(VoidTy,              BuiltinType::Void);
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p2.
1345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(BoolTy,              BuiltinType::Bool);
1355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p3.
13698be4943e8dc4f3905629a7102668960873cf863Chris Lattner  if (Target.isCharSigned())
1375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_S);
1385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  else
1395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    InitBuiltinType(CharTy,            BuiltinType::Char_U);
1405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p4.
1415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(SignedCharTy,        BuiltinType::SChar);
1425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(ShortTy,             BuiltinType::Short);
1435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(IntTy,               BuiltinType::Int);
1445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongTy,              BuiltinType::Long);
1455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongLongTy,          BuiltinType::LongLong);
1465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p6.
1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedCharTy,      BuiltinType::UChar);
1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedShortTy,     BuiltinType::UShort);
1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedIntTy,       BuiltinType::UInt);
1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongTy,      BuiltinType::ULong);
1525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(UnsignedLongLongTy,  BuiltinType::ULongLong);
1535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p10.
1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(FloatTy,             BuiltinType::Float);
1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(DoubleTy,            BuiltinType::Double);
1575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  InitBuiltinType(LongDoubleTy,        BuiltinType::LongDouble);
1585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // C99 6.2.5p11.
1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FloatComplexTy      = getComplexType(FloatTy);
1615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  DoubleComplexTy     = getComplexType(DoubleTy);
1625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  LongDoubleComplexTy = getComplexType(LongDoubleTy);
1637e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
1647e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  BuiltinVaListType = QualType();
165a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCIdType = QualType();
1667e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  IdStructType = 0;
167a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCClassType = QualType();
1688baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  ClassStructType = 0;
1698baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
170a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = QualType();
17133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
17233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // void * type
17333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  VoidPtrTy = getPointerType(VoidTy);
1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
176464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
177464175bba1318bef7905122e9fda20cff926df78Chris Lattner//                         Type Sizing and Analysis
178464175bba1318bef7905122e9fda20cff926df78Chris Lattner//===----------------------------------------------------------------------===//
179a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
180a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// getTypeSize - Return the size of the specified type, in bits.  This method
181a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner/// does not work on incomplete types.
182d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattnerstd::pair<uint64_t, unsigned>
18398be4943e8dc4f3905629a7102668960873cf863Chris LattnerASTContext::getTypeInfo(QualType T) {
184f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  T = getCanonicalType(T);
1859e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  uint64_t Width;
186d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner  unsigned Align;
187a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  switch (T->getTypeClass()) {
188030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::TypeName: assert(0 && "Not a canonical type!");
1895d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionNoProto:
1905d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::FunctionProto:
191692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner  default:
192b1c2df99ba67ec6c29ac7dceaa4eb2c8cda4a017Chris Lattner    assert(0 && "Incomplete types have no size!");
193fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::VariableArray:
194fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff    assert(0 && "VLAs not implemented yet!");
195fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  case Type::ConstantArray: {
196fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff    ConstantArrayType *CAT = cast<ConstantArrayType>(T);
197030d8846c7e520330007087e949f621989876e3aChris Lattner
19898be4943e8dc4f3905629a7102668960873cf863Chris Lattner    std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
1999e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*CAT->getSize().getZExtValue();
200030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
201030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
2025c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  }
2035c09a02a5db85e08a432b6eeced9aa656349710dChristopher Lamb  case Type::OCUVector:
204030d8846c7e520330007087e949f621989876e3aChris Lattner  case Type::Vector: {
205030d8846c7e520330007087e949f621989876e3aChris Lattner    std::pair<uint64_t, unsigned> EltInfo =
20698be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<VectorType>(T)->getElementType());
2079e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
208030d8846c7e520330007087e949f621989876e3aChris Lattner    // FIXME: Vector alignment is not the alignment of its elements.
209030d8846c7e520330007087e949f621989876e3aChris Lattner    Align = EltInfo.second;
210030d8846c7e520330007087e949f621989876e3aChris Lattner    break;
211030d8846c7e520330007087e949f621989876e3aChris Lattner  }
2125d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
2139e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  case Type::Builtin:
214a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    // FIXME: need to use TargetInfo to derive the target specific sizes. This
215a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    // implementation will suffice for play with vector support.
216a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    switch (cast<BuiltinType>(T)->getKind()) {
217692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    default: assert(0 && "Unknown builtin type!");
218d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner    case BuiltinType::Void:
219d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner      assert(0 && "Incomplete types have no size!");
2206f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Bool:
2219e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getBoolWidth();
2229e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getBoolAlign();
2236f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
224692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_S:
225692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::Char_U:
226692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UChar:
2276f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::SChar:
2289e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getCharWidth();
2299e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getCharAlign();
2306f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
231692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UShort:
2326f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Short:
2339e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getShortWidth();
2349e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getShortAlign();
2356f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
236692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::UInt:
2376f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Int:
2389e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getIntWidth();
2399e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getIntAlign();
2406f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
241692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULong:
2426f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Long:
2439e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongWidth();
2449e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongAlign();
2456f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
246692233e90a99c3a81dd04879d36eb9688f137c44Chris Lattner    case BuiltinType::ULongLong:
2476f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongLong:
2489e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongLongWidth();
2499e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongLongAlign();
2506f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
2516f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Float:
2529e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getFloatWidth();
2539e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getFloatAlign();
2546f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
2556f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::Double:
2569e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner        Width = Target.getDoubleWidth();
2579e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner        Align = Target.getDoubleAlign();
2586f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
2596f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    case BuiltinType::LongDouble:
2609e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Width = Target.getLongDoubleWidth();
2619e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner      Align = Target.getLongDoubleAlign();
2626f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner      break;
263a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner    }
264bfef6d7c67831a135d6ab79931f010f750a730adChris Lattner    break;
265ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  case Type::ASQual:
26698be4943e8dc4f3905629a7102668960873cf863Chris Lattner    // FIXME: Pointers into different addr spaces could have different sizes and
26798be4943e8dc4f3905629a7102668960873cf863Chris Lattner    // alignment requirements: getPointerInfo should take an AddrSpace.
26898be4943e8dc4f3905629a7102668960873cf863Chris Lattner    return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
269a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  case Type::ObjCQualifiedId:
2709e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width  = Target.getPointerWidth(0);
271f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(0);
2726f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    break;
273f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  case Type::Pointer: {
274f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
2759e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width  = Target.getPointerWidth(AS);
276f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    Align = Target.getPointerAlign(AS);
277f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner    break;
278f72a44330b9d9a4b2d93e9b91cfb8ab7bd4a0643Chris Lattner  }
279a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  case Type::Reference:
2807ab2ed8e881ffdc84e890f5265c41b930df17ceeChris Lattner    // "When applied to a reference or a reference type, the result is the size
2815d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // of the referenced type." C++98 5.3.3p2: expr.sizeof.
2826f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // FIXME: This is wrong for struct layout: a reference in a struct has
2836f62c2abd8077bf70d2166d37e8caa426b34d8e4Chris Lattner    // pointer size.
284bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner    return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
2855d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
2865d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  case Type::Complex: {
2875d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // Complex types have the same alignment as their elements, but twice the
2885d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    // size.
2895d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    std::pair<uint64_t, unsigned> EltInfo =
29098be4943e8dc4f3905629a7102668960873cf863Chris Lattner      getTypeInfo(cast<ComplexType>(T)->getElementType());
2919e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner    Width = EltInfo.first*2;
2925d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    Align = EltInfo.second;
2935d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner    break;
2945d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  }
2957176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  case Type::Tagged: {
2967176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
2977176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner      return getTypeInfo(ET->getDecl()->getIntegerType());
2987176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner
2997176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    RecordType *RT = cast<RecordType>(T);
3007176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
3017176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Width = Layout.getSize();
3027176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    Align = Layout.getAlignment();
303dc0d73e6495404418acf8548875aeaff07791a74Chris Lattner    break;
304a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner  }
3057176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner  }
306d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner
307464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
3089e9b6dc3fd413f5341fab54b681420eeb21cd169Chris Lattner  return std::make_pair(Width, Align);
309a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner}
310a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
31188a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel/// getASTRecordLayout - Get or compute information about the layout of the
312464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// specified record (struct/union/class), which indicates its size and field
313464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// position information.
31498be4943e8dc4f3905629a7102668960873cf863Chris Lattnerconst ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
315464175bba1318bef7905122e9fda20cff926df78Chris Lattner  assert(D->isDefinition() && "Cannot get layout of forward declarations!");
316464175bba1318bef7905122e9fda20cff926df78Chris Lattner
317464175bba1318bef7905122e9fda20cff926df78Chris Lattner  // Look up this layout, if already laid out, return what we have.
31888a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel  const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
319464175bba1318bef7905122e9fda20cff926df78Chris Lattner  if (Entry) return *Entry;
320464175bba1318bef7905122e9fda20cff926df78Chris Lattner
32188a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel  // Allocate and assign into ASTRecordLayouts here.  The "Entry" reference can
32288a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel  // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
32388a981b47c7face1b1fdaa9074256245107b9ca9Devang Patel  ASTRecordLayout *NewEntry = new ASTRecordLayout();
324464175bba1318bef7905122e9fda20cff926df78Chris Lattner  Entry = NewEntry;
325464175bba1318bef7905122e9fda20cff926df78Chris Lattner
326464175bba1318bef7905122e9fda20cff926df78Chris Lattner  uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
327464175bba1318bef7905122e9fda20cff926df78Chris Lattner  uint64_t RecordSize = 0;
328464175bba1318bef7905122e9fda20cff926df78Chris Lattner  unsigned RecordAlign = 8;  // Default alignment = 1 byte = 8 bits.
329464175bba1318bef7905122e9fda20cff926df78Chris Lattner
330464175bba1318bef7905122e9fda20cff926df78Chris Lattner  if (D->getKind() != Decl::Union) {
331042c4e7e9f0b54104f4f2e098c028aa8247b6bedAnders Carlsson    if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
332042c4e7e9f0b54104f4f2e098c028aa8247b6bedAnders Carlsson      RecordAlign = std::max(RecordAlign, AA->getAlignment());
333042c4e7e9f0b54104f4f2e098c028aa8247b6bedAnders Carlsson
3346a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson    bool StructIsPacked = D->getAttr<PackedAttr>();
3356a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson
336464175bba1318bef7905122e9fda20cff926df78Chris Lattner    // Layout each field, for now, just sequentially, respecting alignment.  In
337464175bba1318bef7905122e9fda20cff926df78Chris Lattner    // the future, this will need to be tweakable by targets.
338464175bba1318bef7905122e9fda20cff926df78Chris Lattner    for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
339464175bba1318bef7905122e9fda20cff926df78Chris Lattner      const FieldDecl *FD = D->getMember(i);
3406a24acbb3dbb3bea9426716bee6ad6023ad15f3fAnders Carlsson      bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
34175afb585496b8c521672a6a91b8dd1838d36185aEli Friedman      uint64_t FieldSize;
34275afb585496b8c521672a6a91b8dd1838d36185aEli Friedman      unsigned FieldAlign;
3438af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson
3448af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson      if (const Expr *BitWidthExpr = FD->getBitWidth()) {
3458af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        llvm::APSInt I(32);
3468af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        bool BitWidthIsICE =
3478af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          BitWidthExpr->isIntegerConstantExpr(I, *this);
3488af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        assert (BitWidthIsICE  && "Invalid BitField size expression");
3498af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        FieldSize = I.getZExtValue();
3508af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson
35198be4943e8dc4f3905629a7102668960873cf863Chris Lattner        std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
3528af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        uint64_t TypeSize = TypeInfo.first;
353042c4e7e9f0b54104f4f2e098c028aa8247b6bedAnders Carlsson
354042c4e7e9f0b54104f4f2e098c028aa8247b6bedAnders Carlsson        if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
355042c4e7e9f0b54104f4f2e098c028aa8247b6bedAnders Carlsson          FieldAlign = AA->getAlignment();
356042c4e7e9f0b54104f4f2e098c028aa8247b6bedAnders Carlsson        else if (FieldIsPacked)
357042c4e7e9f0b54104f4f2e098c028aa8247b6bedAnders Carlsson          FieldAlign = 8;
358042c4e7e9f0b54104f4f2e098c028aa8247b6bedAnders Carlsson        else {
3598af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          // FIXME: This is X86 specific, use 32-bit alignment for long long.
3608af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          if (FD->getType()->isIntegerType() && TypeInfo.second > 32)
3618af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson            FieldAlign = 32;
3628af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          else
3638af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson            FieldAlign = TypeInfo.second;
364042c4e7e9f0b54104f4f2e098c028aa8247b6bedAnders Carlsson        }
3658af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson
3668af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        // Check if we need to add padding to give the field the correct
3678af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        // alignment.
3688af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        if (RecordSize % FieldAlign + FieldSize > TypeSize)
3698af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
3708af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson
37175afb585496b8c521672a6a91b8dd1838d36185aEli Friedman      } else {
3728af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        if (FD->getType()->isIncompleteType()) {
3738af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          // This must be a flexible array member; we can't directly
3748af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          // query getTypeInfo about these, so we figure it out here.
3758af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          // Flexible array members don't have any size, but they
3768af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          // have to be aligned appropriately for their element type.
377042c4e7e9f0b54104f4f2e098c028aa8247b6bedAnders Carlsson
3788af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
3798af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson            FieldAlign = AA->getAlignment();
3808af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          else if (FieldIsPacked)
3818af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson            FieldAlign = 8;
3828af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          else {
3838af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson            const ArrayType* ATy = FD->getType()->getAsArrayType();
38498be4943e8dc4f3905629a7102668960873cf863Chris Lattner            FieldAlign = getTypeAlign(ATy->getElementType());
3858af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          }
3868af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          FieldSize = 0;
3878af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        } else {
38898be4943e8dc4f3905629a7102668960873cf863Chris Lattner          std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
3898af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          FieldSize = FieldInfo.first;
3908af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson
3918af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
3928af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson            FieldAlign = AA->getAlignment();
3938af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          else if (FieldIsPacked)
3948af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson            FieldAlign = 8;
3958af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson          else
3968af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson            FieldAlign = FieldInfo.second;
3978af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        }
39875afb585496b8c521672a6a91b8dd1838d36185aEli Friedman
3998af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        // Round up the current record size to the field's alignment boundary.
4008af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
4018af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson      }
402464175bba1318bef7905122e9fda20cff926df78Chris Lattner
403464175bba1318bef7905122e9fda20cff926df78Chris Lattner      // Place this field at the current location.
404464175bba1318bef7905122e9fda20cff926df78Chris Lattner      FieldOffsets[i] = RecordSize;
405464175bba1318bef7905122e9fda20cff926df78Chris Lattner
406464175bba1318bef7905122e9fda20cff926df78Chris Lattner      // Reserve space for this field.
407464175bba1318bef7905122e9fda20cff926df78Chris Lattner      RecordSize += FieldSize;
408464175bba1318bef7905122e9fda20cff926df78Chris Lattner
409464175bba1318bef7905122e9fda20cff926df78Chris Lattner      // Remember max struct/class alignment.
410464175bba1318bef7905122e9fda20cff926df78Chris Lattner      RecordAlign = std::max(RecordAlign, FieldAlign);
411464175bba1318bef7905122e9fda20cff926df78Chris Lattner    }
412464175bba1318bef7905122e9fda20cff926df78Chris Lattner
413464175bba1318bef7905122e9fda20cff926df78Chris Lattner    // Finally, round the size of the total struct up to the alignment of the
414464175bba1318bef7905122e9fda20cff926df78Chris Lattner    // struct itself.
415464175bba1318bef7905122e9fda20cff926df78Chris Lattner    RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
416464175bba1318bef7905122e9fda20cff926df78Chris Lattner  } else {
417464175bba1318bef7905122e9fda20cff926df78Chris Lattner    // Union layout just puts each member at the start of the record.
418464175bba1318bef7905122e9fda20cff926df78Chris Lattner    for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
419464175bba1318bef7905122e9fda20cff926df78Chris Lattner      const FieldDecl *FD = D->getMember(i);
42098be4943e8dc4f3905629a7102668960873cf863Chris Lattner      std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
421464175bba1318bef7905122e9fda20cff926df78Chris Lattner      uint64_t FieldSize = FieldInfo.first;
422464175bba1318bef7905122e9fda20cff926df78Chris Lattner      unsigned FieldAlign = FieldInfo.second;
423464175bba1318bef7905122e9fda20cff926df78Chris Lattner
4248af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson      // FIXME: This is X86 specific, use 32-bit alignment for long long.
4258af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson      if (FD->getType()->isIntegerType() && FieldAlign > 32)
4268af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson        FieldAlign = 32;
4278af226a62dd9bb4a33bb86a8265532bbd75d76fcAnders Carlsson
428464175bba1318bef7905122e9fda20cff926df78Chris Lattner      // Round up the current record size to the field's alignment boundary.
429464175bba1318bef7905122e9fda20cff926df78Chris Lattner      RecordSize = std::max(RecordSize, FieldSize);
430464175bba1318bef7905122e9fda20cff926df78Chris Lattner
431464175bba1318bef7905122e9fda20cff926df78Chris Lattner      // Place this field at the start of the record.
432464175bba1318bef7905122e9fda20cff926df78Chris Lattner      FieldOffsets[i] = 0;
433464175bba1318bef7905122e9fda20cff926df78Chris Lattner
434464175bba1318bef7905122e9fda20cff926df78Chris Lattner      // Remember max struct/class alignment.
435464175bba1318bef7905122e9fda20cff926df78Chris Lattner      RecordAlign = std::max(RecordAlign, FieldAlign);
436464175bba1318bef7905122e9fda20cff926df78Chris Lattner    }
437464175bba1318bef7905122e9fda20cff926df78Chris Lattner  }
4385d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner
4395d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
4405d2a6303467184b1f159bb6556efc434e50e3c28Chris Lattner  return *NewEntry;
441464175bba1318bef7905122e9fda20cff926df78Chris Lattner}
442464175bba1318bef7905122e9fda20cff926df78Chris Lattner
443a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
444a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//                   Type creation/memoization methods
445a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner//===----------------------------------------------------------------------===//
446a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
447ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher LambQualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
448f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType CanT = getCanonicalType(T);
449f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (CanT.getAddressSpace() == AddressSpace)
450f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner    return T;
451f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner
452f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner  // Type's cannot have multiple ASQuals, therefore we know we only have to deal
453f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner  // with CVR qualifiers from here on out.
454f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  assert(CanT.getAddressSpace() == 0 &&
455f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner         "Type is already address space qualified");
456f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner
457f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner  // Check if we've already instantiated an address space qual'd type of this
458f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner  // type.
459ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  llvm::FoldingSetNodeID ID;
460f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner  ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
461ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  void *InsertPos = 0;
462ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
463ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb    return QualType(ASQy, 0);
464ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
465ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  // If the base type isn't canonical, this won't be a canonical type either,
466ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  // so fill in the canonical type field.
467ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  QualType Canonical;
468ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  if (!T->isCanonical()) {
469f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getASQualType(CanT, AddressSpace);
470ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
471ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb    // Get the new insert position for the node we care about.
472ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb    ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
473ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb    assert(NewIP == 0 && "Shouldn't be in the map!");
474ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  }
475f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner  ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
476ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  ASQualTypes.InsertNode(New, InsertPos);
477ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  Types.push_back(New);
478f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner  return QualType(New, T.getCVRQualifiers());
479ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb}
480ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb
481a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43dChris Lattner
4825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getComplexType - Return the uniqued reference to the type for a complex
4835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// number with the specified element type.
4845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getComplexType(QualType T) {
4855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
4865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
4875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
4885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType::Profile(ID, T);
4895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
4915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
4925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(CT, 0);
4935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
4945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
4955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
4965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
4975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
498f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getComplexType(getCanonicalType(T));
4995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
5015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
5025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(NewIP == 0 && "Shouldn't be in the map!");
5035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexType *New = new ComplexType(T, Canonical);
5055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
5065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ComplexTypes.InsertNode(New, InsertPos);
5075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
5085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getPointerType - Return the uniqued reference to the type for a pointer to
5125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// the specified type.
5135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getPointerType(QualType T) {
5145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
5155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
5165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
5175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType::Profile(ID, T);
5185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
5205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
5215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(PT, 0);
5225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the pointee type isn't canonical, this won't be a canonical type either,
5245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
5255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
5265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
527f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getPointerType(getCanonicalType(T));
5285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
5305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
5315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(NewIP == 0 && "Shouldn't be in the map!");
5325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerType *New = new PointerType(T, Canonical);
5345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
5355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  PointerTypes.InsertNode(New, InsertPos);
5365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
5375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getReferenceType - Return the uniqued reference to the type for a reference
5405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// to the specified type.
5415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getReferenceType(QualType T) {
5425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique pointers, to guarantee there is only one pointer of a particular
5435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
5445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
5455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ReferenceType::Profile(ID, T);
5465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
5485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
5495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(RT, 0);
5505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the referencee type isn't canonical, this won't be a canonical type
5525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // either, so fill in the canonical type field.
5535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
5545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!T->isCanonical()) {
555f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getReferenceType(getCanonicalType(T));
5565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
5585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
5595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(NewIP == 0 && "Shouldn't be in the map!");
5605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ReferenceType *New = new ReferenceType(T, Canonical);
5635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
5645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ReferenceTypes.InsertNode(New, InsertPos);
5655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
5665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
5675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
568fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// getConstantArrayType - Return the unique reference to the type for an
569fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff/// array of the specified element type.
570fb22d96692c5240fb8d611290dbf7eeed3759c73Steve NaroffQualType ASTContext::getConstantArrayType(QualType EltTy,
571c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          const llvm::APInt &ArySize,
572c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
573c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          unsigned EltTypeQuals) {
5745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
575fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff  ConstantArrayType::Profile(ID, EltTy, ArySize);
5765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
5787192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  if (ConstantArrayType *ATP =
5797192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
5805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(ATP, 0);
5815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
5825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
5835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
5845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
5855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!EltTy->isCanonical()) {
586f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
587c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                     ASM, EltTypeQuals);
5885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
5897192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek    ConstantArrayType *NewIP =
5907192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek      ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
5917192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
5925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(NewIP == 0 && "Shouldn't be in the map!");
5935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
5945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
595c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff  ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
596c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                                 ASM, EltTypeQuals);
5977192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek  ConstantArrayTypes.InsertNode(New, InsertPos);
5985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
5995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
6005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
602bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// getVariableArrayType - Returns a non-unique reference to the type for a
603bdbf7b030a3e0ddb95240076683830e6f78c79a5Steve Naroff/// variable array of the specified element type.
604c9406125e2cac9208098655ac8058c095c2c3a65Steve NaroffQualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
605c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          ArrayType::ArraySizeModifier ASM,
606c9406125e2cac9208098655ac8058c095c2c3a65Steve Naroff                                          unsigned EltTypeQuals) {
607c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // Since we don't unique expressions, it isn't possible to unique VLA's
608c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // that have an expression provided for their size.
609c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
610c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
611c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                                 ASM, EltTypeQuals);
612c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
613c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  VariableArrayTypes.push_back(New);
614c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
615c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
616c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman}
617c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
618c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli FriedmanQualType ASTContext::getIncompleteArrayType(QualType EltTy,
619c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            ArrayType::ArraySizeModifier ASM,
620c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                            unsigned EltTypeQuals) {
621c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  llvm::FoldingSetNodeID ID;
622c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  IncompleteArrayType::Profile(ID, EltTy);
623c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
624c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  void *InsertPos = 0;
625c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  if (IncompleteArrayType *ATP =
626c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman       IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
627c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    return QualType(ATP, 0);
628c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
629c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // If the element type isn't canonical, this won't be a canonical type
630c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  // either, so fill in the canonical type field.
631c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  QualType Canonical;
632c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
633c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  if (!EltTy->isCanonical()) {
634f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
6352bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek                                       ASM, EltTypeQuals);
636c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
637c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    // Get the new insert position for the node we care about.
638c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    IncompleteArrayType *NewIP =
639c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman      IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
640c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
641c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman    assert(NewIP == 0 && "Shouldn't be in the map!");
6422bd24ba6d10f8c811c8e2a57c8397e07082ba497Ted Kremenek  }
643c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
644c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
645c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman                                                     ASM, EltTypeQuals);
646c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman
647c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  IncompleteArrayTypes.InsertNode(New, InsertPos);
648c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  Types.push_back(New);
649c5773c4b8ce1ed6ed5c7112c9020c954a47dce96Eli Friedman  return QualType(New, 0);
650fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff}
651fb22d96692c5240fb8d611290dbf7eeed3759c73Steve Naroff
65273322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getVectorType - Return the unique reference to a vector type of
65373322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
65473322924127c873c13101b705dd823f5539ffa5fSteve NaroffQualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
6555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  BuiltinType *baseType;
6565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
657f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
65873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
6595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Check if we've already instantiated a vector of this type.
6615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
66273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorType::Profile(ID, vecType, NumElts, Type::Vector);
6635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
6645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
6655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(VTP, 0);
6665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If the element type isn't canonical, this won't be a canonical type either,
6685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // so fill in the canonical type field.
6695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
6705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!vecType->isCanonical()) {
671f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getVectorType(getCanonicalType(vecType), NumElts);
6725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
6735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
6745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
6755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(NewIP == 0 && "Shouldn't be in the map!");
6765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
6775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorType *New = new VectorType(vecType, NumElts, Canonical);
6785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  VectorTypes.InsertNode(New, InsertPos);
6795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
6805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
6815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
6825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
68373322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// getOCUVectorType - Return the unique reference to an OCU vector type of
68473322924127c873c13101b705dd823f5539ffa5fSteve Naroff/// the specified element type and size. VectorType must be a built-in type.
68573322924127c873c13101b705dd823f5539ffa5fSteve NaroffQualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
68673322924127c873c13101b705dd823f5539ffa5fSteve Naroff  BuiltinType *baseType;
68773322924127c873c13101b705dd823f5539ffa5fSteve Naroff
688f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
68973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
69073322924127c873c13101b705dd823f5539ffa5fSteve Naroff
69173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // Check if we've already instantiated a vector of this type.
69273322924127c873c13101b705dd823f5539ffa5fSteve Naroff  llvm::FoldingSetNodeID ID;
69373322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
69473322924127c873c13101b705dd823f5539ffa5fSteve Naroff  void *InsertPos = 0;
69573322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
69673322924127c873c13101b705dd823f5539ffa5fSteve Naroff    return QualType(VTP, 0);
69773322924127c873c13101b705dd823f5539ffa5fSteve Naroff
69873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // If the element type isn't canonical, this won't be a canonical type either,
69973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  // so fill in the canonical type field.
70073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  QualType Canonical;
70173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  if (!vecType->isCanonical()) {
702f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getOCUVectorType(getCanonicalType(vecType), NumElts);
70373322924127c873c13101b705dd823f5539ffa5fSteve Naroff
70473322924127c873c13101b705dd823f5539ffa5fSteve Naroff    // Get the new insert position for the node we care about.
70573322924127c873c13101b705dd823f5539ffa5fSteve Naroff    VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
70673322924127c873c13101b705dd823f5539ffa5fSteve Naroff    assert(NewIP == 0 && "Shouldn't be in the map!");
70773322924127c873c13101b705dd823f5539ffa5fSteve Naroff  }
70873322924127c873c13101b705dd823f5539ffa5fSteve Naroff  OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
70973322924127c873c13101b705dd823f5539ffa5fSteve Naroff  VectorTypes.InsertNode(New, InsertPos);
71073322924127c873c13101b705dd823f5539ffa5fSteve Naroff  Types.push_back(New);
71173322924127c873c13101b705dd823f5539ffa5fSteve Naroff  return QualType(New, 0);
71273322924127c873c13101b705dd823f5539ffa5fSteve Naroff}
71373322924127c873c13101b705dd823f5539ffa5fSteve Naroff
7145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
7155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
7165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
7175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
7185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
7195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
7205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FunctionTypeNoProto::Profile(ID, ResultTy);
7215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
7235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (FunctionTypeNoProto *FT =
7245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
7255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FT, 0);
7265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
7285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!ResultTy->isCanonical()) {
729f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
7305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
7325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    FunctionTypeNoProto *NewIP =
7335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
7345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(NewIP == 0 && "Shouldn't be in the map!");
7355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
7385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(New);
73956cd7e3848c2d59ca2ec3b72d0834192edf0bcdfEli Friedman  FunctionTypeNoProtos.InsertNode(New, InsertPos);
7405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(New, 0);
7415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFunctionType - Return a normal function type with a typed argument
7445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// list.  isVariadic indicates whether the argument list includes '...'.
7455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
7465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                     unsigned NumArgs, bool isVariadic) {
7475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Unique functions, to guarantee there is only one function of a particular
7485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // structure.
7495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::FoldingSetNodeID ID;
7505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
7515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  void *InsertPos = 0;
7535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (FunctionTypeProto *FTP =
7545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer        FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
7555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return QualType(FTP, 0);
7565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Determine whether the type being created is already canonical or not.
7585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  bool isCanonical = ResultTy->isCanonical();
7595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
7605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    if (!ArgArray[i]->isCanonical())
7615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      isCanonical = false;
7625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // If this type isn't canonical, get the canonical version of it.
7645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  QualType Canonical;
7655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (!isCanonical) {
7665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    llvm::SmallVector<QualType, 16> CanonicalArgs;
7675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    CanonicalArgs.reserve(NumArgs);
7685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    for (unsigned i = 0; i != NumArgs; ++i)
769f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner      CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
7705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
771f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getFunctionType(getCanonicalType(ResultTy),
7725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                &CanonicalArgs[0], NumArgs,
7735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                                isVariadic);
7745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    // Get the new insert position for the node we care about.
7765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    FunctionTypeProto *NewIP =
7775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
7785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    assert(NewIP == 0 && "Shouldn't be in the map!");
7795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FunctionTypeProto objects are not allocated with new because they have a
7825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // variable size array (for parameter types) at the end of them.
7835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FunctionTypeProto *FTP =
7845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
785942cfd37297528918616d06cd6e4e8bd6e4915a2Chris Lattner                               NumArgs*sizeof(QualType));
7865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
7875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer                              Canonical);
7885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(FTP);
7895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  FunctionTypeProtos.InsertNode(FTP, InsertPos);
7905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(FTP, 0);
7915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
7925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
7935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTypedefType - Return the unique reference to the type for the
7945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified typename decl.
7955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getTypedefType(TypedefDecl *Decl) {
7965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
7975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
798f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
799c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
8005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  Types.push_back(Decl->TypeForDecl);
8015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return QualType(Decl->TypeForDecl, 0);
8025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
8035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
804a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCInterfaceType - Return the unique reference to the type for the
8053536b443bc50d58a79f14fca9b6842541a434854Steve Naroff/// specified ObjC interface decl.
806a526c5c67e5a0473c340903ee542ce570119665fTed KremenekQualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
8073536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
8083536b443bc50d58a79f14fca9b6842541a434854Steve Naroff
809a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
8103536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  Types.push_back(Decl->TypeForDecl);
8113536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  return QualType(Decl->TypeForDecl, 0);
8123536b443bc50d58a79f14fca9b6842541a434854Steve Naroff}
8133536b443bc50d58a79f14fca9b6842541a434854Steve Naroff
81488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// CmpProtocolNames - Comparison predicate for sorting protocols
81588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// alphabetically.
81688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
81788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                            const ObjCProtocolDecl *RHS) {
81888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  return strcmp(LHS->getName(), RHS->getName()) < 0;
81988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
82088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
82188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattnerstatic void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
82288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner                                   unsigned &NumProtocols) {
82388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
82488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
82588cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort protocols, keyed by name.
82688cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
82788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
82888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Remove duplicates.
82988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
83088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  NumProtocols = ProtocolsEnd-Protocols;
83188cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner}
83288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
83388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
834065f0d7b00c3cd2b3139ebd105f50462fc778859Chris Lattner/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
835065f0d7b00c3cd2b3139ebd105f50462fc778859Chris Lattner/// the given interface decl and the conforming protocol list.
836a526c5c67e5a0473c340903ee542ce570119665fTed KremenekQualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
837a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                       ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
83888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort the protocol list alphabetically to canonicalize it.
83988cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  SortAndUniqueProtocols(Protocols, NumProtocols);
84088cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
8414b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  llvm::FoldingSetNodeID ID;
842a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
8434b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
8444b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  void *InsertPos = 0;
845a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  if (ObjCQualifiedInterfaceType *QT =
846a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
8474b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    return QualType(QT, 0);
8484b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
8494b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  // No Match;
850a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCQualifiedInterfaceType *QType =
851a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
8524b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  Types.push_back(QType);
853a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
8544b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  return QualType(QType, 0);
8554b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian}
8564b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian
85788cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
85888cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner/// and the conforming protocol list.
859a526c5c67e5a0473c340903ee542ce570119665fTed KremenekQualType ASTContext::getObjCQualifiedIdType(QualType idType,
860a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek                                            ObjCProtocolDecl **Protocols,
861c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian                                            unsigned NumProtocols) {
86288cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  // Sort the protocol list alphabetically to canonicalize it.
86388cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner  SortAndUniqueProtocols(Protocols, NumProtocols);
86488cb27a160adc305783a44f922ee4b216006ebf9Chris Lattner
865c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  llvm::FoldingSetNodeID ID;
866a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
867c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian
868c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  void *InsertPos = 0;
869a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  if (ObjCQualifiedIdType *QT =
870a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
871c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    return QualType(QT, 0);
872c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian
873c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  // No Match;
874d58fabf7ed279be18a5e82617f809c9deff9be67Fariborz Jahanian  QualType Canonical;
875d58fabf7ed279be18a5e82617f809c9deff9be67Fariborz Jahanian  if (!idType->isCanonical()) {
876f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
877d58fabf7ed279be18a5e82617f809c9deff9be67Fariborz Jahanian                                       Protocols, NumProtocols);
878a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCQualifiedIdType *NewQT =
879a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
880d58fabf7ed279be18a5e82617f809c9deff9be67Fariborz Jahanian    assert(NewQT == 0 && "Shouldn't be in the map!");
881d58fabf7ed279be18a5e82617f809c9deff9be67Fariborz Jahanian  }
882d58fabf7ed279be18a5e82617f809c9deff9be67Fariborz Jahanian
883a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCQualifiedIdType *QType =
884a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
885c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  Types.push_back(QType);
886a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
887c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  return QualType(QType, 0);
888c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian}
889c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian
8909752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
8919752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfExpr AST's (since expression's are never shared). For example,
8929752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// multiple declarations that refer to "typeof(x)" all contain different
8939752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// DeclRefExpr's. This doesn't effect the type checker, since it operates
8949752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
8958d1a3b8ca1e5fcc4567b5a6f51d82be2e460de1cSteve NaroffQualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
896f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofExpr->getType());
8979752f25748d954df99087d741ea35db37ff16beaSteve Naroff  TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
8989752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(toe);
8999752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(toe, 0);
900d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
901d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
9029752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// getTypeOfType -  Unlike many "get<Type>" functions, we don't unique
9039752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// TypeOfType AST's. The only motivation to unique these nodes would be
9049752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
9059752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// an issue. This doesn't effect the type checker, since it operates
9069752f25748d954df99087d741ea35db37ff16beaSteve Naroff/// on canonical type's (which are always unique).
907d1861fd633d5096a00777c918eb8575ea7162fe7Steve NaroffQualType ASTContext::getTypeOfType(QualType tofType) {
908f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  QualType Canonical = getCanonicalType(tofType);
9099752f25748d954df99087d741ea35db37ff16beaSteve Naroff  TypeOfType *tot = new TypeOfType(tofType, Canonical);
9109752f25748d954df99087d741ea35db37ff16beaSteve Naroff  Types.push_back(tot);
9119752f25748d954df99087d741ea35db37ff16beaSteve Naroff  return QualType(tot, 0);
912d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff}
913d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff
9145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getTagDeclType - Return the unique reference to the type for the
9155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// specified TagDecl (struct/union/class/enum) decl.
9165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getTagDeclType(TagDecl *Decl) {
917d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  assert (Decl);
918d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek
9195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // The decl stores the type cache.
920d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
922ea0c6fb7c52df99349066ff1aed58e4dfc4c7289Ted Kremenek  TagType* T = new TagType(Decl, QualType());
923d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  Types.push_back(T);
924d778f88d32b96a74c9edb7342c81357606a7cdc0Ted Kremenek  Decl->TypeForDecl = T;
925ea0c6fb7c52df99349066ff1aed58e4dfc4c7289Ted Kremenek
926ea0c6fb7c52df99349066ff1aed58e4dfc4c7289Ted Kremenek  return QualType(T, 0);
9275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
9295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
9305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
9315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// needs to agree with the definition in <stddef.h>.
9325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid SpencerQualType ASTContext::getSizeType() const {
9335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // On Darwin, size_t is defined as a "long unsigned int".
9345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // FIXME: should derive from "Target".
9355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  return UnsignedLongTy;
9365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
9375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
938fd888a581d6d329f5b447c8ff4d37cf396315993Eli Friedman/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
939fd888a581d6d329f5b447c8ff4d37cf396315993Eli Friedman/// width of characters in wide strings, The value is target dependent and
940fd888a581d6d329f5b447c8ff4d37cf396315993Eli Friedman/// needs to agree with the definition in <stddef.h>.
941fd888a581d6d329f5b447c8ff4d37cf396315993Eli FriedmanQualType ASTContext::getWcharType() const {
942fd888a581d6d329f5b447c8ff4d37cf396315993Eli Friedman  // On Darwin, wchar_t is defined as a "int".
943fd888a581d6d329f5b447c8ff4d37cf396315993Eli Friedman  // FIXME: should derive from "Target".
944fd888a581d6d329f5b447c8ff4d37cf396315993Eli Friedman  return IntTy;
945fd888a581d6d329f5b447c8ff4d37cf396315993Eli Friedman}
946fd888a581d6d329f5b447c8ff4d37cf396315993Eli Friedman
9478b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
9488b9023ba35a86838789e2c9034a6128728c547aaChris Lattner/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
9498b9023ba35a86838789e2c9034a6128728c547aaChris LattnerQualType ASTContext::getPointerDiffType() const {
9508b9023ba35a86838789e2c9034a6128728c547aaChris Lattner  // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
9518b9023ba35a86838789e2c9034a6128728c547aaChris Lattner  // FIXME: should derive from "Target".
9528b9023ba35a86838789e2c9034a6128728c547aaChris Lattner  return IntTy;
9538b9023ba35a86838789e2c9034a6128728c547aaChris Lattner}
9548b9023ba35a86838789e2c9034a6128728c547aaChris Lattner
955e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
956e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//                              Type Operators
957e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner//===----------------------------------------------------------------------===//
958e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
95977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// getCanonicalType - Return the canonical (structural) type corresponding to
96077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// the specified potentially non-canonical type.  The non-canonical version
96177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// of a type may have many "decorated" versions of types.  Decorators can
96277c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
96377c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// to be free of any of these, allowing two canonical types to be compared
96477c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner/// for exact equality with a simple pointer comparison.
96577c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris LattnerQualType ASTContext::getCanonicalType(QualType T) {
96677c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner  QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
96777c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner  return QualType(CanType.getTypePtr(),
96877c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner                  T.getCVRQualifiers() | CanType.getCVRQualifiers());
96977c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner}
97077c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
97177c9647cae939104c6cb2b6a4dd8ca859d2e5770Chris Lattner
972e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// getArrayDecayedType - Return the properly qualified result of decaying the
973e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// specified array type to a pointer.  This operation is non-trivial when
974e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// handling typedefs etc.  The canonical type of "T" must be an array type,
975e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// this returns a pointer to a properly qualified element of the array.
976e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner///
977e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
978e6327747b72bb687c948270f702ff53c30f411a6Chris LattnerQualType ASTContext::getArrayDecayedType(QualType Ty) {
979e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // Handle the common case where typedefs are not involved directly.
980e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  QualType EltTy;
981e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  unsigned ArrayQuals = 0;
982e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  unsigned PointerQuals = 0;
983e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
984e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // Since T "isa" an array type, it could not have had an address space
985e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // qualifier, just CVR qualifiers.  The properly qualified element pointer
986e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // gets the union of the CVR qualifiers from the element and the array, and
987e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // keeps any address space qualifier on the element type if present.
988e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    EltTy = AT->getElementType();
989e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    ArrayQuals = Ty.getCVRQualifiers();
990e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    PointerQuals = AT->getIndexTypeQualifier();
991e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  } else {
992e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // Otherwise, we have an ASQualType or a typedef, etc.  Make sure we don't
993e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // lose qualifiers when dealing with typedefs. Example:
994e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    //   typedef int arr[10];
995e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    //   void test2() {
996e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    //     const arr b;
997e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    //     b[4] = 1;
998e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    //   }
999e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    //
1000e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // The decayed type of b is "const int*" even though the element type of the
1001e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // array is "int".
1002f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    QualType CanTy = getCanonicalType(Ty);
1003e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    const ArrayType *PrettyArrayType = Ty->getAsArrayType();
1004e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    assert(PrettyArrayType && "Not an array type!");
1005e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
1006e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // Get the element type with 'getAsArrayType' so that we don't lose any
1007e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // typedefs in the element type of the array.
1008e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    EltTy = PrettyArrayType->getElementType();
1009e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
1010e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // If the array was address-space qualifier, make sure to ASQual the element
1011e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // type.  We can just grab the address space from the canonical type.
1012e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    if (unsigned AS = CanTy.getAddressSpace())
1013e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner      EltTy = getASQualType(EltTy, AS);
1014e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
1015e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // To properly handle [multiple levels of] typedefs, typeof's etc, we take
1016e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // the CVR qualifiers directly from the canonical type, which is guaranteed
1017e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    // to have the full set unioned together.
1018e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    ArrayQuals = CanTy.getCVRQualifiers();
1019e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner    PointerQuals = PrettyArrayType->getIndexTypeQualifier();
1020e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  }
1021e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
1022d9654557562e77564309f6b83b493a9a424e008aChris Lattner  // Apply any CVR qualifiers from the array type to the element type.  This
1023d9654557562e77564309f6b83b493a9a424e008aChris Lattner  // implements C99 6.7.3p8: "If the specification of an array type includes
1024d9654557562e77564309f6b83b493a9a424e008aChris Lattner  // any type qualifiers, the element type is so qualified, not the array type."
1025e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
1026e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
1027e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  QualType PtrTy = getPointerType(EltTy);
1028e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
1029e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  // int x[restrict 4] ->  int *restrict
1030e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  PtrTy = PtrTy.getQualifiedType(PointerQuals);
1031e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
1032e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner  return PtrTy;
1033e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner}
1034e6327747b72bb687c948270f702ff53c30f411a6Chris Lattner
10355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// getFloatingRank - Return a relative rank for floating point types.
10365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// This routine will assert if passed a built-in type that isn't a float.
1037a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerstatic FloatingRank getFloatingRank(QualType T) {
1038ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  if (const ComplexType *CT = T->getAsComplexType())
10395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    return getFloatingRank(CT->getElementType());
1040a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
1041ebb97e98c03f8d7034bd3748a10e35f39a95c289Christopher Lamb  switch (T->getAsBuiltinType()->getKind()) {
1042a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  default: assert(0 && "getFloatingRank(): not a floating type");
10435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Float:      return FloatRank;
10445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::Double:     return DoubleRank;
10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  case BuiltinType::LongDouble: return LongDoubleRank;
10465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1049716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1050716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// point or a complex type (based on typeDomain/typeSize).
1051716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeDomain' is a real floating point or complex type.
1052716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff/// 'typeSize' is a real floating point or complex type.
10531361b11066239ea15764a2a844405352d87296b3Chris LattnerQualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
10541361b11066239ea15764a2a844405352d87296b3Chris Lattner                                                       QualType Domain) const {
10551361b11066239ea15764a2a844405352d87296b3Chris Lattner  FloatingRank EltRank = getFloatingRank(Size);
10561361b11066239ea15764a2a844405352d87296b3Chris Lattner  if (Domain->isComplexType()) {
10571361b11066239ea15764a2a844405352d87296b3Chris Lattner    switch (EltRank) {
1058716c7304ff5d27a95e1e7823acd1d09d5ec3e37fSteve Naroff    default: assert(0 && "getFloatingRank(): illegal value for rank");
1059f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case FloatRank:      return FloatComplexTy;
1060f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case DoubleRank:     return DoubleComplexTy;
1061f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    case LongDoubleRank: return LongDoubleComplexTy;
1062f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff    }
1063f1448a0e4a1e868ff873a8530a61a09cb68666ccSteve Naroff  }
10641361b11066239ea15764a2a844405352d87296b3Chris Lattner
10651361b11066239ea15764a2a844405352d87296b3Chris Lattner  assert(Domain->isRealFloatingType() && "Unknown domain!");
10661361b11066239ea15764a2a844405352d87296b3Chris Lattner  switch (EltRank) {
10671361b11066239ea15764a2a844405352d87296b3Chris Lattner  default: assert(0 && "getFloatingRank(): illegal value for rank");
10681361b11066239ea15764a2a844405352d87296b3Chris Lattner  case FloatRank:      return FloatTy;
10691361b11066239ea15764a2a844405352d87296b3Chris Lattner  case DoubleRank:     return DoubleTy;
10701361b11066239ea15764a2a844405352d87296b3Chris Lattner  case LongDoubleRank: return LongDoubleTy;
10715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
10725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
10747cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getFloatingTypeOrder - Compare the rank of the two specified floating
10757cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// point types, ignoring the domain of the type (i.e. 'double' ==
10767cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// '_Complex double').  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
10777cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1.
1078a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattnerint ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1079a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank LHSR = getFloatingRank(LHS);
1080a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  FloatingRank RHSR = getFloatingRank(RHS);
1081a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner
1082a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR == RHSR)
1083fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 0;
1084a75cea3f6be0daa8054d36af81a6ffda1713f82dChris Lattner  if (LHSR > RHSR)
1085fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff    return 1;
1086fb0d49669aa370b4c0993c5cee60275ef9fd6518Steve Naroff  return -1;
10875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
10885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1089f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1090f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// routine will assert if passed a built-in type that isn't an integer or enum,
1091f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner/// or if it is not canonicalized.
1092f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattnerstatic unsigned getIntegerRank(Type *T) {
1093f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  assert(T->isCanonical() && "T should be canonicalized");
1094f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  if (isa<EnumType>(T))
1095f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner    return 4;
1096f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
1097f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  switch (cast<BuiltinType>(T)->getKind()) {
10987cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  default: assert(0 && "getIntegerRank(): not a built-in integer");
10997cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Bool:
11007cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return 1;
11017cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_S:
11027cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Char_U:
11037cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::SChar:
11047cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UChar:
11057cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return 2;
11067cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Short:
11077cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UShort:
11087cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return 3;
11097cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Int:
11107cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::UInt:
11117cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return 4;
11127cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::Long:
11137cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULong:
11147cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return 5;
11157cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::LongLong:
11167cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  case BuiltinType::ULongLong:
11177cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return 6;
1118f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  }
1119f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner}
1120f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner
11217cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// getIntegerTypeOrder - Returns the highest ranked integer type:
11227cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// C99 6.3.1.8p1.  If LHS > RHS, return 1.  If LHS == RHS, return 0. If
11237cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner/// LHS < RHS, return -1.
11247cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattnerint ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
1125f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *LHSC = getCanonicalType(LHS).getTypePtr();
1126f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  Type *RHSC = getCanonicalType(RHS).getTypePtr();
11277cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSC == RHSC) return 0;
11285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1129f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1130f52ab250ff92bc51a9ac9a8e19bd43b63a5f844fChris Lattner  bool RHSUnsigned = RHSC->isUnsignedIntegerType();
11315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11327cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned LHSRank = getIntegerRank(LHSC);
11337cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  unsigned RHSRank = getIntegerRank(RHSC);
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11357cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned == RHSUnsigned) {  // Both signed or both unsigned.
11367cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank == RHSRank) return 0;
11377cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return LHSRank > RHSRank ? 1 : -1;
11387cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
11397cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
11407cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
11417cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (LHSUnsigned) {
11427cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the unsigned [LHS] type is larger, return it.
11437cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    if (LHSRank >= RHSRank)
11447cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner      return 1;
11457cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
11467cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // If the signed type can represent all values of the unsigned type, it
11477cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // wins.  Because we are dealing with 2's complement and types that are
11487cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    // powers of two larger than each other, this is always safe.
11497cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
11507cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  }
11517cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner
11527cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the unsigned [RHS] type is larger, return it.
11537cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  if (RHSRank >= LHSRank)
11547cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner    return -1;
11555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
11567cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // If the signed type can represent all values of the unsigned type, it
11577cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // wins.  Because we are dealing with 2's complement and types that are
11587cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  // powers of two larger than each other, this is always safe.
11597cfeb08f2466d6263ec6ff1402298f93f6d6991fChris Lattner  return 1;
11605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
116171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
116271993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson// getCFConstantStringType - Return the type used for constant CFStrings.
116371993dd85eed9cc42c6b2fa61ee5c53026b74817Anders CarlssonQualType ASTContext::getCFConstantStringType() {
116471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  if (!CFConstantStringTypeDecl) {
11656c2b6eb8d836da19007f7540709e16d5e39a1cbaChris Lattner    CFConstantStringTypeDecl =
11660ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner      RecordDecl::Create(*this, Decl::Struct, NULL, SourceLocation(),
1167c63e660882ff93841fa234d70ef6757038302b92Chris Lattner                         &Idents.get("NSConstantString"), 0);
1168f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    QualType FieldTypes[4];
116971993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
117071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const int *isa;
117171993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
1172f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    // int flags;
1173f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[1] = IntTy;
117471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // const char *str;
1175f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
117671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // long length;
1177f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldTypes[3] = LongTy;
117871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    // Create fields
1179f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    FieldDecl *FieldDecls[4];
118071993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
1181f06273f8bbacb086a46bde456429c8d08f6d07eeAnders Carlsson    for (unsigned i = 0; i < 4; ++i)
1182b048c9835969c4f7fe06264748be18ed4b442116Chris Lattner      FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
11838e25d8681822d8094bfeb97b2239363552548171Chris Lattner                                        FieldTypes[i]);
118471993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
118571993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson    CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
118671993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  }
118771993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson
118871993dd85eed9cc42c6b2fa61ee5c53026b74817Anders Carlsson  return getTagDeclType(CFConstantStringTypeDecl);
11898467583c2704e7a9691ea56939a029015f0ade0aGabor Greif}
1190b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
1191e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// This returns true if a type has been typedefed to BOOL:
1192e8c49533521c40643653f943d47229e62d277f88Anders Carlsson// typedef <type> BOOL;
11932d99833e8c956775f2183601cd120b65b569c867Chris Lattnerstatic bool isTypeTypedefedAsBOOL(QualType T) {
1194e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const TypedefType *TT = dyn_cast<TypedefType>(T))
11952d99833e8c956775f2183601cd120b65b569c867Chris Lattner    return !strcmp(TT->getDecl()->getName(), "BOOL");
119685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
119785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  return false;
119885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
119985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
1200a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingTypeSize returns size of type for objective-c encoding
120133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// purpose.
1202a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekint ASTContext::getObjCEncodingTypeSize(QualType type) {
120398be4943e8dc4f3905629a7102668960873cf863Chris Lattner  uint64_t sz = getTypeSize(type);
120433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
120533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Make all integer and enum types at least as large as an int
120633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  if (sz > 0 && type->isIntegralType())
120798be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = std::max(sz, getTypeSize(IntTy));
120833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Treat arrays as pointers, since that's how they're passed in.
120933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  else if (type->isArrayType())
121098be4943e8dc4f3905629a7102668960873cf863Chris Lattner    sz = getTypeSize(VoidPtrTy);
121198be4943e8dc4f3905629a7102668960873cf863Chris Lattner  return sz / getTypeSize(CharTy);
121233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
121333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
1214a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// getObjCEncodingForMethodDecl - Return the encoded type for this method
121533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian/// declaration.
1216a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
121733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian                                              std::string& S)
121833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian{
1219ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  // Encode type qualifer, 'in', 'inout', etc. for the return type.
1220a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
122133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Encode result type.
12227d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian  getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
122333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Compute size of all parameters.
122433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Start with computing size of a pointer in number of bytes.
122533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // FIXME: There might(should) be a better way of doing this computation!
122633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  SourceLocation Loc;
122798be4943e8dc4f3905629a7102668960873cf863Chris Lattner  int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
122833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // The first two arguments (self and _cmd) are pointers; account for
122933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // their size.
123033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  int ParmOffset = 2 * PtrSize;
123133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  int NumOfParams = Decl->getNumParams();
123233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  for (int i = 0; i < NumOfParams; i++) {
123333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    QualType PType = Decl->getParamDecl(i)->getType();
1234a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    int sz = getObjCEncodingTypeSize (PType);
1235a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
123633e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    ParmOffset += sz;
123733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
123833e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(ParmOffset);
123933e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += "@0:";
124033e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  S += llvm::utostr(PtrSize);
124133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
124233e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  // Argument types.
124333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  ParmOffset = 2 * PtrSize;
124433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  for (int i = 0; i < NumOfParams; i++) {
124533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    QualType PType = Decl->getParamDecl(i)->getType();
1246ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    // Process argument qualifiers for user supplied arguments; such as,
124733e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    // 'in', 'inout', etc.
1248a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    getObjCEncodingForTypeQualifier(
1249a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
12507d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian    getObjCEncodingForType(PType, S, EncodingRecordTypes);
125133e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian    S += llvm::utostr(ParmOffset);
1252a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ParmOffset += getObjCEncodingTypeSize(PType);
125333e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian  }
125433e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian}
125533e1d64ab5cd5d27f8530ccd056191fe2c9f3f2eFariborz Jahanian
12567d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanianvoid ASTContext::getObjCEncodingForType(QualType T, std::string& S,
12577d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian       llvm::SmallVector<const RecordType *, 8> &ERType) const
125885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson{
1259e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  // FIXME: This currently doesn't encode:
1260e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  // @ An object (whether statically typed or typed id)
1261e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  // # A class object (Class)
1262e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  // : A method selector (SEL)
1263e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  // {name=type...} A structure
1264e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  // (name=type...) A union
1265e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  // bnum A bit field of num bits
1266e8c49533521c40643653f943d47229e62d277f88Anders Carlsson
1267e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  if (const BuiltinType *BT = T->getAsBuiltinType()) {
126885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    char encoding;
126985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    switch (BT->getKind()) {
12707176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    default: assert(0 && "Unhandled builtin type kind");
12717176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::Void:       encoding = 'v'; break;
12727176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::Bool:       encoding = 'B'; break;
127385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    case BuiltinType::Char_U:
12747176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::UChar:      encoding = 'C'; break;
12757176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::UShort:     encoding = 'S'; break;
12767176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::UInt:       encoding = 'I'; break;
12777176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::ULong:      encoding = 'L'; break;
12787176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::ULongLong:  encoding = 'Q'; break;
127985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    case BuiltinType::Char_S:
12807176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::SChar:      encoding = 'c'; break;
12817176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::Short:      encoding = 's'; break;
12827176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::Int:        encoding = 'i'; break;
12837176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::Long:       encoding = 'l'; break;
12847176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::LongLong:   encoding = 'q'; break;
12857176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::Float:      encoding = 'f'; break;
12867176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::Double:     encoding = 'd'; break;
12877176331b0f5cfaaa2b5aa487a6660e859e371119Chris Lattner    case BuiltinType::LongDouble: encoding = 'd'; break;
128885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
128985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
129085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += encoding;
1291c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  }
1292a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  else if (T->isObjCQualifiedIdType()) {
1293c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian    // Treat id<P...> same as 'id' for encoding purposes.
12947d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian    return getObjCEncodingForType(getObjCIdType(), S, ERType);
1295c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian
1296c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  }
1297c569249ca0ab755ac79d8cbbfcb2bcae19743624Fariborz Jahanian  else if (const PointerType *PT = T->getAsPointerType()) {
129885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    QualType PointeeTy = PT->getPointeeType();
1299a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
1300c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian      S += '@';
1301c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian      return;
1302a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    } else if (isObjCClassType(PointeeTy)) {
13038baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      S += '#';
13048baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      return;
1305a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    } else if (isObjCSelType(PointeeTy)) {
13068baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      S += ':';
13078baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson      return;
1308c2939bc82ce177c0413feb0cd9ce70aefd6235fbFariborz Jahanian    }
130985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
131085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (PointeeTy->isCharType()) {
131185f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // char pointer types should be encoded as '*' unless it is a
131285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      // type that has been typedef'd to 'BOOL'.
1313e8c49533521c40643653f943d47229e62d277f88Anders Carlsson      if (!isTypeTypedefedAsBOOL(PointeeTy)) {
131485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        S += '*';
131585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson        return;
131685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      }
131785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    }
131885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
131985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '^';
13207d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian    getObjCEncodingForType(PT->getPointeeType(), S, ERType);
1321e8c49533521c40643653f943d47229e62d277f88Anders Carlsson  } else if (const ArrayType *AT = T->getAsArrayType()) {
132285f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += '[';
132385f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
132485f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
132585f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      S += llvm::utostr(CAT->getSize().getZExtValue());
132685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    else
132785f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson      assert(0 && "Unhandled array type!");
132885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
13297d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian    getObjCEncodingForType(AT->getElementType(), S, ERType);
133085f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson    S += ']';
1331c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson  } else if (T->getAsFunctionType()) {
1332c0a87b7db06643178ad2cbce0767548c139ea387Anders Carlsson    S += '?';
13336de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian  } else if (const RecordType *RTy = T->getAsRecordType()) {
13346de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    RecordDecl *RDecl= RTy->getDecl();
13356de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    S += '{';
13366de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    S += RDecl->getName();
13377d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian    bool found = false;
13387d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian    for (unsigned i = 0, e = ERType.size(); i != e; ++i)
13397d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      if (ERType[i] == RTy) {
13407d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian        found = true;
13417d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian        break;
13427d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
13437d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian    if (!found) {
13447d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      ERType.push_back(RTy);
13457d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      S += '=';
13467d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      for (int i = 0; i < RDecl->getNumMembers(); i++) {
13477d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian        FieldDecl *field = RDecl->getMember(i);
13487d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian        getObjCEncodingForType(field->getType(), S, ERType);
13497d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      }
13507d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      assert(ERType.back() == RTy && "Record Type stack mismatch.");
13517d6b46d9a9d75dea8ef9f6973dd50633c1f37963Fariborz Jahanian      ERType.pop_back();
13526de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    }
13536de88a873a4cbe06d72602eef57d68006730a80bFariborz Jahanian    S += '}';
13545e71124dabe8017f17ce8996e4161a202694e3e6Steve Naroff  } else if (T->isEnumeralType()) {
13555e71124dabe8017f17ce8996e4161a202694e3e6Steve Naroff    S += 'i';
135685f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson  } else
1357f69cc5d6606fc65a76e3acd6eb6e13efd0098295Steve Naroff    assert(0 && "@encode for type not implemented!");
135885f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson}
135985f9bceab1542aafff012d4d28e998f4ba16e362Anders Carlsson
1360a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
1361ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian                                                 std::string& S) const {
1362ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_In)
1363ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'n';
1364ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Inout)
1365ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'N';
1366ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Out)
1367ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'o';
1368ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Bycopy)
1369ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'O';
1370ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Byref)
1371ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'R';
1372ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian  if (QT & Decl::OBJC_TQ_Oneway)
1373ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian    S += 'V';
1374ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian}
1375ecb01e666665efabd2aa76a76f6080e2a78965faFariborz Jahanian
1376b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlssonvoid ASTContext::setBuiltinVaListType(QualType T)
1377b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson{
1378b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1379b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
1380b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson  BuiltinVaListType = T;
1381b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson}
1382b2cf3573d7351094f6247fcca94703ce88eb9ee0Anders Carlsson
1383a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCIdType(TypedefDecl *TD)
13847e219e47de26346885d667131977bd9ca2d7662aSteve Naroff{
1385a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  assert(ObjCIdType.isNull() && "'id' type already set!");
13867e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
1387a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCIdType = getTypedefType(TD);
13887e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
13897e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  // typedef struct objc_object *id;
13907e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
13917e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  assert(ptr && "'id' incorrectly typed");
13927e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
13937e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  assert(rec && "'id' incorrectly typed");
13947e219e47de26346885d667131977bd9ca2d7662aSteve Naroff  IdStructType = rec;
13957e219e47de26346885d667131977bd9ca2d7662aSteve Naroff}
13967e219e47de26346885d667131977bd9ca2d7662aSteve Naroff
1397a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCSelType(TypedefDecl *TD)
1398b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian{
1399a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  assert(ObjCSelType.isNull() && "'SEL' type already set!");
1400b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
1401a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCSelType = getTypedefType(TD);
1402b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
1403b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  // typedef struct objc_selector *SEL;
1404b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1405b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  assert(ptr && "'SEL' incorrectly typed");
1406b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1407b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  assert(rec && "'SEL' incorrectly typed");
1408b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  SelStructType = rec;
1409b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian}
1410b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian
1411a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCProtoType(QualType QT)
1412390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian{
1413a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1414a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCProtoType = QT;
1415390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian}
1416390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian
1417a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCClassType(TypedefDecl *TD)
14188baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson{
1419a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  assert(ObjCClassType.isNull() && "'Class' type already set!");
14208baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
1421a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCClassType = getTypedefType(TD);
14228baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
14238baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  // typedef struct objc_class *Class;
14248baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
14258baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  assert(ptr && "'Class' incorrectly typed");
14268baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
14278baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  assert(rec && "'Class' incorrectly typed");
14288baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson  ClassStructType = rec;
14298baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson}
14308baaca50f07d0c10bba69c8d88c1b9078c92d06dAnders Carlsson
1431a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1432a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  assert(ObjCConstantStringType.isNull() &&
14332198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff         "'NSConstantString' type already set!");
14342198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
1435a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCConstantStringType = getObjCInterfaceType(Decl);
14362198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff}
14372198891824c38d45b2279de5d5e3ef9394eb457cSteve Naroff
1438ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroffbool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1439ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1440ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1441ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1442ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  return lBuiltin->getKind() == rBuiltin->getKind();
1443ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
1444ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1445b145e7dedebe2ac3c6e8233bece6ffc3f67b3a66Fariborz Jahanian/// objcTypesAreCompatible - This routine is called when two types
1446b145e7dedebe2ac3c6e8233bece6ffc3f67b3a66Fariborz Jahanian/// are of different class; one is interface type or is
1447b145e7dedebe2ac3c6e8233bece6ffc3f67b3a66Fariborz Jahanian/// a qualified interface type and the other type is of a different class.
1448b145e7dedebe2ac3c6e8233bece6ffc3f67b3a66Fariborz Jahanian/// Example, II or II<P>.
1449439dbadeada35a73e0b182270456ed76dda635d4Chris Lattnerbool ASTContext::objcTypesAreCompatible(QualType LHS, QualType RHS) {
1450439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner  // ID is compatible with all interface types.
1451439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner  if (LHS->isObjCInterfaceType() && isObjCIdType(RHS))
1452ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    return true;
1453439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner  else if (isObjCIdType(LHS) && RHS->isObjCInterfaceType())
1454ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    return true;
1455368eefa081d12f0a265ee90ee8ec61b54168d57dChris Lattner
1456439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner  // II is compatible with II<P> if the base is the same.  Otherwise, no two
1457439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner  // qualified interface types are the same.
1458439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner  if (const ObjCInterfaceType *LHSIT = LHS->getAsObjCInterfaceType()) {
1459439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner    if (const ObjCInterfaceType *RHSIT = RHS->getAsObjCInterfaceType()) {
1460439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner      // If the base decls match and one is a qualified interface and one isn't,
1461439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner      // then they are compatible.
1462439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner      return LHSIT->getDecl() == RHSIT->getDecl() &&
1463439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner                 isa<ObjCQualifiedInterfaceType>(LHSIT) !=
1464439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner                 isa<ObjCQualifiedInterfaceType>(RHSIT);
1465439dbadeada35a73e0b182270456ed76dda635d4Chris Lattner    }
1466b145e7dedebe2ac3c6e8233bece6ffc3f67b3a66Fariborz Jahanian  }
1467ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  return false;
1468ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
1469ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1470065f0d7b00c3cd2b3139ebd105f50462fc778859Chris Lattner/// areCompatObjCQualInterfaces - Return true if the two qualified interface
1471065f0d7b00c3cd2b3139ebd105f50462fc778859Chris Lattner/// types are compatible for assignment from RHS to LHS.
1472065f0d7b00c3cd2b3139ebd105f50462fc778859Chris Lattner///
14733b27546b42bc3d5ec261f3228192350affb370beChris Lattnerstatic bool
14743b27546b42bc3d5ec261f3228192350affb370beChris LattnerareCompatObjCQualInterfaces(const ObjCQualifiedInterfaceType *LHS,
14753b27546b42bc3d5ec261f3228192350affb370beChris Lattner                            const ObjCQualifiedInterfaceType *RHS) {
14763b27546b42bc3d5ec261f3228192350affb370beChris Lattner  // Verify that the base decls are compatible: the RHS must be a subclass of
147753efc251792bf2c9c5f295bd3507facc51a1fe7eChris Lattner  // the LHS.
14783b27546b42bc3d5ec261f3228192350affb370beChris Lattner  if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
14794ffc54111fdc0baa45bb287f59774e06dea8caa4Fariborz Jahanian    return false;
1480c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner
1481821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  // All protocols in LHS must have a presence in RHS.  Since the protocol lists
1482821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  // are both sorted alphabetically and have no duplicates, we can scan RHS and
1483821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  // LHS in a single parallel scan until we run out of elements in LHS.
1484821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHS->qual_begin();
1485821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHS->qual_end();
1486821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHS->qual_begin();
1487821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHS->qual_end();
1488821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner
1489821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1490821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  ObjCProtocolDecl *LHSProto = *LHSPI;
1491821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner
1492821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  while (RHSPI != RHSPE) {
1493821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner    ObjCProtocolDecl *RHSProto = *RHSPI++;
1494821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner    // If the RHS has a protocol that the LHS doesn't, ignore it.
1495821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner    if (RHSProto != LHSProto)
1496821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner      continue;
1497821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner
1498821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner    // Otherwise, the RHS does have this element.
1499821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner    ++LHSPI;
1500821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner    if (LHSPI == LHSPE)
1501821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner      return true;  // All protocols in LHS exist in RHS.
1502821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner
1503821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner    LHSProto = *LHSPI;
15044ffc54111fdc0baa45bb287f59774e06dea8caa4Fariborz Jahanian  }
1505821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner
1506821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1507821a01baa2968dd27720c631a6722ec60686ce27Chris Lattner  return false;
15084ffc54111fdc0baa45bb287f59774e06dea8caa4Fariborz Jahanian}
15094ffc54111fdc0baa45bb287f59774e06dea8caa4Fariborz Jahanian
1510f62f9cd5a78e5445a02e37b277f7a2df9c19b7a3Chris Lattner/// areCompatVectorTypes - Return true if the two specified vector types are
1511f62f9cd5a78e5445a02e37b277f7a2df9c19b7a3Chris Lattner/// compatible.
1512f62f9cd5a78e5445a02e37b277f7a2df9c19b7a3Chris Lattnerstatic bool areCompatVectorTypes(const VectorType *LHS,
1513f62f9cd5a78e5445a02e37b277f7a2df9c19b7a3Chris Lattner                                 const VectorType *RHS) {
1514f62f9cd5a78e5445a02e37b277f7a2df9c19b7a3Chris Lattner  assert(LHS->isCanonical() && RHS->isCanonical());
1515f62f9cd5a78e5445a02e37b277f7a2df9c19b7a3Chris Lattner  return LHS->getElementType() == RHS->getElementType() &&
1516f62f9cd5a78e5445a02e37b277f7a2df9c19b7a3Chris Lattner         LHS->getNumElements() == RHS->getNumElements();
1517770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner}
1518770951b5bb6028a8d326ddb4a13cef7d4a128162Chris Lattner
1519ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff// C99 6.2.7p1: If both are complete types, then the following additional
1520ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff// requirements apply...FIXME (handle compatibility across source files).
1521ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroffbool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1522ab373097926c4f24dd653e8b9aebcbf2eff17881Steve Naroff  // "Class" and "id" are compatible built-in structure types.
1523a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1524a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      isObjCClassType(lhs) && isObjCIdType(rhs))
1525ab373097926c4f24dd653e8b9aebcbf2eff17881Steve Naroff    return true;
1526d57405250498a1b5f23c97c6e089318f6f3eefd7Eli Friedman
1527d57405250498a1b5f23c97c6e089318f6f3eefd7Eli Friedman  // Within a translation unit a tag type is
1528d57405250498a1b5f23c97c6e089318f6f3eefd7Eli Friedman  // only compatible with itself.
1529d57405250498a1b5f23c97c6e089318f6f3eefd7Eli Friedman  return lhs.getCanonicalType() == rhs.getCanonicalType();
1530ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
1531ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1532ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroffbool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1533ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1534ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  // identically qualified and both shall be pointers to compatible types.
1535f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner  if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1536f46699ce225811d8d9dbab9d00189a0e54469457Chris Lattner      lhs.getAddressSpace() != rhs.getAddressSpace())
1537ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    return false;
1538ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1539ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1540ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1541ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1542ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  return typesAreCompatible(ltype, rtype);
1543ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
1544ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
154543d69750e7f7b26076e7474dec8839bb777b260fBill Wendling// C++ 5.17p6: When the left operand of an assignment operator denotes a
1546ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff// reference to T, the operation assigns to the object of type T denoted by the
1547ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff// reference.
1548ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroffbool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1549ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  QualType ltype = lhs;
1550ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1551ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  if (lhs->isReferenceType())
1552bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner    ltype = cast<ReferenceType>(lhs.getCanonicalType())->getPointeeType();
1553ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1554ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  QualType rtype = rhs;
1555ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1556ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  if (rhs->isReferenceType())
1557bdcd637c29ec1540f912ea6860c88b910e78c329Chris Lattner    rtype = cast<ReferenceType>(rhs.getCanonicalType())->getPointeeType();
1558ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1559ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  return typesAreCompatible(ltype, rtype);
1560ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
1561ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1562ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroffbool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1563ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1564ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1565ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1566ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1567ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1568ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  // first check the return types (common between C99 and K&R).
1569ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1570ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    return false;
1571ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1572ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  if (lproto && rproto) { // two C99 style function prototypes
1573ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    unsigned lproto_nargs = lproto->getNumArgs();
1574ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    unsigned rproto_nargs = rproto->getNumArgs();
1575ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1576ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    if (lproto_nargs != rproto_nargs)
1577ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff      return false;
1578ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1579ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    // both prototypes have the same number of arguments.
1580ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1581ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff        (rproto->isVariadic() && !lproto->isVariadic()))
1582ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff      return false;
1583ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1584ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    // The use of ellipsis agree...now check the argument types.
1585ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    for (unsigned i = 0; i < lproto_nargs; i++)
1586f69cc5d6606fc65a76e3acd6eb6e13efd0098295Steve Naroff      // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1587f69cc5d6606fc65a76e3acd6eb6e13efd0098295Steve Naroff      // is taken as having the unqualified version of it's declared type.
1588ba03eda1599dd89da935a2b46da10659afe46addSteve Naroff      if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
1589f69cc5d6606fc65a76e3acd6eb6e13efd0098295Steve Naroff                              rproto->getArgType(i).getUnqualifiedType()))
1590ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff        return false;
1591ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    return true;
1592ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
1593ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1594ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    return true;
1595ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1596ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  // we have a mixture of K&R style with C99 prototypes
1597ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  const FunctionTypeProto *proto = lproto ? lproto : rproto;
1598ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1599ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  if (proto->isVariadic())
1600ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    return false;
1601ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1602ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  // FIXME: Each parameter type T in the prototype must be compatible with the
1603ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  // type resulting from applying the usual argument conversions to T.
1604ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  return true;
1605ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
1606ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1607ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroffbool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
16084e92acf3b747b994e50fbf7bfe8ef71cdda20c50Eli Friedman  // Compatible arrays must have compatible element types
16094e92acf3b747b994e50fbf7bfe8ef71cdda20c50Eli Friedman  QualType ltype = lhs->getAsArrayType()->getElementType();
16104e92acf3b747b994e50fbf7bfe8ef71cdda20c50Eli Friedman  QualType rtype = rhs->getAsArrayType()->getElementType();
16114e92acf3b747b994e50fbf7bfe8ef71cdda20c50Eli Friedman
1612ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  if (!typesAreCompatible(ltype, rtype))
1613ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    return false;
16144e92acf3b747b994e50fbf7bfe8ef71cdda20c50Eli Friedman
16154e92acf3b747b994e50fbf7bfe8ef71cdda20c50Eli Friedman  // Compatible arrays must be the same size
16164e92acf3b747b994e50fbf7bfe8ef71cdda20c50Eli Friedman  if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
16174e92acf3b747b994e50fbf7bfe8ef71cdda20c50Eli Friedman    if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
16184e92acf3b747b994e50fbf7bfe8ef71cdda20c50Eli Friedman      return RCAT->getSize() == LCAT->getSize();
16194e92acf3b747b994e50fbf7bfe8ef71cdda20c50Eli Friedman
1620ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  return true;
1621ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
1622ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1623ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1624ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// both shall have the identically qualified version of a compatible type.
1625ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// C99 6.2.7p1: Two types have compatible types if their types are the
1626ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff/// same. See 6.7.[2,3,5] for additional rules.
1627c4e405996217f4be20f73186da53b23b5c4783dcChris Lattnerbool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
1628c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner  QualType LHS = LHS_NC.getCanonicalType();
1629c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner  QualType RHS = RHS_NC.getCanonicalType();
1630988ee6ef540d1203cf4aa52971f3c135c796bdf7Chris Lattner
163143d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // C++ [expr]: If an expression initially has the type "reference to T", the
163243d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // type is adjusted to "T" prior to any further analysis, the expression
163343d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // designates the object or function denoted by the reference, and the
163443d69750e7f7b26076e7474dec8839bb777b260fBill Wendling  // expression is an lvalue.
1635c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner  if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1636c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    LHS = RT->getPointeeType();
1637c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner  if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1638c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    RHS = RT->getPointeeType();
16391adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
1640f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If two types are identical, they are compatible.
1641f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  if (LHS == RHS)
1642f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner    return true;
1643f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
1644f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner  // If qualifiers differ, the types are different.
1645a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1646a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
1647f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner    return false;
1648a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner
1649a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  // Strip off ASQual's if present.
1650a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSAS) {
1651a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHS = LHS.getUnqualifiedType();
1652a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHS = RHS.getUnqualifiedType();
1653a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  }
1654f3692dc4a47dc48d10cec0415c6e9e39b7a39707Chris Lattner
1655c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner  Type::TypeClass LHSClass = LHS->getTypeClass();
1656c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner  Type::TypeClass RHSClass = RHS->getTypeClass();
16571adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
16581adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // We want to consider the two function types to be the same for these
16591adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  // comparisons, just force one to the other.
16601adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
16611adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
16624c721d381fb279899337d120edd4a24d405e56b2Eli Friedman
16634c721d381fb279899337d120edd4a24d405e56b2Eli Friedman  // Same as above for arrays
1664a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1665a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    LHSClass = Type::ConstantArray;
1666a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1667a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    RHSClass = Type::ConstantArray;
1668a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner
1669a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  // Canonicalize OCUVector -> Vector.
1670a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (LHSClass == Type::OCUVector) LHSClass = Type::Vector;
1671a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  if (RHSClass == Type::OCUVector) RHSClass = Type::Vector;
1672ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff
1673a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  // If the canonical type classes don't match.
16741adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  if (LHSClass != RHSClass) {
1675ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    // For Objective-C, it is possible for two types to be compatible
1676ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    // when their classes don't match (when dealing with "id"). If either type
1677ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    // is an interface, we defer to objcTypesAreCompatible().
1678c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    if (LHS->isObjCInterfaceType() || RHS->isObjCInterfaceType())
1679c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner      return objcTypesAreCompatible(LHS, RHS);
1680f69cc5d6606fc65a76e3acd6eb6e13efd0098295Steve Naroff
16811adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
16821adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    // a signed integer type, or an unsigned integer type.
1683c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1684c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner      EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1685c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner      return EDecl->getIntegerType() == RHS;
1686bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
1687c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1688c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner      EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1689c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner      return EDecl->getIntegerType() == LHS;
1690bab96968886f4b77083f4e26a28986ddb1e42d67Eli Friedman    }
16911adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner
1692ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff    return false;
1693ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
1694a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner
16954a74678ed6c3dedac05d02b1ee341f1db869f049Steve Naroff  // The canonical type classes match.
16961adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  switch (LHSClass) {
1697a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  case Type::ASQual:
1698a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  case Type::FunctionProto:
1699a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  case Type::VariableArray:
1700a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  case Type::IncompleteArray:
1701a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner  case Type::Reference:
1702a36a61f218b9f7a97f2c0f511e0b29eb42e8f78bChris Lattner    assert(0 && "Canonicalized away above");
17031adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Pointer:
1704c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    return pointerTypesAreCompatible(LHS, RHS);
17051adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ConstantArray:
1706c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    return arrayTypesAreCompatible(LHS, RHS);
17071adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::FunctionNoProto:
1708c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    return functionTypesAreCompatible(LHS, RHS);
17091adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Tagged: // handle structures, unions
1710c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    return tagTypesAreCompatible(LHS, RHS);
17111adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Builtin:
1712c4e405996217f4be20f73186da53b23b5c4783dcChris Lattner    return builtinTypesAreCompatible(LHS, RHS);
17131adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ObjCInterface:
171453efc251792bf2c9c5f295bd3507facc51a1fe7eChris Lattner    // The LHS must be a superclass of the RHS.
171553efc251792bf2c9c5f295bd3507facc51a1fe7eChris Lattner    return cast<ObjCInterfaceType>(LHS)->getDecl()->isSuperClassOf(
171653efc251792bf2c9c5f295bd3507facc51a1fe7eChris Lattner                                   cast<ObjCInterfaceType>(RHS)->getDecl());
17171adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::Vector:
1718f62f9cd5a78e5445a02e37b277f7a2df9c19b7a3Chris Lattner    return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
17191adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  case Type::ObjCQualifiedInterface:
17203b27546b42bc3d5ec261f3228192350affb370beChris Lattner    return areCompatObjCQualInterfaces(cast<ObjCQualifiedInterfaceType>(LHS),
17213b27546b42bc3d5ec261f3228192350affb370beChris Lattner                                       cast<ObjCQualifiedInterfaceType>(RHS));
17221adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner  default:
17231adb88370beab45af2f065afe86b51ccd59ec50dChris Lattner    assert(0 && "unexpected type");
1724ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  }
1725ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff  return true; // should never get here...
1726ec0550fa3653d46560bf4484a2e988329c228e39Steve Naroff}
17277192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
17287192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek/// Emit - Serialize an ASTContext object to Bitcode.
17297192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenekvoid ASTContext::Emit(llvm::Serializer& S) const {
17305451350ea6881c55c9e05f3a15486471b97f0757Ted Kremenek  S.EmitRef(SourceMgr);
17315451350ea6881c55c9e05f3a15486471b97f0757Ted Kremenek  S.EmitRef(Target);
17325451350ea6881c55c9e05f3a15486471b97f0757Ted Kremenek  S.EmitRef(Idents);
17335451350ea6881c55c9e05f3a15486471b97f0757Ted Kremenek  S.EmitRef(Selectors);
17347192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
1735fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek  // Emit the size of the type vector so that we can reserve that size
1736fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek  // when we reconstitute the ASTContext object.
1737a4559c3f69f3a5b01cced8df4f69c03c45821c74Ted Kremenek  S.EmitInt(Types.size());
1738a4559c3f69f3a5b01cced8df4f69c03c45821c74Ted Kremenek
173903ed44061df258e74a40383bda849e14b892a8c6Ted Kremenek  for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
174003ed44061df258e74a40383bda849e14b892a8c6Ted Kremenek                                          I!=E;++I)
174103ed44061df258e74a40383bda849e14b892a8c6Ted Kremenek    (*I)->Emit(S);
1742a4559c3f69f3a5b01cced8df4f69c03c45821c74Ted Kremenek
1743a9a4a24592a2164114a8a36717650e6341eb67a4Ted Kremenek  // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
17447192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek}
17457192f8e9592729882a09d84d77838db26e39ebd4Ted Kremenek
17460f84c0059cec39fd1c73ac05bc2864dca664e7f4Ted KremenekASTContext* ASTContext::Create(llvm::Deserializer& D) {
1747fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek  SourceManager &SM = D.ReadRef<SourceManager>();
1748fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek  TargetInfo &t = D.ReadRef<TargetInfo>();
1749fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek  IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1750fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek  SelectorTable &sels = D.ReadRef<SelectorTable>();
17510ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1752fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek  unsigned size_reserve = D.ReadInt();
1753fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek
1754fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek  ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1755fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek
175603ed44061df258e74a40383bda849e14b892a8c6Ted Kremenek  for (unsigned i = 0; i < size_reserve; ++i)
175703ed44061df258e74a40383bda849e14b892a8c6Ted Kremenek    Type::Create(*A,i,D);
17580ed844b04ea4387caa4e1cf3dc375d269657536bChris Lattner
1759a9a4a24592a2164114a8a36717650e6341eb67a4Ted Kremenek  // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
1760fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek
1761fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek  return A;
1762fee0452973f28691a61aab0fb074468ce3e34b9bTed Kremenek}
1763