2006-11-10 14:34:16 +08:00
|
|
|
//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-11-10 14:34:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the ASTContext interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ASTContext.h"
|
2006-11-20 12:02:15 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2007-10-02 03:00:59 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
2007-05-21 07:50:58 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2006-12-02 15:52:18 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2007-10-29 13:01:08 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2007-11-01 01:10:13 +08:00
|
|
|
#include "llvm/Bitcode/Serialize.h"
|
|
|
|
#include "llvm/Bitcode/Deserialize.h"
|
2007-10-29 13:01:08 +08:00
|
|
|
|
2006-11-10 14:34:16 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2007-04-28 05:51:21 +08:00
|
|
|
enum FloatingRank {
|
|
|
|
FloatRank, DoubleRank, LongDoubleRank
|
|
|
|
};
|
|
|
|
|
2006-11-12 08:53:46 +08:00
|
|
|
ASTContext::~ASTContext() {
|
|
|
|
// Deallocate all the types.
|
|
|
|
while (!Types.empty()) {
|
2006-12-02 15:52:18 +08:00
|
|
|
if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
|
|
|
|
// Destroy the object, but don't call delete. These are malloc'd.
|
|
|
|
FT->~FunctionTypeProto();
|
|
|
|
free(FT);
|
|
|
|
} else {
|
|
|
|
delete Types.back();
|
|
|
|
}
|
2006-11-12 08:53:46 +08:00
|
|
|
Types.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-26 09:27:23 +08:00
|
|
|
void ASTContext::PrintStats() const {
|
|
|
|
fprintf(stderr, "*** AST Context Stats:\n");
|
|
|
|
fprintf(stderr, " %d types total.\n", (int)Types.size());
|
|
|
|
unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
|
2007-07-18 13:50:59 +08:00
|
|
|
unsigned NumVector = 0, NumComplex = 0;
|
2007-05-27 18:15:43 +08:00
|
|
|
unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
|
2007-01-26 09:27:23 +08:00
|
|
|
|
|
|
|
unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
|
2008-01-08 03:49:32 +08:00
|
|
|
unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
|
|
|
|
unsigned NumObjCQualifiedIds = 0;
|
2007-01-26 09:27:23 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = Types.size(); i != e; ++i) {
|
|
|
|
Type *T = Types[i];
|
|
|
|
if (isa<BuiltinType>(T))
|
|
|
|
++NumBuiltin;
|
|
|
|
else if (isa<PointerType>(T))
|
|
|
|
++NumPointer;
|
2007-05-27 18:15:43 +08:00
|
|
|
else if (isa<ReferenceType>(T))
|
|
|
|
++NumReference;
|
2007-07-18 13:50:59 +08:00
|
|
|
else if (isa<ComplexType>(T))
|
|
|
|
++NumComplex;
|
2007-01-26 09:27:23 +08:00
|
|
|
else if (isa<ArrayType>(T))
|
|
|
|
++NumArray;
|
2007-07-18 13:50:59 +08:00
|
|
|
else if (isa<VectorType>(T))
|
|
|
|
++NumVector;
|
2007-01-26 09:27:23 +08:00
|
|
|
else if (isa<FunctionTypeNoProto>(T))
|
|
|
|
++NumFunctionNP;
|
|
|
|
else if (isa<FunctionTypeProto>(T))
|
|
|
|
++NumFunctionP;
|
2007-01-26 10:01:53 +08:00
|
|
|
else if (isa<TypedefType>(T))
|
2007-01-26 09:27:23 +08:00
|
|
|
++NumTypeName;
|
2007-03-24 06:27:02 +08:00
|
|
|
else if (TagType *TT = dyn_cast<TagType>(T)) {
|
2007-01-26 09:27:23 +08:00
|
|
|
++NumTagged;
|
|
|
|
switch (TT->getDecl()->getKind()) {
|
|
|
|
default: assert(0 && "Unknown tagged type!");
|
|
|
|
case Decl::Struct: ++NumTagStruct; break;
|
|
|
|
case Decl::Union: ++NumTagUnion; break;
|
|
|
|
case Decl::Class: ++NumTagClass; break;
|
|
|
|
case Decl::Enum: ++NumTagEnum; break;
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (isa<ObjCInterfaceType>(T))
|
|
|
|
++NumObjCInterfaces;
|
|
|
|
else if (isa<ObjCQualifiedInterfaceType>(T))
|
|
|
|
++NumObjCQualifiedInterfaces;
|
|
|
|
else if (isa<ObjCQualifiedIdType>(T))
|
|
|
|
++NumObjCQualifiedIds;
|
2007-09-17 22:16:13 +08:00
|
|
|
else {
|
2007-12-12 14:43:05 +08:00
|
|
|
QualType(T, 0).dump();
|
2007-01-26 09:27:23 +08:00
|
|
|
assert(0 && "Unknown type!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, " %d builtin types\n", NumBuiltin);
|
|
|
|
fprintf(stderr, " %d pointer types\n", NumPointer);
|
2007-05-27 18:15:43 +08:00
|
|
|
fprintf(stderr, " %d reference types\n", NumReference);
|
2007-07-18 13:50:59 +08:00
|
|
|
fprintf(stderr, " %d complex types\n", NumComplex);
|
2007-01-26 09:27:23 +08:00
|
|
|
fprintf(stderr, " %d array types\n", NumArray);
|
2007-07-18 13:50:59 +08:00
|
|
|
fprintf(stderr, " %d vector types\n", NumVector);
|
2007-01-26 09:27:23 +08:00
|
|
|
fprintf(stderr, " %d function types with proto\n", NumFunctionP);
|
|
|
|
fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
|
|
|
|
fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
|
|
|
|
fprintf(stderr, " %d tagged types\n", NumTagged);
|
|
|
|
fprintf(stderr, " %d struct types\n", NumTagStruct);
|
|
|
|
fprintf(stderr, " %d union types\n", NumTagUnion);
|
|
|
|
fprintf(stderr, " %d class types\n", NumTagClass);
|
|
|
|
fprintf(stderr, " %d enum types\n", NumTagEnum);
|
2008-01-08 03:49:32 +08:00
|
|
|
fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
|
2007-12-12 14:43:05 +08:00
|
|
|
fprintf(stderr, " %d protocol qualified interface types\n",
|
2008-01-08 03:49:32 +08:00
|
|
|
NumObjCQualifiedInterfaces);
|
2007-12-18 05:03:50 +08:00
|
|
|
fprintf(stderr, " %d protocol qualified id types\n",
|
2008-01-08 03:49:32 +08:00
|
|
|
NumObjCQualifiedIds);
|
2007-05-24 08:40:54 +08:00
|
|
|
fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
|
2007-05-24 05:48:04 +08:00
|
|
|
NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
|
2007-07-18 13:50:59 +08:00
|
|
|
NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
|
2007-05-24 08:40:54 +08:00
|
|
|
NumFunctionP*sizeof(FunctionTypeProto)+
|
|
|
|
NumFunctionNP*sizeof(FunctionTypeNoProto)+
|
|
|
|
NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
|
2007-01-26 09:27:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-06 06:36:20 +08:00
|
|
|
void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
|
|
|
|
Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
|
2006-11-12 08:53:46 +08:00
|
|
|
}
|
|
|
|
|
2006-11-12 08:37:36 +08:00
|
|
|
void ASTContext::InitBuiltinTypes() {
|
|
|
|
assert(VoidTy.isNull() && "Context reinitialized?");
|
|
|
|
|
|
|
|
// C99 6.2.5p19.
|
2006-12-03 10:57:32 +08:00
|
|
|
InitBuiltinType(VoidTy, BuiltinType::Void);
|
2006-11-12 08:37:36 +08:00
|
|
|
|
|
|
|
// C99 6.2.5p2.
|
2006-12-03 10:57:32 +08:00
|
|
|
InitBuiltinType(BoolTy, BuiltinType::Bool);
|
2006-11-12 08:37:36 +08:00
|
|
|
// C99 6.2.5p3.
|
2008-03-06 02:54:05 +08:00
|
|
|
if (Target.isCharSigned())
|
2007-06-03 15:25:34 +08:00
|
|
|
InitBuiltinType(CharTy, BuiltinType::Char_S);
|
|
|
|
else
|
|
|
|
InitBuiltinType(CharTy, BuiltinType::Char_U);
|
2006-11-12 08:37:36 +08:00
|
|
|
// C99 6.2.5p4.
|
2006-12-03 10:57:32 +08:00
|
|
|
InitBuiltinType(SignedCharTy, BuiltinType::SChar);
|
|
|
|
InitBuiltinType(ShortTy, BuiltinType::Short);
|
|
|
|
InitBuiltinType(IntTy, BuiltinType::Int);
|
|
|
|
InitBuiltinType(LongTy, BuiltinType::Long);
|
|
|
|
InitBuiltinType(LongLongTy, BuiltinType::LongLong);
|
2006-11-12 08:37:36 +08:00
|
|
|
|
|
|
|
// C99 6.2.5p6.
|
2006-12-03 10:57:32 +08:00
|
|
|
InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
|
|
|
|
InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
|
|
|
|
InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
|
|
|
|
InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
|
|
|
|
InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
|
2006-11-12 08:37:36 +08:00
|
|
|
|
|
|
|
// C99 6.2.5p10.
|
2006-12-03 10:57:32 +08:00
|
|
|
InitBuiltinType(FloatTy, BuiltinType::Float);
|
|
|
|
InitBuiltinType(DoubleTy, BuiltinType::Double);
|
|
|
|
InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
|
2006-11-12 08:37:36 +08:00
|
|
|
|
|
|
|
// C99 6.2.5p11.
|
2007-06-23 04:56:16 +08:00
|
|
|
FloatComplexTy = getComplexType(FloatTy);
|
|
|
|
DoubleComplexTy = getComplexType(DoubleTy);
|
|
|
|
LongDoubleComplexTy = getComplexType(LongDoubleTy);
|
2007-10-15 22:41:52 +08:00
|
|
|
|
|
|
|
BuiltinVaListType = QualType();
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCIdType = QualType();
|
2007-10-15 22:41:52 +08:00
|
|
|
IdStructType = 0;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCClassType = QualType();
|
2007-10-31 10:53:19 +08:00
|
|
|
ClassStructType = 0;
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCConstantStringType = QualType();
|
2007-10-30 06:57:28 +08:00
|
|
|
|
|
|
|
// void * type
|
|
|
|
VoidPtrTy = getPointerType(VoidTy);
|
2006-11-12 08:37:36 +08:00
|
|
|
}
|
|
|
|
|
2007-07-19 01:52:12 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Type Sizing and Analysis
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-07-14 06:13:22 +08:00
|
|
|
|
|
|
|
/// getTypeSize - Return the size of the specified type, in bits. This method
|
|
|
|
/// does not work on incomplete types.
|
2007-07-14 09:29:45 +08:00
|
|
|
std::pair<uint64_t, unsigned>
|
2008-03-06 02:54:05 +08:00
|
|
|
ASTContext::getTypeInfo(QualType T) {
|
2008-04-07 06:59:24 +08:00
|
|
|
T = getCanonicalType(T);
|
2008-03-08 16:52:55 +08:00
|
|
|
uint64_t Width;
|
2007-07-14 09:29:45 +08:00
|
|
|
unsigned Align;
|
2007-07-14 06:13:22 +08:00
|
|
|
switch (T->getTypeClass()) {
|
2007-07-20 06:06:24 +08:00
|
|
|
case Type::TypeName: assert(0 && "Not a canonical type!");
|
2007-07-19 02:26:58 +08:00
|
|
|
case Type::FunctionNoProto:
|
|
|
|
case Type::FunctionProto:
|
2007-07-14 06:27:08 +08:00
|
|
|
default:
|
2007-07-21 02:13:33 +08:00
|
|
|
assert(0 && "Incomplete types have no size!");
|
2007-08-30 09:06:46 +08:00
|
|
|
case Type::VariableArray:
|
|
|
|
assert(0 && "VLAs not implemented yet!");
|
|
|
|
case Type::ConstantArray: {
|
|
|
|
ConstantArrayType *CAT = cast<ConstantArrayType>(T);
|
2007-07-20 06:06:24 +08:00
|
|
|
|
2008-03-06 02:54:05 +08:00
|
|
|
std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = EltInfo.first*CAT->getSize().getZExtValue();
|
2007-07-20 06:06:24 +08:00
|
|
|
Align = EltInfo.second;
|
|
|
|
break;
|
2007-12-29 13:10:55 +08:00
|
|
|
}
|
|
|
|
case Type::OCUVector:
|
2007-07-20 06:06:24 +08:00
|
|
|
case Type::Vector: {
|
|
|
|
std::pair<uint64_t, unsigned> EltInfo =
|
2008-03-06 02:54:05 +08:00
|
|
|
getTypeInfo(cast<VectorType>(T)->getElementType());
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
|
2007-07-20 06:06:24 +08:00
|
|
|
// FIXME: Vector alignment is not the alignment of its elements.
|
|
|
|
Align = EltInfo.second;
|
|
|
|
break;
|
|
|
|
}
|
2007-07-19 02:26:58 +08:00
|
|
|
|
2008-03-08 16:52:55 +08:00
|
|
|
case Type::Builtin:
|
2007-07-14 06:13:22 +08:00
|
|
|
// FIXME: need to use TargetInfo to derive the target specific sizes. This
|
|
|
|
// implementation will suffice for play with vector support.
|
|
|
|
switch (cast<BuiltinType>(T)->getKind()) {
|
2007-07-14 06:27:08 +08:00
|
|
|
default: assert(0 && "Unknown builtin type!");
|
2007-07-14 09:29:45 +08:00
|
|
|
case BuiltinType::Void:
|
|
|
|
assert(0 && "Incomplete types have no size!");
|
2007-12-20 03:23:28 +08:00
|
|
|
case BuiltinType::Bool:
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = Target.getBoolWidth();
|
|
|
|
Align = Target.getBoolAlign();
|
2007-12-20 03:23:28 +08:00
|
|
|
break;
|
2007-07-14 06:27:08 +08:00
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::UChar:
|
2007-12-20 03:23:28 +08:00
|
|
|
case BuiltinType::SChar:
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = Target.getCharWidth();
|
|
|
|
Align = Target.getCharAlign();
|
2007-12-20 03:23:28 +08:00
|
|
|
break;
|
2007-07-14 06:27:08 +08:00
|
|
|
case BuiltinType::UShort:
|
2007-12-20 03:23:28 +08:00
|
|
|
case BuiltinType::Short:
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = Target.getShortWidth();
|
|
|
|
Align = Target.getShortAlign();
|
2007-12-20 03:23:28 +08:00
|
|
|
break;
|
2007-07-14 06:27:08 +08:00
|
|
|
case BuiltinType::UInt:
|
2007-12-20 03:23:28 +08:00
|
|
|
case BuiltinType::Int:
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = Target.getIntWidth();
|
|
|
|
Align = Target.getIntAlign();
|
2007-12-20 03:23:28 +08:00
|
|
|
break;
|
2007-07-14 06:27:08 +08:00
|
|
|
case BuiltinType::ULong:
|
2007-12-20 03:23:28 +08:00
|
|
|
case BuiltinType::Long:
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = Target.getLongWidth();
|
|
|
|
Align = Target.getLongAlign();
|
2007-12-20 03:23:28 +08:00
|
|
|
break;
|
2007-07-14 06:27:08 +08:00
|
|
|
case BuiltinType::ULongLong:
|
2007-12-20 03:23:28 +08:00
|
|
|
case BuiltinType::LongLong:
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = Target.getLongLongWidth();
|
|
|
|
Align = Target.getLongLongAlign();
|
2007-12-20 03:23:28 +08:00
|
|
|
break;
|
|
|
|
case BuiltinType::Float:
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = Target.getFloatWidth();
|
|
|
|
Align = Target.getFloatAlign();
|
2007-12-20 03:23:28 +08:00
|
|
|
break;
|
|
|
|
case BuiltinType::Double:
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = Target.getDoubleWidth();
|
|
|
|
Align = Target.getDoubleAlign();
|
2007-12-20 03:23:28 +08:00
|
|
|
break;
|
|
|
|
case BuiltinType::LongDouble:
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = Target.getLongDoubleWidth();
|
|
|
|
Align = Target.getLongDoubleAlign();
|
2007-12-20 03:23:28 +08:00
|
|
|
break;
|
2007-07-14 06:13:22 +08:00
|
|
|
}
|
2007-07-16 07:46:53 +08:00
|
|
|
break;
|
2008-02-04 10:31:56 +08:00
|
|
|
case Type::ASQual:
|
2008-03-06 02:54:05 +08:00
|
|
|
// FIXME: Pointers into different addr spaces could have different sizes and
|
|
|
|
// alignment requirements: getPointerInfo should take an AddrSpace.
|
|
|
|
return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
|
2008-01-08 03:49:32 +08:00
|
|
|
case Type::ObjCQualifiedId:
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = Target.getPointerWidth(0);
|
2008-03-08 16:34:58 +08:00
|
|
|
Align = Target.getPointerAlign(0);
|
2007-12-20 03:23:28 +08:00
|
|
|
break;
|
2008-03-08 16:34:58 +08:00
|
|
|
case Type::Pointer: {
|
|
|
|
unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = Target.getPointerWidth(AS);
|
2008-03-08 16:34:58 +08:00
|
|
|
Align = Target.getPointerAlign(AS);
|
|
|
|
break;
|
|
|
|
}
|
2007-07-14 06:13:22 +08:00
|
|
|
case Type::Reference:
|
2007-07-14 06:16:13 +08:00
|
|
|
// "When applied to a reference or a reference type, the result is the size
|
2007-07-19 02:26:58 +08:00
|
|
|
// of the referenced type." C++98 5.3.3p2: expr.sizeof.
|
2007-12-20 03:23:28 +08:00
|
|
|
// FIXME: This is wrong for struct layout: a reference in a struct has
|
|
|
|
// pointer size.
|
2008-04-03 01:35:06 +08:00
|
|
|
return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
|
2007-07-19 02:26:58 +08:00
|
|
|
|
|
|
|
case Type::Complex: {
|
|
|
|
// Complex types have the same alignment as their elements, but twice the
|
|
|
|
// size.
|
|
|
|
std::pair<uint64_t, unsigned> EltInfo =
|
2008-03-06 02:54:05 +08:00
|
|
|
getTypeInfo(cast<ComplexType>(T)->getElementType());
|
2008-03-08 16:52:55 +08:00
|
|
|
Width = EltInfo.first*2;
|
2007-07-19 02:26:58 +08:00
|
|
|
Align = EltInfo.second;
|
|
|
|
break;
|
|
|
|
}
|
2008-04-07 06:05:18 +08:00
|
|
|
case Type::Tagged: {
|
|
|
|
if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
|
|
|
|
return getTypeInfo(ET->getDecl()->getIntegerType());
|
|
|
|
|
|
|
|
RecordType *RT = cast<RecordType>(T);
|
|
|
|
const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
|
|
|
|
Width = Layout.getSize();
|
|
|
|
Align = Layout.getAlignment();
|
2007-07-24 06:46:22 +08:00
|
|
|
break;
|
2007-07-14 06:13:22 +08:00
|
|
|
}
|
2008-04-07 06:05:18 +08:00
|
|
|
}
|
2007-07-14 09:29:45 +08:00
|
|
|
|
2007-07-19 01:52:12 +08:00
|
|
|
assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
|
2008-03-08 16:52:55 +08:00
|
|
|
return std::make_pair(Width, Align);
|
2007-07-14 06:13:22 +08:00
|
|
|
}
|
|
|
|
|
2007-11-02 03:11:01 +08:00
|
|
|
/// getASTRecordLayout - Get or compute information about the layout of the
|
2007-07-19 01:52:12 +08:00
|
|
|
/// specified record (struct/union/class), which indicates its size and field
|
|
|
|
/// position information.
|
2008-03-06 02:54:05 +08:00
|
|
|
const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
|
2007-07-19 01:52:12 +08:00
|
|
|
assert(D->isDefinition() && "Cannot get layout of forward declarations!");
|
|
|
|
|
|
|
|
// Look up this layout, if already laid out, return what we have.
|
2007-11-02 03:11:01 +08:00
|
|
|
const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
|
2007-07-19 01:52:12 +08:00
|
|
|
if (Entry) return *Entry;
|
|
|
|
|
2007-11-02 03:11:01 +08:00
|
|
|
// Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
|
|
|
|
// be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
|
|
|
|
ASTRecordLayout *NewEntry = new ASTRecordLayout();
|
2007-07-19 01:52:12 +08:00
|
|
|
Entry = NewEntry;
|
|
|
|
|
|
|
|
uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
|
|
|
|
uint64_t RecordSize = 0;
|
|
|
|
unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
|
|
|
|
|
|
|
|
if (D->getKind() != Decl::Union) {
|
2008-02-17 03:51:27 +08:00
|
|
|
if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
|
|
|
|
RecordAlign = std::max(RecordAlign, AA->getAlignment());
|
|
|
|
|
2008-02-16 09:20:23 +08:00
|
|
|
bool StructIsPacked = D->getAttr<PackedAttr>();
|
|
|
|
|
2007-07-19 01:52:12 +08:00
|
|
|
// Layout each field, for now, just sequentially, respecting alignment. In
|
|
|
|
// the future, this will need to be tweakable by targets.
|
|
|
|
for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
|
|
|
|
const FieldDecl *FD = D->getMember(i);
|
2008-02-16 09:20:23 +08:00
|
|
|
bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
|
2008-02-06 13:33:51 +08:00
|
|
|
uint64_t FieldSize;
|
|
|
|
unsigned FieldAlign;
|
2008-02-18 15:13:09 +08:00
|
|
|
|
|
|
|
if (const Expr *BitWidthExpr = FD->getBitWidth()) {
|
|
|
|
llvm::APSInt I(32);
|
|
|
|
bool BitWidthIsICE =
|
|
|
|
BitWidthExpr->isIntegerConstantExpr(I, *this);
|
|
|
|
assert (BitWidthIsICE && "Invalid BitField size expression");
|
|
|
|
FieldSize = I.getZExtValue();
|
|
|
|
|
2008-03-06 02:54:05 +08:00
|
|
|
std::pair<uint64_t, unsigned> TypeInfo = getTypeInfo(FD->getType());
|
2008-02-18 15:13:09 +08:00
|
|
|
uint64_t TypeSize = TypeInfo.first;
|
2008-02-17 03:51:27 +08:00
|
|
|
|
|
|
|
if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
|
|
|
|
FieldAlign = AA->getAlignment();
|
|
|
|
else if (FieldIsPacked)
|
|
|
|
FieldAlign = 8;
|
|
|
|
else {
|
2008-02-18 15:13:09 +08:00
|
|
|
// FIXME: This is X86 specific, use 32-bit alignment for long long.
|
|
|
|
if (FD->getType()->isIntegerType() && TypeInfo.second > 32)
|
|
|
|
FieldAlign = 32;
|
|
|
|
else
|
|
|
|
FieldAlign = TypeInfo.second;
|
2008-02-17 03:51:27 +08:00
|
|
|
}
|
2008-02-18 15:13:09 +08:00
|
|
|
|
|
|
|
// Check if we need to add padding to give the field the correct
|
|
|
|
// alignment.
|
|
|
|
if (RecordSize % FieldAlign + FieldSize > TypeSize)
|
|
|
|
RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
|
|
|
|
|
2008-02-06 13:33:51 +08:00
|
|
|
} else {
|
2008-02-18 15:13:09 +08:00
|
|
|
if (FD->getType()->isIncompleteType()) {
|
|
|
|
// This must be a flexible array member; we can't directly
|
|
|
|
// query getTypeInfo about these, so we figure it out here.
|
|
|
|
// Flexible array members don't have any size, but they
|
|
|
|
// have to be aligned appropriately for their element type.
|
2008-02-17 03:51:27 +08:00
|
|
|
|
2008-02-18 15:13:09 +08:00
|
|
|
if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
|
|
|
|
FieldAlign = AA->getAlignment();
|
|
|
|
else if (FieldIsPacked)
|
|
|
|
FieldAlign = 8;
|
|
|
|
else {
|
|
|
|
const ArrayType* ATy = FD->getType()->getAsArrayType();
|
2008-03-06 02:54:05 +08:00
|
|
|
FieldAlign = getTypeAlign(ATy->getElementType());
|
2008-02-18 15:13:09 +08:00
|
|
|
}
|
|
|
|
FieldSize = 0;
|
|
|
|
} else {
|
2008-03-06 02:54:05 +08:00
|
|
|
std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
|
2008-02-18 15:13:09 +08:00
|
|
|
FieldSize = FieldInfo.first;
|
|
|
|
|
|
|
|
if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
|
|
|
|
FieldAlign = AA->getAlignment();
|
|
|
|
else if (FieldIsPacked)
|
|
|
|
FieldAlign = 8;
|
|
|
|
else
|
|
|
|
FieldAlign = FieldInfo.second;
|
|
|
|
}
|
2008-02-06 13:33:51 +08:00
|
|
|
|
2008-02-18 15:13:09 +08:00
|
|
|
// Round up the current record size to the field's alignment boundary.
|
|
|
|
RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
|
|
|
|
}
|
2007-07-19 01:52:12 +08:00
|
|
|
|
|
|
|
// Place this field at the current location.
|
|
|
|
FieldOffsets[i] = RecordSize;
|
|
|
|
|
|
|
|
// Reserve space for this field.
|
|
|
|
RecordSize += FieldSize;
|
|
|
|
|
|
|
|
// Remember max struct/class alignment.
|
|
|
|
RecordAlign = std::max(RecordAlign, FieldAlign);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, round the size of the total struct up to the alignment of the
|
|
|
|
// struct itself.
|
|
|
|
RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
|
|
|
|
} else {
|
|
|
|
// Union layout just puts each member at the start of the record.
|
|
|
|
for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
|
|
|
|
const FieldDecl *FD = D->getMember(i);
|
2008-03-06 02:54:05 +08:00
|
|
|
std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType());
|
2007-07-19 01:52:12 +08:00
|
|
|
uint64_t FieldSize = FieldInfo.first;
|
|
|
|
unsigned FieldAlign = FieldInfo.second;
|
|
|
|
|
2008-02-18 15:13:09 +08:00
|
|
|
// FIXME: This is X86 specific, use 32-bit alignment for long long.
|
|
|
|
if (FD->getType()->isIntegerType() && FieldAlign > 32)
|
|
|
|
FieldAlign = 32;
|
|
|
|
|
2007-07-19 01:52:12 +08:00
|
|
|
// Round up the current record size to the field's alignment boundary.
|
|
|
|
RecordSize = std::max(RecordSize, FieldSize);
|
|
|
|
|
|
|
|
// Place this field at the start of the record.
|
|
|
|
FieldOffsets[i] = 0;
|
|
|
|
|
|
|
|
// Remember max struct/class alignment.
|
|
|
|
RecordAlign = std::max(RecordAlign, FieldAlign);
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 02:26:58 +08:00
|
|
|
|
|
|
|
NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
|
|
|
|
return *NewEntry;
|
2007-07-19 01:52:12 +08:00
|
|
|
}
|
|
|
|
|
2007-07-14 06:13:22 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Type creation/memoization methods
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-02-04 10:31:56 +08:00
|
|
|
QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
|
2008-04-07 06:59:24 +08:00
|
|
|
QualType CanT = getCanonicalType(T);
|
|
|
|
if (CanT.getAddressSpace() == AddressSpace)
|
2008-02-21 04:55:12 +08:00
|
|
|
return T;
|
|
|
|
|
|
|
|
// Type's cannot have multiple ASQuals, therefore we know we only have to deal
|
|
|
|
// with CVR qualifiers from here on out.
|
2008-04-07 06:59:24 +08:00
|
|
|
assert(CanT.getAddressSpace() == 0 &&
|
2008-02-21 04:55:12 +08:00
|
|
|
"Type is already address space qualified");
|
|
|
|
|
|
|
|
// Check if we've already instantiated an address space qual'd type of this
|
|
|
|
// type.
|
2008-02-04 10:31:56 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2008-02-21 04:55:12 +08:00
|
|
|
ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
|
2008-02-04 10:31:56 +08:00
|
|
|
void *InsertPos = 0;
|
|
|
|
if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
|
|
|
|
return QualType(ASQy, 0);
|
|
|
|
|
|
|
|
// If the base type isn't canonical, this won't be a canonical type either,
|
|
|
|
// so fill in the canonical type field.
|
|
|
|
QualType Canonical;
|
|
|
|
if (!T->isCanonical()) {
|
2008-04-07 06:59:24 +08:00
|
|
|
Canonical = getASQualType(CanT, AddressSpace);
|
2008-02-04 10:31:56 +08:00
|
|
|
|
|
|
|
// Get the new insert position for the node we care about.
|
|
|
|
ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
assert(NewIP == 0 && "Shouldn't be in the map!");
|
|
|
|
}
|
2008-02-21 04:55:12 +08:00
|
|
|
ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
|
2008-02-04 10:31:56 +08:00
|
|
|
ASQualTypes.InsertNode(New, InsertPos);
|
|
|
|
Types.push_back(New);
|
2008-02-21 04:55:12 +08:00
|
|
|
return QualType(New, T.getCVRQualifiers());
|
2008-02-04 10:31:56 +08:00
|
|
|
}
|
|
|
|
|
2007-07-14 06:13:22 +08:00
|
|
|
|
2007-06-23 04:56:16 +08:00
|
|
|
/// getComplexType - Return the uniqued reference to the type for a complex
|
|
|
|
/// number with the specified element type.
|
|
|
|
QualType ASTContext::getComplexType(QualType T) {
|
|
|
|
// Unique pointers, to guarantee there is only one pointer of a particular
|
|
|
|
// structure.
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
ComplexType::Profile(ID, T);
|
|
|
|
|
|
|
|
void *InsertPos = 0;
|
|
|
|
if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
|
|
|
|
return QualType(CT, 0);
|
|
|
|
|
|
|
|
// If the pointee type isn't canonical, this won't be a canonical type either,
|
|
|
|
// so fill in the canonical type field.
|
|
|
|
QualType Canonical;
|
|
|
|
if (!T->isCanonical()) {
|
2008-04-07 06:59:24 +08:00
|
|
|
Canonical = getComplexType(getCanonicalType(T));
|
2007-06-23 04:56:16 +08:00
|
|
|
|
|
|
|
// Get the new insert position for the node we care about.
|
|
|
|
ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
assert(NewIP == 0 && "Shouldn't be in the map!");
|
|
|
|
}
|
|
|
|
ComplexType *New = new ComplexType(T, Canonical);
|
|
|
|
Types.push_back(New);
|
|
|
|
ComplexTypes.InsertNode(New, InsertPos);
|
|
|
|
return QualType(New, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-12 08:37:36 +08:00
|
|
|
/// getPointerType - Return the uniqued reference to the type for a pointer to
|
|
|
|
/// the specified type.
|
2007-04-06 06:36:20 +08:00
|
|
|
QualType ASTContext::getPointerType(QualType T) {
|
2006-11-12 08:53:46 +08:00
|
|
|
// Unique pointers, to guarantee there is only one pointer of a particular
|
|
|
|
// structure.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2007-01-27 09:29:36 +08:00
|
|
|
PointerType::Profile(ID, T);
|
|
|
|
|
|
|
|
void *InsertPos = 0;
|
|
|
|
if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
|
2007-04-06 06:36:20 +08:00
|
|
|
return QualType(PT, 0);
|
2006-11-12 08:37:36 +08:00
|
|
|
|
2006-11-12 16:50:50 +08:00
|
|
|
// If the pointee type isn't canonical, this won't be a canonical type either,
|
|
|
|
// so fill in the canonical type field.
|
2007-04-06 06:36:20 +08:00
|
|
|
QualType Canonical;
|
2007-01-27 09:29:36 +08:00
|
|
|
if (!T->isCanonical()) {
|
2008-04-07 06:59:24 +08:00
|
|
|
Canonical = getPointerType(getCanonicalType(T));
|
2007-01-27 09:29:36 +08:00
|
|
|
|
|
|
|
// Get the new insert position for the node we care about.
|
|
|
|
PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
assert(NewIP == 0 && "Shouldn't be in the map!");
|
|
|
|
}
|
|
|
|
PointerType *New = new PointerType(T, Canonical);
|
|
|
|
Types.push_back(New);
|
|
|
|
PointerTypes.InsertNode(New, InsertPos);
|
2007-04-06 06:36:20 +08:00
|
|
|
return QualType(New, 0);
|
2006-11-10 14:34:16 +08:00
|
|
|
}
|
|
|
|
|
2007-05-27 18:15:43 +08:00
|
|
|
/// getReferenceType - Return the uniqued reference to the type for a reference
|
|
|
|
/// to the specified type.
|
|
|
|
QualType ASTContext::getReferenceType(QualType T) {
|
|
|
|
// Unique pointers, to guarantee there is only one pointer of a particular
|
|
|
|
// structure.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2007-05-27 18:15:43 +08:00
|
|
|
ReferenceType::Profile(ID, T);
|
|
|
|
|
|
|
|
void *InsertPos = 0;
|
|
|
|
if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
|
|
|
|
return QualType(RT, 0);
|
|
|
|
|
|
|
|
// If the referencee type isn't canonical, this won't be a canonical type
|
|
|
|
// either, so fill in the canonical type field.
|
|
|
|
QualType Canonical;
|
|
|
|
if (!T->isCanonical()) {
|
2008-04-07 06:59:24 +08:00
|
|
|
Canonical = getReferenceType(getCanonicalType(T));
|
2007-05-27 18:15:43 +08:00
|
|
|
|
|
|
|
// Get the new insert position for the node we care about.
|
|
|
|
ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
assert(NewIP == 0 && "Shouldn't be in the map!");
|
|
|
|
}
|
|
|
|
|
|
|
|
ReferenceType *New = new ReferenceType(T, Canonical);
|
|
|
|
Types.push_back(New);
|
|
|
|
ReferenceTypes.InsertNode(New, InsertPos);
|
|
|
|
return QualType(New, 0);
|
|
|
|
}
|
|
|
|
|
2007-08-30 09:06:46 +08:00
|
|
|
/// getConstantArrayType - Return the unique reference to the type for an
|
|
|
|
/// array of the specified element type.
|
|
|
|
QualType ASTContext::getConstantArrayType(QualType EltTy,
|
2007-08-31 02:10:14 +08:00
|
|
|
const llvm::APInt &ArySize,
|
|
|
|
ArrayType::ArraySizeModifier ASM,
|
|
|
|
unsigned EltTypeQuals) {
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2007-08-30 09:06:46 +08:00
|
|
|
ConstantArrayType::Profile(ID, EltTy, ArySize);
|
2007-02-27 06:17:12 +08:00
|
|
|
|
2007-01-27 16:31:04 +08:00
|
|
|
void *InsertPos = 0;
|
2007-11-01 01:10:13 +08:00
|
|
|
if (ConstantArrayType *ATP =
|
|
|
|
ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
|
2007-04-06 06:36:20 +08:00
|
|
|
return QualType(ATP, 0);
|
2006-11-12 16:50:50 +08:00
|
|
|
|
|
|
|
// If the element type isn't canonical, this won't be a canonical type either,
|
|
|
|
// so fill in the canonical type field.
|
2007-04-06 06:36:20 +08:00
|
|
|
QualType Canonical;
|
2007-01-27 16:31:04 +08:00
|
|
|
if (!EltTy->isCanonical()) {
|
2008-04-07 06:59:24 +08:00
|
|
|
Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
|
2007-08-31 02:10:14 +08:00
|
|
|
ASM, EltTypeQuals);
|
2007-01-27 16:31:04 +08:00
|
|
|
// Get the new insert position for the node we care about.
|
2007-11-01 01:10:13 +08:00
|
|
|
ConstantArrayType *NewIP =
|
|
|
|
ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
|
2007-01-27 16:31:04 +08:00
|
|
|
assert(NewIP == 0 && "Shouldn't be in the map!");
|
|
|
|
}
|
2006-11-12 16:50:50 +08:00
|
|
|
|
2007-08-31 02:10:14 +08:00
|
|
|
ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
|
|
|
|
ASM, EltTypeQuals);
|
2007-11-01 01:10:13 +08:00
|
|
|
ConstantArrayTypes.InsertNode(New, InsertPos);
|
2007-01-27 16:31:04 +08:00
|
|
|
Types.push_back(New);
|
2007-04-06 06:36:20 +08:00
|
|
|
return QualType(New, 0);
|
2006-11-12 16:50:50 +08:00
|
|
|
}
|
|
|
|
|
2007-08-31 02:14:25 +08:00
|
|
|
/// getVariableArrayType - Returns a non-unique reference to the type for a
|
|
|
|
/// variable array of the specified element type.
|
2007-08-31 02:10:14 +08:00
|
|
|
QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
|
|
|
|
ArrayType::ArraySizeModifier ASM,
|
|
|
|
unsigned EltTypeQuals) {
|
2008-02-16 02:16:39 +08:00
|
|
|
// Since we don't unique expressions, it isn't possible to unique VLA's
|
|
|
|
// that have an expression provided for their size.
|
|
|
|
|
|
|
|
VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
|
|
|
|
ASM, EltTypeQuals);
|
|
|
|
|
|
|
|
VariableArrayTypes.push_back(New);
|
|
|
|
Types.push_back(New);
|
|
|
|
return QualType(New, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QualType ASTContext::getIncompleteArrayType(QualType EltTy,
|
|
|
|
ArrayType::ArraySizeModifier ASM,
|
|
|
|
unsigned EltTypeQuals) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
IncompleteArrayType::Profile(ID, EltTy);
|
|
|
|
|
|
|
|
void *InsertPos = 0;
|
|
|
|
if (IncompleteArrayType *ATP =
|
|
|
|
IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
|
|
|
|
return QualType(ATP, 0);
|
|
|
|
|
|
|
|
// If the element type isn't canonical, this won't be a canonical type
|
|
|
|
// either, so fill in the canonical type field.
|
|
|
|
QualType Canonical;
|
|
|
|
|
|
|
|
if (!EltTy->isCanonical()) {
|
2008-04-07 06:59:24 +08:00
|
|
|
Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
|
2007-10-30 07:37:31 +08:00
|
|
|
ASM, EltTypeQuals);
|
2008-02-16 02:16:39 +08:00
|
|
|
|
|
|
|
// Get the new insert position for the node we care about.
|
|
|
|
IncompleteArrayType *NewIP =
|
|
|
|
IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
|
|
|
|
assert(NewIP == 0 && "Shouldn't be in the map!");
|
2007-10-30 07:37:31 +08:00
|
|
|
}
|
2008-02-16 02:16:39 +08:00
|
|
|
|
|
|
|
IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
|
|
|
|
ASM, EltTypeQuals);
|
|
|
|
|
|
|
|
IncompleteArrayTypes.InsertNode(New, InsertPos);
|
|
|
|
Types.push_back(New);
|
|
|
|
return QualType(New, 0);
|
2007-08-30 09:06:46 +08:00
|
|
|
}
|
|
|
|
|
2007-07-19 02:00:27 +08:00
|
|
|
/// getVectorType - Return the unique reference to a vector type of
|
|
|
|
/// the specified element type and size. VectorType must be a built-in type.
|
|
|
|
QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
|
2007-07-07 07:09:18 +08:00
|
|
|
BuiltinType *baseType;
|
|
|
|
|
2008-04-07 06:59:24 +08:00
|
|
|
baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
|
2007-07-19 02:00:27 +08:00
|
|
|
assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
|
2007-07-07 07:09:18 +08:00
|
|
|
|
|
|
|
// Check if we've already instantiated a vector of this type.
|
|
|
|
llvm::FoldingSetNodeID ID;
|
2007-07-19 02:00:27 +08:00
|
|
|
VectorType::Profile(ID, vecType, NumElts, Type::Vector);
|
2007-07-07 07:09:18 +08:00
|
|
|
void *InsertPos = 0;
|
|
|
|
if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
|
|
|
|
return QualType(VTP, 0);
|
|
|
|
|
|
|
|
// If the element type isn't canonical, this won't be a canonical type either,
|
|
|
|
// so fill in the canonical type field.
|
|
|
|
QualType Canonical;
|
|
|
|
if (!vecType->isCanonical()) {
|
2008-04-07 06:59:24 +08:00
|
|
|
Canonical = getVectorType(getCanonicalType(vecType), NumElts);
|
2007-07-07 07:09:18 +08:00
|
|
|
|
|
|
|
// Get the new insert position for the node we care about.
|
|
|
|
VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
assert(NewIP == 0 && "Shouldn't be in the map!");
|
|
|
|
}
|
|
|
|
VectorType *New = new VectorType(vecType, NumElts, Canonical);
|
|
|
|
VectorTypes.InsertNode(New, InsertPos);
|
|
|
|
Types.push_back(New);
|
|
|
|
return QualType(New, 0);
|
|
|
|
}
|
|
|
|
|
2007-07-19 02:00:27 +08:00
|
|
|
/// getOCUVectorType - Return the unique reference to an OCU vector type of
|
|
|
|
/// the specified element type and size. VectorType must be a built-in type.
|
|
|
|
QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
|
|
|
|
BuiltinType *baseType;
|
|
|
|
|
2008-04-07 06:59:24 +08:00
|
|
|
baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
|
2007-07-19 02:00:27 +08:00
|
|
|
assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
|
|
|
|
|
|
|
|
// Check if we've already instantiated a vector of this type.
|
|
|
|
llvm::FoldingSetNodeID ID;
|
|
|
|
VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
|
|
|
|
void *InsertPos = 0;
|
|
|
|
if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
|
|
|
|
return QualType(VTP, 0);
|
|
|
|
|
|
|
|
// If the element type isn't canonical, this won't be a canonical type either,
|
|
|
|
// so fill in the canonical type field.
|
|
|
|
QualType Canonical;
|
|
|
|
if (!vecType->isCanonical()) {
|
2008-04-07 06:59:24 +08:00
|
|
|
Canonical = getOCUVectorType(getCanonicalType(vecType), NumElts);
|
2007-07-19 02:00:27 +08:00
|
|
|
|
|
|
|
// Get the new insert position for the node we care about.
|
|
|
|
VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
assert(NewIP == 0 && "Shouldn't be in the map!");
|
|
|
|
}
|
|
|
|
OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
|
|
|
|
VectorTypes.InsertNode(New, InsertPos);
|
|
|
|
Types.push_back(New);
|
|
|
|
return QualType(New, 0);
|
|
|
|
}
|
|
|
|
|
2006-12-02 15:52:18 +08:00
|
|
|
/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
|
|
|
|
///
|
2007-04-06 06:36:20 +08:00
|
|
|
QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
|
2006-12-02 15:52:18 +08:00
|
|
|
// Unique functions, to guarantee there is only one function of a particular
|
|
|
|
// structure.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2007-01-27 16:37:20 +08:00
|
|
|
FunctionTypeNoProto::Profile(ID, ResultTy);
|
|
|
|
|
|
|
|
void *InsertPos = 0;
|
|
|
|
if (FunctionTypeNoProto *FT =
|
|
|
|
FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
|
2007-04-06 06:36:20 +08:00
|
|
|
return QualType(FT, 0);
|
2007-01-27 16:37:20 +08:00
|
|
|
|
2007-04-06 06:36:20 +08:00
|
|
|
QualType Canonical;
|
2007-01-27 16:37:20 +08:00
|
|
|
if (!ResultTy->isCanonical()) {
|
2008-04-07 06:59:24 +08:00
|
|
|
Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
|
2007-01-27 16:37:20 +08:00
|
|
|
|
|
|
|
// Get the new insert position for the node we care about.
|
|
|
|
FunctionTypeNoProto *NewIP =
|
|
|
|
FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
assert(NewIP == 0 && "Shouldn't be in the map!");
|
|
|
|
}
|
2006-12-02 15:52:18 +08:00
|
|
|
|
2007-01-27 16:37:20 +08:00
|
|
|
FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
|
|
|
|
Types.push_back(New);
|
2008-02-26 06:11:40 +08:00
|
|
|
FunctionTypeNoProtos.InsertNode(New, InsertPos);
|
2007-04-06 06:36:20 +08:00
|
|
|
return QualType(New, 0);
|
2006-12-02 15:52:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// getFunctionType - Return a normal function type with a typed argument
|
|
|
|
/// list. isVariadic indicates whether the argument list includes '...'.
|
2007-04-06 06:36:20 +08:00
|
|
|
QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
|
|
|
|
unsigned NumArgs, bool isVariadic) {
|
2006-12-02 15:52:18 +08:00
|
|
|
// Unique functions, to guarantee there is only one function of a particular
|
|
|
|
// structure.
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2007-01-27 09:15:32 +08:00
|
|
|
FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
|
|
|
|
|
|
|
|
void *InsertPos = 0;
|
|
|
|
if (FunctionTypeProto *FTP =
|
|
|
|
FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
|
2007-04-06 06:36:20 +08:00
|
|
|
return QualType(FTP, 0);
|
2007-01-27 09:15:32 +08:00
|
|
|
|
2006-12-02 15:52:18 +08:00
|
|
|
// Determine whether the type being created is already canonical or not.
|
|
|
|
bool isCanonical = ResultTy->isCanonical();
|
|
|
|
for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
|
|
|
|
if (!ArgArray[i]->isCanonical())
|
|
|
|
isCanonical = false;
|
|
|
|
|
|
|
|
// If this type isn't canonical, get the canonical version of it.
|
2007-04-06 06:36:20 +08:00
|
|
|
QualType Canonical;
|
2006-12-02 15:52:18 +08:00
|
|
|
if (!isCanonical) {
|
2007-06-16 07:05:46 +08:00
|
|
|
llvm::SmallVector<QualType, 16> CanonicalArgs;
|
2006-12-02 15:52:18 +08:00
|
|
|
CanonicalArgs.reserve(NumArgs);
|
|
|
|
for (unsigned i = 0; i != NumArgs; ++i)
|
2008-04-07 06:59:24 +08:00
|
|
|
CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
|
2006-12-02 15:52:18 +08:00
|
|
|
|
2008-04-07 06:59:24 +08:00
|
|
|
Canonical = getFunctionType(getCanonicalType(ResultTy),
|
2006-12-02 15:52:18 +08:00
|
|
|
&CanonicalArgs[0], NumArgs,
|
2007-04-06 05:15:20 +08:00
|
|
|
isVariadic);
|
2007-01-27 09:15:32 +08:00
|
|
|
|
|
|
|
// Get the new insert position for the node we care about.
|
|
|
|
FunctionTypeProto *NewIP =
|
|
|
|
FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
assert(NewIP == 0 && "Shouldn't be in the map!");
|
2006-12-02 15:52:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FunctionTypeProto objects are not allocated with new because they have a
|
|
|
|
// variable size array (for parameter types) at the end of them.
|
|
|
|
FunctionTypeProto *FTP =
|
|
|
|
(FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
|
2007-07-21 02:48:28 +08:00
|
|
|
NumArgs*sizeof(QualType));
|
2006-12-02 15:52:18 +08:00
|
|
|
new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
|
|
|
|
Canonical);
|
|
|
|
Types.push_back(FTP);
|
2007-01-27 09:15:32 +08:00
|
|
|
FunctionTypeProtos.InsertNode(FTP, InsertPos);
|
2007-04-06 06:36:20 +08:00
|
|
|
return QualType(FTP, 0);
|
2006-12-02 15:52:18 +08:00
|
|
|
}
|
2006-11-10 15:17:23 +08:00
|
|
|
|
2007-01-26 10:01:53 +08:00
|
|
|
/// getTypedefType - Return the unique reference to the type for the
|
2006-11-20 12:02:15 +08:00
|
|
|
/// specified typename decl.
|
2007-04-06 06:36:20 +08:00
|
|
|
QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
|
|
|
|
if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
|
2006-11-20 12:02:15 +08:00
|
|
|
|
2008-04-07 06:59:24 +08:00
|
|
|
QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
|
2007-12-18 05:03:50 +08:00
|
|
|
Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
|
2007-03-27 04:16:44 +08:00
|
|
|
Types.push_back(Decl->TypeForDecl);
|
2007-04-06 06:36:20 +08:00
|
|
|
return QualType(Decl->TypeForDecl, 0);
|
2006-11-20 12:02:15 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
/// getObjCInterfaceType - Return the unique reference to the type for the
|
2007-09-07 05:24:23 +08:00
|
|
|
/// specified ObjC interface decl.
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
|
2007-09-07 05:24:23 +08:00
|
|
|
if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
|
2007-09-07 05:24:23 +08:00
|
|
|
Types.push_back(Decl->TypeForDecl);
|
|
|
|
return QualType(Decl->TypeForDecl, 0);
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
/// getObjCQualifiedInterfaceType - Return a
|
|
|
|
/// ObjCQualifiedInterfaceType type for the given interface decl and
|
2007-10-11 08:55:41 +08:00
|
|
|
/// the conforming protocol list.
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
|
|
|
|
ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
|
2007-10-11 08:55:41 +08:00
|
|
|
llvm::FoldingSetNodeID ID;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
|
2007-10-11 08:55:41 +08:00
|
|
|
|
|
|
|
void *InsertPos = 0;
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCQualifiedInterfaceType *QT =
|
|
|
|
ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
|
2007-10-11 08:55:41 +08:00
|
|
|
return QualType(QT, 0);
|
|
|
|
|
|
|
|
// No Match;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCQualifiedInterfaceType *QType =
|
|
|
|
new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
|
2007-10-11 08:55:41 +08:00
|
|
|
Types.push_back(QType);
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
|
2007-10-11 08:55:41 +08:00
|
|
|
return QualType(QType, 0);
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
/// getObjCQualifiedIdType - Return a
|
|
|
|
/// getObjCQualifiedIdType type for the 'id' decl and
|
2007-12-18 05:03:50 +08:00
|
|
|
/// the conforming protocol list.
|
2008-01-08 03:49:32 +08:00
|
|
|
QualType ASTContext::getObjCQualifiedIdType(QualType idType,
|
|
|
|
ObjCProtocolDecl **Protocols,
|
2007-12-18 05:03:50 +08:00
|
|
|
unsigned NumProtocols) {
|
|
|
|
llvm::FoldingSetNodeID ID;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
|
2007-12-18 05:03:50 +08:00
|
|
|
|
|
|
|
void *InsertPos = 0;
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCQualifiedIdType *QT =
|
|
|
|
ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
|
2007-12-18 05:03:50 +08:00
|
|
|
return QualType(QT, 0);
|
|
|
|
|
|
|
|
// No Match;
|
2007-12-19 05:33:44 +08:00
|
|
|
QualType Canonical;
|
|
|
|
if (!idType->isCanonical()) {
|
2008-04-07 06:59:24 +08:00
|
|
|
Canonical = getObjCQualifiedIdType(getCanonicalType(idType),
|
2007-12-19 05:33:44 +08:00
|
|
|
Protocols, NumProtocols);
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCQualifiedIdType *NewQT =
|
|
|
|
ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
|
2007-12-19 05:33:44 +08:00
|
|
|
assert(NewQT == 0 && "Shouldn't be in the map!");
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCQualifiedIdType *QType =
|
|
|
|
new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
|
2007-12-18 05:03:50 +08:00
|
|
|
Types.push_back(QType);
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
|
2007-12-18 05:03:50 +08:00
|
|
|
return QualType(QType, 0);
|
|
|
|
}
|
|
|
|
|
2007-08-02 02:02:17 +08:00
|
|
|
/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
|
|
|
|
/// TypeOfExpr AST's (since expression's are never shared). For example,
|
|
|
|
/// multiple declarations that refer to "typeof(x)" all contain different
|
|
|
|
/// DeclRefExpr's. This doesn't effect the type checker, since it operates
|
|
|
|
/// on canonical type's (which are always unique).
|
2007-08-02 01:20:42 +08:00
|
|
|
QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
|
2008-04-07 06:59:24 +08:00
|
|
|
QualType Canonical = getCanonicalType(tofExpr->getType());
|
2007-08-02 02:02:17 +08:00
|
|
|
TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
|
|
|
|
Types.push_back(toe);
|
|
|
|
return QualType(toe, 0);
|
2007-07-31 20:34:36 +08:00
|
|
|
}
|
|
|
|
|
2007-08-02 02:02:17 +08:00
|
|
|
/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
|
|
|
|
/// TypeOfType AST's. The only motivation to unique these nodes would be
|
|
|
|
/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
|
|
|
|
/// an issue. This doesn't effect the type checker, since it operates
|
|
|
|
/// on canonical type's (which are always unique).
|
2007-07-31 20:34:36 +08:00
|
|
|
QualType ASTContext::getTypeOfType(QualType tofType) {
|
2008-04-07 06:59:24 +08:00
|
|
|
QualType Canonical = getCanonicalType(tofType);
|
2007-08-02 02:02:17 +08:00
|
|
|
TypeOfType *tot = new TypeOfType(tofType, Canonical);
|
|
|
|
Types.push_back(tot);
|
|
|
|
return QualType(tot, 0);
|
2007-07-31 20:34:36 +08:00
|
|
|
}
|
|
|
|
|
2007-01-23 13:45:31 +08:00
|
|
|
/// getTagDeclType - Return the unique reference to the type for the
|
|
|
|
/// specified TagDecl (struct/union/class/enum) decl.
|
2007-04-06 06:36:20 +08:00
|
|
|
QualType ASTContext::getTagDeclType(TagDecl *Decl) {
|
2007-11-27 05:16:01 +08:00
|
|
|
assert (Decl);
|
|
|
|
|
2007-01-26 09:42:24 +08:00
|
|
|
// The decl stores the type cache.
|
2007-11-27 05:16:01 +08:00
|
|
|
if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
|
2007-01-23 13:45:31 +08:00
|
|
|
|
2007-11-14 08:03:20 +08:00
|
|
|
TagType* T = new TagType(Decl, QualType());
|
2007-11-27 05:16:01 +08:00
|
|
|
Types.push_back(T);
|
|
|
|
Decl->TypeForDecl = T;
|
2007-11-14 08:03:20 +08:00
|
|
|
|
|
|
|
return QualType(T, 0);
|
2007-01-23 13:45:31 +08:00
|
|
|
}
|
|
|
|
|
2007-04-03 06:35:25 +08:00
|
|
|
/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
|
2007-04-03 07:01:44 +08:00
|
|
|
/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
|
2007-04-03 06:35:25 +08:00
|
|
|
/// needs to agree with the definition in <stddef.h>.
|
2007-04-06 06:36:20 +08:00
|
|
|
QualType ASTContext::getSizeType() const {
|
2007-04-03 06:35:25 +08:00
|
|
|
// On Darwin, size_t is defined as a "long unsigned int".
|
|
|
|
// FIXME: should derive from "Target".
|
|
|
|
return UnsignedLongTy;
|
|
|
|
}
|
2007-01-23 13:45:31 +08:00
|
|
|
|
2008-02-12 16:29:21 +08:00
|
|
|
/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
|
|
|
|
/// width of characters in wide strings, The value is target dependent and
|
|
|
|
/// needs to agree with the definition in <stddef.h>.
|
|
|
|
QualType ASTContext::getWcharType() const {
|
|
|
|
// On Darwin, wchar_t is defined as a "int".
|
|
|
|
// FIXME: should derive from "Target".
|
|
|
|
return IntTy;
|
|
|
|
}
|
|
|
|
|
2007-07-13 11:05:23 +08:00
|
|
|
/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
|
|
|
|
/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
|
|
|
|
QualType ASTContext::getPointerDiffType() const {
|
|
|
|
// On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
|
|
|
|
// FIXME: should derive from "Target".
|
|
|
|
return IntTy;
|
|
|
|
}
|
|
|
|
|
2008-04-02 13:18:44 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Type Operators
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-04-07 06:41:35 +08:00
|
|
|
/// getCanonicalType - Return the canonical (structural) type corresponding to
|
|
|
|
/// the specified potentially non-canonical type. The non-canonical version
|
|
|
|
/// of a type may have many "decorated" versions of types. Decorators can
|
|
|
|
/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
|
|
|
|
/// to be free of any of these, allowing two canonical types to be compared
|
|
|
|
/// for exact equality with a simple pointer comparison.
|
|
|
|
QualType ASTContext::getCanonicalType(QualType T) {
|
|
|
|
QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
|
|
|
|
return QualType(CanType.getTypePtr(),
|
|
|
|
T.getCVRQualifiers() | CanType.getCVRQualifiers());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-04-02 13:18:44 +08:00
|
|
|
/// getArrayDecayedType - Return the properly qualified result of decaying the
|
|
|
|
/// specified array type to a pointer. This operation is non-trivial when
|
|
|
|
/// handling typedefs etc. The canonical type of "T" must be an array type,
|
|
|
|
/// this returns a pointer to a properly qualified element of the array.
|
|
|
|
///
|
|
|
|
/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
|
|
|
|
QualType ASTContext::getArrayDecayedType(QualType Ty) {
|
|
|
|
// Handle the common case where typedefs are not involved directly.
|
|
|
|
QualType EltTy;
|
|
|
|
unsigned ArrayQuals = 0;
|
|
|
|
unsigned PointerQuals = 0;
|
|
|
|
if (ArrayType *AT = dyn_cast<ArrayType>(Ty)) {
|
|
|
|
// Since T "isa" an array type, it could not have had an address space
|
|
|
|
// qualifier, just CVR qualifiers. The properly qualified element pointer
|
|
|
|
// gets the union of the CVR qualifiers from the element and the array, and
|
|
|
|
// keeps any address space qualifier on the element type if present.
|
|
|
|
EltTy = AT->getElementType();
|
|
|
|
ArrayQuals = Ty.getCVRQualifiers();
|
|
|
|
PointerQuals = AT->getIndexTypeQualifier();
|
|
|
|
} else {
|
|
|
|
// Otherwise, we have an ASQualType or a typedef, etc. Make sure we don't
|
|
|
|
// lose qualifiers when dealing with typedefs. Example:
|
|
|
|
// typedef int arr[10];
|
|
|
|
// void test2() {
|
|
|
|
// const arr b;
|
|
|
|
// b[4] = 1;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// The decayed type of b is "const int*" even though the element type of the
|
|
|
|
// array is "int".
|
2008-04-07 06:59:24 +08:00
|
|
|
QualType CanTy = getCanonicalType(Ty);
|
2008-04-02 13:18:44 +08:00
|
|
|
const ArrayType *PrettyArrayType = Ty->getAsArrayType();
|
|
|
|
assert(PrettyArrayType && "Not an array type!");
|
|
|
|
|
|
|
|
// Get the element type with 'getAsArrayType' so that we don't lose any
|
|
|
|
// typedefs in the element type of the array.
|
|
|
|
EltTy = PrettyArrayType->getElementType();
|
|
|
|
|
|
|
|
// If the array was address-space qualifier, make sure to ASQual the element
|
|
|
|
// type. We can just grab the address space from the canonical type.
|
|
|
|
if (unsigned AS = CanTy.getAddressSpace())
|
|
|
|
EltTy = getASQualType(EltTy, AS);
|
|
|
|
|
|
|
|
// To properly handle [multiple levels of] typedefs, typeof's etc, we take
|
|
|
|
// the CVR qualifiers directly from the canonical type, which is guaranteed
|
|
|
|
// to have the full set unioned together.
|
|
|
|
ArrayQuals = CanTy.getCVRQualifiers();
|
|
|
|
PointerQuals = PrettyArrayType->getIndexTypeQualifier();
|
|
|
|
}
|
|
|
|
|
2008-04-02 14:06:35 +08:00
|
|
|
// Apply any CVR qualifiers from the array type to the element type. This
|
|
|
|
// implements C99 6.7.3p8: "If the specification of an array type includes
|
|
|
|
// any type qualifiers, the element type is so qualified, not the array type."
|
2008-04-02 13:18:44 +08:00
|
|
|
EltTy = EltTy.getQualifiedType(ArrayQuals | EltTy.getCVRQualifiers());
|
|
|
|
|
|
|
|
QualType PtrTy = getPointerType(EltTy);
|
|
|
|
|
|
|
|
// int x[restrict 4] -> int *restrict
|
|
|
|
PtrTy = PtrTy.getQualifiedType(PointerQuals);
|
|
|
|
|
|
|
|
return PtrTy;
|
|
|
|
}
|
|
|
|
|
2007-04-28 05:51:21 +08:00
|
|
|
/// getFloatingRank - Return a relative rank for floating point types.
|
|
|
|
/// This routine will assert if passed a built-in type that isn't a float.
|
2008-04-07 07:38:49 +08:00
|
|
|
static FloatingRank getFloatingRank(QualType T) {
|
2008-02-04 10:31:56 +08:00
|
|
|
if (const ComplexType *CT = T->getAsComplexType())
|
2007-06-23 04:56:16 +08:00
|
|
|
return getFloatingRank(CT->getElementType());
|
2008-04-07 07:38:49 +08:00
|
|
|
|
2008-02-04 10:31:56 +08:00
|
|
|
switch (T->getAsBuiltinType()->getKind()) {
|
2008-04-07 07:38:49 +08:00
|
|
|
default: assert(0 && "getFloatingRank(): not a floating type");
|
2007-06-23 04:56:16 +08:00
|
|
|
case BuiltinType::Float: return FloatRank;
|
|
|
|
case BuiltinType::Double: return DoubleRank;
|
|
|
|
case BuiltinType::LongDouble: return LongDoubleRank;
|
2007-04-28 02:30:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-27 09:41:48 +08:00
|
|
|
/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
|
|
|
|
/// point or a complex type (based on typeDomain/typeSize).
|
|
|
|
/// 'typeDomain' is a real floating point or complex type.
|
|
|
|
/// 'typeSize' is a real floating point or complex type.
|
2007-08-27 09:27:54 +08:00
|
|
|
QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
|
|
|
|
QualType typeSize, QualType typeDomain) const {
|
|
|
|
if (typeDomain->isComplexType()) {
|
|
|
|
switch (getFloatingRank(typeSize)) {
|
2007-08-27 09:41:48 +08:00
|
|
|
default: assert(0 && "getFloatingRank(): illegal value for rank");
|
2007-08-27 09:27:54 +08:00
|
|
|
case FloatRank: return FloatComplexTy;
|
|
|
|
case DoubleRank: return DoubleComplexTy;
|
|
|
|
case LongDoubleRank: return LongDoubleComplexTy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (typeDomain->isRealFloatingType()) {
|
|
|
|
switch (getFloatingRank(typeSize)) {
|
2007-08-27 09:41:48 +08:00
|
|
|
default: assert(0 && "getFloatingRank(): illegal value for rank");
|
2007-08-27 09:27:54 +08:00
|
|
|
case FloatRank: return FloatTy;
|
|
|
|
case DoubleRank: return DoubleTy;
|
|
|
|
case LongDoubleRank: return LongDoubleTy;
|
|
|
|
}
|
2007-04-28 05:51:21 +08:00
|
|
|
}
|
2007-08-27 09:27:54 +08:00
|
|
|
assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
|
2007-09-17 03:23:47 +08:00
|
|
|
//an invalid return value, but the assert
|
|
|
|
//will ensure that this code is never reached.
|
|
|
|
return VoidTy;
|
2007-04-28 02:30:00 +08:00
|
|
|
}
|
|
|
|
|
2008-04-07 07:38:49 +08:00
|
|
|
/// getFloatingTypeOrder - Handles 3 different combos:
|
2007-08-27 23:30:22 +08:00
|
|
|
/// float/float, float/complex, complex/complex.
|
|
|
|
/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
|
2008-04-07 07:38:49 +08:00
|
|
|
int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
|
|
|
|
FloatingRank LHSR = getFloatingRank(LHS);
|
|
|
|
FloatingRank RHSR = getFloatingRank(RHS);
|
|
|
|
|
|
|
|
if (LHSR == RHSR)
|
2007-08-27 23:30:22 +08:00
|
|
|
return 0;
|
2008-04-07 07:38:49 +08:00
|
|
|
if (LHSR > RHSR)
|
2007-08-27 23:30:22 +08:00
|
|
|
return 1;
|
|
|
|
return -1;
|
2007-04-28 02:30:00 +08:00
|
|
|
}
|
|
|
|
|
2008-04-07 06:59:24 +08:00
|
|
|
/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
|
|
|
|
/// routine will assert if passed a built-in type that isn't an integer or enum,
|
|
|
|
/// or if it is not canonicalized.
|
|
|
|
static unsigned getIntegerRank(Type *T) {
|
|
|
|
assert(T->isCanonical() && "T should be canonicalized");
|
|
|
|
if (isa<EnumType>(T))
|
|
|
|
return 4;
|
|
|
|
|
|
|
|
switch (cast<BuiltinType>(T)->getKind()) {
|
|
|
|
default: assert(0 && "getIntegerRank(): not a built-in integer");
|
|
|
|
case BuiltinType::Bool:
|
|
|
|
return 1;
|
|
|
|
case BuiltinType::Char_S:
|
|
|
|
case BuiltinType::Char_U:
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
return 2;
|
|
|
|
case BuiltinType::Short:
|
|
|
|
case BuiltinType::UShort:
|
|
|
|
return 3;
|
|
|
|
case BuiltinType::Int:
|
|
|
|
case BuiltinType::UInt:
|
|
|
|
return 4;
|
|
|
|
case BuiltinType::Long:
|
|
|
|
case BuiltinType::ULong:
|
|
|
|
return 5;
|
|
|
|
case BuiltinType::LongLong:
|
|
|
|
case BuiltinType::ULongLong:
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-07 07:38:49 +08:00
|
|
|
// getMaxIntegerType - Returns the highest ranked integer type. Handles 3 case:
|
2007-04-28 05:51:21 +08:00
|
|
|
// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
|
2008-04-07 07:38:49 +08:00
|
|
|
QualType ASTContext::getMaxIntegerType(QualType LHS, QualType RHS) {
|
2008-04-07 06:59:24 +08:00
|
|
|
Type *LHSC = getCanonicalType(LHS).getTypePtr();
|
|
|
|
Type *RHSC = getCanonicalType(RHS).getTypePtr();
|
|
|
|
if (LHSC == RHSC) return LHS;
|
2007-06-03 15:25:34 +08:00
|
|
|
|
2008-04-07 06:59:24 +08:00
|
|
|
bool LHSUnsigned = LHSC->isUnsignedIntegerType();
|
|
|
|
bool RHSUnsigned = RHSC->isUnsignedIntegerType();
|
2007-04-28 02:30:00 +08:00
|
|
|
|
2008-04-07 06:59:24 +08:00
|
|
|
if (LHSUnsigned == RHSUnsigned) // Both signed or both unsigned.
|
|
|
|
return getIntegerRank(LHSC) >= getIntegerRank(RHSC) ? LHS : RHS;
|
2007-04-28 02:30:00 +08:00
|
|
|
|
2008-04-07 06:59:24 +08:00
|
|
|
// We have two integer types with differing signs.
|
|
|
|
Type *UnsignedType = LHSC, *SignedType = RHSC;
|
2007-04-28 02:30:00 +08:00
|
|
|
|
2008-04-07 06:59:24 +08:00
|
|
|
if (RHSUnsigned)
|
|
|
|
std::swap(UnsignedType, SignedType);
|
|
|
|
|
|
|
|
// If the unsigned type is larger, return it.
|
|
|
|
if (getIntegerRank(UnsignedType) >= getIntegerRank(SignedType))
|
|
|
|
return LHSUnsigned ? LHS : RHS;
|
|
|
|
|
|
|
|
// FIXME: Need to check if the signed type can represent all values of the
|
|
|
|
// unsigned type. If it can, then the result is the signed type.
|
|
|
|
// If it can't, then the result is the unsigned version of the signed type.
|
|
|
|
// Should probably add a helper that returns a signed integer type from
|
|
|
|
// an unsigned (and vice versa). C99 6.3.1.8.
|
|
|
|
return RHSUnsigned ? LHS : RHS;
|
2007-04-28 02:30:00 +08:00
|
|
|
}
|
2007-08-17 13:31:46 +08:00
|
|
|
|
|
|
|
// getCFConstantStringType - Return the type used for constant CFStrings.
|
|
|
|
QualType ASTContext::getCFConstantStringType() {
|
|
|
|
if (!CFConstantStringTypeDecl) {
|
2008-03-15 14:12:44 +08:00
|
|
|
CFConstantStringTypeDecl =
|
2008-04-04 14:12:32 +08:00
|
|
|
RecordDecl::Create(*this, Decl::Struct, NULL, SourceLocation(),
|
2008-03-16 05:32:50 +08:00
|
|
|
&Idents.get("NSConstantString"), 0);
|
2007-11-19 08:25:30 +08:00
|
|
|
QualType FieldTypes[4];
|
2007-08-17 13:31:46 +08:00
|
|
|
|
|
|
|
// const int *isa;
|
|
|
|
FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
|
2007-11-19 08:25:30 +08:00
|
|
|
// int flags;
|
|
|
|
FieldTypes[1] = IntTy;
|
2007-08-17 13:31:46 +08:00
|
|
|
// const char *str;
|
2007-11-19 08:25:30 +08:00
|
|
|
FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
|
2007-08-17 13:31:46 +08:00
|
|
|
// long length;
|
2007-11-19 08:25:30 +08:00
|
|
|
FieldTypes[3] = LongTy;
|
2007-08-17 13:31:46 +08:00
|
|
|
// Create fields
|
2007-11-19 08:25:30 +08:00
|
|
|
FieldDecl *FieldDecls[4];
|
2007-08-17 13:31:46 +08:00
|
|
|
|
2007-11-19 08:25:30 +08:00
|
|
|
for (unsigned i = 0; i < 4; ++i)
|
2008-04-06 12:47:34 +08:00
|
|
|
FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
|
2008-03-16 08:16:02 +08:00
|
|
|
FieldTypes[i]);
|
2007-08-17 13:31:46 +08:00
|
|
|
|
|
|
|
CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
return getTagDeclType(CFConstantStringTypeDecl);
|
2007-09-11 23:32:40 +08:00
|
|
|
}
|
2007-10-11 09:00:40 +08:00
|
|
|
|
2007-10-29 14:33:42 +08:00
|
|
|
// This returns true if a type has been typedefed to BOOL:
|
|
|
|
// typedef <type> BOOL;
|
2007-10-31 04:27:44 +08:00
|
|
|
static bool isTypeTypedefedAsBOOL(QualType T) {
|
2007-10-29 14:33:42 +08:00
|
|
|
if (const TypedefType *TT = dyn_cast<TypedefType>(T))
|
2007-10-31 04:27:44 +08:00
|
|
|
return !strcmp(TT->getDecl()->getName(), "BOOL");
|
2007-10-29 13:01:08 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
/// getObjCEncodingTypeSize returns size of type for objective-c encoding
|
2007-10-30 06:57:28 +08:00
|
|
|
/// purpose.
|
2008-01-08 03:49:32 +08:00
|
|
|
int ASTContext::getObjCEncodingTypeSize(QualType type) {
|
2008-03-06 02:54:05 +08:00
|
|
|
uint64_t sz = getTypeSize(type);
|
2007-10-30 06:57:28 +08:00
|
|
|
|
|
|
|
// Make all integer and enum types at least as large as an int
|
|
|
|
if (sz > 0 && type->isIntegralType())
|
2008-03-06 02:54:05 +08:00
|
|
|
sz = std::max(sz, getTypeSize(IntTy));
|
2007-10-30 06:57:28 +08:00
|
|
|
// Treat arrays as pointers, since that's how they're passed in.
|
|
|
|
else if (type->isArrayType())
|
2008-03-06 02:54:05 +08:00
|
|
|
sz = getTypeSize(VoidPtrTy);
|
|
|
|
return sz / getTypeSize(CharTy);
|
2007-10-30 06:57:28 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
/// getObjCEncodingForMethodDecl - Return the encoded type for this method
|
2007-10-30 06:57:28 +08:00
|
|
|
/// declaration.
|
2008-01-08 03:49:32 +08:00
|
|
|
void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
|
2007-10-30 06:57:28 +08:00
|
|
|
std::string& S)
|
|
|
|
{
|
2007-11-02 01:18:37 +08:00
|
|
|
// Encode type qualifer, 'in', 'inout', etc. for the return type.
|
2008-01-08 03:49:32 +08:00
|
|
|
getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
|
2007-10-30 06:57:28 +08:00
|
|
|
// Encode result type.
|
2008-01-23 06:44:46 +08:00
|
|
|
getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
|
2007-10-30 06:57:28 +08:00
|
|
|
// Compute size of all parameters.
|
|
|
|
// Start with computing size of a pointer in number of bytes.
|
|
|
|
// FIXME: There might(should) be a better way of doing this computation!
|
|
|
|
SourceLocation Loc;
|
2008-03-06 02:54:05 +08:00
|
|
|
int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
|
2007-10-30 06:57:28 +08:00
|
|
|
// The first two arguments (self and _cmd) are pointers; account for
|
|
|
|
// their size.
|
|
|
|
int ParmOffset = 2 * PtrSize;
|
|
|
|
int NumOfParams = Decl->getNumParams();
|
|
|
|
for (int i = 0; i < NumOfParams; i++) {
|
|
|
|
QualType PType = Decl->getParamDecl(i)->getType();
|
2008-01-08 03:49:32 +08:00
|
|
|
int sz = getObjCEncodingTypeSize (PType);
|
|
|
|
assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
|
2007-10-30 06:57:28 +08:00
|
|
|
ParmOffset += sz;
|
|
|
|
}
|
|
|
|
S += llvm::utostr(ParmOffset);
|
|
|
|
S += "@0:";
|
|
|
|
S += llvm::utostr(PtrSize);
|
|
|
|
|
|
|
|
// Argument types.
|
|
|
|
ParmOffset = 2 * PtrSize;
|
|
|
|
for (int i = 0; i < NumOfParams; i++) {
|
|
|
|
QualType PType = Decl->getParamDecl(i)->getType();
|
2007-11-02 01:18:37 +08:00
|
|
|
// Process argument qualifiers for user supplied arguments; such as,
|
2007-10-30 06:57:28 +08:00
|
|
|
// 'in', 'inout', etc.
|
2008-01-08 03:49:32 +08:00
|
|
|
getObjCEncodingForTypeQualifier(
|
|
|
|
Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
|
2008-01-23 06:44:46 +08:00
|
|
|
getObjCEncodingForType(PType, S, EncodingRecordTypes);
|
2007-10-30 06:57:28 +08:00
|
|
|
S += llvm::utostr(ParmOffset);
|
2008-01-08 03:49:32 +08:00
|
|
|
ParmOffset += getObjCEncodingTypeSize(PType);
|
2007-10-30 06:57:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-23 06:44:46 +08:00
|
|
|
void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
|
|
|
|
llvm::SmallVector<const RecordType *, 8> &ERType) const
|
2007-10-29 13:01:08 +08:00
|
|
|
{
|
2007-10-29 14:33:42 +08:00
|
|
|
// FIXME: This currently doesn't encode:
|
|
|
|
// @ An object (whether statically typed or typed id)
|
|
|
|
// # A class object (Class)
|
|
|
|
// : A method selector (SEL)
|
|
|
|
// {name=type...} A structure
|
|
|
|
// (name=type...) A union
|
|
|
|
// bnum A bit field of num bits
|
|
|
|
|
|
|
|
if (const BuiltinType *BT = T->getAsBuiltinType()) {
|
2007-10-29 13:01:08 +08:00
|
|
|
char encoding;
|
|
|
|
switch (BT->getKind()) {
|
2008-04-07 06:05:18 +08:00
|
|
|
default: assert(0 && "Unhandled builtin type kind");
|
|
|
|
case BuiltinType::Void: encoding = 'v'; break;
|
|
|
|
case BuiltinType::Bool: encoding = 'B'; break;
|
2007-10-29 13:01:08 +08:00
|
|
|
case BuiltinType::Char_U:
|
2008-04-07 06:05:18 +08:00
|
|
|
case BuiltinType::UChar: encoding = 'C'; break;
|
|
|
|
case BuiltinType::UShort: encoding = 'S'; break;
|
|
|
|
case BuiltinType::UInt: encoding = 'I'; break;
|
|
|
|
case BuiltinType::ULong: encoding = 'L'; break;
|
|
|
|
case BuiltinType::ULongLong: encoding = 'Q'; break;
|
2007-10-29 13:01:08 +08:00
|
|
|
case BuiltinType::Char_S:
|
2008-04-07 06:05:18 +08:00
|
|
|
case BuiltinType::SChar: encoding = 'c'; break;
|
|
|
|
case BuiltinType::Short: encoding = 's'; break;
|
|
|
|
case BuiltinType::Int: encoding = 'i'; break;
|
|
|
|
case BuiltinType::Long: encoding = 'l'; break;
|
|
|
|
case BuiltinType::LongLong: encoding = 'q'; break;
|
|
|
|
case BuiltinType::Float: encoding = 'f'; break;
|
|
|
|
case BuiltinType::Double: encoding = 'd'; break;
|
|
|
|
case BuiltinType::LongDouble: encoding = 'd'; break;
|
2007-10-29 13:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
S += encoding;
|
2007-12-18 05:03:50 +08:00
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
else if (T->isObjCQualifiedIdType()) {
|
2007-12-18 05:03:50 +08:00
|
|
|
// Treat id<P...> same as 'id' for encoding purposes.
|
2008-01-23 06:44:46 +08:00
|
|
|
return getObjCEncodingForType(getObjCIdType(), S, ERType);
|
2007-12-18 05:03:50 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else if (const PointerType *PT = T->getAsPointerType()) {
|
2007-10-29 13:01:08 +08:00
|
|
|
QualType PointeeTy = PT->getPointeeType();
|
2008-01-08 03:49:32 +08:00
|
|
|
if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
|
2007-10-31 01:06:23 +08:00
|
|
|
S += '@';
|
|
|
|
return;
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (isObjCClassType(PointeeTy)) {
|
2007-10-31 10:53:19 +08:00
|
|
|
S += '#';
|
|
|
|
return;
|
2008-01-08 03:49:32 +08:00
|
|
|
} else if (isObjCSelType(PointeeTy)) {
|
2007-10-31 10:53:19 +08:00
|
|
|
S += ':';
|
|
|
|
return;
|
2007-10-31 01:06:23 +08:00
|
|
|
}
|
2007-10-29 13:01:08 +08:00
|
|
|
|
|
|
|
if (PointeeTy->isCharType()) {
|
|
|
|
// char pointer types should be encoded as '*' unless it is a
|
|
|
|
// type that has been typedef'd to 'BOOL'.
|
2007-10-29 14:33:42 +08:00
|
|
|
if (!isTypeTypedefedAsBOOL(PointeeTy)) {
|
2007-10-29 13:01:08 +08:00
|
|
|
S += '*';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
S += '^';
|
2008-01-23 06:44:46 +08:00
|
|
|
getObjCEncodingForType(PT->getPointeeType(), S, ERType);
|
2007-10-29 14:33:42 +08:00
|
|
|
} else if (const ArrayType *AT = T->getAsArrayType()) {
|
2007-10-29 13:01:08 +08:00
|
|
|
S += '[';
|
|
|
|
|
|
|
|
if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
|
|
|
|
S += llvm::utostr(CAT->getSize().getZExtValue());
|
|
|
|
else
|
|
|
|
assert(0 && "Unhandled array type!");
|
|
|
|
|
2008-01-23 06:44:46 +08:00
|
|
|
getObjCEncodingForType(AT->getElementType(), S, ERType);
|
2007-10-29 13:01:08 +08:00
|
|
|
S += ']';
|
2007-10-30 08:06:20 +08:00
|
|
|
} else if (T->getAsFunctionType()) {
|
|
|
|
S += '?';
|
2007-11-14 07:21:38 +08:00
|
|
|
} else if (const RecordType *RTy = T->getAsRecordType()) {
|
|
|
|
RecordDecl *RDecl= RTy->getDecl();
|
|
|
|
S += '{';
|
|
|
|
S += RDecl->getName();
|
2008-01-23 06:44:46 +08:00
|
|
|
bool found = false;
|
|
|
|
for (unsigned i = 0, e = ERType.size(); i != e; ++i)
|
|
|
|
if (ERType[i] == RTy) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
ERType.push_back(RTy);
|
|
|
|
S += '=';
|
|
|
|
for (int i = 0; i < RDecl->getNumMembers(); i++) {
|
|
|
|
FieldDecl *field = RDecl->getMember(i);
|
|
|
|
getObjCEncodingForType(field->getType(), S, ERType);
|
|
|
|
}
|
|
|
|
assert(ERType.back() == RTy && "Record Type stack mismatch.");
|
|
|
|
ERType.pop_back();
|
2007-11-14 07:21:38 +08:00
|
|
|
}
|
|
|
|
S += '}';
|
2007-12-13 06:30:11 +08:00
|
|
|
} else if (T->isEnumeralType()) {
|
|
|
|
S += 'i';
|
2007-10-29 13:01:08 +08:00
|
|
|
} else
|
2008-01-31 03:17:43 +08:00
|
|
|
assert(0 && "@encode for type not implemented!");
|
2007-10-29 13:01:08 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
|
2007-11-02 01:18:37 +08:00
|
|
|
std::string& S) const {
|
|
|
|
if (QT & Decl::OBJC_TQ_In)
|
|
|
|
S += 'n';
|
|
|
|
if (QT & Decl::OBJC_TQ_Inout)
|
|
|
|
S += 'N';
|
|
|
|
if (QT & Decl::OBJC_TQ_Out)
|
|
|
|
S += 'o';
|
|
|
|
if (QT & Decl::OBJC_TQ_Bycopy)
|
|
|
|
S += 'O';
|
|
|
|
if (QT & Decl::OBJC_TQ_Byref)
|
|
|
|
S += 'R';
|
|
|
|
if (QT & Decl::OBJC_TQ_Oneway)
|
|
|
|
S += 'V';
|
|
|
|
}
|
|
|
|
|
2007-10-11 09:00:40 +08:00
|
|
|
void ASTContext::setBuiltinVaListType(QualType T)
|
|
|
|
{
|
|
|
|
assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
|
|
|
|
|
|
|
|
BuiltinVaListType = T;
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void ASTContext::setObjCIdType(TypedefDecl *TD)
|
2007-10-15 22:41:52 +08:00
|
|
|
{
|
2008-01-08 03:49:32 +08:00
|
|
|
assert(ObjCIdType.isNull() && "'id' type already set!");
|
2007-10-15 22:41:52 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCIdType = getTypedefType(TD);
|
2007-10-15 22:41:52 +08:00
|
|
|
|
|
|
|
// typedef struct objc_object *id;
|
|
|
|
const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
|
|
|
|
assert(ptr && "'id' incorrectly typed");
|
|
|
|
const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
|
|
|
|
assert(rec && "'id' incorrectly typed");
|
|
|
|
IdStructType = rec;
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void ASTContext::setObjCSelType(TypedefDecl *TD)
|
2007-10-17 04:40:23 +08:00
|
|
|
{
|
2008-01-08 03:49:32 +08:00
|
|
|
assert(ObjCSelType.isNull() && "'SEL' type already set!");
|
2007-10-17 04:40:23 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCSelType = getTypedefType(TD);
|
2007-10-17 04:40:23 +08:00
|
|
|
|
|
|
|
// typedef struct objc_selector *SEL;
|
|
|
|
const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
|
|
|
|
assert(ptr && "'SEL' incorrectly typed");
|
|
|
|
const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
|
|
|
|
assert(rec && "'SEL' incorrectly typed");
|
|
|
|
SelStructType = rec;
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void ASTContext::setObjCProtoType(QualType QT)
|
2007-10-18 00:58:11 +08:00
|
|
|
{
|
2008-01-08 03:49:32 +08:00
|
|
|
assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
|
|
|
|
ObjCProtoType = QT;
|
2007-10-18 00:58:11 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void ASTContext::setObjCClassType(TypedefDecl *TD)
|
2007-10-31 10:53:19 +08:00
|
|
|
{
|
2008-01-08 03:49:32 +08:00
|
|
|
assert(ObjCClassType.isNull() && "'Class' type already set!");
|
2007-10-31 10:53:19 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCClassType = getTypedefType(TD);
|
2007-10-31 10:53:19 +08:00
|
|
|
|
|
|
|
// typedef struct objc_class *Class;
|
|
|
|
const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
|
|
|
|
assert(ptr && "'Class' incorrectly typed");
|
|
|
|
const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
|
|
|
|
assert(rec && "'Class' incorrectly typed");
|
|
|
|
ClassStructType = rec;
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
|
|
|
|
assert(ObjCConstantStringType.isNull() &&
|
2007-10-16 07:35:17 +08:00
|
|
|
"'NSConstantString' type already set!");
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCConstantStringType = getObjCInterfaceType(Decl);
|
2007-10-16 07:35:17 +08:00
|
|
|
}
|
|
|
|
|
2007-10-16 04:41:53 +08:00
|
|
|
bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
|
|
|
|
const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
|
|
|
|
const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
|
|
|
|
|
|
|
|
return lBuiltin->getKind() == rBuiltin->getKind();
|
|
|
|
}
|
|
|
|
|
2007-12-22 01:34:43 +08:00
|
|
|
/// objcTypesAreCompatible - This routine is called when two types
|
|
|
|
/// are of different class; one is interface type or is
|
|
|
|
/// a qualified interface type and the other type is of a different class.
|
|
|
|
/// Example, II or II<P>.
|
2007-10-16 04:41:53 +08:00
|
|
|
bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
|
2008-01-08 03:49:32 +08:00
|
|
|
if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
|
2007-10-16 04:41:53 +08:00
|
|
|
return true;
|
2008-01-08 03:49:32 +08:00
|
|
|
else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
|
2007-10-16 04:41:53 +08:00
|
|
|
return true;
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCInterfaceType *lhsIT =
|
|
|
|
dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
|
|
|
|
ObjCQualifiedInterfaceType *rhsQI =
|
|
|
|
dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
|
2007-12-22 01:34:43 +08:00
|
|
|
return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
else if (ObjCInterfaceType *rhsIT =
|
|
|
|
dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
|
|
|
|
ObjCQualifiedInterfaceType *lhsQI =
|
|
|
|
dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
|
2007-12-22 01:34:43 +08:00
|
|
|
return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
|
|
|
|
}
|
2007-10-16 04:41:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-01-08 04:12:21 +08:00
|
|
|
/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
|
|
|
|
/// must be canonical types.
|
2007-10-16 04:41:53 +08:00
|
|
|
bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
|
2008-01-08 04:12:21 +08:00
|
|
|
assert (lhs->isCanonical() &&
|
|
|
|
"interfaceTypesAreCompatible strip typedefs of lhs");
|
|
|
|
assert (rhs->isCanonical() &&
|
|
|
|
"interfaceTypesAreCompatible strip typedefs of rhs");
|
2007-12-21 06:37:58 +08:00
|
|
|
if (lhs == rhs)
|
|
|
|
return true;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
|
|
|
|
ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
|
|
|
|
ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
|
|
|
|
ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
|
2007-12-21 06:37:58 +08:00
|
|
|
// rhs is derived from lhs it is OK; else it is not OK.
|
|
|
|
while (rhsIDecl != NULL) {
|
|
|
|
if (rhsIDecl == lhsIDecl)
|
|
|
|
return true;
|
|
|
|
rhsIDecl = rhsIDecl->getSuperClass();
|
|
|
|
}
|
|
|
|
return false;
|
2007-10-16 04:41:53 +08:00
|
|
|
}
|
|
|
|
|
2007-12-12 09:00:23 +08:00
|
|
|
bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
|
|
|
|
QualType rhs) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCQualifiedInterfaceType *lhsQI =
|
|
|
|
dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
|
2007-12-12 09:00:23 +08:00
|
|
|
assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCQualifiedInterfaceType *rhsQI =
|
|
|
|
dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
|
2007-12-12 09:00:23 +08:00
|
|
|
assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
|
2008-01-08 04:12:21 +08:00
|
|
|
if (!interfaceTypesAreCompatible(
|
|
|
|
getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
|
|
|
|
getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
|
2007-12-12 09:00:23 +08:00
|
|
|
return false;
|
|
|
|
/* All protocols in lhs must have a presense in rhs. */
|
|
|
|
for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
|
|
|
|
bool match = false;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
|
2007-12-12 09:00:23 +08:00
|
|
|
for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
|
2007-12-12 09:00:23 +08:00
|
|
|
if (lhsProto == rhsProto) {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!match)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-12-21 08:33:59 +08:00
|
|
|
/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
|
|
|
|
/// inheritance hierarchy of 'rProto'.
|
2008-01-08 03:49:32 +08:00
|
|
|
static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
|
|
|
|
ObjCProtocolDecl *rProto) {
|
2007-12-21 08:33:59 +08:00
|
|
|
if (lProto == rProto)
|
|
|
|
return true;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
|
2007-12-21 08:33:59 +08:00
|
|
|
for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
|
|
|
|
if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-12-22 06:22:33 +08:00
|
|
|
/// ClassImplementsProtocol - Checks that 'lProto' protocol
|
|
|
|
/// has been implemented in IDecl class, its super class or categories (if
|
|
|
|
/// lookupCategory is true).
|
2008-01-08 03:49:32 +08:00
|
|
|
static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
|
|
|
|
ObjCInterfaceDecl *IDecl,
|
2007-12-22 06:22:33 +08:00
|
|
|
bool lookupCategory) {
|
|
|
|
|
|
|
|
// 1st, look up the class.
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
|
2007-12-22 06:22:33 +08:00
|
|
|
for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
|
|
|
|
if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2nd, look up the category.
|
|
|
|
if (lookupCategory)
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
|
2007-12-22 06:22:33 +08:00
|
|
|
CDecl = CDecl->getNextClassCategory()) {
|
|
|
|
protoList = CDecl->getReferencedProtocols();
|
|
|
|
for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
|
|
|
|
if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3rd, look up the super class(s)
|
|
|
|
if (IDecl->getSuperClass())
|
|
|
|
return
|
|
|
|
ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
|
2007-12-21 08:33:59 +08:00
|
|
|
/// one of which is a protocol qualified 'id' type. When 'compare'
|
|
|
|
/// is true it is for comparison; when false, for assignment/initialization.
|
2008-01-08 03:49:32 +08:00
|
|
|
bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
|
2007-12-21 08:33:59 +08:00
|
|
|
QualType rhs,
|
|
|
|
bool compare) {
|
2007-12-20 01:45:58 +08:00
|
|
|
// match id<P..> with an 'id' type in all cases.
|
|
|
|
if (const PointerType *PT = lhs->getAsPointerType()) {
|
|
|
|
QualType PointeeTy = PT->getPointeeType();
|
2008-01-08 03:49:32 +08:00
|
|
|
if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
|
2007-12-20 01:45:58 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if (const PointerType *PT = rhs->getAsPointerType()) {
|
|
|
|
QualType PointeeTy = PT->getPointeeType();
|
2008-01-08 03:49:32 +08:00
|
|
|
if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
|
2007-12-20 01:45:58 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCQualifiedInterfaceType *lhsQI = 0;
|
|
|
|
ObjCQualifiedInterfaceType *rhsQI = 0;
|
|
|
|
ObjCInterfaceDecl *lhsID = 0;
|
|
|
|
ObjCInterfaceDecl *rhsID = 0;
|
|
|
|
ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
|
|
|
|
ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
|
2007-12-20 01:45:58 +08:00
|
|
|
|
|
|
|
if (lhsQID) {
|
|
|
|
if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
|
|
|
|
QualType rtype =
|
|
|
|
cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
|
|
|
|
rhsQI =
|
2008-01-08 03:49:32 +08:00
|
|
|
dyn_cast<ObjCQualifiedInterfaceType>(
|
2007-12-20 01:45:58 +08:00
|
|
|
rtype.getCanonicalType().getTypePtr());
|
2007-12-21 03:24:10 +08:00
|
|
|
if (!rhsQI) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
|
2007-12-21 03:24:10 +08:00
|
|
|
rtype.getCanonicalType().getTypePtr());
|
|
|
|
if (IT)
|
|
|
|
rhsID = IT->getDecl();
|
|
|
|
}
|
2007-12-20 01:45:58 +08:00
|
|
|
}
|
2007-12-21 03:24:10 +08:00
|
|
|
if (!rhsQI && !rhsQID && !rhsID)
|
2007-12-20 01:45:58 +08:00
|
|
|
return false;
|
|
|
|
|
2008-01-04 04:01:35 +08:00
|
|
|
unsigned numRhsProtocols = 0;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl **rhsProtoList = 0;
|
2007-12-22 06:22:33 +08:00
|
|
|
if (rhsQI) {
|
|
|
|
numRhsProtocols = rhsQI->getNumProtocols();
|
|
|
|
rhsProtoList = rhsQI->getReferencedProtocols();
|
|
|
|
}
|
|
|
|
else if (rhsQID) {
|
|
|
|
numRhsProtocols = rhsQID->getNumProtocols();
|
|
|
|
rhsProtoList = rhsQID->getReferencedProtocols();
|
|
|
|
}
|
|
|
|
|
2007-12-20 01:45:58 +08:00
|
|
|
for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
|
2007-12-22 06:22:33 +08:00
|
|
|
bool match = false;
|
|
|
|
|
|
|
|
// when comparing an id<P> on lhs with a static type on rhs,
|
|
|
|
// see if static class implements all of id's protocols, directly or
|
|
|
|
// through its super class and categories.
|
|
|
|
if (rhsID) {
|
|
|
|
if (ClassImplementsProtocol(lhsProto, rhsID, true))
|
|
|
|
match = true;
|
2007-12-21 03:24:10 +08:00
|
|
|
}
|
2007-12-22 06:22:33 +08:00
|
|
|
else for (unsigned j = 0; j < numRhsProtocols; j++) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl *rhsProto = rhsProtoList[j];
|
2007-12-21 08:33:59 +08:00
|
|
|
if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
|
|
|
|
compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
|
2007-12-20 01:45:58 +08:00
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!match)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rhsQID) {
|
|
|
|
if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
|
|
|
|
QualType ltype =
|
|
|
|
cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
|
|
|
|
lhsQI =
|
2008-01-08 03:49:32 +08:00
|
|
|
dyn_cast<ObjCQualifiedInterfaceType>(
|
2007-12-20 01:45:58 +08:00
|
|
|
ltype.getCanonicalType().getTypePtr());
|
2007-12-21 03:24:10 +08:00
|
|
|
if (!lhsQI) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
|
2007-12-21 03:24:10 +08:00
|
|
|
ltype.getCanonicalType().getTypePtr());
|
|
|
|
if (IT)
|
|
|
|
lhsID = IT->getDecl();
|
|
|
|
}
|
2007-12-20 01:45:58 +08:00
|
|
|
}
|
2007-12-21 03:24:10 +08:00
|
|
|
if (!lhsQI && !lhsQID && !lhsID)
|
2007-12-20 01:45:58 +08:00
|
|
|
return false;
|
2007-12-21 03:24:10 +08:00
|
|
|
|
2008-01-04 04:01:35 +08:00
|
|
|
unsigned numLhsProtocols = 0;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl **lhsProtoList = 0;
|
2007-12-20 01:45:58 +08:00
|
|
|
if (lhsQI) {
|
|
|
|
numLhsProtocols = lhsQI->getNumProtocols();
|
|
|
|
lhsProtoList = lhsQI->getReferencedProtocols();
|
|
|
|
}
|
2007-12-21 03:24:10 +08:00
|
|
|
else if (lhsQID) {
|
2007-12-20 01:45:58 +08:00
|
|
|
numLhsProtocols = lhsQID->getNumProtocols();
|
|
|
|
lhsProtoList = lhsQID->getReferencedProtocols();
|
2007-12-22 06:22:33 +08:00
|
|
|
}
|
|
|
|
bool match = false;
|
|
|
|
// for static type vs. qualified 'id' type, check that class implements
|
|
|
|
// one of 'id's protocols.
|
|
|
|
if (lhsID) {
|
|
|
|
for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
|
2007-12-22 06:22:33 +08:00
|
|
|
if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else for (unsigned i =0; i < numLhsProtocols; i++) {
|
|
|
|
match = false;
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl *lhsProto = lhsProtoList[i];
|
2007-12-20 01:45:58 +08:00
|
|
|
for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
|
2007-12-21 08:33:59 +08:00
|
|
|
if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
|
|
|
|
compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
|
2007-12-20 01:45:58 +08:00
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-12-22 06:22:33 +08:00
|
|
|
}
|
|
|
|
if (!match)
|
|
|
|
return false;
|
2007-12-20 01:45:58 +08:00
|
|
|
}
|
|
|
|
return true;
|
2007-12-18 05:03:50 +08:00
|
|
|
}
|
|
|
|
|
2007-11-01 13:03:41 +08:00
|
|
|
bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
|
|
|
|
const VectorType *lVector = lhs->getAsVectorType();
|
|
|
|
const VectorType *rVector = rhs->getAsVectorType();
|
|
|
|
|
|
|
|
if ((lVector->getElementType().getCanonicalType() ==
|
|
|
|
rVector->getElementType().getCanonicalType()) &&
|
|
|
|
(lVector->getNumElements() == rVector->getNumElements()))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-10-16 04:41:53 +08:00
|
|
|
// C99 6.2.7p1: If both are complete types, then the following additional
|
|
|
|
// requirements apply...FIXME (handle compatibility across source files).
|
|
|
|
bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
|
2007-11-07 14:03:51 +08:00
|
|
|
// "Class" and "id" are compatible built-in structure types.
|
2008-01-08 03:49:32 +08:00
|
|
|
if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
|
|
|
|
isObjCClassType(lhs) && isObjCIdType(rhs))
|
2007-11-07 14:03:51 +08:00
|
|
|
return true;
|
2008-02-15 14:03:44 +08:00
|
|
|
|
|
|
|
// Within a translation unit a tag type is
|
|
|
|
// only compatible with itself.
|
|
|
|
return lhs.getCanonicalType() == rhs.getCanonicalType();
|
2007-10-16 04:41:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
|
|
|
|
// C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
|
|
|
|
// identically qualified and both shall be pointers to compatible types.
|
2008-02-21 04:55:12 +08:00
|
|
|
if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
|
|
|
|
lhs.getAddressSpace() != rhs.getAddressSpace())
|
2007-10-16 04:41:53 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
|
|
|
|
QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
|
|
|
|
|
|
|
|
return typesAreCompatible(ltype, rtype);
|
|
|
|
}
|
|
|
|
|
2007-12-03 15:33:35 +08:00
|
|
|
// C++ 5.17p6: When the left operand of an assignment operator denotes a
|
2007-10-16 04:41:53 +08:00
|
|
|
// reference to T, the operation assigns to the object of type T denoted by the
|
|
|
|
// reference.
|
|
|
|
bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
|
|
|
|
QualType ltype = lhs;
|
|
|
|
|
|
|
|
if (lhs->isReferenceType())
|
2008-04-03 01:35:06 +08:00
|
|
|
ltype = cast<ReferenceType>(lhs.getCanonicalType())->getPointeeType();
|
2007-10-16 04:41:53 +08:00
|
|
|
|
|
|
|
QualType rtype = rhs;
|
|
|
|
|
|
|
|
if (rhs->isReferenceType())
|
2008-04-03 01:35:06 +08:00
|
|
|
rtype = cast<ReferenceType>(rhs.getCanonicalType())->getPointeeType();
|
2007-10-16 04:41:53 +08:00
|
|
|
|
|
|
|
return typesAreCompatible(ltype, rtype);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
|
|
|
|
const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
|
|
|
|
const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
|
|
|
|
const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
|
|
|
|
const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
|
|
|
|
|
|
|
|
// first check the return types (common between C99 and K&R).
|
|
|
|
if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (lproto && rproto) { // two C99 style function prototypes
|
|
|
|
unsigned lproto_nargs = lproto->getNumArgs();
|
|
|
|
unsigned rproto_nargs = rproto->getNumArgs();
|
|
|
|
|
|
|
|
if (lproto_nargs != rproto_nargs)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// both prototypes have the same number of arguments.
|
|
|
|
if ((lproto->isVariadic() && !rproto->isVariadic()) ||
|
|
|
|
(rproto->isVariadic() && !lproto->isVariadic()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The use of ellipsis agree...now check the argument types.
|
|
|
|
for (unsigned i = 0; i < lproto_nargs; i++)
|
2008-01-31 03:17:43 +08:00
|
|
|
// C99 6.7.5.3p15: ...and each parameter declared with qualified type
|
|
|
|
// is taken as having the unqualified version of it's declared type.
|
2008-01-29 08:15:50 +08:00
|
|
|
if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
|
2008-01-31 03:17:43 +08:00
|
|
|
rproto->getArgType(i).getUnqualifiedType()))
|
2007-10-16 04:41:53 +08:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!lproto && !rproto) // two K&R style function decls, nothing to do.
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// we have a mixture of K&R style with C99 prototypes
|
|
|
|
const FunctionTypeProto *proto = lproto ? lproto : rproto;
|
|
|
|
|
|
|
|
if (proto->isVariadic())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: Each parameter type T in the prototype must be compatible with the
|
|
|
|
// type resulting from applying the usual argument conversions to T.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
|
2008-02-06 12:53:22 +08:00
|
|
|
// Compatible arrays must have compatible element types
|
|
|
|
QualType ltype = lhs->getAsArrayType()->getElementType();
|
|
|
|
QualType rtype = rhs->getAsArrayType()->getElementType();
|
|
|
|
|
2007-10-16 04:41:53 +08:00
|
|
|
if (!typesAreCompatible(ltype, rtype))
|
|
|
|
return false;
|
2008-02-06 12:53:22 +08:00
|
|
|
|
|
|
|
// Compatible arrays must be the same size
|
|
|
|
if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
|
|
|
|
if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
|
|
|
|
return RCAT->getSize() == LCAT->getSize();
|
|
|
|
|
2007-10-16 04:41:53 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
|
|
|
|
/// both shall have the identically qualified version of a compatible type.
|
|
|
|
/// C99 6.2.7p1: Two types have compatible types if their types are the
|
|
|
|
/// same. See 6.7.[2,3,5] for additional rules.
|
|
|
|
bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
|
|
|
|
QualType lcanon = lhs.getCanonicalType();
|
|
|
|
QualType rcanon = rhs.getCanonicalType();
|
2008-04-03 13:07:04 +08:00
|
|
|
|
2007-10-16 04:41:53 +08:00
|
|
|
// If two types are identical, they are are compatible
|
|
|
|
if (lcanon == rcanon)
|
|
|
|
return true;
|
2008-04-03 13:07:04 +08:00
|
|
|
|
|
|
|
if (lcanon.getCVRQualifiers() != rcanon.getCVRQualifiers() ||
|
|
|
|
lcanon.getAddressSpace() != rcanon.getAddressSpace())
|
|
|
|
return false;
|
2007-12-03 15:33:35 +08:00
|
|
|
|
|
|
|
// C++ [expr]: If an expression initially has the type "reference to T", the
|
|
|
|
// type is adjusted to "T" prior to any further analysis, the expression
|
|
|
|
// designates the object or function denoted by the reference, and the
|
|
|
|
// expression is an lvalue.
|
2008-01-14 13:45:46 +08:00
|
|
|
if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
|
2008-04-03 01:35:06 +08:00
|
|
|
lcanon = RT->getPointeeType();
|
2008-01-14 13:45:46 +08:00
|
|
|
if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
|
2008-04-03 01:35:06 +08:00
|
|
|
rcanon = RT->getPointeeType();
|
2008-01-14 13:45:46 +08:00
|
|
|
|
|
|
|
Type::TypeClass LHSClass = lcanon->getTypeClass();
|
|
|
|
Type::TypeClass RHSClass = rcanon->getTypeClass();
|
|
|
|
|
|
|
|
// We want to consider the two function types to be the same for these
|
|
|
|
// comparisons, just force one to the other.
|
|
|
|
if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
|
|
|
|
if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
|
2008-02-12 16:23:06 +08:00
|
|
|
|
|
|
|
// Same as above for arrays
|
|
|
|
if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
|
|
|
|
if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
|
2008-02-16 02:16:39 +08:00
|
|
|
if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
|
|
|
|
if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
|
2007-10-16 04:41:53 +08:00
|
|
|
|
2008-01-10 06:43:08 +08:00
|
|
|
// If the canonical type classes don't match...
|
2008-01-14 13:45:46 +08:00
|
|
|
if (LHSClass != RHSClass) {
|
2007-10-16 04:41:53 +08:00
|
|
|
// For Objective-C, it is possible for two types to be compatible
|
|
|
|
// when their classes don't match (when dealing with "id"). If either type
|
|
|
|
// is an interface, we defer to objcTypesAreCompatible().
|
2008-01-08 03:49:32 +08:00
|
|
|
if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
|
2007-10-16 04:41:53 +08:00
|
|
|
return objcTypesAreCompatible(lcanon, rcanon);
|
2008-01-31 03:17:43 +08:00
|
|
|
|
2008-01-14 13:45:46 +08:00
|
|
|
// C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
|
|
|
|
// a signed integer type, or an unsigned integer type.
|
2008-02-12 16:46:17 +08:00
|
|
|
if (lcanon->isEnumeralType() && rcanon->isIntegralType()) {
|
2008-04-07 06:05:18 +08:00
|
|
|
EnumDecl* EDecl = cast<EnumType>(lcanon)->getDecl();
|
2008-02-12 16:46:17 +08:00
|
|
|
return EDecl->getIntegerType() == rcanon;
|
|
|
|
}
|
|
|
|
if (rcanon->isEnumeralType() && lcanon->isIntegralType()) {
|
2008-04-07 06:05:18 +08:00
|
|
|
EnumDecl* EDecl = cast<EnumType>(rcanon)->getDecl();
|
2008-02-12 16:46:17 +08:00
|
|
|
return EDecl->getIntegerType() == lcanon;
|
|
|
|
}
|
2008-01-14 13:45:46 +08:00
|
|
|
|
2007-10-16 04:41:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
2008-01-10 06:43:08 +08:00
|
|
|
// The canonical type classes match.
|
2008-01-14 13:45:46 +08:00
|
|
|
switch (LHSClass) {
|
|
|
|
case Type::FunctionProto: assert(0 && "Canonicalized away above");
|
|
|
|
case Type::Pointer:
|
|
|
|
return pointerTypesAreCompatible(lcanon, rcanon);
|
|
|
|
case Type::ConstantArray:
|
|
|
|
case Type::VariableArray:
|
2008-02-16 02:16:39 +08:00
|
|
|
case Type::IncompleteArray:
|
2008-01-14 13:45:46 +08:00
|
|
|
return arrayTypesAreCompatible(lcanon, rcanon);
|
|
|
|
case Type::FunctionNoProto:
|
|
|
|
return functionTypesAreCompatible(lcanon, rcanon);
|
|
|
|
case Type::Tagged: // handle structures, unions
|
|
|
|
return tagTypesAreCompatible(lcanon, rcanon);
|
|
|
|
case Type::Builtin:
|
|
|
|
return builtinTypesAreCompatible(lcanon, rcanon);
|
|
|
|
case Type::ObjCInterface:
|
|
|
|
return interfaceTypesAreCompatible(lcanon, rcanon);
|
|
|
|
case Type::Vector:
|
|
|
|
case Type::OCUVector:
|
|
|
|
return vectorTypesAreCompatible(lcanon, rcanon);
|
|
|
|
case Type::ObjCQualifiedInterface:
|
|
|
|
return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
|
|
|
|
default:
|
|
|
|
assert(0 && "unexpected type");
|
2007-10-16 04:41:53 +08:00
|
|
|
}
|
|
|
|
return true; // should never get here...
|
|
|
|
}
|
2007-11-01 01:10:13 +08:00
|
|
|
|
|
|
|
/// Emit - Serialize an ASTContext object to Bitcode.
|
|
|
|
void ASTContext::Emit(llvm::Serializer& S) const {
|
2007-11-01 04:00:03 +08:00
|
|
|
S.EmitRef(SourceMgr);
|
|
|
|
S.EmitRef(Target);
|
|
|
|
S.EmitRef(Idents);
|
|
|
|
S.EmitRef(Selectors);
|
2007-11-01 01:10:13 +08:00
|
|
|
|
2007-11-01 06:44:07 +08:00
|
|
|
// Emit the size of the type vector so that we can reserve that size
|
|
|
|
// when we reconstitute the ASTContext object.
|
2007-11-07 06:26:16 +08:00
|
|
|
S.EmitInt(Types.size());
|
|
|
|
|
2007-11-14 06:02:55 +08:00
|
|
|
for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
|
|
|
|
I!=E;++I)
|
|
|
|
(*I)->Emit(S);
|
2007-11-07 06:26:16 +08:00
|
|
|
|
2007-11-02 02:11:32 +08:00
|
|
|
// FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
|
2007-11-01 01:10:13 +08:00
|
|
|
}
|
|
|
|
|
2007-11-13 08:25:37 +08:00
|
|
|
ASTContext* ASTContext::Create(llvm::Deserializer& D) {
|
2007-11-01 06:44:07 +08:00
|
|
|
SourceManager &SM = D.ReadRef<SourceManager>();
|
|
|
|
TargetInfo &t = D.ReadRef<TargetInfo>();
|
|
|
|
IdentifierTable &idents = D.ReadRef<IdentifierTable>();
|
|
|
|
SelectorTable &sels = D.ReadRef<SelectorTable>();
|
2008-04-04 14:12:32 +08:00
|
|
|
|
2007-11-01 06:44:07 +08:00
|
|
|
unsigned size_reserve = D.ReadInt();
|
|
|
|
|
|
|
|
ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
|
|
|
|
|
2007-11-14 06:02:55 +08:00
|
|
|
for (unsigned i = 0; i < size_reserve; ++i)
|
|
|
|
Type::Create(*A,i,D);
|
2008-04-04 14:12:32 +08:00
|
|
|
|
2007-11-02 02:11:32 +08:00
|
|
|
// FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
|
2007-11-01 06:44:07 +08:00
|
|
|
|
|
|
|
return A;
|
|
|
|
}
|