Fix error handling in type code.

This commit is contained in:
Matt Pharr
2012-02-06 06:29:31 -08:00
parent 724a843bbd
commit fa7a7fe23e

215
type.cpp
View File

@@ -982,6 +982,10 @@ PointerType::GetAsUnboundVariabilityType() const {
const PointerType * const PointerType *
PointerType::ResolveUnboundVariability(Variability v) const { PointerType::ResolveUnboundVariability(Variability v) const {
if (baseType == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
return new PointerType(baseType->ResolveUnboundVariability(v), return new PointerType(baseType->ResolveUnboundVariability(v),
(variability == Unbound) ? v : variability, (variability == Unbound) ? v : variability,
isConst); isConst);
@@ -1015,8 +1019,10 @@ PointerType::GetAsNonConstType() const {
std::string std::string
PointerType::GetString() const { PointerType::GetString() const {
if (baseType == NULL) if (baseType == NULL) {
Assert(m->errorCount > 0);
return ""; return "";
}
std::string ret = baseType->GetString(); std::string ret = baseType->GetString();
@@ -1035,8 +1041,10 @@ PointerType::GetString() const {
std::string std::string
PointerType::Mangle() const { PointerType::Mangle() const {
Assert(variability != Unbound); Assert(variability != Unbound);
if (baseType == NULL) if (baseType == NULL) {
Assert(m->errorCount > 0);
return ""; return "";
}
return ((variability == Uniform) ? std::string("uptr<") : std::string("vptr<")) + return ((variability == Uniform) ? std::string("uptr<") : std::string("vptr<")) +
baseType->Mangle() + std::string(">"); baseType->Mangle() + std::string(">");
@@ -1050,8 +1058,10 @@ PointerType::GetCDeclaration(const std::string &name) const {
return ""; return "";
} }
if (baseType == NULL) if (baseType == NULL) {
Assert(m->errorCount > 0);
return ""; return "";
}
std::string ret = baseType->GetCDeclaration(""); std::string ret = baseType->GetCDeclaration("");
ret += std::string(" *"); ret += std::string(" *");
@@ -1065,8 +1075,10 @@ PointerType::GetCDeclaration(const std::string &name) const {
LLVM_TYPE_CONST llvm::Type * LLVM_TYPE_CONST llvm::Type *
PointerType::LLVMType(llvm::LLVMContext *ctx) const { PointerType::LLVMType(llvm::LLVMContext *ctx) const {
Assert(variability != Unbound); Assert(variability != Unbound);
if (baseType == NULL) if (baseType == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
if (variability == Varying) if (variability == Varying)
// always the same, since we currently use int vectors for varying // always the same, since we currently use int vectors for varying
@@ -1120,8 +1132,10 @@ lCreateDIArray(llvm::DIType eltType, int count) {
llvm::DIType llvm::DIType
PointerType::GetDIType(llvm::DIDescriptor scope) const { PointerType::GetDIType(llvm::DIDescriptor scope) const {
Assert(variability != Unbound); Assert(variability != Unbound);
if (baseType == NULL) if (baseType == NULL) {
Assert(m->errorCount > 0);
return llvm::DIType(); return llvm::DIType();
}
llvm::DIType diTargetType = baseType->GetDIType(scope); llvm::DIType diTargetType = baseType->GetDIType(scope);
int bitsSize = g->target.is32Bit ? 32 : 64; int bitsSize = g->target.is32Bit ? 32 : 64;
@@ -1156,12 +1170,16 @@ ArrayType::ArrayType(const Type *c, int a)
LLVM_TYPE_CONST llvm::ArrayType * LLVM_TYPE_CONST llvm::ArrayType *
ArrayType::LLVMType(llvm::LLVMContext *ctx) const { ArrayType::LLVMType(llvm::LLVMContext *ctx) const {
if (!child) if (child == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
LLVM_TYPE_CONST llvm::Type *ct = child->LLVMType(ctx); LLVM_TYPE_CONST llvm::Type *ct = child->LLVMType(ctx);
if (!ct) if (ct == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
return llvm::ArrayType::get(ct, numElements); return llvm::ArrayType::get(ct, numElements);
} }
@@ -1217,62 +1235,80 @@ ArrayType::GetBaseType() const {
const ArrayType * const ArrayType *
ArrayType::GetAsVaryingType() const { ArrayType::GetAsVaryingType() const {
if (child == NULL) if (child == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
return new ArrayType(child->GetAsVaryingType(), numElements); return new ArrayType(child->GetAsVaryingType(), numElements);
} }
const ArrayType * const ArrayType *
ArrayType::GetAsUniformType() const { ArrayType::GetAsUniformType() const {
if (child == NULL) if (child == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
return new ArrayType(child->GetAsUniformType(), numElements); return new ArrayType(child->GetAsUniformType(), numElements);
} }
const ArrayType * const ArrayType *
ArrayType::GetAsUnboundVariabilityType() const { ArrayType::GetAsUnboundVariabilityType() const {
if (child == NULL) if (child == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
return new ArrayType(child->GetAsUnboundVariabilityType(), numElements); return new ArrayType(child->GetAsUnboundVariabilityType(), numElements);
} }
const ArrayType * const ArrayType *
ArrayType::ResolveUnboundVariability(Variability v) const { ArrayType::ResolveUnboundVariability(Variability v) const {
if (child == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
return new ArrayType(child->ResolveUnboundVariability(v), numElements); return new ArrayType(child->ResolveUnboundVariability(v), numElements);
} }
const ArrayType * const ArrayType *
ArrayType::GetAsUnsignedType() const { ArrayType::GetAsUnsignedType() const {
if (child == NULL) if (child == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
return new ArrayType(child->GetAsUnsignedType(), numElements); return new ArrayType(child->GetAsUnsignedType(), numElements);
} }
const Type * const Type *
ArrayType::GetSOAType(int width) const { ArrayType::GetSOAType(int width) const {
if (child == NULL) if (child == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
return new ArrayType(child->GetSOAType(width), numElements); return new ArrayType(child->GetSOAType(width), numElements);
} }
const ArrayType * const ArrayType *
ArrayType::GetAsConstType() const { ArrayType::GetAsConstType() const {
if (child == NULL) if (child == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
return new ArrayType(child->GetAsConstType(), numElements); return new ArrayType(child->GetAsConstType(), numElements);
} }
const ArrayType * const ArrayType *
ArrayType::GetAsNonConstType() const { ArrayType::GetAsNonConstType() const {
if (child == NULL) if (child == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
return new ArrayType(child->GetAsNonConstType(), numElements); return new ArrayType(child->GetAsNonConstType(), numElements);
} }
@@ -1291,7 +1327,12 @@ ArrayType::GetElementType() const {
std::string std::string
ArrayType::GetString() const { ArrayType::GetString() const {
std::string s = GetBaseType()->GetString(); const Type *base = GetBaseType();
if (base == NULL) {
Assert(m->errorCount > 0);
return "";
}
std::string s = base->GetString();
const ArrayType *at = this; const ArrayType *at = this;
// Walk through this and any children arrays and print all of their // Walk through this and any children arrays and print all of their
@@ -1311,8 +1352,10 @@ ArrayType::GetString() const {
std::string std::string
ArrayType::Mangle() const { ArrayType::Mangle() const {
if (child == NULL) if (child == NULL) {
Assert(m->errorCount > 0);
return "(error)"; return "(error)";
}
std::string s = child->Mangle(); std::string s = child->Mangle();
char buf[16]; char buf[16];
if (numElements > 0) if (numElements > 0)
@@ -1325,7 +1368,12 @@ ArrayType::Mangle() const {
std::string std::string
ArrayType::GetCDeclaration(const std::string &name) const { ArrayType::GetCDeclaration(const std::string &name) const {
std::string s = GetBaseType()->GetCDeclaration(name); const Type *base = GetBaseType();
if (base == NULL) {
Assert(m->errorCount > 0);
return "";
}
std::string s = base->GetCDeclaration(name);
const ArrayType *at = this; const ArrayType *at = this;
while (at) { while (at) {
@@ -1344,7 +1392,7 @@ ArrayType::GetCDeclaration(const std::string &name) const {
int int
ArrayType::TotalElementCount() const { ArrayType::TotalElementCount() const {
const ArrayType *ct = dynamic_cast<const ArrayType *>(child); const ArrayType *ct = dynamic_cast<const ArrayType *>(child);
if (ct) if (ct != NULL)
return numElements * ct->TotalElementCount(); return numElements * ct->TotalElementCount();
else else
return numElements; return numElements;
@@ -1353,8 +1401,10 @@ ArrayType::TotalElementCount() const {
llvm::DIType llvm::DIType
ArrayType::GetDIType(llvm::DIDescriptor scope) const { ArrayType::GetDIType(llvm::DIDescriptor scope) const {
if (!child) if (child == NULL) {
Assert(m->errorCount > 0);
return llvm::DIType(); return llvm::DIType();
}
llvm::DIType eltType = child->GetDIType(scope); llvm::DIType eltType = child->GetDIType(scope);
return lCreateDIArray(eltType, numElements); return lCreateDIArray(eltType, numElements);
@@ -2083,36 +2133,60 @@ ReferenceType::ReferenceType(const Type *t)
Type::Variability Type::Variability
ReferenceType::GetVariability() const { ReferenceType::GetVariability() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return Type::Unbound;
}
return targetType->GetVariability(); return targetType->GetVariability();
} }
bool bool
ReferenceType::IsBoolType() const { ReferenceType::IsBoolType() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return false;
}
return targetType->IsBoolType(); return targetType->IsBoolType();
} }
bool bool
ReferenceType::IsFloatType() const { ReferenceType::IsFloatType() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return false;
}
return targetType->IsFloatType(); return targetType->IsFloatType();
} }
bool bool
ReferenceType::IsIntType() const { ReferenceType::IsIntType() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return false;
}
return targetType->IsIntType(); return targetType->IsIntType();
} }
bool bool
ReferenceType::IsUnsignedType() const { ReferenceType::IsUnsignedType() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return false;
}
return targetType->IsUnsignedType(); return targetType->IsUnsignedType();
} }
bool bool
ReferenceType::IsConstType() const { ReferenceType::IsConstType() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return false;
}
return targetType->IsConstType(); return targetType->IsConstType();
} }
@@ -2125,12 +2199,20 @@ ReferenceType::GetReferenceTarget() const {
const Type * const Type *
ReferenceType::GetBaseType() const { ReferenceType::GetBaseType() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
return targetType->GetBaseType(); return targetType->GetBaseType();
} }
const ReferenceType * const ReferenceType *
ReferenceType::GetAsVaryingType() const { ReferenceType::GetAsVaryingType() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
if (IsVaryingType()) if (IsVaryingType())
return this; return this;
return new ReferenceType(targetType->GetAsVaryingType()); return new ReferenceType(targetType->GetAsVaryingType());
@@ -2139,6 +2221,10 @@ ReferenceType::GetAsVaryingType() const {
const ReferenceType * const ReferenceType *
ReferenceType::GetAsUniformType() const { ReferenceType::GetAsUniformType() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
if (IsUniformType()) if (IsUniformType())
return this; return this;
return new ReferenceType(targetType->GetAsUniformType()); return new ReferenceType(targetType->GetAsUniformType());
@@ -2147,6 +2233,10 @@ ReferenceType::GetAsUniformType() const {
const ReferenceType * const ReferenceType *
ReferenceType::GetAsUnboundVariabilityType() const { ReferenceType::GetAsUnboundVariabilityType() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
if (HasUnboundVariability()) if (HasUnboundVariability())
return this; return this;
return new ReferenceType(targetType->GetAsUnboundVariabilityType()); return new ReferenceType(targetType->GetAsUnboundVariabilityType());
@@ -2155,18 +2245,30 @@ ReferenceType::GetAsUnboundVariabilityType() const {
const ReferenceType * const ReferenceType *
ReferenceType::ResolveUnboundVariability(Variability v) const { ReferenceType::ResolveUnboundVariability(Variability v) const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
return new ReferenceType(targetType->ResolveUnboundVariability(v)); return new ReferenceType(targetType->ResolveUnboundVariability(v));
} }
const Type * const Type *
ReferenceType::GetSOAType(int width) const { ReferenceType::GetSOAType(int width) const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
return new ReferenceType(targetType->GetSOAType(width)); return new ReferenceType(targetType->GetSOAType(width));
} }
const ReferenceType * const ReferenceType *
ReferenceType::GetAsConstType() const { ReferenceType::GetAsConstType() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
if (IsConstType()) if (IsConstType())
return this; return this;
return new ReferenceType(targetType->GetAsConstType()); return new ReferenceType(targetType->GetAsConstType());
@@ -2175,6 +2277,10 @@ ReferenceType::GetAsConstType() const {
const ReferenceType * const ReferenceType *
ReferenceType::GetAsNonConstType() const { ReferenceType::GetAsNonConstType() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
if (!IsConstType()) if (!IsConstType())
return this; return this;
return new ReferenceType(targetType->GetAsNonConstType()); return new ReferenceType(targetType->GetAsNonConstType());
@@ -2183,8 +2289,10 @@ ReferenceType::GetAsNonConstType() const {
std::string std::string
ReferenceType::GetString() const { ReferenceType::GetString() const {
if (targetType == NULL) if (targetType == NULL) {
Assert(m->errorCount > 0);
return ""; return "";
}
std::string ret = targetType->GetString(); std::string ret = targetType->GetString();
@@ -2195,6 +2303,10 @@ ReferenceType::GetString() const {
std::string std::string
ReferenceType::Mangle() const { ReferenceType::Mangle() const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return "";
}
std::string ret; std::string ret;
ret += std::string("REF") + targetType->Mangle(); ret += std::string("REF") + targetType->Mangle();
return ret; return ret;
@@ -2203,6 +2315,11 @@ ReferenceType::Mangle() const {
std::string std::string
ReferenceType::GetCDeclaration(const std::string &name) const { ReferenceType::GetCDeclaration(const std::string &name) const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return "";
}
const ArrayType *at = dynamic_cast<const ArrayType *>(targetType); const ArrayType *at = dynamic_cast<const ArrayType *>(targetType);
if (at != NULL) { if (at != NULL) {
if (at->GetElementCount() == 0) { if (at->GetElementCount() == 0) {
@@ -2231,17 +2348,28 @@ ReferenceType::GetCDeclaration(const std::string &name) const {
LLVM_TYPE_CONST llvm::Type * LLVM_TYPE_CONST llvm::Type *
ReferenceType::LLVMType(llvm::LLVMContext *ctx) const { ReferenceType::LLVMType(llvm::LLVMContext *ctx) const {
if (!targetType) if (targetType == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
LLVM_TYPE_CONST llvm::Type *t = targetType->LLVMType(ctx); LLVM_TYPE_CONST llvm::Type *t = targetType->LLVMType(ctx);
if (!t) if (t == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
return llvm::PointerType::get(t, 0); return llvm::PointerType::get(t, 0);
} }
llvm::DIType llvm::DIType
ReferenceType::GetDIType(llvm::DIDescriptor scope) const { ReferenceType::GetDIType(llvm::DIDescriptor scope) const {
if (targetType == NULL) {
Assert(m->errorCount > 0);
return llvm::DIType();
}
llvm::DIType diTargetType = targetType->GetDIType(scope); llvm::DIType diTargetType = targetType->GetDIType(scope);
return m->diBuilder->createReferenceType(diTargetType); return m->diBuilder->createReferenceType(diTargetType);
} }
@@ -2340,11 +2468,21 @@ FunctionType::GetAsUnboundVariabilityType() const {
const FunctionType * const FunctionType *
FunctionType::ResolveUnboundVariability(Variability v) const { FunctionType::ResolveUnboundVariability(Variability v) const {
if (returnType == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
const Type *rt = returnType->ResolveUnboundVariability(v); const Type *rt = returnType->ResolveUnboundVariability(v);
std::vector<const Type *> pt; std::vector<const Type *> pt;
for (unsigned int i = 0; i < paramTypes.size(); ++i) for (unsigned int i = 0; i < paramTypes.size(); ++i) {
pt.push_back((paramTypes[i] == NULL) ? NULL : if (paramTypes[i] == NULL) {
paramTypes[i]->ResolveUnboundVariability(v)); Assert(m->errorCount > 0);
return NULL;
}
pt.push_back(paramTypes[i]->ResolveUnboundVariability(v));
}
return new FunctionType(rt, pt, paramNames, paramDefaults, return new FunctionType(rt, pt, paramNames, paramDefaults,
paramPositions, isTask, isExported, isExternC); paramPositions, isTask, isExported, isExternC);
} }
@@ -2375,10 +2513,17 @@ std::string
FunctionType::GetString() const { FunctionType::GetString() const {
std::string ret; std::string ret;
if (isTask) ret += "task "; if (isTask) ret += "task ";
if (returnType != NULL)
ret += returnType->GetString(); ret += returnType->GetString();
else
ret += "/* ERROR */";
ret += "("; ret += "(";
for (unsigned int i = 0; i < paramTypes.size(); ++i) { for (unsigned int i = 0; i < paramTypes.size(); ++i) {
if (paramTypes[i] == NULL)
ret += "/* ERROR */";
else
ret += paramTypes[i]->GetString(); ret += paramTypes[i]->GetString();
if (i != paramTypes.size() - 1) if (i != paramTypes.size() - 1)
ret += ", "; ret += ", ";
} }
@@ -2391,7 +2536,11 @@ std::string
FunctionType::Mangle() const { FunctionType::Mangle() const {
std::string ret = "___"; std::string ret = "___";
for (unsigned int i = 0; i < paramTypes.size(); ++i) for (unsigned int i = 0; i < paramTypes.size(); ++i)
if (paramTypes[i] == NULL)
Assert(m->errorCount > 0);
else
ret += paramTypes[i]->Mangle(); ret += paramTypes[i]->Mangle();
return ret; return ret;
} }
@@ -2444,18 +2593,23 @@ FunctionType::GetDIType(llvm::DIDescriptor scope) const {
LLVM_TYPE_CONST llvm::FunctionType * LLVM_TYPE_CONST llvm::FunctionType *
FunctionType::LLVMFunctionType(llvm::LLVMContext *ctx, bool includeMask) const { FunctionType::LLVMFunctionType(llvm::LLVMContext *ctx, bool includeMask) const {
if (isTask == true) Assert(includeMask == true); if (isTask == true)
Assert(includeMask == true);
// Get the LLVM Type *s for the function arguments // Get the LLVM Type *s for the function arguments
std::vector<LLVM_TYPE_CONST llvm::Type *> llvmArgTypes; std::vector<LLVM_TYPE_CONST llvm::Type *> llvmArgTypes;
for (unsigned int i = 0; i < paramTypes.size(); ++i) { for (unsigned int i = 0; i < paramTypes.size(); ++i) {
if (!paramTypes[i]) if (paramTypes[i] == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
Assert(paramTypes[i] != AtomicType::Void); Assert(paramTypes[i] != AtomicType::Void);
LLVM_TYPE_CONST llvm::Type *t = paramTypes[i]->LLVMType(ctx); LLVM_TYPE_CONST llvm::Type *t = paramTypes[i]->LLVMType(ctx);
if (t == NULL) if (t == NULL) {
Assert(m->errorCount > 0);
return NULL; return NULL;
}
llvmArgTypes.push_back(t); llvmArgTypes.push_back(t);
} }
@@ -2481,6 +2635,11 @@ FunctionType::LLVMFunctionType(llvm::LLVMContext *ctx, bool includeMask) const {
// Otherwise we already have the types of the arguments // Otherwise we already have the types of the arguments
callTypes = llvmArgTypes; callTypes = llvmArgTypes;
if (returnType == NULL) {
Assert(m->errorCount > 0);
return NULL;
}
return llvm::FunctionType::get(returnType->LLVMType(g->ctx), callTypes, false); return llvm::FunctionType::get(returnType->LLVMType(g->ctx), callTypes, false);
} }